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