ViennaCL - The Vienna Computing Library: viennacl/device_specific/forwards.h Source File

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
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-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
21
26 #include <list>
27 #include <map>
28 #include <set>
29 #include <stdexcept>
30
31 #include "viennacl/scheduler/io.hpp"
32
33 #include "viennacl/ocl/forwards.h"
34 #include "viennacl/tools/shared_ptr.hpp"
35 #include "viennacl/scheduler/forwards.h"
36
37 #include "viennacl/backend/mem_handle.hpp"
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
65 struct index_tuple
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
75 inline bool is_scalar_reduction(scheduler::statement_node const & node)
76 {
77 return node.op.type==scheduler::OPERATION_BINARY_INNER_PROD_TYPE || node.op.type_family==scheduler::OPERATION_VECTOR_REDUCTION_TYPE_FAMILY;
78 }
79
80 inline bool is_vector_reduction(scheduler::statement_node const & node)
81 {
82 return node.op.type==scheduler::OPERATION_BINARY_MAT_VEC_PROD_TYPE
83 || node.op.type_family==scheduler::OPERATION_ROWS_REDUCTION_TYPE_FAMILY
84 || node.op.type_family==scheduler::OPERATION_COLUMNS_REDUCTION_TYPE_FAMILY;
85 }
86
87 inline scheduler::statement_node const & lhs_most(scheduler::statement::container_type const & array, vcl_size_t root)
88 {
89 scheduler::statement_node const * current = &array[root];
90 while (current->lhs.type_family==scheduler::COMPOSITE_OPERATION_FAMILY)
91 current = &array[current->lhs.node_index];
92 return *current;
93 }
94
95 enum expression_type
96 {
97 SCALAR_AXPY_TYPE,
98 VECTOR_AXPY_TYPE,
99 MATRIX_AXPY_TYPE,
100 REDUCTION_TYPE,
101 ROW_WISE_REDUCTION_Nx_TYPE,
102 ROW_WISE_REDUCTION_Tx_TYPE,
103 MATRIX_PRODUCT_NN_TYPE,
104 MATRIX_PRODUCT_TN_TYPE,
105 MATRIX_PRODUCT_NT_TYPE,
106 MATRIX_PRODUCT_TT_TYPE,
107 INVALID_EXPRESSION_TYPE
108 };
109
110 inline const char * expression_type_to_string(expression_type type)
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 template<typename KeyT, typename ValueT>
153 ValueT & at(std::map<KeyT, ValueT> & map, KeyT const & key)
154 {
155 typename std::map<KeyT, ValueT>::iterator it = map.find(key);
156 if (it != map.end())
157 return it->second;
158
159 throw std::out_of_range("Generator: Key not found in map");
160 }
161
163 class generator_not_supported_exception : public std::exception
164 {
165 public:
166 generator_not_supported_exception() : message_() {}
167 generator_not_supported_exception(std::string message) : message_("ViennaCL: Internal error: The generator cannot handle the statement provided: " + message) {}
168 virtual const char* what() const throw() { return message_.c_str(); }
169 virtual ~generator_not_supported_exception() throw() {}
170 private:
171 std::string message_;
172 };
173
174 namespace utils
175 {
176 class kernel_generation_stream;
177 }
178
179
180 enum leaf_t
181 {
182 LHS_NODE_TYPE,
183 PARENT_NODE_TYPE,
184 RHS_NODE_TYPE
185 };
186
187 class mapped_object;
188 class template_base;
189
190 typedef std::pair<vcl_size_t, leaf_t> mapping_key;
191 typedef std::map<mapping_key, tools::shared_ptr<mapped_object> > mapping_type;
192
193
194 namespace tree_parsing
195 {
196
197 template<class Fun>
198 inline void traverse(scheduler::statement const & statement, vcl_size_t root_idx, Fun const & fun, bool inspect);
199
200 inline void process(utils::kernel_generation_stream & stream, leaf_t leaf, std::string const & type_key, std::string const & to_process,
201 scheduler::statement const & statement, vcl_size_t root_idx, mapping_type const & mapping, std::set<std::string> & already_processed);
202 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);
203 }
204
205 using scheduler::INT_TYPE;
206 using scheduler::UINT_TYPE;
207 using scheduler::ULONG_TYPE;
208 using scheduler::LONG_TYPE;
209 using scheduler::FLOAT_TYPE;
210 using scheduler::DOUBLE_TYPE;
211
212 typedef cl_uint vendor_id_type;
213 typedef cl_device_type device_type;
214 typedef std::string device_name_type;
215
216 class symbolic_binder
217 {
218 public:
219 virtual ~symbolic_binder(){ }
220 virtual bool bind(viennacl::backend::mem_handle const * ph) = 0;
221 virtual unsigned int get(viennacl::backend::mem_handle const * ph) = 0;
222 };
223
224 class bind_to_handle : public symbolic_binder
225 {
226 public:
227 bind_to_handle() : current_arg_(0){ }
228 bool bind(viennacl::backend::mem_handle const * ph) {return (ph==NULL)?true:memory.insert(std::make_pair((void*)ph, current_arg_)).second; }
229 unsigned int get(viennacl::backend::mem_handle const * ph){ return bind(ph) ? current_arg_++ : at(memory, (void*)ph); }
230 private:
231 unsigned int current_arg_;
232 std::map<void*,unsigned int> memory;
233 };
234
235 class bind_all_unique : public symbolic_binder
236 {
237 public:
238 bind_all_unique() : current_arg_(0){ }
239 bool bind(viennacl::backend::mem_handle const *) {return true; }
240 unsigned int get(viennacl::backend::mem_handle const *){ return current_arg_++; }
241 private:
242 unsigned int current_arg_;
243 std::map<void*,unsigned int> memory;
244 };
245
246 enum binding_policy_t{
247 BIND_ALL_UNIQUE,
248 BIND_TO_HANDLE
249 };
250
251 inline tools::shared_ptr<symbolic_binder> make_binder(binding_policy_t policy)
252 {
253 if (policy==BIND_TO_HANDLE)
254 return tools::shared_ptr<symbolic_binder>(new bind_to_handle());
255 else
256 return tools::shared_ptr<symbolic_binder>(new bind_all_unique());
257 }
258
259 template<char C>
260 struct char_to_type{ };
261
262 class statements_container
263 {
264 public:
265 typedef std::list<scheduler::statement> data_type;
266 enum order_type { SEQUENTIAL, INDEPENDENT };
267
268 statements_container(data_type const & data, order_type order) : data_(data), order_(order)
269 { }
270
271 statements_container(scheduler::statement const & s0) : order_(INDEPENDENT)
272 {
273 data_.push_back(s0);
274 }
275
276 statements_container(scheduler::statement const & s0, scheduler::statement const & s1, order_type order) : order_(order)
277 {
278 data_.push_back(s0);
279 data_.push_back(s1);
280 }
281
282 std::list<scheduler::statement> const & data() const { return data_; }
283
284 order_type order() const { return order_; }
285
286 private:
287 std::list<scheduler::statement> data_;
288 order_type order_;
289 };
290
291 }
292
293 }
294 #endif
viennacl::device_specific::vendor_id_type
cl_uint vendor_id_type
Definition: forwards.h:212
forwards.h
This file provides the forward declarations for the OpenCL layer of ViennaCL.
viennacl::scheduler::INT_TYPE
Definition: forwards.h:294
viennacl::device_specific::generator_not_supported_exception::what
virtual const char * what() const
Definition: forwards.h:168
viennacl::scheduler::OPERATION_BINARY_MAT_VEC_PROD_TYPE
Definition: forwards.h:144
viennacl::device_specific::BIND_ALL_UNIQUE
Definition: forwards.h:247
viennacl::device_specific::statements_container::INDEPENDENT
Definition: forwards.h:266
viennacl::device_specific::generator_not_supported_exception::~generator_not_supported_exception
virtual ~generator_not_supported_exception()
Definition: forwards.h:169
viennacl::scheduler::OPERATION_VECTOR_REDUCTION_TYPE_FAMILY
Definition: forwards.h:62
viennacl::device_specific::generator_not_supported_exception
Exception for the case the generator is unable to deal with the operation.
Definition: forwards.h:163
viennacl::device_specific::device_name_type
std::string device_name_type
Definition: forwards.h:214
viennacl::device_specific::index_tuple::bound1
std::string bound1
Definition: forwards.h:72
viennacl::device_specific::index_tuple::index_tuple
index_tuple(std::string const &_i, std::string const &_bound0)
Definition: forwards.h:67
viennacl::device_specific::symbolic_binder
Definition: forwards.h:216
viennacl::device_specific::index_tuple::bound0
std::string bound0
Definition: forwards.h:70
viennacl::device_specific::MATRIX_PRODUCT_TN_TYPE
Definition: forwards.h:104
io.hpp
Some helper routines for reading/writing/printing scheduler expressions.
viennacl::scheduler::DOUBLE_TYPE
Definition: forwards.h:300
viennacl::device_specific::bind_all_unique::bind
bool bind(viennacl::backend::mem_handle const *)
Definition: forwards.h:239
viennacl::device_specific::tree_parsing::traverse
void traverse(scheduler::statement const &statement, vcl_size_t root_idx, Fun const &fun, bool inspect)
Recursively execute a functor on a statement.
Definition: tree_parsing.hpp:53
viennacl::device_specific::MATRIX_PRODUCT_NT_TYPE
Definition: forwards.h:105
viennacl::device_specific::statements_container::statements_container
statements_container(scheduler::statement const &s0, scheduler::statement const &s1, order_type order)
Definition: forwards.h:276
viennacl::device_specific::leaf_t
leaf_t
Definition: forwards.h:180
viennacl::device_specific::bind_all_unique
Definition: forwards.h:235
viennacl::scheduler::statement_node::op
op_element op
Definition: forwards.h:481
viennacl::device_specific::template_base
Definition: template_base.hpp:54
viennacl::scheduler::lhs_rhs_element::type_family
statement_node_type_family type_family
Definition: forwards.h:339
s1
viennacl::scalar< float > s1
Definition: global_variables.cpp:57
viennacl::device_specific::SCALAR_AXPY_TYPE
Definition: forwards.h:97
viennacl::device_specific::statements_container::data
std::list< scheduler::statement > const & data() const
Definition: forwards.h:282
viennacl::scheduler::op_element::type_family
operation_node_type_family type_family
Definition: forwards.h:473
viennacl::device_specific::expression_type
expression_type
Definition: forwards.h:95
viennacl::scheduler::OPERATION_COLUMNS_REDUCTION_TYPE_FAMILY
Definition: forwards.h:64
viennacl::device_specific::index_tuple::j
std::string j
Definition: forwards.h:71
viennacl::device_specific::PARENT_NODE_TYPE
Definition: forwards.h:183
viennacl::device_specific::MATRIX_PRODUCT_TT_TYPE
Definition: forwards.h:106
viennacl::device_specific::index_tuple::i
std::string i
Definition: forwards.h:69
viennacl::device_specific::MATRIX_PRODUCT_NN_TYPE
Definition: forwards.h:103
viennacl::scheduler::COMPOSITE_OPERATION_FAMILY
Definition: forwards.h:251
shared_ptr.hpp
Implementation of a shared pointer class (cf. std::shared_ptr, boost::shared_ptr). Will be used until C++11 is widely available.
viennacl::device_specific::lhs_most
scheduler::statement_node const & lhs_most(scheduler::statement::container_type const &array, vcl_size_t root)
Definition: forwards.h:87
viennacl::device_specific::bind_to_handle::bind_to_handle
bind_to_handle()
Definition: forwards.h:227
viennacl::device_specific::is_scalar_reduction
bool is_scalar_reduction(scheduler::statement_node const &node)
Definition: forwards.h:75
viennacl::scheduler::statement::container_type
std::vector< value_type > container_type
Definition: forwards.h:507
viennacl::device_specific::REDUCTION_TYPE
Definition: forwards.h:100
viennacl::scheduler::LONG_TYPE
Definition: forwards.h:296
viennacl::device_specific::symbolic_binder::~symbolic_binder
virtual ~symbolic_binder()
Definition: forwards.h:219
viennacl::device_specific::utils::kernel_generation_stream
Definition: utils.hpp:404
viennacl::scheduler::OPERATION_BINARY_INNER_PROD_TYPE
Definition: forwards.h:146
viennacl::device_specific::generator_not_supported_exception::generator_not_supported_exception
generator_not_supported_exception()
Definition: forwards.h:166
viennacl::device_specific::expression_type_to_string
const char * expression_type_to_string(expression_type type)
Definition: forwards.h:110
viennacl::device_specific::generator_not_supported_exception::generator_not_supported_exception
generator_not_supported_exception(std::string message)
Definition: forwards.h:167
viennacl::device_specific::RHS_NODE_TYPE
Definition: forwards.h:184
viennacl::device_specific::LHS_NODE_TYPE
Definition: forwards.h:182
viennacl::device_specific::index_tuple::index_tuple
index_tuple(std::string const &_i, std::string const &_bound0, std::string const &_j, std::string const &_bound1)
Definition: forwards.h:68
viennacl::device_specific::symbolic_binder::bind
virtual bool bind(viennacl::backend::mem_handle const *ph)=0
viennacl::device_specific::INVALID_EXPRESSION_TYPE
Definition: forwards.h:107
viennacl::device_specific::tree_parsing::evaluate
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)
Definition: tree_parsing.hpp:293
mem_handle.hpp
Implements the multi-memory-domain handle.
viennacl::device_specific::statements_container::statements_container
statements_container(data_type const &data, order_type order)
Definition: forwards.h:268
viennacl::device_specific::VECTOR_AXPY_TYPE
Definition: forwards.h:98
viennacl::tools::shared_ptr
A shared pointer class similar to boost::shared_ptr. Reimplemented in order to avoid a Boost-dependen...
Definition: shared_ptr.hpp:83
viennacl::device_specific::char_to_type
Definition: forwards.h:260
viennacl::scheduler::statement_node::lhs
lhs_rhs_element lhs
Definition: forwards.h:480
viennacl::vcl_size_t
std::size_t vcl_size_t
Definition: forwards.h:75
viennacl::device_specific::statements_container::data_type
std::list< scheduler::statement > data_type
Definition: forwards.h:265
forwards.h
Provides the datastructures for dealing with a single statement such as 'x = y + z;'.
viennacl::device_specific::bind_to_handle
Definition: forwards.h:224
viennacl::device_specific::statements_container::SEQUENTIAL
Definition: forwards.h:266
viennacl::device_specific::mapping_type
std::map< mapping_key, tools::shared_ptr< mapped_object > > mapping_type
Definition: forwards.h:191
viennacl::device_specific::statements_container::statements_container
statements_container(scheduler::statement const &s0)
Definition: forwards.h:271
viennacl::device_specific::device_type
cl_device_type device_type
Definition: forwards.h:213
viennacl::device_specific::bind_to_handle::bind
bool bind(viennacl::backend::mem_handle const *ph)
Definition: forwards.h:228
viennacl::device_specific::ROW_WISE_REDUCTION_Nx_TYPE
Definition: forwards.h:101
viennacl::device_specific::binding_policy_t
binding_policy_t
Definition: forwards.h:246
viennacl::scheduler::op_element::type
operation_node_type type
Definition: forwards.h:474
viennacl::backend::mem_handle
Main abstraction class for multiple memory domains. Represents a buffer in either main RAM...
Definition: mem_handle.hpp:89
viennacl::scheduler::UINT_TYPE
Definition: forwards.h:295
viennacl::scheduler::FLOAT_TYPE
Definition: forwards.h:299
viennacl::scheduler::statement
The main class for representing a statement such as x = inner_prod(y,z); at runtime.
Definition: forwards.h:502
viennacl::device_specific::BIND_TO_HANDLE
Definition: forwards.h:248
viennacl::scheduler::lhs_rhs_element::node_index
vcl_size_t node_index
Definition: forwards.h:346
viennacl::device_specific::index_tuple
Definition: forwards.h:65
viennacl::device_specific::bind_all_unique::bind_all_unique
bind_all_unique()
Definition: forwards.h:238
viennacl::device_specific::MATRIX_AXPY_TYPE
Definition: forwards.h:99
viennacl::device_specific::is_vector_reduction
bool is_vector_reduction(scheduler::statement_node const &node)
Definition: forwards.h:80
viennacl::device_specific::at
ValueT const & at(std::map< KeyT, ValueT > const &map, KeyT const &key)
Emulation of C++11's .at() member for std::map<>, const-version.
Definition: forwards.h:142
viennacl::scheduler::ULONG_TYPE
Definition: forwards.h:297
viennacl::scheduler::OPERATION_ROWS_REDUCTION_TYPE_FAMILY
Definition: forwards.h:63
viennacl::device_specific::statements_container::order
order_type order() const
Definition: forwards.h:284
viennacl::device_specific::statements_container::order_type
order_type
Definition: forwards.h:266
viennacl::device_specific::mapping_key
std::pair< vcl_size_t, leaf_t > mapping_key
Definition: forwards.h:188
viennacl::scheduler::statement_node
Main datastructure for an node in the statement tree.
Definition: forwards.h:478
viennacl::device_specific::statements_container
Definition: forwards.h:262
viennacl::device_specific::make_binder
tools::shared_ptr< symbolic_binder > make_binder(binding_policy_t policy)
Definition: forwards.h:251
viennacl::device_specific::ROW_WISE_REDUCTION_Tx_TYPE
Definition: forwards.h:102
viennacl::device_specific::tree_parsing::process
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)
Definition: tree_parsing.hpp:360