2015-04-29 15:50:57 -04:00
|
|
|
#ifndef ISAAC_BACKEND_PARSE_H
|
|
|
|
#define ISAAC_BACKEND_PARSE_H
|
2015-01-12 13:20:53 -05:00
|
|
|
|
|
|
|
#include <set>
|
2015-08-04 20:56:05 -07:00
|
|
|
#include "isaac/kernels/mapped_object.h"
|
|
|
|
#include "isaac/kernels/binder.h"
|
2015-04-29 15:50:57 -04:00
|
|
|
#include "isaac/symbolic/expression.h"
|
2015-01-12 13:20:53 -05:00
|
|
|
|
2015-04-29 15:50:57 -04:00
|
|
|
namespace isaac
|
2015-01-12 13:20:53 -05:00
|
|
|
{
|
|
|
|
|
|
|
|
namespace detail
|
|
|
|
{
|
|
|
|
|
|
|
|
bool is_node_leaf(op_element const & op);
|
2015-12-12 18:32:06 -05:00
|
|
|
bool is_scalar_reduce_1d(math_expression::node const & node);
|
|
|
|
bool is_vector_reduce_1d(math_expression::node const & node);
|
2015-06-28 17:53:16 -07:00
|
|
|
bool is_assignment(op_element const & op);
|
2015-01-12 13:20:53 -05:00
|
|
|
bool is_elementwise_operator(op_element const & op);
|
|
|
|
bool is_elementwise_function(op_element const & op);
|
2015-01-29 01:00:50 -05:00
|
|
|
bool is_cast(op_element const & op);
|
2015-04-29 15:50:57 -04:00
|
|
|
bool bypass(op_element const & op);
|
2015-01-12 13:20:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
class scalar;
|
|
|
|
|
2015-09-30 15:31:41 -04:00
|
|
|
/** @brief base functor class for traversing a math_expression */
|
2015-01-12 13:20:53 -05:00
|
|
|
class traversal_functor
|
|
|
|
{
|
|
|
|
public:
|
2015-09-30 15:31:41 -04:00
|
|
|
void call_before_expansion(math_expression const &, std::size_t) const { }
|
|
|
|
void call_after_expansion(math_expression const &, std::size_t) const { }
|
2015-01-12 13:20:53 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2015-09-30 15:31:41 -04:00
|
|
|
/** @brief Recursively execute a functor on a math_expression */
|
2015-01-12 13:20:53 -05:00
|
|
|
template<class Fun>
|
2015-09-30 15:31:41 -04:00
|
|
|
inline void traverse(isaac::math_expression const & math_expression, std::size_t root_idx, Fun const & fun, bool inspect)
|
2015-01-12 13:20:53 -05:00
|
|
|
{
|
2015-09-30 15:31:41 -04:00
|
|
|
math_expression::node const & root_node = math_expression.tree()[root_idx];
|
2015-01-12 13:20:53 -05:00
|
|
|
bool recurse = detail::is_node_leaf(root_node.op)?inspect:true;
|
2015-04-29 15:50:57 -04:00
|
|
|
bool bypass = detail::bypass(root_node.op);
|
2015-01-12 13:20:53 -05:00
|
|
|
|
2015-04-29 15:50:57 -04:00
|
|
|
if(!bypass)
|
2015-09-30 15:31:41 -04:00
|
|
|
fun.call_before_expansion(math_expression, root_idx);
|
2015-01-12 13:20:53 -05:00
|
|
|
|
|
|
|
//Lhs:
|
|
|
|
if (recurse)
|
|
|
|
{
|
|
|
|
if (root_node.lhs.type_family==COMPOSITE_OPERATOR_FAMILY)
|
2015-09-30 15:31:41 -04:00
|
|
|
traverse(math_expression, root_node.lhs.node_index, fun, inspect);
|
2015-01-12 13:20:53 -05:00
|
|
|
if (root_node.lhs.type_family != INVALID_TYPE_FAMILY)
|
2015-09-30 15:31:41 -04:00
|
|
|
fun(math_expression, root_idx, LHS_NODE_TYPE);
|
2015-01-12 13:20:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
//Self:
|
2015-04-29 15:50:57 -04:00
|
|
|
if(!bypass)
|
2015-09-30 15:31:41 -04:00
|
|
|
fun(math_expression, root_idx, PARENT_NODE_TYPE);
|
2015-01-12 13:20:53 -05:00
|
|
|
|
|
|
|
//Rhs:
|
|
|
|
if (recurse && root_node.rhs.type_family!=INVALID_TYPE_FAMILY)
|
|
|
|
{
|
|
|
|
if (root_node.rhs.type_family==COMPOSITE_OPERATOR_FAMILY)
|
2015-09-30 15:31:41 -04:00
|
|
|
traverse(math_expression, root_node.rhs.node_index, fun, inspect);
|
2015-01-12 13:20:53 -05:00
|
|
|
if (root_node.rhs.type_family != INVALID_TYPE_FAMILY)
|
2015-09-30 15:31:41 -04:00
|
|
|
fun(math_expression, root_idx, RHS_NODE_TYPE);
|
2015-01-12 13:20:53 -05:00
|
|
|
}
|
|
|
|
|
2015-04-29 15:50:57 -04:00
|
|
|
if(!bypass)
|
2015-09-30 15:31:41 -04:00
|
|
|
fun.call_after_expansion(math_expression, root_idx);
|
2015-01-12 13:20:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
class filter_fun : public traversal_functor
|
|
|
|
{
|
|
|
|
public:
|
2015-09-30 15:31:41 -04:00
|
|
|
typedef bool (*pred_t)(math_expression::node const & node);
|
2015-01-12 13:20:53 -05:00
|
|
|
filter_fun(pred_t pred, std::vector<size_t> & out);
|
2015-09-30 15:31:41 -04:00
|
|
|
void operator()(isaac::math_expression const & math_expression, size_t root_idx, leaf_t) const;
|
2015-01-12 13:20:53 -05:00
|
|
|
private:
|
|
|
|
pred_t pred_;
|
|
|
|
std::vector<size_t> & out_;
|
|
|
|
};
|
|
|
|
|
|
|
|
class filter_elements_fun : public traversal_functor
|
|
|
|
{
|
|
|
|
public:
|
2015-09-30 15:31:41 -04:00
|
|
|
filter_elements_fun(math_expression_node_subtype subtype, std::vector<lhs_rhs_element> & out);
|
|
|
|
void operator()(isaac::math_expression const & math_expression, size_t root_idx, leaf_t) const;
|
2015-01-12 13:20:53 -05:00
|
|
|
private:
|
2015-09-30 15:31:41 -04:00
|
|
|
math_expression_node_subtype subtype_;
|
2015-01-12 13:20:53 -05:00
|
|
|
std::vector<lhs_rhs_element> & out_;
|
|
|
|
};
|
|
|
|
|
2015-09-30 15:31:41 -04:00
|
|
|
std::vector<size_t> filter_nodes(bool (*pred)(math_expression::node const & node),
|
|
|
|
isaac::math_expression const & math_expression,
|
|
|
|
size_t root,
|
2015-01-12 13:20:53 -05:00
|
|
|
bool inspect);
|
|
|
|
|
2015-09-30 15:31:41 -04:00
|
|
|
std::vector<lhs_rhs_element> filter_elements(math_expression_node_subtype subtype,
|
|
|
|
isaac::math_expression const & math_expression);
|
2015-01-12 13:20:53 -05:00
|
|
|
const char * evaluate(operation_node_type type);
|
|
|
|
|
2015-09-30 15:31:41 -04:00
|
|
|
/** @brief functor for generating the expression string from a math_expression */
|
2015-01-12 13:20:53 -05:00
|
|
|
class evaluate_expression_traversal: public traversal_functor
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
std::map<std::string, std::string> const & accessors_;
|
|
|
|
std::string & str_;
|
|
|
|
mapping_type const & mapping_;
|
|
|
|
|
|
|
|
public:
|
|
|
|
evaluate_expression_traversal(std::map<std::string, std::string> const & accessors, std::string & str, mapping_type const & mapping);
|
2015-09-30 15:31:41 -04:00
|
|
|
void call_before_expansion(isaac::math_expression const & math_expression, std::size_t root_idx) const;
|
|
|
|
void call_after_expansion(math_expression const & /*math_expression*/, std::size_t /*root_idx*/) const;
|
|
|
|
void operator()(isaac::math_expression const & math_expression, std::size_t root_idx, leaf_t leaf) const;
|
2015-01-12 13:20:53 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
std::string evaluate(leaf_t leaf, std::map<std::string, std::string> const & accessors,
|
2015-09-30 15:31:41 -04:00
|
|
|
isaac::math_expression const & math_expression, std::size_t root_idx, mapping_type const & mapping);
|
2015-01-12 13:20:53 -05:00
|
|
|
|
|
|
|
void evaluate(kernel_generation_stream & stream, leaf_t leaf, std::map<std::string, std::string> const & accessors,
|
2015-09-30 15:31:41 -04:00
|
|
|
math_expression const & expressions, mapping_type const & mappings);
|
2015-01-12 13:20:53 -05:00
|
|
|
|
2015-09-30 15:31:41 -04:00
|
|
|
/** @brief functor for fetching or writing-back the elements in a math_expression */
|
2015-01-12 13:20:53 -05:00
|
|
|
class process_traversal : public traversal_functor
|
|
|
|
{
|
|
|
|
public:
|
2015-01-17 10:48:02 -05:00
|
|
|
process_traversal(std::map<std::string, std::string> const & accessors, kernel_generation_stream & stream,
|
2015-01-12 13:20:53 -05:00
|
|
|
mapping_type const & mapping, std::set<std::string> & already_processed);
|
2015-09-30 15:31:41 -04:00
|
|
|
void operator()(math_expression const & math_expression, std::size_t root_idx, leaf_t leaf) const;
|
2015-01-12 13:20:53 -05:00
|
|
|
private:
|
2015-01-17 10:48:02 -05:00
|
|
|
std::map<std::string, std::string> accessors_;
|
2015-01-12 13:20:53 -05:00
|
|
|
kernel_generation_stream & stream_;
|
|
|
|
mapping_type const & mapping_;
|
|
|
|
std::set<std::string> & already_processed_;
|
|
|
|
};
|
|
|
|
|
2015-01-17 10:48:02 -05:00
|
|
|
void process(kernel_generation_stream & stream, leaf_t leaf, std::map<std::string, std::string> const & accessors,
|
2015-09-30 15:31:41 -04:00
|
|
|
isaac::math_expression const & math_expression, size_t root_idx, mapping_type const & mapping, std::set<std::string> & already_processed);
|
2015-01-12 13:20:53 -05:00
|
|
|
|
2015-01-17 10:48:02 -05:00
|
|
|
void process(kernel_generation_stream & stream, leaf_t leaf, std::map<std::string, std::string> const & accessors,
|
2015-09-30 15:31:41 -04:00
|
|
|
math_expression const & expressions, mapping_type const & mappings);
|
2015-01-12 13:20:53 -05:00
|
|
|
|
|
|
|
|
2015-09-30 15:31:41 -04:00
|
|
|
class math_expression_representation_functor : public traversal_functor{
|
2015-01-12 13:20:53 -05:00
|
|
|
private:
|
|
|
|
static void append_id(char * & ptr, unsigned int val);
|
2015-04-29 15:50:57 -04:00
|
|
|
void append(driver::Buffer const & h, numeric_type dtype, char prefix) const;
|
2015-09-30 15:31:41 -04:00
|
|
|
void append(lhs_rhs_element const & lhs_rhs, bool is_assigned) const;
|
2015-01-12 13:20:53 -05:00
|
|
|
public:
|
2015-09-30 15:31:41 -04:00
|
|
|
math_expression_representation_functor(symbolic_binder & binder, char *& ptr);
|
2015-01-12 13:20:53 -05:00
|
|
|
void append(char*& p, const char * str) const;
|
2015-09-30 15:31:41 -04:00
|
|
|
void operator()(isaac::math_expression const & math_expression, std::size_t root_idx, leaf_t leaf_t) const;
|
2015-01-12 13:20:53 -05:00
|
|
|
private:
|
|
|
|
symbolic_binder & binder_;
|
|
|
|
char *& ptr_;
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
#endif
|