2015-01-12 13:20:53 -05:00
|
|
|
#include <cassert>
|
2015-07-28 15:45:14 -07:00
|
|
|
#include <algorithm>
|
2015-08-06 20:20:08 -07:00
|
|
|
#include <string>
|
2015-01-12 13:20:53 -05:00
|
|
|
|
2015-04-29 15:50:57 -04:00
|
|
|
#include "isaac/array.h"
|
2015-08-04 20:56:05 -07:00
|
|
|
#include "isaac/kernels/keywords.h"
|
|
|
|
#include "isaac/kernels/templates/axpy.h"
|
|
|
|
#include "isaac/kernels/templates/dot.h"
|
|
|
|
#include "isaac/kernels/templates/ger.h"
|
|
|
|
#include "isaac/kernels/templates/gemv.h"
|
|
|
|
#include "isaac/kernels/templates/gemm.h"
|
|
|
|
#include "isaac/kernels/templates/base.h"
|
|
|
|
#include "isaac/kernels/parse.h"
|
2015-04-29 15:50:57 -04:00
|
|
|
#include "isaac/exception/operation_not_supported.h"
|
|
|
|
#include "isaac/exception/unknown_datatype.h"
|
|
|
|
#include "isaac/symbolic/io.h"
|
|
|
|
|
2015-08-06 16:14:33 -07:00
|
|
|
#include "tools/map.hpp"
|
2015-08-06 20:20:08 -07:00
|
|
|
#include "to_string.hpp"
|
2015-08-06 12:05:12 -07:00
|
|
|
|
2015-04-29 15:50:57 -04:00
|
|
|
namespace isaac
|
2015-01-12 13:20:53 -05:00
|
|
|
{
|
2015-07-11 09:36:01 -04:00
|
|
|
namespace templates
|
|
|
|
{
|
2015-01-12 13:20:53 -05:00
|
|
|
|
2015-01-17 10:48:02 -05:00
|
|
|
base::parameters_type::parameters_type(unsigned int _simd_width, int_t _local_size_1, int_t _local_size_2, int_t _num_kernels) : simd_width(_simd_width), local_size_0(_local_size_1), local_size_1(_local_size_2), num_kernels(_num_kernels)
|
2015-01-12 13:20:53 -05:00
|
|
|
{ }
|
|
|
|
|
|
|
|
|
2015-02-01 22:28:49 -05:00
|
|
|
bool base::requires_fallback(expressions_tuple const & expressions)
|
2015-01-12 13:20:53 -05:00
|
|
|
{
|
2015-02-04 22:06:15 -05:00
|
|
|
for (const auto & elem : expressions.data())
|
|
|
|
for(array_expression::container_type::const_iterator itt = (elem)->tree().begin(); itt != (elem)->tree().end() ; ++itt)
|
2015-04-29 15:50:57 -04:00
|
|
|
if( (itt->lhs.subtype==DENSE_ARRAY_TYPE && (std::max(itt->lhs.array->stride()[0], itt->lhs.array->stride()[1])>1 || std::max(itt->lhs.array->start()[0],itt->lhs.array->start()[1])>0))
|
|
|
|
|| (itt->rhs.subtype==DENSE_ARRAY_TYPE && (std::max(itt->rhs.array->stride()[0], itt->rhs.array->stride()[1])>1 || std::max(itt->rhs.array->start()[0],itt->rhs.array->start()[1])>0)))
|
2015-01-12 13:20:53 -05:00
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-01-31 22:01:48 -05:00
|
|
|
int_t base::vector_size(array_expression::node const & node)
|
2015-01-12 13:20:53 -05:00
|
|
|
{
|
|
|
|
if (node.op.type==OPERATOR_MATRIX_DIAG_TYPE)
|
2015-04-29 15:50:57 -04:00
|
|
|
return std::min<int_t>(node.lhs.array->shape()[0], node.lhs.array->shape()[1]);
|
2015-01-12 13:20:53 -05:00
|
|
|
else if (node.op.type==OPERATOR_MATRIX_ROW_TYPE)
|
2015-04-29 15:50:57 -04:00
|
|
|
return node.lhs.array->shape()[1];
|
2015-01-12 13:20:53 -05:00
|
|
|
else if (node.op.type==OPERATOR_MATRIX_COLUMN_TYPE)
|
2015-04-29 15:50:57 -04:00
|
|
|
return node.lhs.array->shape()[0];
|
2015-01-12 13:20:53 -05:00
|
|
|
else
|
2015-04-29 15:50:57 -04:00
|
|
|
return std::max(node.lhs.array->shape()[0], node.lhs.array->shape()[1]);
|
2015-01-12 13:20:53 -05:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-01-31 22:01:48 -05:00
|
|
|
std::pair<int_t, int_t> base::matrix_size(array_expression::node const & node)
|
2015-01-12 13:20:53 -05:00
|
|
|
{
|
2015-01-17 10:48:02 -05:00
|
|
|
if (node.op.type==OPERATOR_VDIAG_TYPE)
|
2015-01-12 13:20:53 -05:00
|
|
|
{
|
2015-04-29 15:50:57 -04:00
|
|
|
int_t size = node.lhs.array->shape()[0];
|
2015-01-12 13:20:53 -05:00
|
|
|
return std::make_pair(size,size);
|
|
|
|
}
|
2015-01-17 10:48:02 -05:00
|
|
|
else if(node.op.type==OPERATOR_REPEAT_TYPE)
|
2015-04-29 15:50:57 -04:00
|
|
|
return std::make_pair(node.lhs.array->shape()[0]*node.rhs.tuple.rep1, node.lhs.array->shape()[1]*node.rhs.tuple.rep2);
|
2015-01-12 13:20:53 -05:00
|
|
|
else
|
2015-04-29 15:50:57 -04:00
|
|
|
return std::make_pair(node.lhs.array->shape()[0],node.lhs.array->shape()[1]);
|
2015-01-12 13:20:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-01-17 10:48:02 -05:00
|
|
|
base::base(binding_policy_t binding_policy) : binding_policy_(binding_policy)
|
2015-01-12 13:20:53 -05:00
|
|
|
{}
|
|
|
|
|
2015-02-01 22:28:49 -05:00
|
|
|
unsigned int base::lmem_usage(expressions_tuple const &) const
|
2015-01-12 13:20:53 -05:00
|
|
|
{ return 0; }
|
|
|
|
|
2015-02-01 22:28:49 -05:00
|
|
|
unsigned int base::registers_usage(expressions_tuple const &) const
|
2015-01-12 13:20:53 -05:00
|
|
|
{ return 0; }
|
|
|
|
|
2015-01-17 10:48:02 -05:00
|
|
|
base::~base()
|
2015-01-12 13:20:53 -05:00
|
|
|
{ }
|
|
|
|
|
2015-08-05 11:42:08 -07:00
|
|
|
std::string base::generate(std::string const & suffix, expressions_tuple const & expressions, driver::Device const & device)
|
2015-01-12 13:20:53 -05:00
|
|
|
{
|
2015-02-01 22:28:49 -05:00
|
|
|
expressions_tuple::data_type::const_iterator sit;
|
2015-01-12 13:20:53 -05:00
|
|
|
std::vector<mapping_type>::iterator mit;
|
|
|
|
|
2015-04-29 15:50:57 -04:00
|
|
|
if(int err = is_invalid(expressions, device))
|
2015-08-06 20:20:08 -07:00
|
|
|
throw operation_not_supported_exception("The supplied parameters for this template are invalid : err " + tools::to_string(err));
|
2015-01-12 13:20:53 -05:00
|
|
|
|
|
|
|
//Create mapping
|
2015-02-01 22:28:49 -05:00
|
|
|
std::vector<mapping_type> mappings(expressions.data().size());
|
2015-08-06 16:14:33 -07:00
|
|
|
std::unique_ptr<symbolic_binder> binder;
|
|
|
|
if (binding_policy_==BIND_TO_HANDLE)
|
|
|
|
binder.reset(new bind_to_handle());
|
|
|
|
else
|
|
|
|
binder.reset(new bind_all_unique());
|
|
|
|
|
2015-02-01 22:28:49 -05:00
|
|
|
for (mit = mappings.begin(), sit = expressions.data().begin(); sit != expressions.data().end(); ++sit, ++mit)
|
2015-04-29 15:50:57 -04:00
|
|
|
traverse(**sit, (*sit)->root(), map_functor(*binder,*mit,device), true);
|
2015-01-12 13:20:53 -05:00
|
|
|
|
2015-04-29 15:50:57 -04:00
|
|
|
return generate_impl(suffix, expressions, device, mappings);
|
2015-01-12 13:20:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
template<class TType, class PType>
|
2015-04-29 15:50:57 -04:00
|
|
|
int base_impl<TType, PType>::is_invalid_impl(driver::Device const &, expressions_tuple const &) const
|
2015-01-12 13:20:53 -05:00
|
|
|
{ return TEMPLATE_VALID; }
|
|
|
|
|
|
|
|
template<class TType, class PType>
|
2015-01-17 10:48:02 -05:00
|
|
|
base_impl<TType, PType>::base_impl(parameters_type const & parameters, binding_policy_t binding_policy) : base(binding_policy), p_(parameters)
|
2015-01-12 13:20:53 -05:00
|
|
|
{ }
|
|
|
|
|
|
|
|
template<class TType, class PType>
|
2015-01-17 10:48:02 -05:00
|
|
|
int_t base_impl<TType, PType>::local_size_0() const
|
2015-01-12 13:20:53 -05:00
|
|
|
{ return p_.local_size_0; }
|
|
|
|
|
|
|
|
template<class TType, class PType>
|
2015-01-17 10:48:02 -05:00
|
|
|
int_t base_impl<TType, PType>::local_size_1() const
|
2015-01-12 13:20:53 -05:00
|
|
|
{ return p_.local_size_1; }
|
|
|
|
|
|
|
|
template<class TType, class PType>
|
2015-07-28 15:26:10 -07:00
|
|
|
std::shared_ptr<base> base_impl<TType, PType>::clone() const
|
|
|
|
{ return std::shared_ptr<base>(new TType(*dynamic_cast<TType const *>(this))); }
|
2015-01-12 13:20:53 -05:00
|
|
|
|
|
|
|
template<class TType, class PType>
|
2015-04-29 15:50:57 -04:00
|
|
|
int base_impl<TType, PType>::is_invalid(expressions_tuple const & expressions, driver::Device const & device) const
|
2015-01-12 13:20:53 -05:00
|
|
|
{
|
|
|
|
//Query device informations
|
2015-04-29 15:50:57 -04:00
|
|
|
size_t lmem_available = device.local_mem_size();
|
2015-02-01 22:28:49 -05:00
|
|
|
size_t lmem_used = lmem_usage(expressions);
|
2015-01-12 13:20:53 -05:00
|
|
|
if (lmem_used>lmem_available)
|
|
|
|
return TEMPLATE_LOCAL_MEMORY_OVERFLOW;
|
|
|
|
|
|
|
|
//Invalid work group size
|
2015-04-29 15:50:57 -04:00
|
|
|
size_t max_workgroup_size = device.max_work_group_size();
|
|
|
|
std::vector<size_t> max_work_item_sizes = device.max_work_item_sizes();
|
2015-01-12 13:20:53 -05:00
|
|
|
if (p_.local_size_0*p_.local_size_1 > max_workgroup_size)
|
|
|
|
return TEMPLATE_WORK_GROUP_SIZE_OVERFLOW;
|
|
|
|
if (p_.local_size_0 > max_work_item_sizes[0])
|
|
|
|
return TEMPLATE_LOCAL_SIZE_0_OVERFLOW;
|
|
|
|
|
|
|
|
if (p_.local_size_1 > max_work_item_sizes[1])
|
|
|
|
return TEMPLATE_LOCAL_SIZE_1_OVERFLOW;
|
|
|
|
|
|
|
|
//Invalid SIMD Width
|
2015-04-29 15:50:57 -04:00
|
|
|
if (p_.simd_width!=1 && p_.simd_width!=2 && p_.simd_width!=3 && p_.simd_width!=4)
|
2015-01-12 13:20:53 -05:00
|
|
|
return TEMPLATE_INVALID_SIMD_WIDTH;
|
|
|
|
|
2015-04-29 15:50:57 -04:00
|
|
|
return is_invalid_impl(device, expressions);
|
2015-01-12 13:20:53 -05:00
|
|
|
}
|
|
|
|
|
2015-07-11 09:36:01 -04:00
|
|
|
template class base_impl<axpy, axpy_parameters>;
|
|
|
|
template class base_impl<dot, dot_parameters>;
|
|
|
|
template class base_impl<ger, ger_parameters>;
|
|
|
|
template class base_impl<gemv, gemv_parameters>;
|
|
|
|
template class base_impl<gemm, gemm_parameters>;
|
2015-01-12 13:20:53 -05:00
|
|
|
|
|
|
|
}
|
2015-07-11 09:36:01 -04:00
|
|
|
}
|