ViennaCL - The Vienna Computing Library  1.2.0
tools.hpp
Go to the documentation of this file.
1 #ifndef VIENNACL_TOOLS_TOOLS_HPP_
2 #define VIENNACL_TOOLS_TOOLS_HPP_
3 
4 /* =========================================================================
5  Copyright (c) 2010-2011, Institute for Microelectronics,
6  Institute for Analysis and Scientific Computing,
7  TU Wien.
8 
9  -----------------
10  ViennaCL - The Vienna Computing Library
11  -----------------
12 
13  Project Head: Karl Rupp rupp@iue.tuwien.ac.at
14 
15  (A list of authors and contributors can be found in the PDF manual)
16 
17  License: MIT (X11), see file LICENSE in the base directory
18 ============================================================================= */
19 
24 #include <string>
25 #include <fstream>
26 #include <sstream>
27 #include "viennacl/forwards.h"
29 
30 
31 #ifdef VIENNACL_HAVE_UBLAS
32 #include <boost/numeric/ublas/matrix_sparse.hpp>
33 #include <boost/numeric/ublas/matrix.hpp>
34 #endif
35 
36 #ifdef VIENNACL_HAVE_EIGEN
37 #include <Eigen/Core>
38 #include <Eigen/Sparse>
39 #endif
40 
41 #ifdef VIENNACL_HAVE_MTL4
42 #include <boost/numeric/mtl/mtl.hpp>
43 #endif
44 
45 #include <vector>
46 #include <map>
47 
48 namespace viennacl
49 {
50  namespace tools
51  {
52 
54  template <class SCALARTYPE, typename F, unsigned int ALIGNMENT>
55  struct MATRIX_ITERATOR_INCREMENTER<viennacl::row_iteration, viennacl::matrix<SCALARTYPE, F, ALIGNMENT> >
56  {
57  static void apply(const viennacl::matrix<SCALARTYPE, F, ALIGNMENT> & mat, unsigned int & row, unsigned int & col) { ++row; }
58  };
59 
60  template <class SCALARTYPE, typename F, unsigned int ALIGNMENT>
61  struct MATRIX_ITERATOR_INCREMENTER<viennacl::col_iteration, viennacl::matrix<SCALARTYPE, F, ALIGNMENT> >
62  {
63  static void apply(const viennacl::matrix<SCALARTYPE, F, ALIGNMENT> & mat, unsigned int & row, unsigned int & col) { ++col; }
64  };
65 
66 
68  template <typename T>
70  {
71  typedef typename T::ERROR_SCALAR_MUST_HAVE_TEMPLATE_ARGUMENT_FLOAT_OR_DOUBLE ResultType;
72  };
73 
74  template <>
76  {
77  typedef float ResultType;
78  };
79 
80  template <>
82  {
83  typedef double ResultType;
84  };
85 
86 
87 
93  inline std::string readTextFromFile(const std::string & filename)
94  {
95  std::ifstream f(filename.c_str());
96  if (!f) return std::string();
97 
98  std::stringstream result;
99  std::string tmp;
100  while (std::getline(f, tmp))
101  result << tmp << std::endl;
102 
103  return result.str();
104  }
105 
113  inline std::string strReplace(const std::string & text, std::string to_search, std::string to_replace)
114  {
115  std::string::size_type pos = 0;
116  std::string result;
117  std::string::size_type found;
118  while( (found = text.find(to_search, pos)) != std::string::npos )
119  {
120  result.append(text.substr(pos,found-pos));
121  result.append(to_replace);
122  pos = found + to_search.length();
123  }
124  if (pos < text.length())
125  result.append(text.substr(pos));
126  return result;
127  }
128 
136  template <class INT_TYPE>
137  INT_TYPE roundUpToNextMultiple(INT_TYPE to_reach, INT_TYPE base)
138  {
139  if (to_reach % base == 0) return to_reach;
140  return ((to_reach / base) + 1) * base;
141  }
142 
143 
150  inline std::string make_double_kernel(std::string const & source, std::string const & fp_extension)
151  {
152  std::stringstream ss;
153  ss << "#pragma OPENCL EXTENSION " << fp_extension << " : enable\n\n";
154 
155  std::string result = ss.str();
156  result.append(strReplace(source, "float", "double"));
157  return result;
158  }
159 
160 
162  template <typename T>
164  {
165  typedef T ResultType;
166  };
167 
168  template <typename T>
169  struct CONST_REMOVER<const T>
170  {
171  typedef T ResultType;
172  };
173 
174 
180  template <typename LHS, typename RHS>
182  {
183  typedef typename LHS::ERROR_COULD_NOT_EXTRACT_VECTOR_INFORMATION_FROM_VECTOR_EXPRESSION ResultType;
184  };
185 
186  template <typename LHS, typename ScalarType, unsigned int A>
187  struct VECTOR_EXTRACTOR_IMPL<LHS, viennacl::vector<ScalarType, A> >
188  {
190  };
191 
192  template <typename RHS, typename ScalarType, unsigned int A>
193  struct VECTOR_EXTRACTOR_IMPL<viennacl::vector<ScalarType, A>, RHS>
194  {
196  };
197 
198  //resolve ambiguities for previous cases:
199  template <typename ScalarType, unsigned int A>
200  struct VECTOR_EXTRACTOR_IMPL<viennacl::vector<ScalarType, A>, viennacl::vector<ScalarType, A> >
201  {
203  };
204 
205  template <typename LHS, typename RHS>
207  {
210  };
211 
218  template <typename LHS, typename RHS, typename OP>
220  {
221  //take care: using a plain, naive .size() on the left hand side type can cause subtle side-effects!
222  };
223 
224  //Standard case: LHS is the vector type and carries the correct size
225  template <typename ScalarType, unsigned int A, typename RHS>
226  struct VECTOR_SIZE_DEDUCER<const viennacl::vector<ScalarType, A>, RHS, viennacl::op_prod>
227  {
228  static size_t size(const viennacl::vector<ScalarType, A> & lhs,
229  const RHS & rhs) { return lhs.size(); }
230  };
231 
232  template <typename ScalarType, unsigned int A, typename RHS>
233  struct VECTOR_SIZE_DEDUCER<const viennacl::vector<ScalarType, A>, RHS, viennacl::op_div>
234  {
235  static size_t size(const viennacl::vector<ScalarType, A> & lhs,
236  const RHS & rhs) { return lhs.size(); }
237  };
238 
239  //special case: matrix-vector product: Return the number of rows of the matrix
240  template <typename ScalarType, typename F, unsigned int Amat, unsigned int A>
241  struct VECTOR_SIZE_DEDUCER<const viennacl::matrix<ScalarType, F, Amat>, const viennacl::vector<ScalarType, A>, viennacl::op_prod>
242  {
243  static size_t size(const viennacl::matrix<ScalarType, F, Amat> & lhs,
244  const viennacl::vector<ScalarType, A> & rhs) { return lhs.size1(); }
245  };
246 
247  template <typename ScalarType, unsigned int Amat, unsigned int A>
248  struct VECTOR_SIZE_DEDUCER<const viennacl::circulant_matrix<ScalarType, Amat>, const viennacl::vector<ScalarType, A>, viennacl::op_prod>
249  {
251  const viennacl::vector<ScalarType, A> & rhs) { return lhs.size1(); }
252  };
253 
254  template <typename ScalarType, unsigned int Amat, unsigned int A>
255  struct VECTOR_SIZE_DEDUCER<const viennacl::compressed_matrix<ScalarType, Amat>, const viennacl::vector<ScalarType, A>, viennacl::op_prod>
256  {
258  const viennacl::vector<ScalarType, A> & rhs) { return lhs.size1(); }
259  };
260 
261  template <typename ScalarType, unsigned int Amat, unsigned int A>
262  struct VECTOR_SIZE_DEDUCER<const viennacl::coordinate_matrix<ScalarType, Amat>, const viennacl::vector<ScalarType, A>, viennacl::op_prod>
263  {
265  const viennacl::vector<ScalarType, A> & rhs) { return lhs.size1(); }
266  };
267 
268  //special case: transposed matrix-vector product: Return the number of cols(!) of the matrix
269  template <typename ScalarType, typename F, unsigned int Amat, unsigned int A>
270  struct VECTOR_SIZE_DEDUCER<const viennacl::matrix_expression< const viennacl::matrix<ScalarType, F, Amat>,
271  const viennacl::matrix<ScalarType, F, Amat>,
272  op_trans>,
273  const viennacl::vector<ScalarType, A>,
274  viennacl::op_prod>
275  {
278  op_trans> & lhs,
279  const viennacl::vector<ScalarType, A> & rhs) { return lhs.lhs().size2(); }
280  };
281 
282 
283 
284 
285 
290  template <typename T>
292  {
293  //force compiler error if type cannot be deduced
294  //typedef T ResultType;
295  };
296 
297  template <>
298  struct CPU_SCALAR_TYPE_DEDUCER< float >
299  {
300  typedef float ResultType;
301  };
302 
303  template <>
304  struct CPU_SCALAR_TYPE_DEDUCER< double >
305  {
306  typedef double ResultType;
307  };
308 
309  template <typename T>
310  struct CPU_SCALAR_TYPE_DEDUCER< viennacl::scalar<T> >
311  {
312  typedef T ResultType;
313  };
314 
315  template <typename T, unsigned int A>
316  struct CPU_SCALAR_TYPE_DEDUCER< viennacl::vector<T, A> >
317  {
318  typedef T ResultType;
319  };
320 
321  template <typename T, typename F, unsigned int A>
322  struct CPU_SCALAR_TYPE_DEDUCER< viennacl::matrix<T, F, A> >
323  {
324  typedef T ResultType;
325  };
326 
327 
328  template <typename T, typename F, unsigned int A>
329  struct CPU_SCALAR_TYPE_DEDUCER< viennacl::matrix_expression<const matrix<T, F, A>, const matrix<T, F, A>, op_trans> >
330  {
331  typedef T ResultType;
332  };
333 
334 
335  } //namespace tools
336 } //namespace viennacl
337 
338 
339 #endif