ViennaCL - The Vienna Computing Library  1.5.2
execute_util.hpp
Go to the documentation of this file.
1 #ifndef VIENNACL_SCHEDULER_EXECUTE_UTIL_HPP
2 #define VIENNACL_SCHEDULER_EXECUTE_UTIL_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 <assert.h>
27 
28 #include "viennacl/forwards.h"
29 #include "viennacl/scalar.hpp"
30 #include "viennacl/vector.hpp"
31 #include "viennacl/matrix.hpp"
33 
34 namespace viennacl
35 {
36  namespace scheduler
37  {
38  namespace detail
39  {
40  //
41  inline lhs_rhs_element const & extract_representative_vector(statement const & s, lhs_rhs_element const & element)
42  {
43  switch (element.type_family)
44  {
45  case VECTOR_TYPE_FAMILY:
46  return element;
48  {
49  statement_node const & leaf = s.array()[element.node_index];
50 
52  return extract_representative_vector(s, leaf.lhs);
53  switch (leaf.op.type)
54  {
61  return extract_representative_vector(s, leaf.lhs);
63  return extract_representative_vector(s, leaf.rhs);
64  default:
65  throw statement_not_supported_exception("Vector leaf encountered an invalid binary operation!");
66  }
67  }
68  default:
69  throw statement_not_supported_exception("Vector leaf encountered an invalid node type!");
70  }
71  }
72 
73 
74  // helper routines for extracting the scalar type
75  inline float convert_to_float(float f) { return f; }
76  inline float convert_to_float(double d) { return static_cast<float>(d); }
77  inline float convert_to_float(lhs_rhs_element const & el)
78  {
80  return el.host_float;
82  return *el.scalar_float;
83 
84  throw statement_not_supported_exception("Cannot convert to float");
85  }
86 
87  // helper routines for extracting the scalar type
88  inline double convert_to_double(float d) { return static_cast<double>(d); }
89  inline double convert_to_double(double d) { return d; }
90  inline double convert_to_double(lhs_rhs_element const & el)
91  {
93  return el.host_double;
95  return *el.scalar_double;
96 
97  throw statement_not_supported_exception("Cannot convert to double");
98  }
99 
101 
102  inline void new_element(lhs_rhs_element & new_elem, lhs_rhs_element const & old_element)
103  {
104  new_elem.type_family = old_element.type_family;
105  new_elem.subtype = old_element.subtype;
106  new_elem.numeric_type = old_element.numeric_type;
107  if (new_elem.type_family == SCALAR_TYPE_FAMILY)
108  {
109  assert(new_elem.subtype == DEVICE_SCALAR_TYPE && bool("Expected a device scalar in root node"));
110 
111  switch (new_elem.numeric_type)
112  {
113  case FLOAT_TYPE:
114  new_elem.scalar_float = new viennacl::scalar<float>();
115  return;
116  case DOUBLE_TYPE:
117  new_elem.scalar_double = new viennacl::scalar<double>();
118  return;
119  default:
120  throw statement_not_supported_exception("Invalid vector type for vector construction");
121  }
122  }
123  else if (new_elem.type_family == VECTOR_TYPE_FAMILY)
124  {
125  assert(new_elem.subtype == DENSE_VECTOR_TYPE && bool("Expected a dense vector in root node"));
126 
127  switch (new_elem.numeric_type)
128  {
129  case FLOAT_TYPE:
130  new_elem.vector_float = new viennacl::vector<float>((old_element.vector_float)->size());
131  return;
132  case DOUBLE_TYPE:
133  new_elem.vector_double = new viennacl::vector<double>((old_element.vector_float)->size());
134  return;
135  default:
136  throw statement_not_supported_exception("Invalid vector type for vector construction");
137  }
138  }
139  else if (new_elem.type_family == MATRIX_TYPE_FAMILY)
140  {
141  assert( (new_elem.subtype == DENSE_COL_MATRIX_TYPE || new_elem.subtype == DENSE_ROW_MATRIX_TYPE)
142  && bool("Expected a dense matrix in root node"));
143 
144  if (new_elem.subtype == DENSE_COL_MATRIX_TYPE)
145  {
146  switch (new_elem.numeric_type)
147  {
148  case FLOAT_TYPE:
150  return;
151  case DOUBLE_TYPE:
153  return;
154  default:
155  throw statement_not_supported_exception("Invalid vector type for vector construction");
156  }
157  }
158  else if (new_elem.subtype == DENSE_ROW_MATRIX_TYPE)
159  {
160  switch (new_elem.numeric_type)
161  {
162  case FLOAT_TYPE:
164  return;
165  case DOUBLE_TYPE:
167  return;
168  default:
169  throw statement_not_supported_exception("Invalid vector type for vector construction");
170  }
171  }
172  else
173  throw statement_not_supported_exception("Expected a dense matrix in root node when creating a temporary");
174  }
175  else
176  throw statement_not_supported_exception("Unknown type familty when creating new temporary object");
177  }
178 
179  inline void delete_element(lhs_rhs_element & elem)
180  {
181  if (elem.type_family == SCALAR_TYPE_FAMILY)
182  {
183  switch (elem.numeric_type)
184  {
185  case FLOAT_TYPE:
186  delete elem.scalar_float;
187  return;
188  case DOUBLE_TYPE:
189  delete elem.scalar_double;
190  return;
191  default:
192  throw statement_not_supported_exception("Invalid vector type for vector destruction");
193  }
194  }
195  else if (elem.type_family == VECTOR_TYPE_FAMILY)
196  {
197  switch (elem.numeric_type)
198  {
199  case FLOAT_TYPE:
200  delete elem.vector_float;
201  return;
202  case DOUBLE_TYPE:
203  delete elem.vector_double;
204  return;
205  default:
206  throw statement_not_supported_exception("Invalid vector type for vector destruction");
207  }
208  }
209  else if (elem.type_family == MATRIX_TYPE_FAMILY)
210  {
211  if (elem.subtype == DENSE_COL_MATRIX_TYPE)
212  {
213  switch (elem.numeric_type)
214  {
215  case FLOAT_TYPE:
216  delete elem.matrix_col_float;
217  return;
218  case DOUBLE_TYPE:
219  delete elem.matrix_col_double;
220  return;
221  default:
222  throw statement_not_supported_exception("Invalid vector type for vector destruction");
223  }
224  }
225  else if (elem.subtype == DENSE_ROW_MATRIX_TYPE)
226  {
227  switch (elem.numeric_type)
228  {
229  case FLOAT_TYPE:
230  delete elem.matrix_row_float;
231  return;
232  case DOUBLE_TYPE:
233  delete elem.matrix_row_double;
234  return;
235  default:
236  throw statement_not_supported_exception("Invalid vector type for vector destruction");
237  }
238  }
239  else
240  throw statement_not_supported_exception("Expected a dense matrix in root node when deleting temporary");
241  }
242  else
243  throw statement_not_supported_exception("Unknown type familty when deleting temporary object");
244  }
245 
246  } // namespace detail
247 
248 
249  } // namespace scheduler
250 } // namespace viennacl
251 
252 #endif
253 
statement_node_subtype subtype
Definition: forwards.h:270
viennacl::matrix_base< float > * matrix_row_float
Definition: forwards.h:339
vcl_size_t node_index
Definition: forwards.h:276
void new_element(lhs_rhs_element &new_elem, lhs_rhs_element const &old_element)
Definition: execute_util.hpp:102
Implementation of the dense matrix class.
lhs_rhs_element lhs
Definition: forwards.h:422
Definition: forwards.h:176
vcl_size_t size1(MatrixType const &mat)
Generic routine for obtaining the number of rows of a matrix (ViennaCL, uBLAS, etc.)
Definition: size.hpp:216
Definition: forwards.h:217
Definition: forwards.h:185
double convert_to_double(float d)
Definition: execute_util.hpp:88
viennacl::scalar< float > * scalar_float
Definition: forwards.h:303
A dense matrix class.
Definition: forwards.h:293
This file provides the forward declarations for the main types used within ViennaCL.
A class representing the 'data' for the LHS or RHS operand of the respective node.
Definition: forwards.h:267
operation_node_type_family type_family
Definition: forwards.h:415
lhs_rhs_element rhs
Definition: forwards.h:424
result_of::size_type< MatrixType >::type size2(MatrixType const &mat)
Generic routine for obtaining the number of columns of a matrix (ViennaCL, uBLAS, etc...
Definition: size.hpp:245
viennacl::matrix_base< float, viennacl::column_major > * matrix_col_float
Definition: forwards.h:351
viennacl::scalar< double > * scalar_double
Definition: forwards.h:304
void delete_element(lhs_rhs_element &elem)
Definition: execute_util.hpp:179
Main namespace in ViennaCL. Holds all the basic types such as vector, matrix, etc. and defines operations upon them.
Definition: cpu_ram.hpp:29
viennacl::matrix_base< double, viennacl::column_major > * matrix_col_double
Definition: forwards.h:352
Definition: forwards.h:170
viennacl::vector_base< float > * vector_float
Definition: forwards.h:315
vcl_size_t size(VectorType const &vec)
Generic routine for obtaining the size of a vector (ViennaCL, uBLAS, etc.)
Definition: size.hpp:144
statement_node_numeric_type numeric_type
Definition: forwards.h:271
viennacl::vector_base< double > * vector_double
Definition: forwards.h:316
Definition: forwards.h:184
Definition: forwards.h:173
double host_double
Definition: forwards.h:290
float host_float
Definition: forwards.h:289
Provides the datastructures for dealing with a single statement such as 'x = y + z;'.
A vector class representing a linear memory sequence on the GPU. Inspired by boost::numeric::ublas::v...
Definition: forwards.h:208
container_type const & array() const
Definition: forwards.h:473
The vector type with operator-overloads and proxy classes is defined here. Linear algebra operations ...
float convert_to_float(float f)
Definition: execute_util.hpp:75
lhs_rhs_element const & extract_representative_vector(statement const &s, lhs_rhs_element const &element)
Definition: execute_util.hpp:41
viennacl::matrix_base< double > * matrix_row_double
Definition: forwards.h:340
statement_node_type_family type_family
Definition: forwards.h:269
The main class for representing a statement such as x = inner_prod(y,z); at runtime.
Definition: forwards.h:447
Definition: forwards.h:187
op_element op
Definition: forwards.h:423
Implementation of the ViennaCL scalar class.
Definition: forwards.h:216
Main datastructure for an node in the statement tree.
Definition: forwards.h:420
Exception for the case the scheduler is unable to deal with the operation.
Definition: forwards.h:36
operation_node_type type
Definition: forwards.h:416