ViennaCL - The Vienna Computing Library  1.5.1
map_functor.hpp
Go to the documentation of this file.
1 #ifndef VIENNACL_GENERATOR_MAP_GENERATE_PROTOTYPE_HPP
2 #define VIENNACL_GENERATOR_MAP_GENERATE_PROTOTYPE_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 
28 #include "viennacl/forwards.h"
29 #include "viennacl/vector.hpp"
30 #include "viennacl/matrix.hpp"
33 
35 
39 
40 namespace viennacl{
41 
42  namespace generator{
43 
44  namespace detail{
45 
48  std::string create_name(unsigned int & current_arg, std::map<void *, vcl_size_t> & memory, void * handle) const{
49  if(handle==NULL)
50  return "arg" + utils::to_string(current_arg_++);
51  if(memory.insert(std::make_pair(handle, current_arg)).second)
52  return "arg" + utils::to_string(current_arg_++);
53  else
54  return "arg" + utils::to_string(memory[handle]);
55  }
56 
57  public:
59 
60  map_functor(std::map<void *, vcl_size_t> & memory, unsigned int & current_arg, mapping_type & mapping) : memory_(memory), current_arg_(current_arg), mapping_(mapping){ }
61 
63  template<class T>
64  result_type binary_leaf(viennacl::scheduler::statement const * statement, viennacl::scheduler::statement_node const * root_node, mapping_type const * mapping) const {
65  T * p = new T("float");
66 
67  p->info_.statement = statement;
68  p->info_.root_node = root_node;
69  p->info_.mapping = mapping;
70 
71  return container_ptr_type(p);
72  }
73 
74  template<class ScalarType>
75  result_type operator()(ScalarType const & /*scal*/) const {
77  p->name_ = create_name(current_arg_, memory_, NULL);
78  return container_ptr_type(p);
79  }
80 
82  template<class ScalarType>
85  p->name_ = create_name(current_arg_, memory_, (void*)&scal);
86  return container_ptr_type(p);
87  }
88 
90  template<class ScalarType>
93  p->name_ = create_name(current_arg_, memory_, (void*)&vec);
94  if(vec.start() > 0)
95  p->start_name_ = p->name_ +"_start";
96  if(vec.stride() > 1)
97  p->stride_name_ = p->name_ + "_stride";
98  return container_ptr_type(p);
99  }
100 
102  template<class ScalarType>
105 
106  if(vec.is_value_static()==false)
107  p->value_name_ = create_name(current_arg_, memory_, NULL);
108  if(vec.has_index())
109  p->value_name_ = create_name(current_arg_, memory_, NULL);
110  return container_ptr_type(p);
111  }
112 
114  template<class ScalarType, class Layout>
117  p->name_ = create_name(current_arg_, memory_, (void*)&mat);
118  p->is_row_major_ = static_cast<bool>(utils::is_same_type<Layout, viennacl::row_major>::value);
119  if(mat.start1() > 0)
120  p->start1_name_ = p->name_ +"_start1";
121  if(mat.stride1() > 1)
122  p->stride1_name_ = p->name_ + "_stride1";
123  if(mat.start2() > 0)
124  p->start2_name_ = p->name_ +"_start2";
125  if(mat.stride2() > 1)
126  p->stride2_name_ = p->name_ + "_stride2";
127  return container_ptr_type(p);
128  }
129 
131  template<class ScalarType>
134 
135  if(mat.is_value_static()==false)
136  p->value_name_ = create_name(current_arg_, memory_, NULL);
137 
138  return container_ptr_type(p);
139  }
140 
143  const key_type key(root_node, node_type);
145  mapping_.insert(mapping_type::value_type(key, utils::call_on_element(root_node->lhs, *this)));
147  mapping_.insert(mapping_type::value_type(key, utils::call_on_element(root_node->rhs, *this)));
148  else if( node_type== PARENT_NODE_TYPE){
149  viennacl::scheduler::operation_node_type op_type = root_node->op.type;
151  mapping_.insert(mapping_type::value_type(key, binary_leaf<mapped_scalar_reduction>(statement, root_node, &mapping_)));
153  mapping_.insert(mapping_type::value_type(key, binary_leaf<mapped_vector_reduction>(statement, root_node, &mapping_)));
155  mapping_.insert(mapping_type::value_type(key, binary_leaf<mapped_matrix_product>(statement, root_node, &mapping_)));
156  }
157  }
158 
159  private:
160  std::map<void *, vcl_size_t> & memory_;
161  unsigned int & current_arg_;
162  mapping_type & mapping_;
163  };
164 
165  }
166 
167  }
168 
169 }
170 #endif
bool is_value_static() const
Definition: vector.hpp:64
std::map< key_type, container_ptr_type > mapping_type
Definition: forwards.h:122
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()(implicit_matrix_base< ScalarType > const &mat) const
Implicit matrix mapping.
Definition: map_functor.hpp:132
void operator()(viennacl::scheduler::statement const *statement, viennacl::scheduler::statement_node const *root_node, detail::node_type node_type) const
Traversal functor.
Definition: map_functor.hpp:142
Internal utils for a dynamic OpenCL kernel generation.
tools::shared_ptr< detail::mapped_object > container_ptr_type
Definition: forwards.h:121
Implementation of the dense matrix class.
lhs_rhs_element lhs
Definition: forwards.h:422
Helper metafunction for checking whether two types are the same.
Definition: utils.hpp:196
Helper struct for converting a numerical type to its string representation.
Definition: utils.hpp:213
bool has_index() const
Definition: vector.hpp:68
A dense matrix class.
Definition: forwards.h:290
std::pair< viennacl::scheduler::statement_node const *, node_type > key_type
Definition: forwards.h:118
size_type stride2() const
Returns the number of columns.
Definition: matrix.hpp:637
result_type operator()(implicit_vector_base< ScalarType > const &vec) const
Implicit vector mapping.
Definition: map_functor.hpp:103
Mapping of a matrix to a generator class.
Definition: mapped_objects.hpp:236
This file provides the forward declarations for the main types used within ViennaCL.
bool is_value_static() const
Definition: matrix.hpp:57
size_type start() const
Returns the offset within the buffer.
Definition: vector.hpp:867
lhs_rhs_element rhs
Definition: forwards.h:424
std::string name_
Definition: mapped_objects.hpp:172
Mapping of a scalar to a generator class.
Definition: mapped_objects.hpp:176
Implementation of a shared pointer class (cf. std::shared_ptr, boost::shared_ptr). Will be used until C++11 is widely available.
several code generation helpers
map_functor(std::map< void *, vcl_size_t > &memory, unsigned int &current_arg, mapping_type &mapping)
Definition: map_functor.hpp:60
Base class for representing matrices where the individual entries are not all stored explicitly...
Definition: forwards.h:296
Map ViennaCL objects to generator wrappers.
size_type stride1() const
Returns the number of rows.
Definition: matrix.hpp:635
base functor class for traversing a statement
Definition: helpers.hpp:145
size_type start1() const
Returns the number of rows.
Definition: matrix.hpp:630
size_type start2() const
Returns the number of columns.
Definition: matrix.hpp:632
result_type operator()(matrix_base< ScalarType, Layout > const &mat) const
Matrix mapping.
Definition: map_functor.hpp:115
Functor to map the statements to the types defined in mapped_objects.hpp.
Definition: map_functor.hpp:47
result_type operator()(vector_base< ScalarType > const &vec) const
Vector mapping.
Definition: map_functor.hpp:91
A shared pointer class similar to boost::shared_ptr. Reimplemented in order to avoid a Boost-dependen...
Definition: shared_ptr.hpp:83
Forwards declaration.
Provides the datastructures for dealing with a single statement such as 'x = y + z;'.
Mapping of a vector to a generator class.
Definition: mapped_objects.hpp:205
operation_node_type
Enumeration for identifying the possible operations.
Definition: forwards.h:61
Mapping of a host scalar to a generator class.
Definition: mapped_objects.hpp:116
node_type
Definition: forwards.h:112
result_type operator()(scalar< ScalarType > const &scal) const
Scalar mapping.
Definition: map_functor.hpp:83
The vector type with operator-overloads and proxy classes is defined here. Linear algebra operations ...
result_type operator()(ScalarType const &) const
Definition: map_functor.hpp:75
std::string to_string(T const t)
Definition: utils.hpp:204
container_ptr_type result_type
Definition: map_functor.hpp:58
result_type binary_leaf(viennacl::scheduler::statement const *statement, viennacl::scheduler::statement_node const *root_node, mapping_type const *mapping) const
Binary leaf.
Definition: map_functor.hpp:64
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
Common base class for representing vectors where the entries are not all stored explicitly.
Definition: forwards.h:190
viennacl::backend::mem_handle & handle(T &obj)
Returns the generic memory handle of an object. Non-const version.
Definition: handle.hpp:41
Mapping of a implicit matrix to a generator class.
Definition: mapped_objects.hpp:310
op_element op
Definition: forwards.h:423
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:871
operation_node_type type
Definition: forwards.h:416
Mapping of a implicit vector to a generator class.
Definition: mapped_objects.hpp:291