ViennaCL - The Vienna Computing Library  1.7.1
Free open-source GPU-accelerated linear algebra and solver library.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
utils.hpp
Go to the documentation of this file.
1 #ifndef VIENNACL_DEVICE_SPECIFIC_TEMPLATES_REDUCTION_UTILS_HPP
2 #define VIENNACL_DEVICE_SPECIFIC_TEMPLATES_REDUCTION_UTILS_HPP
3 
4 /* =========================================================================
5  Copyright (c) 2010-2016, 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 
27 #include <vector>
28 
30 
33 
34 
35 namespace viennacl
36 {
37 namespace device_specific
38 {
39 
40 inline void compute_reduction(utils::kernel_generation_stream & os, std::string acc, std::string cur, scheduler::op_element const & op)
41 {
43  os << acc << "=" << tree_parsing::evaluate(op.type) << "(" << acc << "," << cur << ");" << std::endl;
44  else
45  os << acc << "= (" << acc << ")" << tree_parsing::evaluate(op.type) << "(" << cur << ");" << std::endl;
46 }
47 
48 inline void compute_index_reduction(utils::kernel_generation_stream & os, std::string acc, std::string cur, std::string const & acc_value, std::string const & cur_value, scheduler::op_element const & op)
49 {
50  // os << acc << " = " << cur_value << ">" << acc_value << "?" << cur << ":" << acc << ";" << std::endl;
51  os << acc << "= select(" << acc << "," << cur << "," << cur_value << ">" << acc_value << ");" << std::endl;
52  os << acc_value << "=";
57  os << "(" << acc_value << "," << cur_value << ");"<< std::endl;
58 }
59 
60 inline void process_all(std::string const & type_key, std::string const & str,
61  utils::kernel_generation_stream & stream, std::vector<mapping_type> const & mappings)
62 {
63  for (std::vector<mapping_type>::const_iterator mit = mappings.begin(); mit != mappings.end(); ++mit)
64  for (mapping_type::const_iterator mmit = mit->begin(); mmit != mit->end(); ++mmit)
65  if (mmit->second->type_key()==type_key)
66  stream << mmit->second->process(str) << std::endl;
67 }
68 
69 
70 inline void process_all_at(std::string const & type_key, std::string const & str,
71  utils::kernel_generation_stream & stream, std::vector<mapping_type> const & mappings,
72  vcl_size_t root_idx, leaf_t leaf)
73 {
74  for (std::vector<mapping_type>::const_iterator mit = mappings.begin(); mit != mappings.end(); ++mit)
75  {
76  mapped_object * obj = at(*mit, mapping_key(root_idx, leaf)).get();
77  if (obj->type_key()==type_key)
78  stream << obj->process(str) << std::endl;
79  }
80 }
81 
82 inline std::string neutral_element(scheduler::op_element const & op)
83 {
84  switch (op.type)
85  {
86  case scheduler::OPERATION_BINARY_ADD_TYPE : return "0";
87  case scheduler::OPERATION_BINARY_MULT_TYPE : return "1";
88  case scheduler::OPERATION_BINARY_DIV_TYPE : return "1";
89  case scheduler::OPERATION_BINARY_ELEMENT_FMAX_TYPE : return "-INFINITY";
90  case scheduler::OPERATION_BINARY_ELEMENT_ARGFMAX_TYPE : return "-INFINITY";
91  case scheduler::OPERATION_BINARY_ELEMENT_MAX_TYPE : return "-INFINITY";
92  case scheduler::OPERATION_BINARY_ELEMENT_ARGMAX_TYPE : return "-INFINITY";
93  case scheduler::OPERATION_BINARY_ELEMENT_FMIN_TYPE : return "INFINITY";
94  case scheduler::OPERATION_BINARY_ELEMENT_ARGFMIN_TYPE : return "INFINITY";
95  case scheduler::OPERATION_BINARY_ELEMENT_MIN_TYPE : return "INFINITY";
96  case scheduler::OPERATION_BINARY_ELEMENT_ARGMIN_TYPE : return "INFINITY";
97 
98  default: throw generator_not_supported_exception("Unsupported reduction operator : no neutral element known");
99  }
100 }
101 
102 }
103 }
104 
105 #endif
Exception for the case the generator is unable to deal with the operation.
Definition: forwards.h:163
Struct for holding the type family as well as the type of an operation (could be addition, subtraction, norm, etc.)
Definition: forwards.h:471
void process_all_at(std::string const &type_key, std::string const &str, utils::kernel_generation_stream &stream, std::vector< mapping_type > const &mappings, vcl_size_t root_idx, leaf_t leaf)
Definition: utils.hpp:70
bool elementwise_function(scheduler::op_element const &op)
Definition: utils.hpp:476
void process_all(std::string const &type_key, std::string const &str, utils::kernel_generation_stream &stream, std::vector< mapping_type > const &mappings)
Definition: utils.hpp:60
std::string evaluate(leaf_t leaf, std::map< std::string, std::string > const &accessors, scheduler::statement const &statement, vcl_size_t root_idx, mapping_type const &mapping)
std::size_t vcl_size_t
Definition: forwards.h:75
std::string process(std::string const &in) const
Provides the datastructures for dealing with a single statement such as 'x = y + z;'.
void compute_reduction(utils::kernel_generation_stream &os, std::string acc, std::string cur, scheduler::op_element const &op)
Definition: utils.hpp:40
Code for parsing the expression trees.
Internal utils.
operation_node_type type
Definition: forwards.h:474
void compute_index_reduction(utils::kernel_generation_stream &os, std::string acc, std::string cur, std::string const &acc_value, std::string const &cur_value, scheduler::op_element const &op)
Definition: utils.hpp:48
ValueT const & at(std::map< KeyT, ValueT > const &map, KeyT const &key)
Emulation of C++11's .at() member for std::map<>, const-version.
Definition: forwards.h:142
std::pair< vcl_size_t, leaf_t > mapping_key
Definition: forwards.h:188
std::string neutral_element(scheduler::op_element const &op)
Definition: utils.hpp:82