ViennaCL - The Vienna Computing Library  1.6.1
Free open-source GPU-accelerated linear algebra and solver library.
preset.hpp
Go to the documentation of this file.
1 #ifndef VIENNACL_SCHEDULER_PRESET_HPP_
2 #define VIENNACL_SCHEDULER_PRESET_HPP_
3 
7 
8 namespace viennacl
9 {
10 namespace scheduler
11 {
12 namespace preset
13 {
14 
15 template<typename NumericT, typename ScalarT1, typename ScalarT2>
16 scheduler::statement avbv(scheduler::operation_node_type ASSIGN_OP, NumericT const * x, NumericT const * y, ScalarT1 const * a, bool flip_a, bool reciprocal_a,
17  NumericT const * z, ScalarT2 const * b, bool flip_b, bool reciprocal_b)
18 {
20  vcl_size_t dummy = 0;
21  //0
22  statement::add_element(dummy, array[0].lhs, *x);
23  array[0].op.type_family = OPERATION_BINARY_TYPE_FAMILY;
24  array[0].op.type = ASSIGN_OP;
25  array[0].rhs.type_family = COMPOSITE_OPERATION_FAMILY;
26  if (z)
27  array[0].rhs.node_index = 1;
28  else
29  array[0].rhs.node_index = flip_a?2:3;
30 
31  //1
32  array[1].lhs.type_family = COMPOSITE_OPERATION_FAMILY;
33  array[1].lhs.node_index = flip_a?2:3;
34  array[1].op.type_family = OPERATION_BINARY_TYPE_FAMILY;
35  array[1].op.type = OPERATION_BINARY_ADD_TYPE;
36  array[1].rhs.type_family = COMPOSITE_OPERATION_FAMILY;
37  array[1].rhs.node_index = flip_b?4:5;
38 
39  //2
40  array[2].lhs.type_family = COMPOSITE_OPERATION_FAMILY;
41  array[2].lhs.node_index = 3;
42  array[2].op.type_family = OPERATION_UNARY_TYPE_FAMILY;
43  array[2].op.type = OPERATION_UNARY_MINUS_TYPE;
44 
45  //3
46  statement::add_element(dummy, array[3].lhs, *y);
47  array[3].op.type_family = OPERATION_BINARY_TYPE_FAMILY;
48  array[3].op.type = reciprocal_a?OPERATION_BINARY_DIV_TYPE:OPERATION_BINARY_MULT_TYPE;
49  statement::add_element(dummy, array[3].rhs, *a);
50 
51 
52  //4
53  array[4].lhs.type_family = COMPOSITE_OPERATION_FAMILY;
54  array[4].lhs.node_index = 5;
55  array[4].op.type_family = OPERATION_UNARY_TYPE_FAMILY;
56  array[4].op.type = OPERATION_UNARY_MINUS_TYPE;
57 
58  //5
59  if (z)
60  {
61  statement::add_element(dummy, array[5].lhs, *z);
62  array[5].op.type_family = OPERATION_BINARY_TYPE_FAMILY;
63  array[5].op.type = reciprocal_b?OPERATION_BINARY_DIV_TYPE:OPERATION_BINARY_MULT_TYPE;
64  statement::add_element(dummy, array[5].rhs, *b);
65  }
66 
67  return statement(array);
68 }
69 
70 template<typename NumericT, typename ScalarT1>
71 scheduler::statement av(scheduler::operation_node_type ASSIGN_OP, NumericT const * x, NumericT const * y, ScalarT1 const * a, bool flip_a, bool reciprocal_a)
72 {
73  return scheduler::preset::avbv(ASSIGN_OP, x, y, a, flip_a, reciprocal_a, (NumericT const*)NULL, (ScalarT1 const*)NULL, false, false);
74 }
75 
76 
77 template<typename NumericT>
78 device_specific::statements_container plane_rotation(vector_base<NumericT> const * x, vector_base<NumericT> const * y, NumericT const * a, NumericT const * b)
79 {
80  return device_specific::statements_container(avbv(OPERATION_BINARY_ASSIGN_TYPE, x, x, a, false, false, y, b, false, false),
81  avbv(OPERATION_BINARY_ASSIGN_TYPE, y, y, a, false, false, x, b, true, false),
83 }
84 
85 template<typename NumericT>
86 device_specific::statements_container swap(NumericT const * x, NumericT const * y)
87 {
88  vcl_size_t dummy = 0;
89  statement::container_type array0(1);
90  statement::container_type array1(1);
91 
92  statement::add_element(dummy, array0[0].lhs, *x);
93  array0[0].op.type_family = OPERATION_BINARY_TYPE_FAMILY;
94  array0[0].op.type = OPERATION_BINARY_ASSIGN_TYPE;
95  statement::add_element(dummy, array0[0].rhs, *y);
96 
97  statement::add_element(dummy, array1[0].lhs, *y);
98  array1[0].op.type_family = OPERATION_BINARY_TYPE_FAMILY;
99  array1[0].op.type = OPERATION_BINARY_ASSIGN_TYPE;
100  statement::add_element(dummy, array1[0].rhs, *x);
101 
103 }
104 
105 template<typename NumericT>
107 {
108  vcl_size_t dummy = 0;
109  statement::container_type array(1);
110  statement::add_element(dummy, array[0].lhs, *x);
111  array[0].op.type_family = OPERATION_BINARY_TYPE_FAMILY;
112  array[0].op.type = OPERATION_BINARY_ASSIGN_TYPE;
113  statement::add_element(dummy, array[0].rhs, *y);
114  return scheduler::statement(array);
115 }
116 
117 template<typename NumericT>
119 {
120  vcl_size_t dummy = 0;
121  statement::container_type array(1);
122  statement::add_element(dummy, array[0].lhs, *x);
123  array[0].op.type_family = OPERATION_BINARY_TYPE_FAMILY;
124  array[0].op.type = OPERATION_BINARY_ASSIGN_TYPE;
125  statement::add_element(dummy, array[0].rhs, *y);
126  return scheduler::statement(array);
127 }
128 
129 template<typename NumericT>
131 {
132  vcl_size_t dummy = 0;
133  statement::container_type array(2);
134  array[0].lhs.type_family = COMPOSITE_OPERATION_FAMILY;
135  array[0].lhs.node_index = 1;
136  array[0].op.type_family = OPERATION_BINARY_TYPE_FAMILY;
137  array[0].op.type = OPERATION_BINARY_ASSIGN_TYPE;
138  statement::add_element(dummy, array[0].rhs, *y);
139 
140  statement::add_element(dummy, array[1].lhs, *x);
141  array[1].op.type_family = OPERATION_BINARY_TYPE_FAMILY;
142  array[1].op.type = OPERATION_BINARY_MATRIX_DIAG_TYPE;
143  statement::add_element(dummy, array[1].rhs, 0);
144 
145  return scheduler::statement(array);
146 }
147 
148 template<typename ScalarT, typename NumericT>
150  scheduler::operation_node_type ROP, bool use_sqrt, bool x_abs)
151 {
152  vcl_size_t dummy = 0;
153  statement::container_type array(5);
154 
155  statement::add_element(dummy, array[0].lhs, *s);
156  array[0].op.type_family = OPERATION_BINARY_TYPE_FAMILY;
157  array[0].op.type = OPERATION_BINARY_ASSIGN_TYPE;
158  array[0].rhs.type_family = COMPOSITE_OPERATION_FAMILY;
159  array[0].rhs.node_index = use_sqrt?1:2;
160 
161  array[1].lhs.type_family = COMPOSITE_OPERATION_FAMILY;
162  array[1].lhs.node_index = 2;
163  array[1].op.type_family = OPERATION_UNARY_TYPE_FAMILY;
164  array[1].op.type = OPERATION_UNARY_SQRT_TYPE;
165 
166 
167  if (x_abs)
168  {
169  array[2].lhs.type_family = COMPOSITE_OPERATION_FAMILY;
170  array[2].lhs.node_index = 3;
171  }
172  else
173  {
174  statement::add_element(dummy, array[2].lhs, *x);
175  }
176  if (y)
177  {
178  array[2].op.type_family = OPERATION_BINARY_TYPE_FAMILY;
179  array[2].op.type = OPERATION_BINARY_INNER_PROD_TYPE;
180  statement::add_element(dummy, array[2].rhs, *y);
181  }
182  else
183  {
184  array[2].op.type_family = OPERATION_VECTOR_REDUCTION_TYPE_FAMILY;
185  array[2].op.type = ROP;
186  }
187 
188  bool is_float_or_double = is_floating_point<NumericT>::value; // assign to variable to avoid compiler warnings about unreachable code
189  if (is_float_or_double)
190  {
191  statement::add_element(dummy, array[3].lhs, *x);
192  array[3].op.type_family = OPERATION_UNARY_TYPE_FAMILY;
193  array[3].op.type = OPERATION_UNARY_FABS_TYPE;
194  }
195  else
196  {
197  array[3].lhs.type_family = COMPOSITE_OPERATION_FAMILY;
198  array[3].lhs.node_index = 4;
199  array[3].op.type_family = OPERATION_UNARY_TYPE_FAMILY;
201 
202  statement::add_element(dummy, array[4].lhs, *x);
203  array[4].op.type_family = OPERATION_UNARY_TYPE_FAMILY;
204  array[4].op.type = OPERATION_UNARY_ABS_TYPE;
205  }
206 
207 
208  return scheduler::statement(array);
209 }
210 
211 template<class ScalarT, typename NumericT>
212 statement inner_prod(ScalarT const * s, vector_base<NumericT> const * x, vector_base<NumericT> const * y)
213 {
214  return preset::reduction_inner_prod(s,x,y, OPERATION_INVALID_TYPE, false, false);
215 }
216 
217 template<typename NumericT>
219 {
221 }
222 
223 template<typename NumericT>
225 {
226  return preset::reduction_inner_prod(s, x, x, OPERATION_INVALID_TYPE, true, false);
227 }
228 
229 template<typename NumericT>
231 {
232  bool is_float_or_double = is_floating_point<NumericT>::value;
234  OPERATION_BINARY_ELEMENT_MAX_TYPE, false, true);
235 }
236 
237 template<typename NumericT>
239 {
240  bool is_float_or_double = is_floating_point<NumericT>::value; //avoid compiler warnings about unreachable code below
243 }
244 
245 template<typename NumericT>
247 {
249 }
250 
251 template<typename NumericT>
253 {
254  bool is_float_or_double = is_floating_point<NumericT>::value; //avoid compiler warnings about unreachable code below
256 }
257 
258 template<typename NumericT>
260 {
261  bool is_float_or_double = is_floating_point<NumericT>::value; //avoid compiler warnings about unreachable code below
263 }
264 
265 
266 template<typename NumericT>
267 statement binary_element_op(NumericT const * x, NumericT const * y, NumericT const * z, scheduler::operation_node_type TYPE)
268 {
269  vcl_size_t dummy = 0;
270  statement::container_type array(2);
271 
272  statement::add_element(dummy, array[0].lhs, *x);
273  array[0].op.type_family = OPERATION_BINARY_TYPE_FAMILY;
274  array[0].op.type = OPERATION_BINARY_ASSIGN_TYPE;
275  array[0].rhs.type_family = COMPOSITE_OPERATION_FAMILY;
276  array[0].rhs.node_index = 1;
277 
278  statement::add_element(dummy, array[1].lhs, *y);
279  array[1].op.type_family = z?OPERATION_BINARY_TYPE_FAMILY:OPERATION_UNARY_TYPE_FAMILY;
280  array[1].op.type = TYPE;
281  if (z)
282  statement::add_element(dummy, array[1].rhs, *z);
283 
284  return statement(array);
285 }
286 
287 template<typename NumericT>
288 statement unary_element_op(NumericT const * x, NumericT const * y, scheduler::operation_node_type TYPE)
289 {
290  return binary_element_op(x, y, static_cast<NumericT const *>(NULL), TYPE);
291 }
292 
293 template<typename NumericT, typename IDT>
295 {
296  vcl_size_t dummy = 0;
297  statement::container_type array(2);
298 
299  if (op==3)
300  statement::add_element(dummy, array[0].lhs, *A);
301  else
302  statement::add_element(dummy, array[0].lhs, *x);
303  array[0].op.type_family = OPERATION_BINARY_TYPE_FAMILY;
304  array[0].op.type = OPERATION_BINARY_ASSIGN_TYPE;
305  array[0].rhs.type_family = COMPOSITE_OPERATION_FAMILY;
306  array[0].rhs.node_index = 1;
307 
308  if (op==3)
309  statement::add_element(dummy, array[1].lhs, *x);
310  else
311  statement::add_element(dummy, array[1].lhs, *A);
312  array[1].op.type_family = OPERATION_BINARY_TYPE_FAMILY;
313  if (op==0)
314  array[1].op.type = OPERATION_BINARY_MATRIX_ROW_TYPE;
315  else if (op==1)
316  array[1].op.type = OPERATION_BINARY_MATRIX_COLUMN_TYPE;
317  else if (op==2)
318  array[1].op.type = OPERATION_BINARY_MATRIX_DIAG_TYPE;
319  else
320  array[1].op.type = OPERATION_BINARY_VECTOR_DIAG_TYPE;
321  statement::add_element(dummy, array[1].rhs, id);
322 
323  return statement(array);
324 }
325 
326 template<typename NumericT>
328 {
329  return matrix_row_column_diag(x, A, id, 0);
330 }
331 
332 template<typename NumericT>
334 {
335  return matrix_row_column_diag(x, A, id, 1);
336 }
337 
338 
339 template<typename NumericT>
341 {
342  return matrix_row_column_diag(x, A, id, 2);
343 }
344 
345 template<typename NumericT>
347 {
348  return matrix_row_column_diag(x, A, id, 3);
349 }
350 
351 template<typename NumericT>
353 {
354  vcl_size_t dummy = 0;
355  statement::container_type array(3);
356 
357  scheduler::statement::add_element(dummy, array[0].lhs, *y);
358  array[0].op.type_family = OPERATION_BINARY_TYPE_FAMILY;
359  array[0].op.type = OPERATION_BINARY_ASSIGN_TYPE;
360  array[0].rhs.type_family = COMPOSITE_OPERATION_FAMILY;
361  array[0].rhs.node_index = 1;
362 
363  if (A_trans)
364  {
365  array[1].lhs.type_family = COMPOSITE_OPERATION_FAMILY;
366  array[1].lhs.node_index = 2;
367 
368  statement::add_element(dummy, array[2].lhs, *A);
369  array[2].op.type_family = OPERATION_UNARY_TYPE_FAMILY;
370  array[2].op.type = OPERATION_UNARY_TRANS_TYPE;
371  }
372  else
373  {
374  statement::add_element(dummy, array[1].lhs, *A);
375  }
376 
377  if (x)
378  {
379  array[1].op.type_family = OPERATION_BINARY_TYPE_FAMILY;
380  array[1].op.type = OPERATION_BINARY_MAT_VEC_PROD_TYPE;
381  statement::add_element(dummy, array[1].rhs, *x);
382  }
383  else
384  {
385  array[1].op.type_family = OPERATION_ROWS_REDUCTION_TYPE_FAMILY;
386  array[1].op.type = ROP;
387  }
388 
389  return statement(array);
390 }
391 
392 template<typename NumericT>
394 {
395  return row_reduction_mat_vec_prod(A, A_trans, x, y, OPERATION_INVALID_TYPE);
396 }
397 
398 template<typename NumericT>
399 statement mat_mat_prod(NumericT alpha, viennacl::matrix_base<NumericT> const * A, bool A_trans,
400  viennacl::matrix_base<NumericT> const * B, bool B_trans,
401  NumericT beta, viennacl::matrix_base<NumericT> const * C)
402 {
403  vcl_size_t dummy = 0;
404  statement::container_type array(7);
405 
406  scheduler::statement::add_element(dummy, array[0].lhs, *C);
407  array[0].op.type_family = OPERATION_BINARY_TYPE_FAMILY;
408  array[0].op.type = OPERATION_BINARY_ASSIGN_TYPE;
409  array[0].rhs.type_family = COMPOSITE_OPERATION_FAMILY;
410  array[0].rhs.node_index = 1;
411 
412  array[1].lhs.type_family = COMPOSITE_OPERATION_FAMILY;
413  array[1].lhs.node_index = 2;
414  array[1].op.type_family = OPERATION_BINARY_TYPE_FAMILY;
415  array[1].op.type = OPERATION_BINARY_ADD_TYPE;
416  array[1].rhs.type_family = COMPOSITE_OPERATION_FAMILY;
417  array[1].rhs.node_index = 6;
418 
419  array[2].lhs.type_family = COMPOSITE_OPERATION_FAMILY;
420  array[2].lhs.node_index = 3;
421  array[2].op.type_family = OPERATION_BINARY_TYPE_FAMILY;
422  array[2].op.type = OPERATION_BINARY_MULT_TYPE;
423  scheduler::statement::add_element(dummy, array[2].rhs, alpha);
424 
425 
426  if (A_trans)
427  {
428  array[3].lhs.type_family = COMPOSITE_OPERATION_FAMILY;
429  array[3].lhs.node_index = 4;
430 
431  statement::add_element(dummy, array[4].lhs, *A);
432  array[4].op.type_family = OPERATION_UNARY_TYPE_FAMILY;
433  array[4].op.type = OPERATION_UNARY_TRANS_TYPE;
434  }
435  else
436  {
437  statement::add_element(dummy, array[3].lhs, *A);
438  }
439 
440  array[3].op.type_family = OPERATION_BINARY_TYPE_FAMILY;
441  array[3].op.type = OPERATION_BINARY_MAT_MAT_PROD_TYPE;
442 
443  if (B_trans)
444  {
445  array[3].rhs.type_family = COMPOSITE_OPERATION_FAMILY;
446  array[3].rhs.node_index = 5;
447 
448  statement::add_element(dummy, array[5].lhs, *B);
449  array[5].op.type_family = OPERATION_UNARY_TYPE_FAMILY;
450  array[5].op.type = OPERATION_UNARY_TRANS_TYPE;
451  }
452  else
453  {
454  statement::add_element(dummy, array[3].rhs, *B);
455  }
456 
457  scheduler::statement::add_element(dummy, array[6].rhs, *C);
458  array[6].op.type_family = OPERATION_BINARY_TYPE_FAMILY;
459  array[6].op.type = OPERATION_BINARY_MULT_TYPE;
460  scheduler::statement::add_element(dummy, array[6].rhs, beta);
461 
462 
463 
464  return statement(array);
465 }
466 
467 }
468 }
469 }
470 
471 #endif
statement matrix_row_column_diag(viennacl::vector_base< NumericT > const *x, viennacl::matrix_base< NumericT > const *A, IDT id, unsigned int op)
Definition: preset.hpp:294
This class represents a single scalar value on the GPU and behaves mostly like a built-in scalar type...
Definition: forwards.h:226
statement matrix_diag_from_vector(viennacl::vector_base< NumericT > const *x, viennacl::matrix_base< NumericT > const *A, int id)
Definition: preset.hpp:346
statement inner_prod(ScalarT const *s, vector_base< NumericT > const *x, vector_base< NumericT > const *y)
Definition: preset.hpp:212
statement max(scalar< NumericT > const *s, vector_base< NumericT > const *x)
Definition: preset.hpp:252
static viennacl::enable_if< viennacl::is_primitive_type< T >::value, vcl_size_t >::type add_element(vcl_size_t next_free, lhs_rhs_element &elem, T const &t)
Definition: forwards.h:622
scheduler::statement avbv(scheduler::operation_node_type ASSIGN_OP, NumericT const *x, NumericT const *y, ScalarT1 const *a, bool flip_a, bool reciprocal_a, NumericT const *z, ScalarT2 const *b, bool flip_b, bool reciprocal_b)
Definition: preset.hpp:16
statement norm_2(scalar< NumericT > const *s, vector_base< NumericT > const *x)
Definition: preset.hpp:224
statement sum(scalar< NumericT > const *s, vector_base< NumericT > const *x)
Definition: preset.hpp:246
Main namespace in ViennaCL. Holds all the basic types such as vector, matrix, etc. and defines operations upon them.
Definition: cpu_ram.hpp:29
statement min(scalar< NumericT > const *s, vector_base< NumericT > const *x)
Definition: preset.hpp:259
statement binary_element_op(NumericT const *x, NumericT const *y, NumericT const *z, scheduler::operation_node_type TYPE)
Definition: preset.hpp:267
std::vector< value_type > container_type
Definition: forwards.h:509
Base class for representing matrices where the individual entries are not all stored explicitly...
Definition: matrix_def.hpp:35
statement row_reduction_mat_vec_prod(viennacl::matrix_base< NumericT > const *A, bool A_trans, viennacl::vector_base< NumericT > const *x, viennacl::vector_base< NumericT > const *y, scheduler::operation_node_type ROP)
Definition: preset.hpp:352
statement mat_vec_prod(viennacl::matrix_base< NumericT > const *A, bool A_trans, viennacl::vector_base< NumericT > const *x, viennacl::vector_base< NumericT > const *y)
Definition: preset.hpp:393
statement index_norm_inf(scalar< NumericT > const *s, vector_base< NumericT > const *x)
Definition: preset.hpp:238
statement norm_1(scalar< NumericT > const *s, vector_base< NumericT > const *x)
Definition: preset.hpp:218
statement norm_inf(scalar< NumericT > const *s, vector_base< NumericT > const *x)
Definition: preset.hpp:230
statement unary_element_op(NumericT const *x, NumericT const *y, scheduler::operation_node_type TYPE)
Definition: preset.hpp:288
Forwards declaration.
scheduler::statement av(scheduler::operation_node_type ASSIGN_OP, NumericT const *x, NumericT const *y, ScalarT1 const *a, bool flip_a, bool reciprocal_a)
Definition: preset.hpp:71
std::size_t vcl_size_t
Definition: forwards.h:74
Provides the datastructures for dealing with a single statement such as 'x = y + z;'.
operation_node_type
Enumeration for identifying the possible operations.
Definition: forwards.h:68
All the predicates used within ViennaCL. Checks for expressions to be vectors, etc.
statement mat_mat_prod(NumericT alpha, viennacl::matrix_base< NumericT > const *A, bool A_trans, viennacl::matrix_base< NumericT > const *B, bool B_trans, NumericT beta, viennacl::matrix_base< NumericT > const *C)
Definition: preset.hpp:399
statement matrix_diag_to_vector(viennacl::vector_base< NumericT > const *x, viennacl::matrix_base< NumericT > const *A, int id)
Definition: preset.hpp:340
device_specific::statements_container swap(NumericT const *x, NumericT const *y)
Definition: preset.hpp:86
scheduler::statement diagonal_assign_cpu(matrix_base< NumericT > const *x, implicit_vector_base< NumericT > const *y)
Definition: preset.hpp:130
statement matrix_row(viennacl::vector_base< NumericT > const *x, viennacl::matrix_base< NumericT > const *A, unsigned int id)
Definition: preset.hpp:327
statement matrix_column(viennacl::vector_base< NumericT > const *x, viennacl::matrix_base< NumericT > const *A, unsigned int id)
Definition: preset.hpp:333
The main class for representing a statement such as x = inner_prod(y,z); at runtime.
Definition: forwards.h:504
Common base class for representing vectors where the entries are not all stored explicitly.
Definition: vector_def.hpp:36
scheduler::statement assign_cpu(vector_base< NumericT > const *x, implicit_vector_base< NumericT > const *y)
Definition: preset.hpp:106
device_specific::statements_container plane_rotation(vector_base< NumericT > const *x, vector_base< NumericT > const *y, NumericT const *a, NumericT const *b)
Definition: preset.hpp:78
scheduler::statement reduction_inner_prod(ScalarT const *s, vector_base< NumericT > const *x, vector_base< NumericT > const *y, scheduler::operation_node_type ROP, bool use_sqrt, bool x_abs)
Definition: preset.hpp:149
A tag class representing element-wise casting operations on vectors and matrices. ...
Definition: forwards.h:125