Code quality: reorganized files structure

This commit is contained in:
Philippe Tillet
2016-04-10 13:13:16 -04:00
parent 509c496b2e
commit 97a0d65a4d
72 changed files with 354 additions and 394 deletions

View File

@@ -0,0 +1,90 @@
/*
* 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
*/
#ifndef ISAAC_BACKEND_BINDER_H
#define ISAAC_BACKEND_BINDER_H
#include <map>
#include "isaac/driver/buffer.h"
#include "isaac/jit/syntax/expression/expression.h"
namespace isaac
{
class array_base;
enum fusion_policy_t
{
FUSE_INDEPENDENT,
FUSE_SEQUENTIAL
};
class symbolic_binder
{
class cmp
{
public:
cmp(driver::backend_type backend) : backend_(backend) {}
bool operator()(handle_t const & x, handle_t const & y) const
{
if(backend_==driver::OPENCL)
return x.cl < y.cl;
else
return x.cu < y.cu;
}
private:
driver::backend_type backend_;
};
public:
symbolic_binder(driver::backend_type backend);
virtual ~symbolic_binder();
virtual bool bind(handle_t const &, bool) = 0;
virtual unsigned int get(handle_t const &, bool) = 0;
unsigned int get();
protected:
unsigned int current_arg_;
std::map<handle_t,unsigned int, cmp> memory;
};
class bind_sequential : public symbolic_binder
{
public:
bind_sequential(driver::backend_type backend);
bool bind(handle_t const & a, bool);
unsigned int get(handle_t const & a, bool);
};
class bind_independent : public symbolic_binder
{
public:
bind_independent(driver::backend_type backend);
bool bind(handle_t const & a, bool);
unsigned int get(const handle_t &a, bool);
};
}
#endif

View File

@@ -0,0 +1,53 @@
/*
* 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
*/
#ifndef ISAAC_SYMBOLIC_ENGINE_MACRO_H
#define ISAAC_SYMBOLIC_ENGINE_MACRO_H
#include <string>
#include <vector>
namespace isaac
{
namespace symbolic
{
//Macro
class macro
{
public:
macro(std::string const & code);
macro(const char * code);
int expand(std::string & str) const;
bool operator<(macro const & o) const;
private:
std::string code_;
std::string name_;
std::vector<std::string> args_;
std::vector<std::string> tokens_;
};
}
}
#endif

View File

@@ -0,0 +1,206 @@
/*
* 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
*/
#ifndef ISAAC_MAPPED_OBJECT_H
#define ISAAC_MAPPED_OBJECT_H
#include <set>
#include <map>
#include <string>
#include "isaac/jit/syntax/engine/macro.h"
#include "isaac/jit/syntax/expression/expression.h"
#include "isaac/jit/generation/engine/stream.h"
#include "isaac/types.h"
namespace isaac
{
namespace symbolic
{
class object;
typedef std::map<size_t, std::shared_ptr<object> > symbols_table;
//Node
class node
{
public:
node(size_t root, op_element op, expression_tree const & tree, symbols_table const & table);
op_element op() const;
object const * lhs() const;
object const * rhs() const;
size_t root() const;
protected:
op_element op_;
object* lhs_;
object* rhs_;
size_t root_;
};
//Object
class object
{
protected:
void add_base(std::string const & name);
void add_load(bool contiguous);
public:
object(driver::Context const & context, std::string const & scalartype, unsigned int id);
object(driver::Context const & context, std::string const & scalartype, std::string const & name);
virtual ~object();
bool hasattr(std::string const & name) const;
std::string process(std::string const & in) const;
virtual std::string evaluate(std::map<std::string, std::string> const & table) const;
protected:
driver::Context const & context_;
std::map<std::string, std::string> attributes_;
std::set<macro> macros_;
std::list<std::string> hierarchy_;
};
//Leaf
class leaf: public object
{
public:
leaf(driver::Context const & context, std::string const & scalartype, unsigned int id);
leaf(driver::Context const & context, std::string const & scalartype, std::string const & name);
};
//Arithmetic node
class arithmetic_node : public object, public node
{
public:
arithmetic_node(unsigned int id, size_t root, op_element op, expression_tree const & tree, symbols_table const & table);
protected:
std::string op_str_;
};
//Binary arithmetic
class binary_arithmetic_node: public arithmetic_node
{
public:
binary_arithmetic_node(unsigned int id, size_t root, op_element op, expression_tree const & tree, symbols_table const & table);
std::string evaluate(std::map<std::string, std::string> const & table) const;
};
//Unary arithmetic
class unary_arithmetic_node: public arithmetic_node
{
public:
unary_arithmetic_node(unsigned int id, size_t root, op_element op, expression_tree const & tree, symbols_table const & table);
std::string evaluate(std::map<std::string, std::string> const & table) const;
};
//Sfor
class sfor: public object, public node
{
public:
sfor(unsigned int id, size_t root, op_element op, expression_tree const & tree, symbols_table const & table);
};
//Reductions
class reduction : public object, public node
{
public:
reduction(unsigned int id, size_t root, op_element op, expression_tree const & tree, symbols_table const & table);
};
class reduce_1d : public reduction
{
public:
reduce_1d(unsigned int id, size_t root, op_element op, expression_tree const & tree, symbols_table const & table);
};
class reduce_2d : public reduction
{
public:
reduce_2d(unsigned int id, size_t root, op_element op, expression_tree const & tree, symbols_table const & table);
};
//Host scalar
class host_scalar : public leaf
{
public:
host_scalar(driver::Context const & context, std::string const & scalartype, unsigned int id);
};
//Placeholder
class placeholder : public leaf
{
public:
placeholder(driver::Context const & context, unsigned int level);
};
//Arrays
class array : public leaf
{
protected:
std::string make_broadcast(tuple const & shape);
public:
array(driver::Context const & context, std::string const & scalartype, unsigned int id);
};
//Buffer
class buffer : public array
{
public:
buffer(driver::Context const & context, std::string const & scalartype, unsigned int id, tuple const & shape, tuple const &strides);
unsigned int dim() const { return dim_; }
private:
std::string ld_;
std::string start_;
std::string stride_;
unsigned int dim_;
};
//Index modifier
class index_modifier: public array, public node
{
public:
index_modifier(std::string const & scalartype, unsigned int id, size_t root, op_element op, expression_tree const & tree, symbols_table const & table);
};
class reshape : public index_modifier
{
public:
reshape(std::string const & scalartype, unsigned int id, size_t root, op_element op, expression_tree const & tree, symbols_table const & table);
};
class trans : public index_modifier
{
public:
trans(std::string const & scalartype, unsigned int id, size_t root, op_element op, expression_tree const & tree, symbols_table const & table);
};
class diag_vector : public index_modifier
{
public:
diag_vector(std::string const & scalartype, unsigned int id, size_t root, op_element op, expression_tree const & tree, symbols_table const & table);
};
}
}
#endif

View File

@@ -0,0 +1,102 @@
#ifndef ISAAC_SYMBOLIC_ENGINE_PROCESS
#define ISAAC_SYMBOLIC_ENGINE_PROCESS
#include <functional>
#include <typeinfo>
#include "isaac/tools/cpp/string.hpp"
#include "isaac/jit/syntax/expression/expression.h"
#include "isaac/jit/syntax/engine/binder.h"
#include "isaac/jit/syntax/engine/object.h"
#include "isaac/array.h"
namespace isaac
{
namespace symbolic
{
//Traverse
template<class FUN>
inline void traverse(expression_tree const & tree, size_t root, FUN const & fun,
std::function<bool(size_t)> const & recurse)
{
expression_tree::node const & node = tree[root];
if (node.type==COMPOSITE_OPERATOR_TYPE && recurse(root)){
traverse(tree, node.binary_operator.lhs, fun, recurse);
traverse(tree, node.binary_operator.rhs, fun, recurse);
}
if (node.type != INVALID_SUBTYPE)
fun(root);
}
template<class FUN>
inline void traverse(expression_tree const & tree, size_t root, FUN const & fun)
{ return traverse(tree, root, fun, [](size_t){return true;}); }
template<class FUN>
inline void traverse(expression_tree const & tree, FUN const & fun)
{ return traverse(tree, tree.root(), fun); }
//Extract symbolic types
template<class T>
inline void extract(expression_tree const & tree, symbols_table const & table,
size_t root, std::set<std::string>& processed, std::vector<T*>& result, bool array_recurse = true)
{
auto extract_impl = [&](size_t index)
{
symbols_table::const_iterator it = table.find(index);
if(it!=table.end())
{
T* obj = dynamic_cast<T*>(&*it->second);
if(obj && processed.insert(obj->process("#name")).second)
result.push_back(obj);
}
};
auto recurse = [&](size_t index){ return array_recurse?true:dynamic_cast<index_modifier*>(&*table.at(index))==0;};
traverse(tree, root, extract_impl, recurse);
}
template<class T>
inline std::vector<T*> extract(expression_tree const & tree, symbols_table const & table, std::vector<size_t> roots, bool array_recurse = true)
{
std::vector<T*> result;
std::set<std::string> processed;
for(size_t root: roots)
extract(tree, table, root, processed, result, array_recurse);
return result;
}
template<class T>
inline std::vector<T*> extract(expression_tree const & tree, symbols_table const & table, size_t root, bool array_recurse = true)
{
return extract<T>(tree, table, std::vector<size_t>{root}, array_recurse);
}
template<class T>
inline std::vector<T*> extract(expression_tree const & tree, symbols_table const & table)
{
return extract<T>(tree, table, tree.root());
}
// Filter nodes
std::vector<size_t> find(expression_tree const & tree, size_t root, std::function<bool (expression_tree::node const &)> const & pred);
std::vector<size_t> find(expression_tree const & tree, std::function<bool (expression_tree::node const &)> const & pred);
std::vector<size_t> assignments(expression_tree const & tree);
std::vector<size_t> lhs_of(expression_tree const & tree, std::vector<size_t> const & in);
std::vector<size_t> rhs_of(expression_tree const & tree, std::vector<size_t> const & in);
// Hash
std::string hash(expression_tree const & tree);
//Set arguments
void set_arguments(expression_tree const & tree, driver::Kernel & kernel, unsigned int & current_arg, fusion_policy_t fusion_policy);
//Symbolize
symbols_table symbolize(fusion_policy_t fusion_policy, isaac::expression_tree const & expression);
}
}
#endif