ViennaCL - The Vienna Computing Library  1.5.2
mapped_objects.hpp
Go to the documentation of this file.
1 #ifndef VIENNACL_GENERATOR_MAPPED_TYPE_HPP
2 #define VIENNACL_GENERATOR_MAPPED_TYPE_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 <string>
27 
31 
32 namespace viennacl{
33 
34  namespace generator{
35 
36  namespace detail{
37 
38 
42  protected:
44  struct node_info{
45  node_info() : mapping(NULL), statement(NULL), root_node(NULL) { }
46  mapping_type const * mapping;
47  scheduler::statement const * statement;
48  scheduler::statement_node const * root_node;
49  };
51  virtual std::string generate_default(std::pair<std::string, std::string> const & index) const = 0;
52  virtual std::string append_vector_size(std::string const & scalartype, unsigned int) const { return scalartype; }
53 
54  public:
55  mapped_object(std::string const & scalartype) : scalartype_(scalartype){ }
56  virtual std::string & append_kernel_arguments(std::set<std::string> &, std::string & str, unsigned int) const{ return str; }
57  std::string const & scalartype() const { return scalartype_; }
58  void access_name(std::string const & str) { access_name_ = str; }
59  std::string const & access_name() const { return access_name_; }
60  virtual std::string generate(std::pair<std::string, std::string> const & index, int) const{
61  if(!access_name_.empty())
62  return access_name_;
63  else
64  return generate_default(index);
65  }
66  virtual ~mapped_object(){ }
67  protected:
68  std::string access_name_;
69  std::string scalartype_;
70  };
71 
75  public:
76  mapped_binary_leaf(std::string const & scalartype) : mapped_object(scalartype){ }
77  mapping_type const & mapping() const { return *info_.mapping; }
78  scheduler::statement const & statement() const { return *info_.statement; }
79  scheduler::statement_node const & root_node() const { return *info_.root_node; }
80  std::string generate_default(std::pair<std::string, std::string> const &) const { return "";}
81  protected:
82  node_info info_;
83  };
84 
87  friend class map_functor;
88  public:
89  mapped_matrix_product(std::string const & scalartype) : mapped_binary_leaf(scalartype){ }
90  };
91 
94  public:
95  mapped_reduction(std::string const & scalartype) : mapped_binary_leaf(scalartype){ }
96  viennacl::scheduler::operation_node_type reduction_type() const { return reduction_type_; }
97  private:
99  };
100 
103  friend class map_functor;
104  public:
105  mapped_scalar_reduction(std::string const & scalartype) : mapped_reduction(scalartype){ }
106  };
107 
110  friend class map_functor;
111  public:
112  mapped_vector_reduction(std::string const & scalartype) : mapped_reduction(scalartype){ }
113  };
114 
117  friend class map_functor;
118  std::string generate_default(std::pair<std::string, std::string> const &) const{ return name_; }
119  public:
120  mapped_host_scalar(std::string const & scalartype) : mapped_object(scalartype){ }
121  std::string const & name() { return name_; }
122  std::string & append_kernel_arguments(std::set<std::string> & already_generated, std::string & str, unsigned int) const{
123  if(already_generated.insert(name_).second)
124  str += detail::generate_value_kernel_argument(scalartype_, name_);
125  return str;
126  }
127 
128  private:
129  std::string name_;
130  };
131 
133  class mapped_handle : public mapped_object{
134  virtual std::string offset(std::pair<std::string, std::string> const & index) const = 0;
135  virtual void append_optional_arguments(std::string &) const{ }
136  std::string generate_default(std::pair<std::string, std::string> const & index) const{ return name_ + '[' + offset(index) + ']'; }
137  public:
138  mapped_handle(std::string const & scalartype) : mapped_object(scalartype){ }
139 
140  std::string const & name() const { return name_; }
141 
142  void fetch(std::pair<std::string, std::string> const & index, unsigned int vectorization, std::set<std::string> & fetched, utils::kernel_generation_stream & stream) {
143  std::string new_access_name = name_ + "_private";
144  if(fetched.find(name_)==fetched.end()){
145  stream << scalartype_;
146  if(vectorization > 1) stream << vectorization;
147  stream << " " << new_access_name << " = " << generate_default(index) << ';' << std::endl;
148  fetched.insert(name_);
149  }
150  access_name_ = new_access_name;
151  }
152 
153  void write_back(std::pair<std::string, std::string> const & index, std::set<std::string> & fetched, utils::kernel_generation_stream & stream) {
154  std::string old_access_name = access_name_ ;
155  access_name_ = "";
156  if(fetched.find(name_)!=fetched.end()){
157  stream << generate_default(index) << " = " << old_access_name << ';' << std::endl;
158  fetched.erase(name_);
159  }
160  }
161 
162  std::string & append_kernel_arguments(std::set<std::string> & already_generated, std::string & str, unsigned int vector_size) const{
163  if(already_generated.insert(name_).second){
164  std::string vector_scalartype = append_vector_size(scalartype_, vector_size);
165  str += detail::generate_pointer_kernel_argument("__global", vector_scalartype, name_);
166  append_optional_arguments(str);
167  }
168  return str;
169  }
170 
171  protected:
172  std::string name_;
173  };
174 
176  class mapped_scalar : public mapped_handle{
177  friend class map_functor;
178  private:
179  std::string offset(std::pair<std::string, std::string> const &) const { return "0"; }
180  public:
181  mapped_scalar(std::string const & scalartype) : mapped_handle(scalartype){ }
182  };
183 
184 
186  class mapped_buffer : public mapped_handle{
187  protected:
188  std::string append_vector_size(std::string const & scalartype, unsigned int vector_size) const {
189  if(vector_size>1)
190  return scalartype + utils::to_string(vector_size);
191  else
192  return scalartype;
193  }
194  public:
195  mapped_buffer(std::string const & scalartype) : mapped_handle(scalartype){ }
196  virtual std::string generate(std::pair<std::string, std::string> const & index, int vector_element) const{
197  if(vector_element>-1)
198  return mapped_object::generate(index, vector_element)+".s"+utils::to_string(vector_element);
199  return mapped_object::generate(index, vector_element);
200  }
201 
202  };
203 
205  class mapped_vector : public mapped_buffer{
206  friend class map_functor;
207  std::string offset(std::pair<std::string, std::string> const & index) const {
208  if(info_.statement){
209  std::string str;
210  detail::generate_all_rhs(*info_.statement, *info_.root_node, index, -1, str, *info_.mapping);
211  return str;
212  }
213  else
214  return index.first;
215  }
216 
217  void append_optional_arguments(std::string & str) const{
218  if(!start_name_.empty())
219  str += detail::generate_value_kernel_argument("unsigned int", start_name_);
220  if(!stride_name_.empty())
221  str += detail::generate_value_kernel_argument("unsigned int", stride_name_);
222  if(!shift_name_.empty())
223  str += detail::generate_value_kernel_argument("unsigned int", shift_name_);
224  }
225  public:
226  mapped_vector(std::string const & scalartype) : mapped_buffer(scalartype){ }
227  private:
228  node_info info_;
229 
230  std::string start_name_;
231  std::string stride_name_;
232  std::string shift_name_;
233  };
234 
236  class mapped_matrix : public mapped_buffer{
237  friend class map_functor;
238  void append_optional_arguments(std::string & str) const{
239  if(!start1_name_.empty())
240  str += detail::generate_value_kernel_argument("unsigned int", start1_name_);
241  if(!stride1_name_.empty())
242  str += detail::generate_value_kernel_argument("unsigned int", stride1_name_);
243  if(!start2_name_.empty())
244  str += detail::generate_value_kernel_argument("unsigned int", start2_name_);
245  if(!stride2_name_.empty())
246  str += detail::generate_value_kernel_argument("unsigned int", stride2_name_);
247  }
248  public:
249  mapped_matrix(std::string const & scalartype) : mapped_buffer(scalartype){ }
250 
251  bool is_row_major() const { return is_row_major_; }
252 
253  std::string const & size1() const { return size1_; }
254 
255  std::string const & size2() const { return size2_; }
256 
257  void bind_sizes(std::string const & size1, std::string const & size2) const{
258  size1_ = size1;
259  size2_ = size2;
260  }
261 
262  std::string offset(std::pair<std::string, std::string> const & index) const {
263  std::string i = index.first;
264  std::string j = index.second;
265  if(is_row_major_)
266  if(j=="0")
267  return '(' + i + ')' + '*' + size2_;
268  else
269  return '(' + i + ')' + '*' + size2_ + "+ (" + j + ')';
270  else
271  if(i=="0")
272  return "(" + j + ')' + '*' + size1_;
273  else
274  return '(' + i + ')' + "+ (" + j + ')' + '*' + size1_;
275  }
276 
277  private:
278  mutable std::string size1_;
279  mutable std::string size2_;
280 
281  std::string start1_name_;
282  std::string stride1_name_;
283  std::string shift1_name_;
284  std::string start2_name_;
285  std::string stride2_name_;
286  std::string shift2_name_;
287  bool is_row_major_;
288  };
289 
292  friend class map_functor;
293  std::string value_name_;
294  std::string index_name_;
295  public:
296  mapped_implicit_vector(std::string const & scalartype) : mapped_object(scalartype){ }
297  std::string generate_default(std::pair<std::string, std::string> const & /*index*/) const{
298  return value_name_;
299  }
300  std::string & append_kernel_arguments(std::set<std::string> & /*already_generated*/, std::string & str, unsigned int /*vector_size*/) const{
301  if(!value_name_.empty())
302  str += detail::generate_value_kernel_argument(scalartype_, value_name_);
303  if(!index_name_.empty())
304  str += detail::generate_value_kernel_argument("unsigned int", index_name_);
305  return str;
306  }
307  };
308 
311  friend class map_functor;
312  std::string value_name_;
313  public:
314  mapped_implicit_matrix(std::string const & scalartype) : mapped_object(scalartype){ }
315  std::string generate_default(std::pair<std::string, std::string> const & /* index */) const{
316  return value_name_;
317  }
318  std::string & append_kernel_arguments(std::set<std::string> & /*already generated*/, std::string & str, unsigned int /*vector size*/) const{
319  if(!value_name_.empty())
320  str += detail::generate_value_kernel_argument(scalartype_, value_name_);
321  return str;
322  }
323  };
324 
325  inline std::string generate(std::pair<std::string, std::string> const & index, int vector_element, mapped_object const & s){
326  return s.generate(index, vector_element);
327  }
328 
329  static void fetch(std::pair<std::string, std::string> const & index, unsigned int vectorization, std::set<std::string> & fetched, utils::kernel_generation_stream & stream, mapped_object & s){
330  if(mapped_handle * p = dynamic_cast<mapped_handle *>(&s))
331  p->fetch(index, vectorization, fetched, stream);
332  }
333 
334  static std::string & append_kernel_arguments(std::set<std::string> & already_generated, std::string & str, unsigned int vector_size, mapped_object const & s){
335  return s.append_kernel_arguments(already_generated, str, vector_size);
336  }
337 
338  }
339 
340  }
341 
342 }
343 #endif
A stream class where the kernel sources are streamed to. Takes care of indentation of the sources...
Definition: utils.hpp:233
std::map< key_type, container_ptr_type > mapping_type
Definition: forwards.h:122
std::string access_name_
Definition: mapped_objects.hpp:68
mapped_handle(std::string const &scalartype)
Definition: mapped_objects.hpp:138
mapped_matrix(std::string const &scalartype)
Definition: mapped_objects.hpp:249
std::string generate_default(std::pair< std::string, std::string > const &) const
Definition: mapped_objects.hpp:297
node_info info_
Definition: mapped_objects.hpp:82
Internal utils for a dynamic OpenCL kernel generation.
std::string offset(std::pair< std::string, std::string > const &index) const
Definition: mapped_objects.hpp:262
scheduler::statement_node const & root_node() const
Definition: mapped_objects.hpp:79
viennacl::scheduler::operation_node_type reduction_type() const
Definition: mapped_objects.hpp:96
Base class for datastructures passed by pointer.
Definition: mapped_objects.hpp:133
Mapping of a matrix to a generator class.
Definition: mapped_objects.hpp:236
Base class for mapping viennacl datastructure to generator-friendly structures.
Definition: mapped_objects.hpp:41
virtual std::string generate(std::pair< std::string, std::string > const &index, int) const
Definition: mapped_objects.hpp:60
Mapping of a scalar reduction (based on inner product)
Definition: mapped_objects.hpp:102
Base class for mapping a reduction.
Definition: mapped_objects.hpp:93
std::string const & size2() const
Definition: mapped_objects.hpp:255
void write_back(std::pair< std::string, std::string > const &index, std::set< std::string > &fetched, utils::kernel_generation_stream &stream)
Definition: mapped_objects.hpp:153
mapped_host_scalar(std::string const &scalartype)
Definition: mapped_objects.hpp:120
std::string const & name() const
Definition: mapped_objects.hpp:140
std::string name_
Definition: mapped_objects.hpp:172
Mapping of a scalar to a generator class.
Definition: mapped_objects.hpp:176
virtual std::string generate(std::pair< std::string, std::string > const &index, int vector_element) const
Definition: mapped_objects.hpp:196
Main namespace in ViennaCL. Holds all the basic types such as vector, matrix, etc. and defines operations upon them.
Definition: cpu_ram.hpp:29
mapped_vector_reduction(std::string const &scalartype)
Definition: mapped_objects.hpp:112
virtual std::string & append_kernel_arguments(std::set< std::string > &, std::string &str, unsigned int) const
Definition: mapped_objects.hpp:56
std::string const & access_name() const
Definition: mapped_objects.hpp:59
void fetch(std::pair< std::string, std::string > const &index, unsigned int vectorization, std::set< std::string > &fetched, utils::kernel_generation_stream &stream)
Definition: mapped_objects.hpp:142
std::string const & size1() const
Definition: mapped_objects.hpp:253
mapped_vector(std::string const &scalartype)
Definition: mapped_objects.hpp:226
mapped_matrix_product(std::string const &scalartype)
Definition: mapped_objects.hpp:89
std::string & append_kernel_arguments(std::set< std::string > &already_generated, std::string &str, unsigned int vector_size) const
Definition: mapped_objects.hpp:162
bool is_row_major() const
Definition: mapped_objects.hpp:251
Functor to map the statements to the types defined in mapped_objects.hpp.
Definition: map_functor.hpp:47
mapped_buffer(std::string const &scalartype)
Definition: mapped_objects.hpp:195
void access_name(std::string const &str)
Definition: mapped_objects.hpp:58
mapped_implicit_vector(std::string const &scalartype)
Definition: mapped_objects.hpp:296
virtual std::string append_vector_size(std::string const &scalartype, unsigned int) const
Definition: mapped_objects.hpp:52
mapped_scalar_reduction(std::string const &scalartype)
Definition: mapped_objects.hpp:105
mapped_implicit_matrix(std::string const &scalartype)
Definition: mapped_objects.hpp:314
Forwards declaration.
Base class for mapping binary leaves (inner product-based, matrix vector product-base, matrix-matrix product based...)
Definition: mapped_objects.hpp:74
Provides the datastructures for dealing with a single statement such as 'x = y + z;'.
Mapping of a vector to a generator class.
Definition: mapped_objects.hpp:205
operation_node_type
Enumeration for identifying the possible operations.
Definition: forwards.h:61
Mapping of a host scalar to a generator class.
Definition: mapped_objects.hpp:116
mapping_type const & mapping() const
Definition: mapped_objects.hpp:77
std::string & append_kernel_arguments(std::set< std::string > &, std::string &str, unsigned int) const
Definition: mapped_objects.hpp:318
std::string append_vector_size(std::string const &scalartype, unsigned int vector_size) const
Definition: mapped_objects.hpp:188
mapped_reduction(std::string const &scalartype)
Definition: mapped_objects.hpp:95
std::string & append_kernel_arguments(std::set< std::string > &already_generated, std::string &str, unsigned int) const
Definition: mapped_objects.hpp:122
mapped_object(std::string const &scalartype)
Definition: mapped_objects.hpp:55
void bind_sizes(std::string const &size1, std::string const &size2) const
Definition: mapped_objects.hpp:257
std::string to_string(T const t)
Definition: utils.hpp:204
std::string generate_default(std::pair< std::string, std::string > const &) const
Definition: mapped_objects.hpp:315
Base class for mapping buffer-based objects to a generator class.
Definition: mapped_objects.hpp:186
std::string generate_default(std::pair< std::string, std::string > const &) const
Definition: mapped_objects.hpp:80
Mapping of a vector reduction (based on matrix-vector product)
Definition: mapped_objects.hpp:109
std::string & append_kernel_arguments(std::set< std::string > &, std::string &str, unsigned int) const
Definition: mapped_objects.hpp:300
mapped_binary_leaf(std::string const &scalartype)
Definition: mapped_objects.hpp:76
scheduler::statement const & statement() const
Definition: mapped_objects.hpp:78
The main class for representing a statement such as x = inner_prod(y,z); at runtime.
Definition: forwards.h:447
virtual std::string generate_default(std::pair< std::string, std::string > const &index) const =0
std::string scalartype_
Definition: mapped_objects.hpp:69
std::string generate(std::pair< std::string, std::string > const &index, int vector_element, mapped_object const &s)
Definition: mapped_objects.hpp:325
std::string const & scalartype() const
Definition: mapped_objects.hpp:57
virtual ~mapped_object()
Definition: mapped_objects.hpp:66
Mapping of a implicit matrix to a generator class.
Definition: mapped_objects.hpp:310
mapped_scalar(std::string const &scalartype)
Definition: mapped_objects.hpp:181
Main datastructure for an node in the statement tree.
Definition: forwards.h:420
std::string const & name()
Definition: mapped_objects.hpp:121
Mapping of a matrix product.
Definition: mapped_objects.hpp:86
Mapping of a implicit vector to a generator class.
Definition: mapped_objects.hpp:291