ViennaCL - The Vienna Computing Library  1.7.1
Free open-source GPU-accelerated linear algebra and solver library.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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-2016, 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 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 NumericT, typename F, unsigned int AlignmentV>
42 struct MATRIX_ITERATOR_INCREMENTER<viennacl::row_iteration, viennacl::matrix<NumericT, F, AlignmentV> >
43 {
44  static void apply(const viennacl::matrix<NumericT, F, AlignmentV> & /*mat*/, unsigned int & row, unsigned int & /*col*/)
45  {
46  ++row;
47  }
48 };
49 
50 template<class NumericT, typename F, unsigned int AlignmentV>
51 struct MATRIX_ITERATOR_INCREMENTER<viennacl::col_iteration, viennacl::matrix<NumericT, F, AlignmentV> >
52 {
53  static void apply(const viennacl::matrix<NumericT, F, AlignmentV> & /*mat*/, unsigned int & /*row*/, unsigned int & col)
54  {
55  ++col;
56  }
57 };
62 template<typename T>
64 {
65  typedef typename T::ERROR_SCALAR_MUST_HAVE_TEMPLATE_ARGUMENT_FLOAT_OR_DOUBLE ResultType;
66 };
67 
69 template<>
71 {
72  typedef float ResultType;
73 };
74 
75 template<>
76 struct CHECK_SCALAR_TEMPLATE_ARGUMENT<double>
77 {
78  typedef double ResultType;
79 };
89 inline std::string read_text_from_file(const std::string & filename)
90 {
91  std::ifstream f(filename.c_str());
92  if (!f) return std::string();
93 
94  std::stringstream result;
95  std::string tmp;
96  while (std::getline(f, tmp))
97  result << tmp << std::endl;
98 
99  return result.str();
100 }
101 
109 inline std::string str_replace(const std::string & text, std::string to_search, std::string to_replace)
110 {
111  std::string::size_type pos = 0;
112  std::string result;
113  std::string::size_type found;
114  while ( (found = text.find(to_search, pos)) != std::string::npos )
115  {
116  result.append(text.substr(pos,found-pos));
117  result.append(to_replace);
118  pos = found + to_search.length();
119  }
120  if (pos < text.length())
121  result.append(text.substr(pos));
122  return result;
123 }
124 
132 template<class INT_TYPE>
134 {
135  if (to_reach % base == 0) return to_reach;
136  return ((to_reach / base) + 1) * base;
137 }
138 
139 
147 template<class INT_TYPE>
149 {
150  if (to_reach % base == 0) return to_reach;
151  return (to_reach / base) * base;
152 }
153 
160 int inline find_and_replace(std::string & source, std::string const & find, std::string const & replace)
161 {
162  int num=0;
163  vcl_size_t fLen = find.size();
164  vcl_size_t rLen = replace.size();
165  for (vcl_size_t pos=0; (pos=source.find(find, pos))!=std::string::npos; pos+=rLen)
166  {
167  num++;
168  source.replace(pos, fLen, replace);
169  }
170  return num;
171 }
172 
174 template<class InputIterator, class UnaryPredicate>
175 bool any_of (InputIterator first, InputIterator last, UnaryPredicate pred)
176 {
177  while (first!=last)
178  {
179  if (pred(*first)) return true;
180  ++first;
181  }
182  return false;
183 }
184 
191 inline std::string make_double_kernel(std::string const & source, std::string const & fp_extension)
192 {
193  std::stringstream ss;
194  ss << "#pragma OPENCL EXTENSION " << fp_extension << " : enable\n\n";
195 
196  std::string result = ss.str();
197  result.append(str_replace(source, "float", "double"));
198  return result;
199 }
200 
201 
203 template<typename T>
205 {
206  typedef T ResultType;
207 };
208 
210 template<typename T>
211 struct CONST_REMOVER<const T>
212 {
213  typedef T ResultType;
214 };
218 
224 template<typename T>
226 {
227  //force compiler error if type cannot be deduced
228  //typedef T ResultType;
229 };
230 
232 template<>
233 struct CPU_SCALAR_TYPE_DEDUCER< float >
234 {
235  typedef float ResultType;
236 };
237 
238 template<>
239 struct CPU_SCALAR_TYPE_DEDUCER< double >
240 {
241  typedef double ResultType;
242 };
243 
244 template<typename T>
245 struct CPU_SCALAR_TYPE_DEDUCER< viennacl::scalar<T> >
246 {
247  typedef T ResultType;
248 };
249 
250 template<typename T, unsigned int A>
251 struct CPU_SCALAR_TYPE_DEDUCER< viennacl::vector<T, A> >
252 {
253  typedef T ResultType;
254 };
255 
256 template<typename T, typename F, unsigned int A>
257 struct CPU_SCALAR_TYPE_DEDUCER< viennacl::matrix<T, F, A> >
258 {
259  typedef T ResultType;
260 };
261 
262 
263 template<typename T, typename F, unsigned int A>
264 struct CPU_SCALAR_TYPE_DEDUCER< viennacl::matrix_expression<const matrix<T, F, A>, const matrix<T, F, A>, op_trans> >
265 {
266  typedef T ResultType;
267 };
270 //
271 // 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)
272 //
273 
274 template<typename HostScalarType>
276 
277 template<typename HostScalarType>
280 viennacl::op_flip_sign> const &
283  viennacl::op_flip_sign> const & s) { return s; }
284 
285 template<typename HostScalarType>
286 HostScalarType promote_if_host_scalar(float s) { return s; }
287 
288 template<typename HostScalarType>
289 HostScalarType promote_if_host_scalar(double s) { return s; }
290 
291 template<typename HostScalarType>
292 HostScalarType promote_if_host_scalar(long s) { return s; }
293 
294 template<typename HostScalarType>
295 HostScalarType promote_if_host_scalar(unsigned long s) { return s; }
296 
297 template<typename HostScalarType>
298 HostScalarType promote_if_host_scalar(int s) { return s; }
299 
300 template<typename HostScalarType>
301 HostScalarType promote_if_host_scalar(unsigned int s) { return s; }
302 
303 template<class T>
304 inline std::string to_string ( T const t )
305 {
306  std::stringstream ss;
307  ss << t;
308  return ss.str();
309 }
310 
311 } //namespace tools
312 } //namespace viennacl
313 
314 
315 #endif
viennacl::scalar< HostScalarType > const & promote_if_host_scalar(viennacl::scalar< HostScalarType > const &s)
Definition: tools.hpp:275
Removes the const qualifier from a type.
Definition: tools.hpp:204
This class represents a single scalar value on the GPU and behaves mostly like a built-in scalar type...
Definition: forwards.h:227
Adapter classes for sparse matrices made of the STL type std::vector >
This file provides the forward declarations for the main types used within ViennaCL.
A dense matrix class.
Definition: forwards.h:375
A proxy for scalar expressions (e.g. from inner vector products)
Definition: forwards.h:230
bool any_of(InputIterator first, InputIterator last, UnaryPredicate pred)
Returns true if pred returns true for any of the elements in the range [first,last), and false otherwise.
Definition: tools.hpp:175
T::ERROR_SCALAR_MUST_HAVE_TEMPLATE_ARGUMENT_FLOAT_OR_DOUBLE ResultType
Definition: tools.hpp:65
Obtain the cpu scalar type from a type, including a GPU type like viennacl::scalar ...
Definition: tools.hpp:225
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:160
std::string read_text_from_file(const std::string &filename)
Reads a text from a file into a std::string.
Definition: tools.hpp:89
std::size_t vcl_size_t
Definition: forwards.h:75
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 align_to_multiple(INT_TYPE to_reach, INT_TYPE base)
Rounds an integer to the next multiple of another integer.
Definition: tools.hpp:133
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:191
INT_TYPE round_down_to_prevous_multiple(INT_TYPE to_reach, INT_TYPE base)
Rounds an integer to the previous multiple of another integer.
Definition: tools.hpp:148
A tag class representing sign flips (for scalars only. Vectors and matrices use the standard multipli...
Definition: forwards.h:223
A guard that checks whether the floating point type of GPU types is either float or double...
Definition: tools.hpp:63
std::string to_string(T const t)
Definition: tools.hpp:304
static void apply(const MATRIXTYPE &, unsigned int &, unsigned int &)
Definition: forwards.h:621
std::string str_replace(const std::string &text, std::string to_search, std::string to_replace)
Replaces all occurances of a substring by another stringstream.
Definition: tools.hpp:109