All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
utils.hpp
Go to the documentation of this file.
#ifndef VIENNACL_DEVICE_SPECIFIC_TEMPLATES_REDUCTION_UTILS_HPP
#define VIENNACL_DEVICE_SPECIFIC_TEMPLATES_REDUCTION_UTILS_HPP
/* =========================================================================
Copyright (c) 2010-2016, Institute for Microelectronics,
Institute for Analysis and Scientific Computing,
TU Wien.
Portions of this software are copyright by UChicago Argonne, LLC.
-----------------
ViennaCL - The Vienna Computing Library
-----------------
Project Head: Karl Rupp rupp@iue.tuwien.ac.at
(A list of authors and contributors can be found in the manual)
License: MIT (X11), see file LICENSE in the base directory
============================================================================= */
#include <vector>
#include "viennacl/scheduler/forwards.h"
#include "viennacl/device_specific/tree_parsing.hpp"
#include "viennacl/device_specific/utils.hpp"
namespace viennacl
{
namespace device_specific
{
inline void compute_reduction(utils::kernel_generation_stream & os, std::string acc, std::string cur, scheduler::op_element const & op)
{
if (utils::elementwise_function(op))
os << acc << "=" << tree_parsing::evaluate(op.type) << "(" << acc << "," << cur << ");" << std::endl;
else
os << acc << "= (" << acc << ")" << tree_parsing::evaluate(op.type) << "(" << cur << ");" << std::endl;
}
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)
{
// os << acc << " = " << cur_value << ">" << acc_value << "?" << cur << ":" << acc << ";" << std::endl;
os << acc << "= select(" << acc << "," << cur << "," << cur_value << ">" << acc_value << ");" << std::endl;
os << acc_value << "=";
if (op.type==scheduler::OPERATION_BINARY_ELEMENT_ARGFMAX_TYPE) os << "fmax";
if (op.type==scheduler::OPERATION_BINARY_ELEMENT_ARGMAX_TYPE) os << "max";
if (op.type==scheduler::OPERATION_BINARY_ELEMENT_ARGFMIN_TYPE) os << "fmin";
if (op.type==scheduler::OPERATION_BINARY_ELEMENT_ARGMIN_TYPE) os << "min";
os << "(" << acc_value << "," << cur_value << ");"<< std::endl;
}
inline void process_all(std::string const & type_key, std::string const & str,
utils::kernel_generation_stream & stream, std::vector<mapping_type> const & mappings)
{
for (std::vector<mapping_type>::const_iterator mit = mappings.begin(); mit != mappings.end(); ++mit)
for (mapping_type::const_iterator mmit = mit->begin(); mmit != mit->end(); ++mmit)
if (mmit->second->type_key()==type_key)
stream << mmit->second->process(str) << std::endl;
}
inline 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)
{
for (std::vector<mapping_type>::const_iterator mit = mappings.begin(); mit != mappings.end(); ++mit)
{
mapped_object * obj = at(*mit, mapping_key(root_idx, leaf)).get();
if (obj->type_key()==type_key)
stream << obj->process(str) << std::endl;
}
}
inline std::string neutral_element(scheduler::op_element const & op)
{
switch (op.type)
{
case scheduler::OPERATION_BINARY_ADD_TYPE : return "0";
case scheduler::OPERATION_BINARY_MULT_TYPE : return "1";
case scheduler::OPERATION_BINARY_DIV_TYPE : return "1";
case scheduler::OPERATION_BINARY_ELEMENT_FMAX_TYPE : return "-INFINITY";
case scheduler::OPERATION_BINARY_ELEMENT_ARGFMAX_TYPE : return "-INFINITY";
case scheduler::OPERATION_BINARY_ELEMENT_MAX_TYPE : return "-INFINITY";
case scheduler::OPERATION_BINARY_ELEMENT_ARGMAX_TYPE : return "-INFINITY";
case scheduler::OPERATION_BINARY_ELEMENT_FMIN_TYPE : return "INFINITY";
case scheduler::OPERATION_BINARY_ELEMENT_ARGFMIN_TYPE : return "INFINITY";
case scheduler::OPERATION_BINARY_ELEMENT_MIN_TYPE : return "INFINITY";
case scheduler::OPERATION_BINARY_ELEMENT_ARGMIN_TYPE : return "INFINITY";
default: throw generator_not_supported_exception("Unsupported reduction operator : no neutral element known");
}
}
}
}
#endif
viennacl::device_specific::mapped_object::type_key
std::string type_key() const
Definition: mapped_objects.hpp:107
viennacl::scheduler::OPERATION_BINARY_ELEMENT_MAX_TYPE
Definition: forwards.h:137
viennacl::device_specific::generator_not_supported_exception
Exception for the case the generator is unable to deal with the operation.
Definition: forwards.h:163
viennacl::device_specific::leaf_t
leaf_t
Definition: forwards.h:180
viennacl::scheduler::op_element
Struct for holding the type family as well as the type of an operation (could be addition, subtraction, norm, etc.)
Definition: forwards.h:471
viennacl::device_specific::process_all_at
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
viennacl::scheduler::OPERATION_BINARY_ELEMENT_FMIN_TYPE
Definition: forwards.h:136
viennacl::device_specific::utils::kernel_generation_stream
Definition: utils.hpp:404
viennacl::device_specific::utils::elementwise_function
bool elementwise_function(scheduler::op_element const &op)
Definition: utils.hpp:476
viennacl::device_specific::process_all
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
viennacl::device_specific::tree_parsing::evaluate
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)
Definition: tree_parsing.hpp:293
viennacl::vcl_size_t
std::size_t vcl_size_t
Definition: forwards.h:75
viennacl::device_specific::mapped_object::process
std::string process(std::string const &in) const
Definition: mapped_objects.hpp:111
forwards.h
Provides the datastructures for dealing with a single statement such as 'x = y + z;'.
viennacl::device_specific::mapped_object
Mapped Object.
Definition: mapped_objects.hpp:43
viennacl::device_specific::compute_reduction
void compute_reduction(utils::kernel_generation_stream &os, std::string acc, std::string cur, scheduler::op_element const &op)
Definition: utils.hpp:40
tree_parsing.hpp
Code for parsing the expression trees.
utils.hpp
Internal utils.
viennacl::scheduler::OPERATION_BINARY_ELEMENT_ARGMAX_TYPE
Definition: forwards.h:124
viennacl::scheduler::op_element::type
operation_node_type type
Definition: forwards.h:474
viennacl::scheduler::OPERATION_BINARY_DIV_TYPE
Definition: forwards.h:121
viennacl::scheduler::OPERATION_BINARY_ELEMENT_FMAX_TYPE
Definition: forwards.h:135
viennacl::device_specific::compute_index_reduction
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
viennacl::scheduler::OPERATION_BINARY_ELEMENT_ARGFMAX_TYPE
Definition: forwards.h:122
viennacl::scheduler::OPERATION_BINARY_ADD_TYPE
Definition: forwards.h:118
viennacl::scheduler::OPERATION_BINARY_ELEMENT_ARGMIN_TYPE
Definition: forwards.h:125
viennacl::scheduler::OPERATION_BINARY_ELEMENT_ARGFMIN_TYPE
Definition: forwards.h:123
viennacl::device_specific::at
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
viennacl::scheduler::OPERATION_BINARY_ELEMENT_MIN_TYPE
Definition: forwards.h:138
viennacl::device_specific::mapping_key
std::pair< vcl_size_t, leaf_t > mapping_key
Definition: forwards.h:188
viennacl::device_specific::neutral_element
std::string neutral_element(scheduler::op_element const &op)
Definition: utils.hpp:82
viennacl::scheduler::OPERATION_BINARY_MULT_TYPE
Definition: forwards.h:120