2015-12-19 21:35:35 -05:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2015, PHILIPPE TILLET. All rights reserved.
|
|
|
|
*
|
|
|
|
* This file is part of ISAAC.
|
|
|
|
*
|
|
|
|
* ISAAC is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
|
|
|
* MA 02110-1301 USA
|
|
|
|
*/
|
2015-12-21 17:04:09 -05:00
|
|
|
|
2015-04-29 15:50:57 -04:00
|
|
|
#ifndef _ISAAC_SYMBOLIC_EXPRESSION_H
|
|
|
|
#define _ISAAC_SYMBOLIC_EXPRESSION_H
|
2015-01-12 13:20:53 -05:00
|
|
|
|
2016-04-02 18:19:33 -04:00
|
|
|
#include <utility>
|
2015-01-12 13:20:53 -05:00
|
|
|
#include <vector>
|
|
|
|
#include <list>
|
2015-06-23 09:38:34 -07:00
|
|
|
#include "isaac/driver/backend.h"
|
2015-04-29 15:50:57 -04:00
|
|
|
#include "isaac/driver/context.h"
|
|
|
|
#include "isaac/driver/command_queue.h"
|
|
|
|
#include "isaac/driver/event.h"
|
|
|
|
#include "isaac/driver/kernel.h"
|
|
|
|
#include "isaac/driver/ndrange.h"
|
|
|
|
#include "isaac/driver/buffer.h"
|
|
|
|
|
2016-04-10 13:13:16 -04:00
|
|
|
#include "isaac/jit/syntax/expression/operations.h"
|
2016-04-02 18:19:33 -04:00
|
|
|
#include "isaac/tools/cpp/tuple.hpp"
|
2015-06-23 09:38:34 -07:00
|
|
|
|
2015-04-29 15:50:57 -04:00
|
|
|
#include "isaac/types.h"
|
|
|
|
#include "isaac/value_scalar.h"
|
2015-07-28 15:26:10 -07:00
|
|
|
#include <memory>
|
2015-02-08 23:19:38 -05:00
|
|
|
#include <iostream>
|
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
|
|
|
{
|
|
|
|
|
2015-11-19 12:37:18 -05:00
|
|
|
class array_base;
|
2015-01-12 13:20:53 -05:00
|
|
|
|
2016-04-02 18:19:33 -04:00
|
|
|
struct placeholder
|
2015-09-30 15:31:41 -04:00
|
|
|
{
|
2015-12-19 02:55:24 -05:00
|
|
|
expression_tree operator=(value_scalar const & ) const;
|
|
|
|
expression_tree operator=(expression_tree const & ) const;
|
2015-09-30 15:31:41 -04:00
|
|
|
|
2015-12-19 02:55:24 -05:00
|
|
|
expression_tree operator+=(value_scalar const & ) const;
|
|
|
|
expression_tree operator-=(value_scalar const & ) const;
|
|
|
|
expression_tree operator*=(value_scalar const & ) const;
|
|
|
|
expression_tree operator/=(value_scalar const & ) const;
|
2015-09-30 15:31:41 -04:00
|
|
|
|
|
|
|
int level;
|
|
|
|
};
|
|
|
|
|
2016-04-02 18:19:33 -04:00
|
|
|
struct invalid_node{};
|
|
|
|
|
2015-12-19 02:04:39 -05:00
|
|
|
enum node_type
|
|
|
|
{
|
|
|
|
INVALID_SUBTYPE = 0,
|
|
|
|
COMPOSITE_OPERATOR_TYPE,
|
|
|
|
VALUE_SCALAR_TYPE,
|
|
|
|
DENSE_ARRAY_TYPE,
|
2016-04-02 18:19:33 -04:00
|
|
|
PLACEHOLDER_TYPE
|
2015-12-19 02:04:39 -05:00
|
|
|
};
|
|
|
|
|
2016-04-02 18:19:33 -04:00
|
|
|
union handle_t
|
2015-01-12 13:20:53 -05:00
|
|
|
{
|
2016-04-02 18:19:33 -04:00
|
|
|
cl_mem cl;
|
|
|
|
CUdeviceptr cu;
|
2015-01-12 13:20:53 -05:00
|
|
|
};
|
|
|
|
|
2016-04-08 18:46:59 -04:00
|
|
|
|
2016-04-02 18:19:33 -04:00
|
|
|
struct array_holder
|
|
|
|
{
|
|
|
|
int_t start;
|
|
|
|
handle_t handle;
|
2016-09-30 00:28:17 -04:00
|
|
|
array_base* base;
|
2016-04-02 18:19:33 -04:00
|
|
|
};
|
2015-01-12 13:20:53 -05:00
|
|
|
|
2015-12-19 02:55:24 -05:00
|
|
|
class expression_tree
|
2015-01-12 13:20:53 -05:00
|
|
|
{
|
|
|
|
public:
|
2015-01-31 22:01:48 -05:00
|
|
|
struct node
|
|
|
|
{
|
2016-04-02 18:19:33 -04:00
|
|
|
//Constructors
|
|
|
|
node();
|
|
|
|
node(invalid_node);
|
|
|
|
node(placeholder x);
|
|
|
|
node(value_scalar const & x);
|
|
|
|
node(array_base const & x);
|
|
|
|
node(int_t lhs, op_element op, int_t rhs, numeric_type dtype, tuple const & shape);
|
|
|
|
|
|
|
|
//Common
|
|
|
|
node_type type;
|
|
|
|
numeric_type dtype;
|
|
|
|
tuple shape;
|
|
|
|
tuple ld;
|
|
|
|
|
|
|
|
//Type-specific
|
|
|
|
union
|
|
|
|
{
|
2016-04-10 13:13:16 -04:00
|
|
|
//Operator
|
2016-04-02 18:19:33 -04:00
|
|
|
struct{
|
|
|
|
int_t lhs;
|
|
|
|
op_element op;
|
|
|
|
int_t rhs;
|
|
|
|
}binary_operator;
|
|
|
|
//Scalar
|
|
|
|
values_holder scalar;
|
|
|
|
//Array
|
|
|
|
array_holder array;
|
|
|
|
//Placeholder
|
|
|
|
placeholder ph;
|
|
|
|
};
|
2015-01-31 22:01:48 -05:00
|
|
|
};
|
|
|
|
|
2016-04-02 18:19:33 -04:00
|
|
|
typedef std::vector<node> data_type;
|
2015-01-12 13:20:53 -05:00
|
|
|
|
2015-01-31 22:01:48 -05:00
|
|
|
public:
|
2016-04-02 18:19:33 -04:00
|
|
|
expression_tree(node const & lhs, node const & rhs, op_element const & op, driver::Context const * context, numeric_type const & dtype, tuple const & shape);
|
|
|
|
expression_tree(expression_tree const & lhs, node const & rhs, op_element const & op, driver::Context const * context, numeric_type const & dtype, tuple const & shape);
|
|
|
|
expression_tree(node const & lhs, expression_tree const & rhs, op_element const & op, driver::Context const * context, numeric_type const & dtype, tuple const & shape);
|
|
|
|
expression_tree(expression_tree const & lhs, expression_tree const & rhs, op_element const & op, driver::Context const * context, numeric_type const & dtype, tuple const & shape);
|
|
|
|
|
|
|
|
tuple shape() const;
|
2015-11-19 12:37:18 -05:00
|
|
|
int_t dim() const;
|
2016-04-02 18:19:33 -04:00
|
|
|
data_type const & data() const;
|
2015-01-12 13:20:53 -05:00
|
|
|
std::size_t root() const;
|
2015-04-29 15:50:57 -04:00
|
|
|
driver::Context const & context() const;
|
2015-01-12 13:20:53 -05:00
|
|
|
numeric_type const & dtype() const;
|
2015-01-16 07:31:39 -05:00
|
|
|
|
2016-04-02 18:19:33 -04:00
|
|
|
node const & operator[](size_t) const;
|
|
|
|
node & operator[](size_t);
|
|
|
|
|
2015-12-19 02:55:24 -05:00
|
|
|
expression_tree operator-();
|
|
|
|
expression_tree operator!();
|
2016-04-02 18:19:33 -04:00
|
|
|
|
2015-01-12 13:20:53 -05:00
|
|
|
private:
|
2016-04-02 18:19:33 -04:00
|
|
|
data_type tree_;
|
2015-01-31 22:01:48 -05:00
|
|
|
std::size_t root_;
|
2015-09-30 15:31:41 -04:00
|
|
|
driver::Context const * context_;
|
2015-01-12 13:20:53 -05:00
|
|
|
};
|
|
|
|
|
2016-04-02 18:19:33 -04:00
|
|
|
template<class T> typename std::enable_if<!std::is_arithmetic<T>::value, T const &>::type wrap_generic(T const & x){ return x;}
|
|
|
|
template<class T> typename std::enable_if<std::is_arithmetic<T>::value, value_scalar>::type wrap_generic(T x) { return value_scalar(x); }
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
ISAACAPI typename std::conditional<std::is_arithmetic<T>::value, value_scalar, T const &>::type make_tuple(driver::Context const &, T const & x)
|
|
|
|
{ return wrap_generic(x); }
|
2015-01-12 13:20:53 -05:00
|
|
|
|
2016-04-02 18:19:33 -04:00
|
|
|
template<typename T, typename... Args>
|
|
|
|
ISAACAPI expression_tree make_tuple(driver::Context const & context, T const & x, Args... args)
|
|
|
|
{ return expression_tree(wrap_generic(x), make_tuple(context, args...), op_element(BINARY_ARITHMETIC, PAIR_TYPE), &context, numeric_type_of(x), {1}); }
|
2015-01-12 13:20:53 -05:00
|
|
|
|
2016-04-10 13:13:16 -04:00
|
|
|
//io
|
|
|
|
std::string to_string(node_type const & f);
|
|
|
|
std::string to_string(expression_tree::node const & e);
|
|
|
|
std::ostream & operator<<(std::ostream & os, expression_tree::node const & s_node);
|
|
|
|
std::string to_string(isaac::expression_tree const & s);
|
|
|
|
|
2015-01-12 13:20:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|