ViennaCL - The Vienna Computing Library  1.5.2
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-2014, Institute for Microelectronics,
6  Institute for Analysis and Scientific Computing,
7  TU Wien.
8  Portions of this software are copyright by UChicago Argonne, LLC.
9 
10  -----------------
11  ViennaCL - The Vienna Computing Library
12  -----------------
13 
14  Project Head: Karl Rupp rupp@iue.tuwien.ac.at
15 
16  (A list of authors and contributors can be found in the PDF manual)
17 
18  License: MIT (X11), see file LICENSE in the base directory
19 ============================================================================= */
20 
25 #include <string>
26 #include <fstream>
27 #include <sstream>
28 #include "viennacl/forwards.h"
30 
31 #include <vector>
32 #include <map>
33 
34 namespace viennacl
35 {
36  namespace tools
37  {
38 
41  template <class SCALARTYPE, typename F, unsigned int ALIGNMENT>
42  struct MATRIX_ITERATOR_INCREMENTER<viennacl::row_iteration, viennacl::matrix<SCALARTYPE, F, ALIGNMENT> >
43  {
44  static void apply(const viennacl::matrix<SCALARTYPE, F, ALIGNMENT> & /*mat*/, unsigned int & row, unsigned int & /*col*/) { ++row; }
45  };
46 
47  template <class SCALARTYPE, typename F, unsigned int ALIGNMENT>
48  struct MATRIX_ITERATOR_INCREMENTER<viennacl::col_iteration, viennacl::matrix<SCALARTYPE, F, ALIGNMENT> >
49  {
50  static void apply(const viennacl::matrix<SCALARTYPE, F, ALIGNMENT> & /*mat*/, unsigned int & /*row*/, unsigned int & col) { ++col; }
51  };
56  template <typename T>
58  {
59  typedef typename T::ERROR_SCALAR_MUST_HAVE_TEMPLATE_ARGUMENT_FLOAT_OR_DOUBLE ResultType;
60  };
61 
63  template <>
64  struct CHECK_SCALAR_TEMPLATE_ARGUMENT<float>
65  {
66  typedef float ResultType;
67  };
68 
69  template <>
70  struct CHECK_SCALAR_TEMPLATE_ARGUMENT<double>
71  {
72  typedef double ResultType;
73  };
83  inline std::string readTextFromFile(const std::string & filename)
84  {
85  std::ifstream f(filename.c_str());
86  if (!f) return std::string();
87 
88  std::stringstream result;
89  std::string tmp;
90  while (std::getline(f, tmp))
91  result << tmp << std::endl;
92 
93  return result.str();
94  }
95 
103  inline std::string strReplace(const std::string & text, std::string to_search, std::string to_replace)
104  {
105  std::string::size_type pos = 0;
106  std::string result;
107  std::string::size_type found;
108  while( (found = text.find(to_search, pos)) != std::string::npos )
109  {
110  result.append(text.substr(pos,found-pos));
111  result.append(to_replace);
112  pos = found + to_search.length();
113  }
114  if (pos < text.length())
115  result.append(text.substr(pos));
116  return result;
117  }
118 
126  template <class INT_TYPE>
128  {
129  if (to_reach % base == 0) return to_reach;
130  return ((to_reach / base) + 1) * base;
131  }
132 
133 
141  template <class INT_TYPE>
143  {
144  if (to_reach % base == 0) return to_reach;
145  return (to_reach / base) * base;
146  }
147 
154  int inline find_and_replace(std::string & source, std::string const & find, std::string const & replace)
155  {
156  int num=0;
157  vcl_size_t fLen = find.size();
158  vcl_size_t rLen = replace.size();
159  for (vcl_size_t pos=0; (pos=source.find(find, pos))!=std::string::npos; pos+=rLen)
160  {
161  num++;
162  source.replace(pos, fLen, replace);
163  }
164  return num;
165  }
166 
173  inline std::string make_double_kernel(std::string const & source, std::string const & fp_extension)
174  {
175  std::stringstream ss;
176  ss << "#pragma OPENCL EXTENSION " << fp_extension << " : enable\n\n";
177 
178  std::string result = ss.str();
179  result.append(strReplace(source, "float", "double"));
180  return result;
181  }
182 
183 
185  template <typename T>
187  {
188  typedef T ResultType;
189  };
190 
192  template <typename T>
193  struct CONST_REMOVER<const T>
194  {
195  typedef T ResultType;
196  };
200 
206  template <typename T>
208  {
209  //force compiler error if type cannot be deduced
210  //typedef T ResultType;
211  };
212 
214  template <>
215  struct CPU_SCALAR_TYPE_DEDUCER< float >
216  {
217  typedef float ResultType;
218  };
219 
220  template <>
221  struct CPU_SCALAR_TYPE_DEDUCER< double >
222  {
223  typedef double ResultType;
224  };
225 
226  template <typename T>
227  struct CPU_SCALAR_TYPE_DEDUCER< viennacl::scalar<T> >
228  {
229  typedef T ResultType;
230  };
231 
232  template <typename T, unsigned int A>
233  struct CPU_SCALAR_TYPE_DEDUCER< viennacl::vector<T, A> >
234  {
235  typedef T ResultType;
236  };
237 
238  template <typename T, typename F, unsigned int A>
239  struct CPU_SCALAR_TYPE_DEDUCER< viennacl::matrix<T, F, A> >
240  {
241  typedef T ResultType;
242  };
243 
244 
245  template <typename T, typename F, unsigned int A>
246  struct CPU_SCALAR_TYPE_DEDUCER< viennacl::matrix_expression<const matrix<T, F, A>, const matrix<T, F, A>, op_trans> >
247  {
248  typedef T ResultType;
249  };
252  //
253  // Converts a scalar type when necessary unless it is a viennacl::scalar<> (typical use-case: convert user-provided floats to double (and vice versa) for OpenCL kernels)
254  //
255 
256  template <typename HostScalarType>
258 
259  template <typename HostScalarType>
262  viennacl::op_flip_sign> const &
263  promote_if_host_scalar(viennacl::scalar_expression<const viennacl::scalar<HostScalarType>,
264  const viennacl::scalar<HostScalarType>,
265  viennacl::op_flip_sign> const & s) { return s; }
266 
267  template <typename HostScalarType>
268  HostScalarType promote_if_host_scalar(float s) { return s; }
269 
270  template <typename HostScalarType>
271  HostScalarType promote_if_host_scalar(double s) { return s; }
272 
273  template <typename HostScalarType>
274  HostScalarType promote_if_host_scalar(long s) { return s; }
275 
276  template <typename HostScalarType>
277  HostScalarType promote_if_host_scalar(unsigned long s) { return s; }
278 
279  template <typename HostScalarType>
280  HostScalarType promote_if_host_scalar(int s) { return s; }
281 
282  template <typename HostScalarType>
283  HostScalarType promote_if_host_scalar(unsigned int s) { return s; }
284 
285  } //namespace tools
286 } //namespace viennacl
287 
288 
289 #endif
viennacl::scalar< HostScalarType > const & promote_if_host_scalar(viennacl::scalar< HostScalarType > const &s)
Definition: tools.hpp:257
std::size_t vcl_size_t
Definition: forwards.h:58
Removes the const qualifier from a type.
Definition: tools.hpp:186
Adapter classes for sparse matrices made of the STL type std::vector >
This class represents a single scalar value on the GPU and behaves mostly like a built-in scalar type...
Definition: forwards.h:172
A dense matrix class.
Definition: forwards.h:293
This file provides the forward declarations for the main types used within ViennaCL.
T::ERROR_SCALAR_MUST_HAVE_TEMPLATE_ARGUMENT_FLOAT_OR_DOUBLE ResultType
Definition: tools.hpp:59
Main namespace in ViennaCL. Holds all the basic types such as vector, matrix, etc. and defines operations upon them.
Definition: cpu_ram.hpp:29
Obtain the cpu scalar type from a type, including a GPU type like viennacl::scalar ...
Definition: tools.hpp:207
int find_and_replace(std::string &source, std::string const &find, std::string const &replace)
Replace in a source string a pattern by another.
Definition: tools.hpp:154
A proxy for scalar expressions (e.g. from inner vector products)
Definition: forwards.h:175
std::string strReplace(const std::string &text, std::string to_search, std::string to_replace)
Replaces all occurances of a substring by another stringstream.
Definition: tools.hpp:103
vector_expression< const matrix_base< NumericT, F >, const unsigned int, op_row > row(const matrix_base< NumericT, F > &A, unsigned int i)
Definition: matrix.hpp:910
INT_TYPE roundDownToPreviousMultiple(INT_TYPE to_reach, INT_TYPE base)
Rounds an integer to the previous multiple of another integer.
Definition: tools.hpp:142
T ResultType
Definition: tools.hpp:188
INT_TYPE align_to_multiple(INT_TYPE to_reach, INT_TYPE base)
Rounds an integer to the next multiple of another integer.
Definition: tools.hpp:127
Definition: forwards.h:211
std::string make_double_kernel(std::string const &source, std::string const &fp_extension)
Create a double precision kernel out of a single precision kernel.
Definition: tools.hpp:173
static void apply(const MATRIXTYPE &, unsigned int &, unsigned int &)
Definition: forwards.h:523
std::string readTextFromFile(const std::string &filename)
Reads a text from a file into a std::string.
Definition: tools.hpp:83
A tag class representing sign flips (for scalars only. Vectors and matrices use the standard multipli...
Definition: forwards.h:168
A guard that checks whether the floating point type of GPU types is either float or double...
Definition: tools.hpp:57