ViennaCL - The Vienna Computing Library  1.7.0
Free open-source GPU-accelerated linear algebra and solver library.
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-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 
21 
26 #include <iostream>
27 #include <sstream>
28 
29 #include "viennacl/forwards.h"
31 
32 
33 namespace viennacl
34 {
35 namespace scheduler
36 {
37 namespace detail
38 {
39 #define VIENNACL_TRANSLATE_OP_TO_STRING(NAME) case NAME: return #NAME;
40 
42  inline std::string to_string(viennacl::scheduler::op_element op_elem)
43  {
45  {
46  switch (op_elem.type)
47  {
69 
71 
72  default: throw statement_not_supported_exception("Cannot convert unary operation to string");
73  }
74  }
75  else if (op_elem.type_family == OPERATION_BINARY_TYPE_FAMILY)
76  {
77  switch (op_elem.type)
78  {
91 
92  default: throw statement_not_supported_exception("Cannot convert unary operation to string");
93  }
94  }
95  else if (op_elem.type_family == OPERATION_INVALID_TYPE_FAMILY)
96  {
97  if (op_elem.type == OPERATION_INVALID_TYPE)
98  return "OPERATION_INVALID_TYPE";
99  else
100  throw statement_not_supported_exception("Unknown invalid operation type when converting to string");
101  }
102  else
103  throw statement_not_supported_exception("Unknown operation family when converting to string");
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_MATRIX_TYPE)
183  {
184  ss << ", DENSE_MATRIX_TYPE ";
185  switch (element.numeric_type)
186  {
187  //VIENNACL_TRANSLATE_ELEMENT_TO_STRING(CHAR_TYPE, matrix_char)
188  //VIENNACL_TRANSLATE_ELEMENT_TO_STRING(UCHAR_TYPE, matrix_uchar)
189  //VIENNACL_TRANSLATE_ELEMENT_TO_STRING(SHORT_TYPE, matrix_short)
190  //VIENNACL_TRANSLATE_ELEMENT_TO_STRING(USHORT_TYPE, matrix_ushort)
191  //VIENNACL_TRANSLATE_ELEMENT_TO_STRING(INT_TYPE, matrix_int)
192  //VIENNACL_TRANSLATE_ELEMENT_TO_STRING(UINT_TYPE, matrix_uint)
193  //VIENNACL_TRANSLATE_ELEMENT_TO_STRING(LONG_TYPE, matrix_long)
194  //VIENNACL_TRANSLATE_ELEMENT_TO_STRING(ULONG_TYPE, matrix_ulong)
195  //VIENNACL_TRANSLATE_ELEMENT_TO_STRING(HALF_TYPE, matrix_half)
198 
199  default: throw statement_not_supported_exception("Cannot convert dense matrix type to string");
200  }
201  }
202  else
203  throw statement_not_supported_exception("Cannot convert matrix sub-type to string");
204  }
205  else if (element.type_family == INVALID_TYPE_FAMILY)
206  {
207  return "INVALID_TYPE_FAMILY";
208  }
209  else
210  throw statement_not_supported_exception("Unknown operation family when converting to string");
211  }
212 
213 #undef VIENNACL_TRANSLATE_ELEMENT_TO_STRING
214 
215 } // namespace detail
216 
217 
219 inline std::ostream & operator<<(std::ostream & os, viennacl::scheduler::statement_node const & s_node)
220 {
221  os << "LHS: " << detail::to_string(s_node.lhs) << ", "
222  << "OP: " << detail::to_string(s_node.op) << ", "
223  << "RHS: " << detail::to_string(s_node.rhs);
224 
225  return os;
226 }
227 
228 namespace detail
229 {
231  inline void print_node(std::ostream & os, viennacl::scheduler::statement const & s, vcl_size_t node_index, vcl_size_t indent = 0)
232  {
233  typedef viennacl::scheduler::statement::container_type StatementNodeContainer;
234  typedef viennacl::scheduler::statement::value_type StatementNode;
235 
236  StatementNodeContainer const & nodes = s.array();
237  StatementNode const & current_node = nodes[node_index];
238 
239  for (vcl_size_t i=0; i<indent; ++i)
240  os << " ";
241 
242  os << "Node " << node_index << ": " << current_node << std::endl;
243 
244  if (current_node.lhs.type_family == COMPOSITE_OPERATION_FAMILY)
245  print_node(os, s, current_node.lhs.node_index, indent+1);
246 
247  if (current_node.rhs.type_family == COMPOSITE_OPERATION_FAMILY)
248  print_node(os, s, current_node.rhs.node_index, indent+1);
249  }
250 }
251 
258 inline std::ostream & operator<<(std::ostream & os, viennacl::scheduler::statement const & s)
259 {
260  detail::print_node(os, s, s.root());
261  return os;
262 }
263 
264 }
265 } //namespace viennacl
266 
267 #endif
268 
statement_node_subtype subtype
Definition: forwards.h:340
This file provides the forward declarations for the main types used within ViennaCL.
statement_node_type_family type_family
Definition: forwards.h:339
A class representing the 'data' for the LHS or RHS operand of the respective node.
Definition: forwards.h:337
container_type const & array() const
Definition: forwards.h:528
#define VIENNACL_TRANSLATE_OP_TO_STRING(NAME)
Definition: io.hpp:39
operation_node_type_family type_family
Definition: forwards.h:473
Struct for holding the type family as well as the type of an operation (could be addition, subtraction, norm, etc.)
Definition: forwards.h:471
Main namespace in ViennaCL. Holds all the basic types such as vector, matrix, etc. and defines operations upon them.
Definition: cpu_ram.hpp:34
std::vector< value_type > container_type
Definition: forwards.h:507
std::string to_string(viennacl::scheduler::op_element op_elem)
Helper routine for converting the operation enums to string.
Definition: io.hpp:42
Definition: blas3.hpp:36
#define VIENNACL_TRANSLATE_ELEMENT_TO_STRING(NAME, ELEMENT)
Definition: io.hpp:107
statement_node_numeric_type numeric_type
Definition: forwards.h:341
std::size_t vcl_size_t
Definition: forwards.h:75
Provides the datastructures for dealing with a single statement such as 'x = y + z;'.
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:231
std::ostream & operator<<(std::ostream &os, viennacl::scheduler::statement_node const &s_node)
Print a single statement_node. Non-recursive.
Definition: io.hpp:219
operation_node_type type
Definition: forwards.h:474
size_type root() const
Definition: forwards.h:530
The main class for representing a statement such as x = inner_prod(y,z); at runtime.
Definition: forwards.h:502
Main datastructure for an node in the statement tree.
Definition: forwards.h:478
Exception for the case the scheduler is unable to deal with the operation.
Definition: forwards.h:38