ViennaCL - The Vienna Computing Library  1.5.2
statement_representation_functor.hpp
Go to the documentation of this file.
1 #ifndef VIENNACL_GENERATOR_STATEMENT_REPRESENTATION_HPP
2 #define VIENNACL_GENERATOR_STATEMENT_REPRESENTATION_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 <set>
27 #include <cstring>
28 
29 #include "viennacl/forwards.h"
32 
34 
35 #include "viennacl/ocl/backend.hpp"
36 #include "viennacl/ocl/kernel.hpp"
37 
40 
44 
45 namespace viennacl{
46 
47  namespace generator{
48 
49  namespace detail{
50 
53  private:
54  unsigned int get_id(void * handle) const{
55  unsigned int i = 0;
56  for( ; i < 64 ; ++i){
57  void* current = memory_[i];
58  if(current==NULL)
59  break;
60  if(current==handle)
61  return i;
62  }
63  memory_[i] = handle;
64  return i;
65  }
66 
67  static void append_id(char * & ptr, unsigned int val){
68  if(val==0)
69  *ptr++='0';
70  else
71  while(val>0)
72  {
73  *ptr++=static_cast<char>('0') + static_cast<char>(val % 10);
74  val /= 10;
75  }
76  }
77 
78  public:
79  typedef void result_type;
80 
81  statement_representation_functor(void* (&memory)[64], unsigned int , char *& ptr) : memory_(memory), ptr_(ptr){ }
82 
83  template<class ScalarType>
84  result_type operator()(ScalarType const & /*scal*/) const {
85  *ptr_++='h'; //host
86  *ptr_++='s'; //scalar
88  }
89 
91  template<class ScalarType>
92  result_type operator()(scalar<ScalarType> const & scal) const {
93  *ptr_++='s'; //scalar
95  append_id(ptr_, get_id((void*)&scal));
96  }
97 
99  template<class ScalarType>
100  result_type operator()(vector_base<ScalarType> const & vec) const {
101  *ptr_++='v'; //vector
102  if(viennacl::traits::start(vec)>0)
103  *ptr_++='r';
104  if(vec.stride()>1)
105  *ptr_++='s';
107  append_id(ptr_, get_id((void*)&vec));
108  }
109 
111  template<class ScalarType>
112  result_type operator()(implicit_vector_base<ScalarType> const & vec) const {
113  *ptr_++='i'; //implicit
114  *ptr_++='v'; //vector
115  if(vec.is_value_static())
116  *ptr_++='v'; //value
117  if(vec.has_index())
118  *ptr_++='i';
120  }
121 
123  template<class ScalarType, class Layout>
124  result_type operator()(matrix_base<ScalarType, Layout> const & mat) const {
125  *ptr_++='m'; //vector
126  if(viennacl::traits::start1(mat)>0)
127  *ptr_++='r';
128  if(viennacl::traits::stride1(mat)>1)
129  *ptr_++='s';
130  if(viennacl::traits::start2(mat)>0)
131  *ptr_++='r';
132  if(viennacl::traits::stride2(mat)>1)
133  *ptr_++='s';
136  append_id(ptr_, get_id((void*)&mat));
137  }
138 
140  template<class ScalarType>
141  result_type operator()(implicit_matrix_base<ScalarType> const & mat) const {
142  *ptr_++='i'; //implicit
143  *ptr_++='m'; //matrix
144  if(mat.is_value_static())
145  *ptr_++='v'; //value
147  }
148 
151  utils::call_on_element(root_node->lhs, *this);
152  else if(node_type==RHS_NODE_TYPE && root_node->rhs.type_family != scheduler::COMPOSITE_OPERATION_FAMILY)
153  utils::call_on_element(root_node->rhs, *this);
154  else if(node_type==PARENT_NODE_TYPE){
155  const char * op_expr = detail::generate(root_node->op.type);
156  vcl_size_t n = std::strlen(op_expr);
157  std::memcpy(ptr_, op_expr, n);
158  ptr_+=n;
159  }
160  }
161 
162  private:
163  void* (&memory_)[64];
164  char *& ptr_;
165  };
166 
167  }
168 
169  }
170 
171 }
172 #endif
result_type operator()(implicit_vector_base< ScalarType > const &vec) const
Implicit vector mapping.
Definition: statement_representation_functor.hpp:112
std::size_t vcl_size_t
Definition: forwards.h:58
result_type operator()(ScalarType const &) const
Definition: statement_representation_functor.hpp:84
result_of::size_type< matrix_base< NumericT, F > >::type stride2(matrix_base< NumericT, F > const &s)
Definition: stride.hpp:68
bool is_value_static() const
Definition: vector.hpp:64
Internal utils for a dynamic OpenCL kernel generation.
Extracts the underlying OpenCL start index handle from a vector, a matrix, an expression etc...
This class represents a single scalar value on the GPU and behaves mostly like a built-in scalar type...
Definition: forwards.h:172
result_type operator()(matrix_base< ScalarType, Layout > const &mat) const
Matrix mapping.
Definition: statement_representation_functor.hpp:124
lhs_rhs_element lhs
Definition: forwards.h:422
Common base class for representing vectors where the entries are not all stored explicitly.
Definition: forwards.h:190
result_type operator()(vector_base< ScalarType > const &vec) const
Vector mapping.
Definition: statement_representation_functor.hpp:100
Helper struct for obtaining the first letter of a type. Used internally by the generator only...
Definition: utils.hpp:223
result_type operator()(scalar< ScalarType > const &scal) const
Scalar mapping.
Definition: statement_representation_functor.hpp:92
This file provides the forward declarations for the main types used within ViennaCL.
result_of::size_type< T >::type start1(T const &obj)
Definition: start.hpp:64
Determines row and column increments for matrices and matrix proxies.
lhs_rhs_element rhs
Definition: forwards.h:424
Helper class for the OpenCL kernel generator, representing a statement.
Definition: statement_representation_functor.hpp:52
Implementation of a shared pointer class (cf. std::shared_ptr, boost::shared_ptr). Will be used until C++11 is widely available.
A dense matrix class.
Definition: forwards.h:290
result_of::size_type< matrix_base< NumericT, F > >::type stride1(matrix_base< NumericT, F > const &s)
Definition: stride.hpp:57
Main namespace in ViennaCL. Holds all the basic types such as vector, matrix, etc. and defines operations upon them.
Definition: cpu_ram.hpp:29
several code generation helpers
result_of::size_type< T >::type start2(T const &obj)
Definition: start.hpp:83
Map ViennaCL objects to generator wrappers.
base functor class for traversing a statement
Definition: helpers.hpp:145
Base class for representing matrices where the individual entries are not all stored explicitly...
Definition: forwards.h:296
result_of::size_type< T >::type start(T const &obj)
Definition: start.hpp:43
void operator()(scheduler::statement const *, scheduler::statement_node const *root_node, detail::node_type node_type) const
Definition: statement_representation_functor.hpp:149
Forwards declaration.
bool is_value_static() const
Definition: matrix.hpp:57
Provides the datastructures for dealing with a single statement such as 'x = y + z;'.
Implementations of the OpenCL backend, where all contexts are stored in.
void result_type
Definition: statement_representation_functor.hpp:79
node_type
Definition: forwards.h:112
Representation of an OpenCL kernel in ViennaCL.
result_type operator()(implicit_matrix_base< ScalarType > const &mat) const
Implicit matrix mapping.
Definition: statement_representation_functor.hpp:141
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
std::string generate(std::pair< std::string, std::string > const &index, int vector_element, mapped_object const &s)
Definition: mapped_objects.hpp:325
viennacl::backend::mem_handle & handle(T &obj)
Returns the generic memory handle of an object. Non-const version.
Definition: handle.hpp:41
op_element op
Definition: forwards.h:423
statement_representation_functor(void *(&memory)[64], unsigned int, char *&ptr)
Definition: statement_representation_functor.hpp:81
Main datastructure for an node in the statement tree.
Definition: forwards.h:420
size_type stride() const
Returns the stride within the buffer (in multiples of sizeof(SCALARTYPE))
Definition: vector.hpp:849
operation_node_type type
Definition: forwards.h:416
bool has_index() const
Definition: vector.hpp:68