ViennaCL - The Vienna Computing Library  1.6.1
Free open-source GPU-accelerated linear algebra and solver library.
forwards.h
Go to the documentation of this file.
1 #ifndef VIENNACL_DEVICE_SPECIFIC_FORWARDS_H
2 #define VIENNACL_DEVICE_SPECIFIC_FORWARDS_H
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 <list>
27 #include <map>
28 #include <set>
29 #include <stdexcept>
30 
32 
33 #include "viennacl/ocl/forwards.h"
36 
38 
39 namespace viennacl
40 {
41 namespace device_specific
42 {
43 
44 //Error codes
45 static const int TEMPLATE_VALID = 0;
46 static const int TEMPLATE_LOCAL_MEMORY_OVERFLOW = -1;
47 static const int TEMPLATE_WORK_GROUP_SIZE_OVERFLOW = -2;
48 static const int TEMPLATE_LOCAL_SIZE_0_OVERFLOW = -3;
49 static const int TEMPLATE_LOCAL_SIZE_1_OVERFLOW = -4;
50 static const int TEMPLATE_LOCAL_SIZE_2_OVERFLOW = -5;
51 static const int TEMPLATE_LOCAL_SIZE_NOT_WARP_MULTIPLE = -6;
52 static const int TEMPLATE_INVALID_SIMD_WIDTH = -7;
53 static const int TEMPLATE_INVALID_FETCHING_POLICY_TYPE= -9;
54 
55 static const int TEMPLATE_GLOBAL_MEMORY_REQUIRES_ZERO_LOCAL_FETCH = -10;
56 static const int TEMPLATE_MS_NS_MUST_BE_SIMD_WIDTH_MULTIPLE = -11;
57 static const int TEMPLATE_KS_MUST_BE_SMALLER_THAN_KL = -12;
58 static const int TEMPLATE_SIMD_WIDTH_MUST_BE_ONE = -13;
59 static const int TEMPLATE_LOCAL_FETCH_PRODUCT_MUST_MATCH_LOCAL_SIZE_PRODUCT = -14;
60 static const int TEMPLATE_LOCAL_FETCH_0_MUST_BE_KL_MULTIPLE = -15;
61 static const int TEMPLATE_LOCAL_FETCH_0_MUST_BE_NL_MULTIPLE = -16;
62 static const int TEMPLATE_LOCAL_FETCH_1_MUST_BE_KL_MULTIPLE = -17;
63 static const int TEMPLATE_LOCAL_FETCH_1_MUST_BE_ML_MULTIPLE = -18;
64 
66 {
67  index_tuple(std::string const & _i, std::string const & _bound0) : i(_i), bound0(_bound0), j(""), bound1(""){ }
68  index_tuple(std::string const & _i, std::string const & _bound0, std::string const & _j, std::string const & _bound1) : i(_i), bound0(_bound0), j(_j), bound1(_bound1){ }
69  std::string i;
70  std::string bound0;
71  std::string j;
72  std::string bound1;
73 };
74 
76 {
78 }
79 
81 {
85 }
86 
88 {
89  scheduler::statement_node const * current = &array[root];
91  current = &array[current->lhs.node_index];
92  return *current;
93 }
94 
96 {
108 };
109 
111 {
112  switch (type)
113  {
114  case SCALAR_AXPY_TYPE : return "Scalar AXPY";
115  case VECTOR_AXPY_TYPE : return "Vector AXPY";
116  case MATRIX_AXPY_TYPE : return "Matrix AXPY";
117  case REDUCTION_TYPE : return "Reduction";
118  case ROW_WISE_REDUCTION_Nx_TYPE : return "Row-wise reduction: Ax";
119  case ROW_WISE_REDUCTION_Tx_TYPE : return "Row-wise reduction : Tx";
120  case MATRIX_PRODUCT_NN_TYPE : return "Matrix-Matrix Product : AA";
121  case MATRIX_PRODUCT_TN_TYPE : return "Matrix-Matrix Product : TA";
122  case MATRIX_PRODUCT_NT_TYPE : return "Matrix-Matrix Product : AT";
123  case MATRIX_PRODUCT_TT_TYPE : return "Matrix-Matrix Product : TT";
124  default : return "INVALID EXPRESSION";
125  }
126 }
127 
129 static std::string generate_value_kernel_argument(std::string const & scalartype, std::string const & name)
130 {
131  return scalartype + ' ' + name + ",";
132 }
133 
135 static std::string generate_pointer_kernel_argument(std::string const & address_space, std::string const & scalartype, std::string const & name)
136 {
137  return address_space + " " + scalartype + "* " + name + ",";
138 }
139 
141 template<typename KeyT, typename ValueT>
142 ValueT const & at(std::map<KeyT, ValueT> const & map, KeyT const & key)
143 {
144  typename std::map<KeyT, ValueT>::const_iterator it = map.find(key);
145  if (it != map.end())
146  return it->second;
147 
148  throw std::out_of_range("Generator: Key not found in map");
149 }
150 
152 class generator_not_supported_exception : public std::exception
153 {
154 public:
156  generator_not_supported_exception(std::string message) : message_("ViennaCL: Internal error: The generator cannot handle the statement provided: " + message) {}
157  virtual const char* what() const throw() { return message_.c_str(); }
159 private:
160  std::string message_;
161 };
162 
163 namespace utils
164 {
166 }
167 
168 
169 enum leaf_t
170 {
174 };
175 
176 class mapped_object;
178 
179 typedef std::pair<vcl_size_t, leaf_t> mapping_key;
180 typedef std::map<mapping_key, tools::shared_ptr<mapped_object> > mapping_type;
181 
182 
183 namespace tree_parsing
184 {
185 
186  template<class Fun>
187  inline void traverse(scheduler::statement const & statement, vcl_size_t root_idx, Fun const & fun, bool inspect);
188 
189  inline void process(utils::kernel_generation_stream & stream, leaf_t leaf, std::string const & type_key, std::string const & to_process,
190  scheduler::statement const & statement, vcl_size_t root_idx, mapping_type const & mapping, std::set<std::string> & already_processed);
191  inline std::string evaluate(leaf_t leaf, std::map<std::string, std::string> const & accessors, scheduler::statement const & statement, vcl_size_t root_idx,mapping_type const & mapping);
192 }
193 
194 using scheduler::INT_TYPE;
200 
201 typedef cl_uint vendor_id_type;
202 typedef cl_device_type device_type;
203 typedef std::string device_name_type;
204 
206 {
207 public:
208  virtual ~symbolic_binder(){ }
209  virtual bool bind(viennacl::backend::mem_handle const * ph) = 0;
210  virtual unsigned int get(viennacl::backend::mem_handle const * ph) = 0;
211 };
212 
214 {
215 public:
216  bind_to_handle() : current_arg_(0){ }
217  bool bind(viennacl::backend::mem_handle const * ph) {return (ph==NULL)?true:memory.insert(std::make_pair((void*)ph, current_arg_)).second; }
218  unsigned int get(viennacl::backend::mem_handle const * ph){ return bind(ph)?current_arg_++:memory.at((void*)ph); }
219 private:
220  unsigned int current_arg_;
221  std::map<void*,unsigned int> memory;
222 };
223 
225 {
226 public:
227  bind_all_unique() : current_arg_(0){ }
228  bool bind(viennacl::backend::mem_handle const *) {return true; }
229  unsigned int get(viennacl::backend::mem_handle const *){ return current_arg_++; }
230 private:
231  unsigned int current_arg_;
232  std::map<void*,unsigned int> memory;
233 };
234 
238 };
239 
241 {
242  if (policy==BIND_TO_HANDLE)
244  else
246 }
247 
248 template<char C>
249 struct char_to_type{ };
250 
252 {
253 public:
254  typedef std::list<scheduler::statement> data_type;
256 
257  statements_container(data_type const & data, order_type order) : data_(data), order_(order)
258  { }
259 
261  {
262  data_.push_back(s0);
263  }
264 
266  {
267  data_.push_back(s0);
268  data_.push_back(s1);
269  }
270 
271  std::list<scheduler::statement> const & data() const { return data_; }
272 
273  order_type order() const { return order_; }
274 
275 private:
276  std::list<scheduler::statement> data_;
277  order_type order_;
278 };
279 
280 }
281 
282 }
283 #endif
This file provides the forward declarations for the OpenCL layer of ViennaCL.
Exception for the case the generator is unable to deal with the operation.
Definition: forwards.h:152
std::string device_name_type
Definition: forwards.h:203
index_tuple(std::string const &_i, std::string const &_bound0)
Definition: forwards.h:67
Some helper routines for reading/writing/printing scheduler expressions.
bool bind(viennacl::backend::mem_handle const *)
Definition: forwards.h:228
void traverse(scheduler::statement const &statement, vcl_size_t root_idx, Fun const &fun, bool inspect)
Recursively execute a functor on a statement.
statements_container(scheduler::statement const &s0, scheduler::statement const &s1, order_type order)
Definition: forwards.h:265
statement_node_type_family type_family
Definition: forwards.h:341
viennacl::scalar< float > s1
std::list< scheduler::statement > const & data() const
Definition: forwards.h:271
operation_node_type_family type_family
Definition: forwards.h:475
Implementation of a shared pointer class (cf. std::shared_ptr, boost::shared_ptr). Will be used until C++11 is widely available.
scheduler::statement_node const & lhs_most(scheduler::statement::container_type const &array, vcl_size_t root)
Definition: forwards.h:87
Main namespace in ViennaCL. Holds all the basic types such as vector, matrix, etc. and defines operations upon them.
Definition: cpu_ram.hpp:29
bool is_scalar_reduction(scheduler::statement_node const &node)
Definition: forwards.h:75
std::vector< value_type > container_type
Definition: forwards.h:509
const char * expression_type_to_string(expression_type type)
Definition: forwards.h:110
index_tuple(std::string const &_i, std::string const &_bound0, std::string const &_j, std::string const &_bound1)
Definition: forwards.h:68
virtual bool bind(viennacl::backend::mem_handle const *ph)=0
std::string evaluate(leaf_t leaf, std::map< std::string, std::string > const &accessors, scheduler::statement const &statement, vcl_size_t root_idx, mapping_type const &mapping)
Implements the multi-memory-domain handle.
statements_container(data_type const &data, order_type order)
Definition: forwards.h:257
A shared pointer class similar to boost::shared_ptr. Reimplemented in order to avoid a Boost-dependen...
Definition: shared_ptr.hpp:83
std::size_t vcl_size_t
Definition: forwards.h:74
std::list< scheduler::statement > data_type
Definition: forwards.h:254
Provides the datastructures for dealing with a single statement such as 'x = y + z;'.
std::map< mapping_key, tools::shared_ptr< mapped_object > > mapping_type
Definition: forwards.h:180
statements_container(scheduler::statement const &s0)
Definition: forwards.h:260
cl_device_type device_type
Definition: forwards.h:202
bool bind(viennacl::backend::mem_handle const *ph)
Definition: forwards.h:217
operation_node_type type
Definition: forwards.h:476
Main abstraction class for multiple memory domains. Represents a buffer in either main RAM...
Definition: mem_handle.hpp:89
The main class for representing a statement such as x = inner_prod(y,z); at runtime.
Definition: forwards.h:504
bool is_vector_reduction(scheduler::statement_node const &node)
Definition: forwards.h:80
ValueT const & at(std::map< KeyT, ValueT > const &map, KeyT const &key)
Emulation of C++11's .at() member for std::map<>
Definition: forwards.h:142
std::pair< vcl_size_t, leaf_t > mapping_key
Definition: forwards.h:177
Main datastructure for an node in the statement tree.
Definition: forwards.h:480
tools::shared_ptr< symbolic_binder > make_binder(binding_policy_t policy)
Definition: forwards.h:240
void process(utils::kernel_generation_stream &stream, leaf_t leaf, std::string const &type_key, std::string const &to_process, scheduler::statement const &statement, vcl_size_t root_idx, mapping_type const &mapping, std::set< std::string > &already_processed)