ViennaCL - The Vienna Computing Library  1.5.2
utils.hpp
Go to the documentation of this file.
1 #ifndef VIENNACL_GENERATOR_UTILS_HPP
2 #define VIENNACL_GENERATOR_UTILS_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 
21 
26 #include <sstream>
27 
28 #include "viennacl/ocl/forwards.h"
29 
30 #include "viennacl/traits/size.hpp"
31 
33 
34 namespace viennacl{
35 
36  namespace generator{
37 
38  namespace utils{
39 
40  template<class Fun>
41  static typename Fun::result_type call_on_host_scalar(scheduler::lhs_rhs_element element, Fun const & fun){
42  assert(element.type_family == scheduler::SCALAR_TYPE_FAMILY && bool("Must be called on a host scalar"));
43  switch(element.numeric_type){
45  return fun(element.host_float);
47  return fun(element.host_double);
48  default :
49  throw "not implemented";
50  }
51  }
52 
53  template<class Fun>
54  static typename Fun::result_type call_on_scalar(scheduler::lhs_rhs_element element, Fun const & fun){
55  assert(element.type_family == scheduler::SCALAR_TYPE_FAMILY && bool("Must be called on a scalar"));
56  switch(element.numeric_type){
58  return fun(*element.scalar_float);
60  return fun(*element.scalar_double);
61  default :
62  throw "not implemented";
63  }
64  }
65 
66  template<class Fun>
67  static typename Fun::result_type call_on_vector(scheduler::lhs_rhs_element element, Fun const & fun){
68  assert(element.type_family == scheduler::VECTOR_TYPE_FAMILY && bool("Must be called on a vector"));
69  switch(element.numeric_type){
71  return fun(*element.vector_float);
73  return fun(*element.vector_double);
74  default :
75  throw "not implemented";
76  }
77  }
78 
79  template<class Fun>
80  static typename Fun::result_type call_on_implicit_vector(scheduler::lhs_rhs_element element, Fun const & fun){
81  assert(element.type_family == scheduler::VECTOR_TYPE_FAMILY && bool("Must be called on a implicit_vector"));
82  assert(element.subtype == scheduler::IMPLICIT_VECTOR_TYPE && bool("Must be called on a implicit_vector"));
83  switch(element.numeric_type){
85  return fun(*element.implicit_vector_float);
87  return fun(*element.implicit_vector_double);
88  default :
89  throw "not implemented";
90  }
91  }
92 
93  template<class Fun>
94  static typename Fun::result_type call_on_matrix(scheduler::lhs_rhs_element element, Fun const & fun){
95  assert(element.type_family == scheduler::MATRIX_TYPE_FAMILY && bool("Must be called on a matrix"));
96  if (element.subtype == scheduler::DENSE_ROW_MATRIX_TYPE)
97  {
98  switch(element.numeric_type){
100  return fun(*element.matrix_row_float);
102  return fun(*element.matrix_row_double);
103  default :
104  throw "not implemented";
105  }
106  }
107  else
108  {
109  switch(element.numeric_type){
110  case scheduler::FLOAT_TYPE :
111  return fun(*element.matrix_col_float);
113  return fun(*element.matrix_col_double);
114  default :
115  throw "not implemented";
116  }
117  }
118  }
119 
120 
121  template<class Fun>
122  static typename Fun::result_type call_on_implicit_matrix(scheduler::lhs_rhs_element element, Fun const & fun){
123  assert(element.type_family == scheduler::MATRIX_TYPE_FAMILY && bool("Must be called on a matrix_vector"));
124  assert(element.subtype == scheduler::IMPLICIT_MATRIX_TYPE && bool("Must be called on a matrix_vector"));
125  switch(element.numeric_type){
126  case scheduler::FLOAT_TYPE :
127  return fun(*element.implicit_matrix_float);
129  return fun(*element.implicit_matrix_double);
130  default :
131  throw "not implemented";
132  }
133  }
134 
135  template<class Fun>
136  static typename Fun::result_type call_on_element(scheduler::lhs_rhs_element const & element, Fun const & fun){
137  switch(element.type_family){
139  if (element.subtype == scheduler::HOST_SCALAR_TYPE)
140  return call_on_host_scalar(element, fun);
141  else
142  return call_on_scalar(element, fun);
144  if (element.subtype == scheduler::IMPLICIT_VECTOR_TYPE)
145  return call_on_implicit_vector(element, fun);
146  else
147  return call_on_vector(element, fun);
149  if (element.subtype == scheduler::IMPLICIT_MATRIX_TYPE)
150  return call_on_implicit_matrix(element, fun);
151  else
152  return call_on_matrix(element,fun);
153  default:
154  throw "not implemented";
155  }
156  }
157 
161  result_type operator()(float const &) const { return sizeof(float); }
162  result_type operator()(double const &) const { return sizeof(double); }
163  template<class T> result_type operator()(T const &) const { return sizeof(typename viennacl::result_of::cpu_value_type<T>::type); }
164  };
165 
169  template<class T>
170  result_type operator()(T const &t) const { return viennacl::traits::internal_size(t); }
171  };
172 
174  struct handle_fun{
175  typedef cl_mem result_type;
176  template<class T>
177  result_type operator()(T const &t) const { return t.handle().opencl_handle(); }
178  };
179 
183  template<class T>
184  result_type operator()(T const &t) const { return viennacl::traits::internal_size1(t); }
185  };
186 
190  template<class T>
191  result_type operator()(T const &t) const { return viennacl::traits::internal_size2(t); }
192  };
193 
195  template<class T, class U>
196  struct is_same_type { enum { value = 0 }; };
197 
199  template<class T>
200  struct is_same_type<T,T> { enum { value = 1 }; };
203  template <class T>
204  inline std::string to_string ( T const t )
205  {
206  std::stringstream ss;
207  ss << t;
208  return ss.str();
209  }
210 
212  template<class T>
214 
215 
217  template<> struct type_to_string<float> { static const char * value() { return "float"; } };
218  template<> struct type_to_string<double> { static const char * value() { return "double"; } };
222  template<class T>
224 
226  template<> struct first_letter_of_type<float> { static char value() { return 'f'; } };
227  template<> struct first_letter_of_type<double> { static char value() { return 'd'; } };
228  template<> struct first_letter_of_type<viennacl::row_major> { static char value() { return 'r'; } };
229  template<> struct first_letter_of_type<viennacl::column_major> { static char value() { return 'c'; } };
233  class kernel_generation_stream : public std::ostream{
234  private:
235 
236  class kgenstream : public std::stringbuf{
237  public:
238  kgenstream(std::ostringstream& oss,unsigned int const & tab_count) : oss_(oss), tab_count_(tab_count){ }
239  int sync() {
240  for(unsigned int i=0 ; i<tab_count_;++i)
241  oss_ << " ";
242  oss_ << str();
243  str("");
244  return !oss_;
245  }
246  ~kgenstream() { pubsync(); }
247  private:
248  std::ostream& oss_;
249  unsigned int const & tab_count_;
250  };
251 
252  public:
253  kernel_generation_stream() : std::ostream(new kgenstream(oss,tab_count_)), tab_count_(0){ }
254 
255  std::string str(){ return oss.str(); }
256 
257  void inc_tab(){ ++tab_count_; }
258 
259  void dec_tab(){ --tab_count_; }
260 
261  ~kernel_generation_stream(){ delete rdbuf(); }
262 
263  private:
264  unsigned int tab_count_;
265  std::ostringstream oss;
266  };
267 
268 
269  }
270 
271  }
272 
273 }
274 #endif
A stream class where the kernel sources are streamed to. Takes care of indentation of the sources...
Definition: utils.hpp:233
This file provides the forward declarations for the OpenCL layer of ViennaCL.
Functor for obtaining the OpenCL handle from ViennaCL objects (vector, matrix, etc.).
Definition: utils.hpp:174
std::size_t vcl_size_t
Definition: forwards.h:58
Generic size and resize functionality for different vector and matrix types.
Definition: forwards.h:176
vcl_size_t result_type
Definition: utils.hpp:160
std::string str()
Definition: utils.hpp:255
vcl_size_t result_type
Definition: utils.hpp:182
Definition: forwards.h:217
Helper struct for obtaining the first letter of a type. Used internally by the generator only...
Definition: utils.hpp:223
Helper metafunction for checking whether two types are the same.
Definition: utils.hpp:196
Helper struct for converting a numerical type to its string representation.
Definition: utils.hpp:213
Functor for obtaining the internal number of columns of a ViennaCL matrix.
Definition: utils.hpp:188
result_type operator()(T const &t) const
Definition: utils.hpp:170
vcl_size_t result_type
Definition: utils.hpp:189
vcl_size_t internal_size(vector_base< NumericT > const &vec)
Helper routine for obtaining the buffer length of a ViennaCL vector.
Definition: size.hpp:268
result_type operator()(T const &) const
Definition: utils.hpp:163
Definition: forwards.h:170
Functor for obtaining the internal number of rows of a ViennaCL matrix.
Definition: utils.hpp:181
result_type operator()(float const &) const
Definition: utils.hpp:161
Functor for returning the size of the underlying scalar type in bytes.
Definition: utils.hpp:159
Definition: forwards.h:184
Definition: forwards.h:173
Provides the datastructures for dealing with a single statement such as 'x = y + z;'.
vcl_size_t internal_size2(matrix_base< NumericT, F > const &mat)
Helper routine for obtaining the internal number of entries per column of a ViennaCL matrix...
Definition: size.hpp:287
T::ERROR_CANNOT_DEDUCE_CPU_SCALAR_TYPE_FOR_T type
Definition: result_of.hpp:276
std::string to_string(T const t)
Definition: utils.hpp:204
std::string type_to_string(viennacl::row_major)
Definition: matrix.hpp:868
result_type operator()(T const &t) const
Definition: utils.hpp:191
result_type operator()(double const &) const
Definition: utils.hpp:162
vcl_size_t internal_size1(matrix_base< NumericT, F > const &mat)
Helper routine for obtaining the internal number of entries per row of a ViennaCL matrix...
Definition: size.hpp:279
Definition: forwards.h:216
result_type operator()(T const &t) const
Definition: utils.hpp:184
result_type operator()(T const &t) const
Definition: utils.hpp:177
Functor for returning the internal size of a vector.
Definition: utils.hpp:167
vcl_size_t result_type
Definition: utils.hpp:168
cl_mem result_type
Definition: utils.hpp:175