2015-04-29 15:50:57 -04:00
|
|
|
#ifndef ISAAC_MAPPED_OBJECT_H
|
|
|
|
#define ISAAC_MAPPED_OBJECT_H
|
2015-01-12 13:20:53 -05:00
|
|
|
|
|
|
|
#include <map>
|
|
|
|
#include <string>
|
2015-04-29 15:50:57 -04:00
|
|
|
#include "isaac/types.h"
|
|
|
|
#include "isaac/backend/stream.h"
|
|
|
|
#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
|
|
|
{
|
|
|
|
|
|
|
|
enum leaf_t
|
|
|
|
{
|
|
|
|
LHS_NODE_TYPE,
|
|
|
|
PARENT_NODE_TYPE,
|
|
|
|
RHS_NODE_TYPE
|
|
|
|
};
|
|
|
|
|
|
|
|
class mapped_object;
|
|
|
|
|
|
|
|
typedef std::pair<int_t, leaf_t> mapping_key;
|
2015-02-08 23:19:38 -05:00
|
|
|
typedef std::map<mapping_key, tools::shared_ptr<mapped_object> > mapping_type;
|
2015-01-12 13:20:53 -05:00
|
|
|
|
|
|
|
/** @brief Mapped Object
|
|
|
|
*
|
2015-01-31 22:01:48 -05:00
|
|
|
* This object populates the symbolic mapping associated with a array_expression. (root_id, LHS|RHS|PARENT) => mapped_object
|
2015-01-12 13:20:53 -05:00
|
|
|
* The tree can then be reconstructed in its symbolic form
|
|
|
|
*/
|
|
|
|
class mapped_object
|
|
|
|
{
|
|
|
|
private:
|
2015-01-16 19:39:26 -05:00
|
|
|
virtual void preprocess(std::string &) const;
|
2015-01-17 10:48:02 -05:00
|
|
|
virtual void postprocess(std::string &) const;
|
2015-01-12 13:20:53 -05:00
|
|
|
|
|
|
|
protected:
|
2015-01-16 19:39:26 -05:00
|
|
|
struct MorphBase {
|
|
|
|
virtual std::string operator()(std::string const & i) const = 0;
|
|
|
|
virtual std::string operator()(std::string const & i, std::string const & j) const = 0;
|
|
|
|
virtual ~MorphBase(){}
|
|
|
|
};
|
2015-01-12 13:20:53 -05:00
|
|
|
|
2015-01-16 19:39:26 -05:00
|
|
|
static void replace_macro(std::string & str, std::string const &, MorphBase const & morph);
|
2015-01-12 13:20:53 -05:00
|
|
|
void register_attribute(std::string & attribute, std::string const & key, std::string const & value);
|
|
|
|
|
|
|
|
public:
|
|
|
|
struct node_info
|
|
|
|
{
|
2015-01-31 22:01:48 -05:00
|
|
|
node_info(mapping_type const * _mapping, array_expression const * _array_expression, int_t _root_idx);
|
2015-01-12 13:20:53 -05:00
|
|
|
mapping_type const * mapping;
|
2015-04-29 15:50:57 -04:00
|
|
|
isaac::array_expression const * array_expression;
|
2015-01-12 13:20:53 -05:00
|
|
|
int_t root_idx;
|
|
|
|
};
|
|
|
|
|
|
|
|
public:
|
|
|
|
mapped_object(std::string const & scalartype, unsigned int id, std::string const & type_key);
|
|
|
|
virtual ~mapped_object();
|
|
|
|
|
|
|
|
std::string type_key() const;
|
|
|
|
std::string const & name() const;
|
|
|
|
std::map<std::string, std::string> const & keywords() const;
|
|
|
|
|
|
|
|
std::string process(std::string const & in) const;
|
|
|
|
std::string evaluate(std::map<std::string, std::string> const & accessors) const;
|
|
|
|
protected:
|
|
|
|
std::string name_;
|
|
|
|
std::string scalartype_;
|
|
|
|
std::string type_key_;
|
|
|
|
std::map<std::string, std::string> keywords_;
|
|
|
|
};
|
|
|
|
|
|
|
|
class binary_leaf
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
binary_leaf(mapped_object::node_info info);
|
|
|
|
|
2015-01-17 10:48:02 -05:00
|
|
|
void process_recursive(kernel_generation_stream & stream, leaf_t leaf, std::map<std::string, std::string> const & accessors);
|
2015-01-12 13:20:53 -05:00
|
|
|
std::string evaluate_recursive(leaf_t leaf, std::map<std::string, std::string> const & accessors);
|
|
|
|
protected:
|
|
|
|
mapped_object::node_info info_;
|
|
|
|
};
|
|
|
|
|
|
|
|
/** @brief Matrix product
|
|
|
|
*
|
|
|
|
* Maps prod(matrix_expression, matrix_expression)
|
|
|
|
*/
|
|
|
|
class mapped_mproduct : public mapped_object, public binary_leaf
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
mapped_mproduct(std::string const & scalartype, unsigned int id, node_info info);
|
|
|
|
};
|
|
|
|
|
|
|
|
/** @brief Reduction
|
|
|
|
*
|
|
|
|
* Base class for mapping a reduction
|
|
|
|
*/
|
|
|
|
class mapped_reduction : public mapped_object, public binary_leaf
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
mapped_reduction(std::string const & scalartype, unsigned int id, node_info info, std::string const & type_key);
|
|
|
|
|
|
|
|
int_t root_idx() const;
|
2015-04-29 15:50:57 -04:00
|
|
|
isaac::array_expression const & array_expression() const;
|
2015-01-31 22:01:48 -05:00
|
|
|
array_expression::node root_node() const;
|
2015-01-12 13:20:53 -05:00
|
|
|
bool is_index_reduction() const;
|
|
|
|
op_element root_op() const;
|
|
|
|
};
|
|
|
|
|
|
|
|
/** @brief Scalar reduction
|
|
|
|
*
|
|
|
|
* Maps a scalar reduction (max, min, argmax, inner_prod, etc..)
|
|
|
|
*/
|
|
|
|
class mapped_scalar_reduction : public mapped_reduction
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
mapped_scalar_reduction(std::string const & scalartype, unsigned int id, node_info info);
|
|
|
|
};
|
|
|
|
|
|
|
|
/** @brief Vector reduction
|
|
|
|
*
|
|
|
|
* Maps a row-wise reduction (max, min, argmax, matrix-vector product, etc..)
|
|
|
|
*/
|
|
|
|
class mapped_mreduction : public mapped_reduction
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
mapped_mreduction(std::string const & scalartype, unsigned int id, node_info info);
|
|
|
|
};
|
|
|
|
|
|
|
|
/** @brief Host scalar
|
|
|
|
*
|
|
|
|
* Maps a host scalar (passed by value)
|
|
|
|
*/
|
|
|
|
class mapped_host_scalar : public mapped_object
|
|
|
|
{
|
2015-01-16 19:39:26 -05:00
|
|
|
void preprocess(std::string & str) const;
|
2015-01-12 13:20:53 -05:00
|
|
|
public:
|
|
|
|
mapped_host_scalar(std::string const & scalartype, unsigned int id);
|
|
|
|
};
|
|
|
|
|
|
|
|
/** @brief Tuple
|
|
|
|
*
|
|
|
|
* Maps an object passed by pointer
|
|
|
|
*/
|
|
|
|
class mapped_tuple : public mapped_object
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
mapped_tuple(std::string const & scalartype, unsigned int id, size_t size);
|
|
|
|
private:
|
|
|
|
size_t size_;
|
|
|
|
std::vector<std::string> names_;
|
|
|
|
};
|
|
|
|
|
|
|
|
/** @brief Handle
|
|
|
|
*
|
|
|
|
* Maps an object passed by pointer
|
|
|
|
*/
|
|
|
|
class mapped_handle : public mapped_object
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
mapped_handle(std::string const & scalartype, unsigned int id, std::string const & type_key);
|
|
|
|
private:
|
|
|
|
std::string pointer_;
|
|
|
|
};
|
|
|
|
|
|
|
|
/** @brief Buffered
|
|
|
|
*
|
|
|
|
* Maps a buffered object (vector, matrix)
|
|
|
|
*/
|
|
|
|
class mapped_buffer : public mapped_handle
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
mapped_buffer(std::string const & scalartype, unsigned int id, std::string const & type_key);
|
|
|
|
};
|
|
|
|
|
|
|
|
class mapped_array : public mapped_buffer
|
|
|
|
{
|
|
|
|
private:
|
2015-01-16 19:39:26 -05:00
|
|
|
void preprocess(std::string & str) const;
|
2015-01-12 13:20:53 -05:00
|
|
|
public:
|
|
|
|
mapped_array(std::string const & scalartype, unsigned int id, char type);
|
|
|
|
private:
|
|
|
|
std::string ld_;
|
|
|
|
std::string start1_;
|
|
|
|
std::string start2_;
|
|
|
|
std::string stride1_;
|
|
|
|
std::string stride2_;
|
|
|
|
char type_;
|
|
|
|
};
|
|
|
|
|
2015-01-17 10:48:02 -05:00
|
|
|
class mapped_vdiag : public mapped_object, public binary_leaf
|
2015-01-12 13:20:53 -05:00
|
|
|
{
|
|
|
|
private:
|
2015-01-17 10:48:02 -05:00
|
|
|
void postprocess(std::string &res) const;
|
2015-01-12 13:20:53 -05:00
|
|
|
public:
|
2015-01-17 10:48:02 -05:00
|
|
|
mapped_vdiag(std::string const & scalartype, unsigned int id, node_info info);
|
2015-01-12 13:20:53 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
class mapped_matrix_row : public mapped_object, binary_leaf
|
|
|
|
{
|
|
|
|
private:
|
2015-01-17 10:48:02 -05:00
|
|
|
void postprocess(std::string &res) const;
|
2015-01-12 13:20:53 -05:00
|
|
|
public:
|
|
|
|
mapped_matrix_row(std::string const & scalartype, unsigned int id, node_info info);
|
|
|
|
};
|
|
|
|
|
|
|
|
class mapped_matrix_column : public mapped_object, binary_leaf
|
|
|
|
{
|
|
|
|
private:
|
2015-01-17 10:48:02 -05:00
|
|
|
void postprocess(std::string &res) const;
|
2015-01-12 13:20:53 -05:00
|
|
|
public:
|
|
|
|
mapped_matrix_column(std::string const & scalartype, unsigned int id, node_info info);
|
|
|
|
};
|
|
|
|
|
2015-01-17 10:48:02 -05:00
|
|
|
class mapped_repeat : public mapped_object, binary_leaf
|
2015-01-12 13:20:53 -05:00
|
|
|
{
|
|
|
|
private:
|
2015-04-29 15:50:57 -04:00
|
|
|
static char get_type(node_info const & info);
|
2015-01-17 10:48:02 -05:00
|
|
|
void postprocess(std::string &res) const;
|
2015-01-12 13:20:53 -05:00
|
|
|
public:
|
2015-01-17 10:48:02 -05:00
|
|
|
mapped_repeat(std::string const & scalartype, unsigned int id, node_info info);
|
2015-04-29 15:50:57 -04:00
|
|
|
private:
|
|
|
|
char type_;
|
2015-01-12 13:20:53 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
class mapped_matrix_diag : public mapped_object, binary_leaf
|
|
|
|
{
|
|
|
|
private:
|
2015-01-17 10:48:02 -05:00
|
|
|
void postprocess(std::string &res) const;
|
2015-01-12 13:20:53 -05:00
|
|
|
public:
|
|
|
|
mapped_matrix_diag(std::string const & scalartype, unsigned int id, node_info info);
|
|
|
|
};
|
|
|
|
|
2015-01-17 10:48:02 -05:00
|
|
|
class mapped_outer : public mapped_object, binary_leaf
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
void postprocess(std::string &res) const;
|
|
|
|
public:
|
|
|
|
mapped_outer(std::string const & scalartype, unsigned int id, node_info info);
|
|
|
|
};
|
|
|
|
|
2015-01-29 01:00:50 -05:00
|
|
|
class mapped_cast : public mapped_object
|
|
|
|
{
|
|
|
|
static std::string operator_to_str(operation_node_type type);
|
|
|
|
public:
|
|
|
|
mapped_cast(operation_node_type type, unsigned int id);
|
|
|
|
};
|
2015-01-17 10:48:02 -05:00
|
|
|
|
2015-01-12 13:20:53 -05:00
|
|
|
}
|
|
|
|
#endif
|