Driver: moved programs allocation logic to a static variable

This commit is contained in:
Philippe Tillet
2015-07-30 14:35:41 -07:00
parent d3f82e535f
commit 21a2566904
18 changed files with 92 additions and 55 deletions

View File

@@ -25,7 +25,7 @@ public:
axpy(axpy::parameters_type const & parameters, binding_policy_t binding_policy = BIND_ALL_UNIQUE);
axpy(unsigned int _simd_width, unsigned int _group_size, unsigned int _num_groups, fetching_policy_type _fetching_policy, binding_policy_t binding_policy = BIND_ALL_UNIQUE);
std::vector<int_t> input_sizes(expressions_tuple const & expressions) const;
void enqueue(driver::CommandQueue & queue, driver::Program & program, const char * suffix, base & fallback, controller<expressions_tuple> const &);
void enqueue(driver::CommandQueue & queue, driver::Program const & program, const char * suffix, base & fallback, controller<expressions_tuple> const &);
};
}

View File

@@ -182,7 +182,7 @@ public:
virtual ~base();
std::string generate(const char * suffix, expressions_tuple const & expressions, driver::Device const & device);
virtual int is_invalid(expressions_tuple const & expressions, driver::Device const & device) const = 0;
virtual void enqueue(driver::CommandQueue & queue, driver::Program & program, const char * suffix, base & fallback, controller<expressions_tuple> const & expressions) = 0;
virtual void enqueue(driver::CommandQueue & queue, driver::Program const & program, const char * suffix, base & fallback, controller<expressions_tuple> const & expressions) = 0;
virtual std::shared_ptr<base> clone() const = 0;
private:
binding_policy_t binding_policy_;

View File

@@ -30,7 +30,7 @@ public:
dot(dot::parameters_type const & parameters, binding_policy_t binding_policy = BIND_ALL_UNIQUE);
dot(unsigned int simd, unsigned int ls, unsigned int ng, fetching_policy_type fetch, binding_policy_t bind = BIND_ALL_UNIQUE);
std::vector<int_t> input_sizes(expressions_tuple const & expressions) const;
void enqueue(driver::CommandQueue & queue, driver::Program & program, const char * suffix, base & fallback, controller<expressions_tuple> const &);
void enqueue(driver::CommandQueue & queue, driver::Program const & program, const char * suffix, base & fallback, controller<expressions_tuple> const &);
private:
std::vector< driver::Buffer > tmp_;
std::vector< driver::Buffer > tmpidx_;

View File

@@ -47,7 +47,7 @@ private:
int is_invalid_impl(driver::Device const &, expressions_tuple const &) const;
std::string generate_impl(const char * suffix, expressions_tuple const & expressions, driver::Device const & device, std::vector<mapping_type> const &) const;
void enqueue_block(driver::CommandQueue & queue, int_t M, int_t N, int_t K, array const & A, array const & B, array const & C,
value_scalar const &alpha, value_scalar const &beta, driver::Program & program, const char * suffix, execution_options_type const & options);
value_scalar const &alpha, value_scalar const &beta, driver::Program const & program, const char * suffix, execution_options_type const & options);
array create_slice(array & M, int_t s0_0, int_t s0_1, int_t s1_0, int_t s1_1, bool swap);
std::vector<int_t> infos(expressions_tuple const & expressions, isaac::symbolic::preset::gemm::args &arguments) const;
public:
@@ -55,7 +55,7 @@ public:
std::vector<int_t> input_sizes(expressions_tuple const & expressions) const;
void cleanup(values_holder beta, controller<expressions_tuple> const & ctr, model & fallback,
lhs_rhs_element* eA, lhs_rhs_element* eB, lhs_rhs_element* eC, lhs_rhs_element* ebeta, array const & A, array const & B, array const & C);
void enqueue(driver::CommandQueue & queue, driver::Program & program, const char * suffix, base & fallback, controller<expressions_tuple> const &ctr);
void enqueue(driver::CommandQueue & queue, driver::Program const & program, const char * suffix, base & fallback, controller<expressions_tuple> const &ctr);
private:
const char A_trans_;
const char B_trans_;

View File

@@ -36,7 +36,7 @@ private:
std::string generate_impl(const char * suffix, expressions_tuple const &, driver::Device const & device, std::vector<mapping_type> const &) const;
public:
virtual std::vector<int_t> input_sizes(expressions_tuple const & expressions) const;
void enqueue(driver::CommandQueue & queue, driver::Program & program, const char * suffix, base & fallback, controller<expressions_tuple> const &);
void enqueue(driver::CommandQueue & queue, driver::Program const & program, const char * suffix, base & fallback, controller<expressions_tuple> const &);
private:
dot_type dot_type_;
};

View File

@@ -28,7 +28,7 @@ public:
ger(parameters_type const & parameters, binding_policy_t binding_policy = BIND_ALL_UNIQUE);
ger(unsigned int simd, unsigned int ls1, unsigned int ls2, unsigned int ng1, unsigned int ng2, fetching_policy_type fetch, binding_policy_t bind = BIND_ALL_UNIQUE);
std::vector<int_t> input_sizes(expressions_tuple const & expressions) const;
void enqueue(driver::CommandQueue & queue, driver::Program & program, const char * suffix, base & fallback, controller<expressions_tuple> const &);
void enqueue(driver::CommandQueue & queue, driver::Program const & program, const char * suffix, base & fallback, controller<expressions_tuple> const &);
};
}

View File

@@ -38,6 +38,10 @@ public:
Device(int ordinal);
#endif
Device(cl_device_id const & device, bool take_ownership = true);
bool operator==(Device const &) const;
bool operator<(Device const &) const;
backend_type backend() const;
size_t clock_rate() const;
unsigned int address_bits() const;

View File

@@ -3,7 +3,6 @@
#include "isaac/defines.h"
#include "isaac/driver/common.h"
#include "isaac/driver/context.h"
#include "isaac/driver/handle.h"
namespace isaac
@@ -13,6 +12,7 @@ namespace driver
{
class Context;
class Device;
class ISAACAPI Program
{
@@ -22,11 +22,20 @@ public:
Context const & context() const;
private:
backend_type backend_;
Context context_;
Context const & context_;
std::string source_;
HANDLE_TYPE(cl_program, CUmodule) h_;
};
class ISAACAPI ProgramsHandler
{
public:
static Program const & add(Context const & scontext, std::string const & name, std::string const & src);
static Program const * find(Context const & context, std::string const & name);
private:
static std::map<driver::Context, std::map<std::string, Program> > programs_;
};
}
}

View File

@@ -20,7 +20,7 @@ namespace isaac
private:
std::string define_extension(std::string const & extensions, std::string const & ext);
inline void fill_program_name(char* program_name, expressions_tuple const & expressions, binding_policy_t binding_policy);
driver::Program& init(controller<expressions_tuple> const &);
driver::Program const & init(controller<expressions_tuple> const &);
public:
model(expression_type, numeric_type, predictors::random_forest const &, std::vector< std::shared_ptr<templates::base> > const &, driver::CommandQueue const &);
@@ -29,15 +29,11 @@ namespace isaac
void execute(controller<expressions_tuple> const &);
templates_container const & templates() const;
void test() const
{ std::cout << queue_.device().backend() << std::endl;}
private:
templates_container templates_;
template_pointer fallback_;
std::shared_ptr<predictors::random_forest> predictor_;
std::map<std::vector<int_t>, int> hardcoded_;
std::map<driver::Context, std::map<std::string, std::shared_ptr<driver::Program> > > programs_;
driver::CommandQueue queue_;
};
@@ -47,7 +43,7 @@ namespace isaac
model_map_t& models(driver::CommandQueue & queue);
extern std::map<std::pair<expression_type, numeric_type>, std::shared_ptr<templates::base> > fallbacks;
extern std::map<driver::CommandQueue, model_map_t> models_;
extern std::map<driver::Device, model_map_t> models_;
}

View File

@@ -110,7 +110,7 @@ std::vector<int_t> axpy::input_sizes(expressions_tuple const & expressions) cons
return tools::make_vector<int_t>() << std::max(shape[0], shape[1]);
}
void axpy::enqueue(driver::CommandQueue & queue, driver::Program & program, const char * suffix, base & fallback, controller<expressions_tuple> const & controller)
void axpy::enqueue(driver::CommandQueue & queue, driver::Program const & program, const char * suffix, base & fallback, controller<expressions_tuple> const & controller)
{
expressions_tuple const & expressions = controller.x();
//Size

View File

@@ -279,7 +279,7 @@ std::vector<int_t> dot::input_sizes(expressions_tuple const & expressions) const
return tools::make_vector<int_t>() << N;
}
void dot::enqueue(driver::CommandQueue & queue, driver::Program & program, const char * suffix, base & fallback, controller<expressions_tuple> const & controller)
void dot::enqueue(driver::CommandQueue & queue, driver::Program const & program, const char * suffix, base & fallback, controller<expressions_tuple> const & controller)
{
expressions_tuple const & expressions = controller.x();

View File

@@ -574,7 +574,7 @@ gemm_parameters::gemm_parameters(unsigned int simd_width
void gemm::enqueue_block(driver::CommandQueue & /*queue*/, int_t M, int_t N, int_t K,
array const & A, array const & B, array const & C,
value_scalar const & alpha, value_scalar const & beta,
driver::Program & program, const char * suffix, execution_options_type const & options)
driver::Program const & program, const char * suffix, execution_options_type const & options)
{
using tools::align;
@@ -685,7 +685,7 @@ gemm_parameters::gemm_parameters(unsigned int simd_width
return infos(expressions, dummy);
}
void gemm::enqueue(driver::CommandQueue & queue, driver::Program & program, const char * suffix, base & fallback_base, controller<expressions_tuple> const & ctr)
void gemm::enqueue(driver::CommandQueue & queue, driver::Program const & program, const char * suffix, base & fallback_base, controller<expressions_tuple> const & ctr)
{
using namespace tools;
// std::cout << p_.simd_width << " " << p_.mL << " " << p_.kL << " " << p_.mS << " " << p_.depth << " " << p_.local_size_0 << std::endl;

View File

@@ -336,7 +336,7 @@ std::vector<int_t> gemv::input_sizes(expressions_tuple const & expressions) cons
return tools::make_vector<int_t>() << MN.first << MN.second;
}
void gemv::enqueue(driver::CommandQueue & queue, driver::Program & program, const char * suffix, base & fallback, controller<expressions_tuple> const & controller)
void gemv::enqueue(driver::CommandQueue & queue, driver::Program const & program, const char * suffix, base & fallback, controller<expressions_tuple> const & controller)
{
expressions_tuple const & expressions = controller.x();
driver::Context const & context = expressions.context();

View File

@@ -114,7 +114,7 @@ std::vector<int_t> ger::input_sizes(expressions_tuple const & expressions) const
return tools::make_vector<int_t>() << size.first << size.second;
}
void ger::enqueue(driver::CommandQueue & /*queue*/, driver::Program & program, const char * suffix, base &, controller<expressions_tuple> const & controller)
void ger::enqueue(driver::CommandQueue & /*queue*/, driver::Program const & program, const char * suffix, base &, controller<expressions_tuple> const & controller)
{
expressions_tuple const & expressions = controller.x();
char name[32] = {"axpy"};

View File

@@ -48,10 +48,14 @@ Context::Context(Device const & device) : backend_(device.backend_), device_(dev
}
bool Context::operator==(Context const & other) const
{ return h_==other.h_; }
{
return h_==other.h_;
}
bool Context::operator<(Context const & other) const
{ return h_<other.h_; }
{
return h_<other.h_;
}
Device const & Context::device() const
{ return device_; }

View File

@@ -27,6 +27,18 @@ Device::Device(int ordinal): backend_(CUDA), h_(backend_, true)
Device::Device(cl_device_id const & device, bool take_ownership) : backend_(OPENCL), h_(backend_, take_ownership)
{ h_.cl() = device; }
bool Device::operator==(Device const & other) const
{
return h_==other.h_;
}
bool Device::operator<(Device const & other) const
{
return h_<other.h_;
}
backend_type Device::backend() const
{ return backend_; }

View File

@@ -159,6 +159,32 @@ Program::Program(Context const & context, std::string const & source) : backend_
Context const & Program::context() const
{ return context_; }
Program const & ProgramsHandler::add(Context const & context, std::string const & name, std::string const & src)
{
std::map<std::string, Program> & pgms = programs_[context];
std::map<std::string, Program>::iterator it = pgms.find(name);
if(it==pgms.end())
{
std::string extensions;
std::string ext = "cl_khr_fp64";
if(context.device().extensions().find(ext)!=std::string::npos)
extensions = "#pragma OPENCL EXTENSION " + ext + " : enable\n";
return pgms.insert(std::make_pair(name, driver::Program(context, extensions + src))).first->second;
}
return it->second;
}
const Program * ProgramsHandler::find(Context const & context, const std::string &name)
{
std::map<std::string, Program> & pgms = programs_[context];
std::map<std::string, Program>::const_iterator it = pgms.find(name);
if(it==pgms.end())
return NULL;
return &it->second;
}
std::map<driver::Context, std::map<std::string, Program>> ProgramsHandler::programs_;
}
}

View File

@@ -12,6 +12,7 @@
#include "isaac/backend/templates/ger.h"
#include "isaac/backend/templates/gemv.h"
#include "isaac/backend/templates/gemm.h"
#include "isaac/driver/program.h"
#include "isaac/exception/unknown_datatype.h"
#include "isaac/exception/operation_not_supported.h"
#include "isaac/model/model.h"
@@ -26,14 +27,6 @@ namespace isaac
static double time_event(unsigned long sum, driver::Event const & e)
{ return sum + e.elapsed_time();}
std::string model::define_extension(std::string const & extensions, std::string const & ext)
{
if(extensions.find(ext)!=std::string::npos)
return std::string("#pragma OPENCL EXTENSION " + ext + " : enable\n");
return std::string("");
}
void model::fill_program_name(char* program_name, expressions_tuple const & expressions, binding_policy_t binding_policy)
{
if (expressions.order()==expressions_tuple::INDEPENDENT)
@@ -51,9 +44,9 @@ void model::fill_program_name(char* program_name, expressions_tuple const & expr
delete binder;
}
driver::Program& model::init(controller<expressions_tuple> const & expressions)
driver::Program const & model::init(controller<expressions_tuple> const & expressions)
{
driver::Context const & context = expressions.x().context();
driver::Context & context = (driver::Context&)expressions.x().context();
std::string pname;
compilation_options_type const & opt = expressions.compilation_options();
if(opt.program_name.empty())
@@ -65,24 +58,18 @@ driver::Program& model::init(controller<expressions_tuple> const & expressions)
else
pname = expressions.compilation_options().program_name;
driver::Program const * program = driver::ProgramsHandler::find(context, pname);
if(program)
return *program;
std::shared_ptr<driver::Program> & program = programs_[context][pname];
if(!program)
{
driver::Device device = queue_.device();
std::string extensions = device.extensions();
std::string all_extensions = define_extension(extensions, "cl_khr_fp64");
std::string srcs;
for(unsigned int i = 0 ; i < templates_.size() ; ++i){
char buffer[16];
sprintf(buffer,"%d",i);
srcs += templates_[i]->generate(buffer, expressions.x(), device);
}
srcs += fallback_->generate("fallback", expressions.x(), device);
program.reset(new driver::Program(context, all_extensions + srcs));
}
return *program;
std::string srcs;
for(unsigned int i = 0 ; i < templates_.size() ; ++i){
char buffer[16];
sprintf(buffer,"%d",i);
srcs += templates_[i]->generate(buffer, expressions.x(), context.device());
}
srcs += fallback_->generate("fallback", expressions.x(), context.device());
return driver::ProgramsHandler::add(context, pname, srcs);
}
model::model(expression_type etype, numeric_type dtype, predictors::random_forest const & predictor, std::vector< std::shared_ptr<templates::base> > const & templates, driver::CommandQueue const & queue) :
@@ -95,7 +82,7 @@ model::model(expression_type etype, numeric_type dtype, templates::base const &
void model::execute(controller<expressions_tuple> const & expr)
{
driver::Program & program = init(expr);
driver::Program const & program = init(expr);
std::vector<int_t> x = templates_[0]->input_sizes(expr.x());
//Specific tuning if requested
@@ -280,13 +267,12 @@ model_map_t init_models(driver::CommandQueue & queue)
model_map_t& models(driver::CommandQueue & queue)
{
std::map<driver::CommandQueue, model_map_t>::iterator it = models_.find(queue);
static std::map<driver::Device, model_map_t> models_;
std::map<driver::Device, model_map_t>::iterator it = models_.find(queue.device());
if(it == models_.end())
return models_.insert(std::make_pair(queue, init_models(queue))).first->second;
return models_.insert(std::make_pair(queue.device(), init_models(queue))).first->second;
return it->second;
}
std::map<std::pair<expression_type, numeric_type>, std::shared_ptr<templates::base> > fallbacks = init_fallback();
std::map<driver::CommandQueue, model_map_t> models_;
}