ViennaCL - The Vienna Computing Library  1.5.2
io.hpp
Go to the documentation of this file.
1 #ifndef VIENNACL_SCHEDULER_IO_HPP
2 #define VIENNACL_SCHEDULER_IO_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 <iostream>
27 #include <sstream>
28 
29 #include "viennacl/forwards.h"
31 
32 
33 namespace viennacl
34 {
35  namespace scheduler
36  {
37 
38  namespace detail
39  {
40 #define VIENNACL_TRANSLATE_OP_TO_STRING(NAME) case NAME: return #NAME;
41 
44  {
46  {
47  switch (op_elem.type)
48  {
70 
71  default: throw statement_not_supported_exception("Cannot convert unary operation to string");
72  }
73  }
74  else if (op_elem.type_family == OPERATION_BINARY_TYPE_FAMILY)
75  {
76  switch (op_elem.type)
77  {
90 
91  default: throw statement_not_supported_exception("Cannot convert unary operation to string");
92  }
93  }
94  else if (op_elem.type_family == OPERATION_INVALID_TYPE_FAMILY)
95  {
96  if (op_elem.type == OPERATION_INVALID_TYPE)
97  return "OPERATION_INVALID_TYPE";
98  else
99  throw statement_not_supported_exception("Unknown invalid operation type when converting to string");
100  }
101  else
102  throw statement_not_supported_exception("Unknown operation family when converting to string");
103  }
104 
105 #undef VIENNACL_TRANSLATE_OP_TO_STRING
106 
107 #define VIENNACL_TRANSLATE_ELEMENT_TO_STRING(NAME, ELEMENT) case NAME: ss << "(" << element.ELEMENT << ")"; return #NAME + ss.str();
108 
111  {
112  std::stringstream ss;
113 
115  {
116  ss << "(" << element.node_index << ")";
117  return "COMPOSITE_OPERATION_FAMILY" + ss.str();
118  }
119  else if (element.type_family == SCALAR_TYPE_FAMILY)
120  {
121  if (element.subtype == HOST_SCALAR_TYPE)
122  {
123  ss << ", HOST_SCALAR_TYPE ";
124  switch (element.numeric_type)
125  {
136 
137  default: throw statement_not_supported_exception("Cannot convert host scalar type to string");
138  }
139  }
140  else
141  {
142  ss << ", DEVICE_SCALAR_TYPE";
143  switch (element.numeric_type)
144  {
145  //VIENNACL_TRANSLATE_ELEMENT_TO_STRING(CHAR_TYPE, scalar_char)
146  //VIENNACL_TRANSLATE_ELEMENT_TO_STRING(UCHAR_TYPE, scalar_uchar)
147  //VIENNACL_TRANSLATE_ELEMENT_TO_STRING(SHORT_TYPE, scalar_short)
148  //VIENNACL_TRANSLATE_ELEMENT_TO_STRING(USHORT_TYPE, scalar_ushort)
149  //VIENNACL_TRANSLATE_ELEMENT_TO_STRING(INT_TYPE, scalar_int)
150  //VIENNACL_TRANSLATE_ELEMENT_TO_STRING(UINT_TYPE, scalar_uint)
151  //VIENNACL_TRANSLATE_ELEMENT_TO_STRING(LONG_TYPE, scalar_long)
152  //VIENNACL_TRANSLATE_ELEMENT_TO_STRING(ULONG_TYPE, scalar_ulong)
153  //VIENNACL_TRANSLATE_ELEMENT_TO_STRING(HALF_TYPE, scalar_half)
156  default: throw statement_not_supported_exception("Cannot convert scalar type to string");
157  }
158  }
159  }
160  else if (element.type_family == VECTOR_TYPE_FAMILY)
161  {
162  ss << ", DENSE_VECTOR_TYPE ";
163  switch (element.numeric_type)
164  {
165  //VIENNACL_TRANSLATE_ELEMENT_TO_STRING(CHAR_TYPE, vector_char)
166  //VIENNACL_TRANSLATE_ELEMENT_TO_STRING(UCHAR_TYPE, vector_uchar)
167  //VIENNACL_TRANSLATE_ELEMENT_TO_STRING(SHORT_TYPE, vector_short)
168  //VIENNACL_TRANSLATE_ELEMENT_TO_STRING(USHORT_TYPE, vector_ushort)
169  //VIENNACL_TRANSLATE_ELEMENT_TO_STRING(INT_TYPE, vector_int)
170  //VIENNACL_TRANSLATE_ELEMENT_TO_STRING(UINT_TYPE, vector_uint)
171  //VIENNACL_TRANSLATE_ELEMENT_TO_STRING(LONG_TYPE, vector_long)
172  //VIENNACL_TRANSLATE_ELEMENT_TO_STRING(ULONG_TYPE, vector_ulong)
173  //VIENNACL_TRANSLATE_ELEMENT_TO_STRING(HALF_TYPE, vector_half)
176 
177  default: throw statement_not_supported_exception("Cannot convert vector type to string");
178  }
179  }
180  else if (element.type_family == MATRIX_TYPE_FAMILY)
181  {
182  if (element.subtype == DENSE_ROW_MATRIX_TYPE)
183  {
184  ss << ", DENSE_ROW_MATRIX_TYPE ";
185  switch (element.numeric_type)
186  {
187  //VIENNACL_TRANSLATE_ELEMENT_TO_STRING(CHAR_TYPE, matrix_row_char)
188  //VIENNACL_TRANSLATE_ELEMENT_TO_STRING(UCHAR_TYPE, matrix_row_uchar)
189  //VIENNACL_TRANSLATE_ELEMENT_TO_STRING(SHORT_TYPE, matrix_row_short)
190  //VIENNACL_TRANSLATE_ELEMENT_TO_STRING(USHORT_TYPE, matrix_row_ushort)
191  //VIENNACL_TRANSLATE_ELEMENT_TO_STRING(INT_TYPE, matrix_row_int)
192  //VIENNACL_TRANSLATE_ELEMENT_TO_STRING(UINT_TYPE, matrix_row_uint)
193  //VIENNACL_TRANSLATE_ELEMENT_TO_STRING(LONG_TYPE, matrix_row_long)
194  //VIENNACL_TRANSLATE_ELEMENT_TO_STRING(ULONG_TYPE, matrix_row_ulong)
195  //VIENNACL_TRANSLATE_ELEMENT_TO_STRING(HALF_TYPE, matrix_row_half)
198 
199  default: throw statement_not_supported_exception("Cannot convert row-major matrix type to string");
200  }
201  }
202  else
203  {
204  ss << ", DENSE_COL_MATRIX_TYPE ";
205  switch (element.numeric_type)
206  {
207  //VIENNACL_TRANSLATE_ELEMENT_TO_STRING(CHAR_TYPE, matrix_col_char)
208  //VIENNACL_TRANSLATE_ELEMENT_TO_STRING(UCHAR_TYPE, matrix_col_uchar)
209  //VIENNACL_TRANSLATE_ELEMENT_TO_STRING(SHORT_TYPE, matrix_col_short)
210  //VIENNACL_TRANSLATE_ELEMENT_TO_STRING(USHORT_TYPE, matrix_col_ushort)
211  //VIENNACL_TRANSLATE_ELEMENT_TO_STRING(INT_TYPE, matrix_col_int)
212  //VIENNACL_TRANSLATE_ELEMENT_TO_STRING(UINT_TYPE, matrix_col_uint)
213  //VIENNACL_TRANSLATE_ELEMENT_TO_STRING(LONG_TYPE, matrix_col_long)
214  //VIENNACL_TRANSLATE_ELEMENT_TO_STRING(ULONG_TYPE, matrix_col_ulong)
215  //VIENNACL_TRANSLATE_ELEMENT_TO_STRING(HALF_TYPE, matrix_col_half)
218 
219  default: throw statement_not_supported_exception("Cannot convert column-major matrix type to string");
220  }
221  }
222  }
223  else if (element.type_family == INVALID_TYPE_FAMILY)
224  {
225  return "INVALID_TYPE_FAMILY";
226  }
227  else
228  throw statement_not_supported_exception("Unknown operation family when converting to string");
229  }
230 
231 #undef VIENNACL_TRANSLATE_ELEMENT_TO_STRING
232 
233  } // namespace detail
234 
235 
237  std::ostream & operator<<(std::ostream & os, viennacl::scheduler::statement_node const & s_node)
238  {
239  os << "LHS: " << detail::to_string(s_node.lhs) << ", "
240  << "OP: " << detail::to_string(s_node.op) << ", "
241  << "RHS: " << detail::to_string(s_node.rhs);
242 
243  return os;
244  }
245 
246 
247 
248 
249 
250  namespace detail
251  {
253  void print_node(std::ostream & os, viennacl::scheduler::statement const & s, vcl_size_t node_index, vcl_size_t indent = 0)
254  {
255  typedef viennacl::scheduler::statement::container_type StatementNodeContainer;
256  typedef viennacl::scheduler::statement::value_type StatementNode;
257 
258  StatementNodeContainer const & nodes = s.array();
259  StatementNode const & current_node = nodes[node_index];
260 
261  for (vcl_size_t i=0; i<indent; ++i)
262  os << " ";
263 
264  os << "Node " << node_index << ": " << current_node << std::endl;
265 
266  if (current_node.lhs.type_family == COMPOSITE_OPERATION_FAMILY)
267  print_node(os, s, current_node.lhs.node_index, indent+1);
268 
269  if (current_node.rhs.type_family == COMPOSITE_OPERATION_FAMILY)
270  print_node(os, s, current_node.rhs.node_index, indent+1);
271  }
272  }
273 
280  std::ostream & operator<<(std::ostream & os, viennacl::scheduler::statement const & s)
281  {
282  detail::print_node(os, s, s.root());
283  return os;
284  }
285  }
286 
287 } //namespace viennacl
288 
289 #endif
290 
statement_node_subtype subtype
Definition: forwards.h:270
std::size_t vcl_size_t
Definition: forwards.h:58
Definition: forwards.h:210
vcl_size_t node_index
Definition: forwards.h:276
lhs_rhs_element lhs
Definition: forwards.h:422
Definition: forwards.h:176
Definition: forwards.h:217
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
#define VIENNACL_TRANSLATE_OP_TO_STRING(NAME)
Definition: io.hpp:40
operation_node_type_family type_family
Definition: forwards.h:415
lhs_rhs_element rhs
Definition: forwards.h:424
Struct for holding the type family as well as the type of an operation (could be addition, subtraction, norm, etc.)
Definition: forwards.h:413
Definition: forwards.h:214
Main namespace in ViennaCL. Holds all the basic types such as vector, matrix, etc. and defines operations upon them.
Definition: cpu_ram.hpp:29
Definition: forwards.h:170
std::string to_string(viennacl::scheduler::op_element op_elem)
Helper routine for converting the operation enums to string.
Definition: io.hpp:43
statement_node_numeric_type numeric_type
Definition: forwards.h:271
#define VIENNACL_TRANSLATE_ELEMENT_TO_STRING(NAME, ELEMENT)
Definition: io.hpp:107
Definition: forwards.h:184
size_type root() const
Definition: forwards.h:475
Definition: forwards.h:173
Definition: forwards.h:209
Provides the datastructures for dealing with a single statement such as 'x = y + z;'.
std::vector< value_type > container_type
Definition: forwards.h:452
void print_node(std::ostream &os, viennacl::scheduler::statement const &s, vcl_size_t node_index, vcl_size_t indent=0)
Recursive worker routine for printing a whole statement.
Definition: io.hpp:253
container_type const & array() const
Definition: forwards.h:473
std::ostream & operator<<(std::ostream &os, viennacl::scheduler::statement_node const &s_node)
Print a single statement_node. Non-recursive.
Definition: io.hpp:237
Definition: forwards.h:211
Definition: forwards.h:207
Definition: forwards.h:212
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
op_element op
Definition: forwards.h:423
Definition: forwards.h:208
Definition: forwards.h:216
Main datastructure for an node in the statement tree.
Definition: forwards.h:420
Definition: forwards.h:213
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