Driver: Fixed issue in ownership handling for BLAS

This commit is contained in:
Philippe Tillet
2015-07-26 21:13:28 -07:00
parent 16d381dfc6
commit 4715723e61
18 changed files with 51 additions and 62 deletions

View File

@@ -19,7 +19,7 @@ class ISAACAPI Buffer
friend class CommandQueue;
friend class Kernel;
public:
Buffer(cl_mem Buffer);
Buffer(cl_mem Buffer, bool take_ownership = true);
Buffer(Context const & context, std::size_t size);
Context const & context() const;
bool operator<(Buffer const &) const;

View File

@@ -23,7 +23,7 @@ class Buffer;
class ISAACAPI CommandQueue
{
public:
CommandQueue(cl_command_queue const & queue);
CommandQueue(cl_command_queue const & queue, bool take_ownership = true);
CommandQueue(Context const & context, Device const & device, cl_command_queue_properties properties = 0);
Context const & context() const;
Device const & device() const;

View File

@@ -21,7 +21,7 @@ class ISAACAPI Context
friend class Buffer;
public:
explicit Context(cl_context const & context);
explicit Context(cl_context const & context, bool take_ownership = true);
explicit Context(Device const & device);
backend_type backend() const;
Device const & device() const;

View File

@@ -37,7 +37,7 @@ public:
#ifdef ISAAC_WITH_CUDA
Device(int ordinal);
#endif
Device(cl_device_id const & device);
Device(cl_device_id const & device, bool take_ownership = true);
backend_type backend() const;
size_t clock_rate() const;
unsigned int address_bits() const;

View File

@@ -16,7 +16,7 @@ class ISAACAPI Event
{
friend class CommandQueue;
public:
Event(cl_event const & event);
Event(cl_event const & event, bool take_ownership = true);
Event(backend_type backend);
long elapsed_time() const;
HANDLE_TYPE(cl_event, cu_event_t)& handle();

View File

@@ -43,9 +43,10 @@ private:
static void release(cl_program x);
public:
Handle(backend_type backend);
Handle(backend_type backend, bool take_ownership = true);
bool operator==(Handle const & other) const;
bool operator<(Handle const & other) const;
bool has_ownership() const;
CLType & cl();
CLType const & cl() const;
#ifdef ISAAC_WITH_CUDA
@@ -58,6 +59,7 @@ private:
std::shared_ptr<CUType> cu_;
private:
backend_type backend_;
bool has_ownership_;
};
}

View File

@@ -338,6 +338,7 @@ void dot::enqueue(driver::CommandQueue & queue, driver::Program & program, const
kernel.setArg(n_arg++, tmp_[i]);
i++;
}
set_arguments(expressions, kernel, n_arg);
}

View File

@@ -8,12 +8,12 @@ namespace isaac
namespace driver
{
Buffer::Buffer(cl_mem buffer) : backend_(OPENCL), context_(ocl::info<CL_MEM_CONTEXT>(buffer)), h_(backend_)
Buffer::Buffer(cl_mem buffer, bool take_ownership) : backend_(OPENCL), context_(ocl::info<CL_MEM_CONTEXT>(buffer), take_ownership), h_(backend_, take_ownership)
{
h_.cl() = buffer;
}
Buffer::Buffer(Context const & context, std::size_t size) : backend_(context.backend_), context_(context), h_(backend_)
Buffer::Buffer(Context const & context, std::size_t size) : backend_(context.backend_), context_(context), h_(backend_, context.h_.has_ownership())
{
switch(backend_)

View File

@@ -17,12 +17,12 @@ namespace isaac
namespace driver
{
CommandQueue::CommandQueue(cl_command_queue const & queue) : backend_(OPENCL), context_(ocl::info<CL_QUEUE_CONTEXT>(queue)), device_(ocl::info<CL_QUEUE_DEVICE>(queue)), h_(backend_)
CommandQueue::CommandQueue(cl_command_queue const & queue, bool take_ownership) : backend_(OPENCL), context_(ocl::info<CL_QUEUE_CONTEXT>(queue), take_ownership), device_(ocl::info<CL_QUEUE_DEVICE>(queue), take_ownership), h_(backend_, take_ownership)
{
h_.cl() = queue;
}
CommandQueue::CommandQueue(Context const & context, Device const & device, cl_command_queue_properties properties): backend_(device.backend_), context_(context), device_(device), h_(backend_)
CommandQueue::CommandQueue(Context const & context, Device const & device, cl_command_queue_properties properties): backend_(device.backend_), context_(context), device_(device), h_(backend_, context.h_.has_ownership())
{
switch(backend_)
{

View File

@@ -8,12 +8,12 @@ namespace isaac
namespace driver
{
Context::Context(cl_context const & context) : backend_(OPENCL), device_(ocl::info<CL_CONTEXT_DEVICES>(context)[0]), h_(backend_)
Context::Context(cl_context const & context, bool take_ownership) : backend_(OPENCL), device_(ocl::info<CL_CONTEXT_DEVICES>(context)[0], take_ownership), h_(backend_, take_ownership)
{
h_.cl() = context;
}
Context::Context(Device const & device) : backend_(device.backend_), device_(device), h_(backend_)
Context::Context(Device const & device) : backend_(device.backend_), device_(device), h_(backend_, device.h_.has_ownership())
{
#ifndef ANDROID
if (std::getenv("ISAAC_CACHE_PATH"))

View File

@@ -18,13 +18,13 @@ int Device::cuGetInfo() const
return res;
}
Device::Device(int ordinal): backend_(CUDA), h_(backend_)
Device::Device(int ordinal, bool take_ownership): backend_(CUDA), h_(backend_, take_ownership)
{ cuda::check(cuDeviceGet(h_.cu.get(), ordinal)); }
#endif
Device::Device(cl_device_id const & device) : backend_(OPENCL), h_(backend_)
Device::Device(cl_device_id const & device, bool take_ownership) : backend_(OPENCL), h_(backend_, take_ownership)
{ h_.cl() = device; }
backend_type Device::backend() const

View File

@@ -7,7 +7,7 @@ namespace isaac
namespace driver
{
Event::Event(backend_type backend) : backend_(backend), h_(backend_)
Event::Event(backend_type backend) : backend_(backend), h_(backend_, true)
{
switch(backend_)
{
@@ -22,7 +22,7 @@ Event::Event(backend_type backend) : backend_(backend), h_(backend_)
}
}
Event::Event(cl_event const & event) : backend_(OPENCL), h_(backend_)
Event::Event(cl_event const & event, bool take_ownership) : backend_(OPENCL), h_(backend_, take_ownership)
{
h_.cl() = event;
}

View File

@@ -57,7 +57,7 @@ template<class CLType, class CUType>
void Handle<CLType, CUType>::release(cl_program x) { ocl::check(clReleaseProgram(x)); }
template<class CLType, class CUType>
Handle<CLType, CUType>::Handle(backend_type backend): backend_(backend)
Handle<CLType, CUType>::Handle(backend_type backend, bool take_ownership): backend_(backend), has_ownership_(take_ownership)
{
switch(backend_)
{
@@ -100,10 +100,10 @@ template<class CLType, class CUType>
Handle<CLType, CUType>::~Handle()
{
#ifdef ISAAC_WITH_CUDA
if(cu_ && cu_.unique())
if(has_ownership_ && cu_ && cu_.unique())
_delete(*cu_);
#endif
if(cl_ && cl_.unique())
if(has_ownership_ && cl_ && cl_.unique())
release(*cl_);
}
@@ -115,6 +115,10 @@ template<class CLType, class CUType>
CLType const & Handle<CLType, CUType>::cl() const
{ return *cl_; }
template<class CLType, class CUType>
bool Handle<CLType,CUType>::has_ownership() const
{ return has_ownership_; }
#ifdef ISAAC_WITH_CUDA
template<class CLType, class CUType>
CUType & Handle<CLType, CUType>::cu()

View File

@@ -8,7 +8,7 @@ namespace isaac
namespace driver
{
Kernel::Kernel(Program const & program, const char * name) : backend_(program.backend_), address_bits_(program.context().device().address_bits()), h_(backend_)
Kernel::Kernel(Program const & program, const char * name) : backend_(program.backend_), address_bits_(program.context().device().address_bits()), h_(backend_, program.h_.has_ownership())
{
switch(backend_)
{

View File

@@ -11,7 +11,7 @@ namespace driver
{
#ifdef ISAAC_WITH_CUDA
Platform::Platform(backend_type backend): backend_(backend){}
Platform::Platform(backend_type backend): backend_(backend, take_ownership){}
#endif
Platform::Platform(cl_platform_id const & platform) : backend_(OPENCL)

View File

@@ -16,7 +16,7 @@ namespace isaac
namespace driver
{
Program::Program(Context const & context, std::string const & source) : backend_(context.backend_), context_(context), source_(source), h_(backend_)
Program::Program(Context const & context, std::string const & source) : backend_(context.backend_), context_(context), source_(source), h_(backend_, context.h_.has_ownership())
{
// std::cout << source << std::endl;
std::string cache_path = context.cache_path_;

View File

@@ -28,9 +28,7 @@ extern "C"
for(cl_uint i = 0 ; i < numCommandQueues ; ++i)
{
std::list<is::driver::Event> levents;
is::driver::CommandQueue queue(commandQueues[i]);
clRetainCommandQueue(commandQueues[i]);
is::execution_options_type options(queue, &levents, &waitlist);
is::execution_options_type options(is::driver::CommandQueue(commandQueues[i],false), &levents, &waitlist);
is::execute(is::control(operation, options), is::models(options.queue(context)));
if(events)
{
@@ -54,10 +52,8 @@ extern "C"
cl_uint numEventsInWaitList, const cl_event *eventWaitList, \
cl_event *events) \
{ \
is::array x(N, TYPE_ISAAC, mx, offx, incx); \
clRetainMemObject(mx); \
is::array y(N, TYPE_ISAAC, my, offy, incy); \
clRetainMemObject(my); \
is::array x(N, TYPE_ISAAC, is::driver::Buffer(mx,false), offx, incx); \
is::array y(N, TYPE_ISAAC, is::driver::Buffer(my,false), offy, incy); \
execute(is::assign(y, alpha*x + y), y.context(), numCommandQueues, commandQueues, numEventsInWaitList, eventWaitList, events); \
return clblasSuccess; \
}
@@ -72,8 +68,7 @@ extern "C"
cl_uint numCommandQueues, cl_command_queue *commandQueues,\
cl_uint numEventsInWaitList, const cl_event *eventWaitList, cl_event *events)\
{\
is::array x(N, TYPE_ISAAC, mx, offx, incx);\
clRetainMemObject(mx);\
is::array x(N, TYPE_ISAAC, is::driver::Buffer(mx,false), offx, incx);\
execute(is::assign(x, alpha*x), x.context(), numCommandQueues, commandQueues, numEventsInWaitList, eventWaitList, events);\
return clblasSuccess;\
}
@@ -89,10 +84,8 @@ extern "C"
cl_uint numCommandQueues, cl_command_queue *commandQueues,\
cl_uint numEventsInWaitList, const cl_event *eventWaitList, cl_event *events)\
{\
const is::array x(N, TYPE_ISAAC, mx, offx, incx);\
clRetainMemObject(mx);\
is::array y(N, TYPE_ISAAC, my, offy, incy);\
clRetainMemObject(my);\
const is::array x(N, TYPE_ISAAC, is::driver::Buffer(mx, false), offx, incx);\
is::array y(N, TYPE_ISAAC, is::driver::Buffer(my, false), offy, incy);\
execute(is::assign(y, x), y.context(), numCommandQueues, commandQueues, numEventsInWaitList, eventWaitList, events);\
return clblasSuccess;\
}
@@ -109,12 +102,9 @@ extern "C"
cl_command_queue *commandQueues, cl_uint numEventsInWaitList, \
const cl_event *eventWaitList, cl_event *events) \
{ \
is::array x(N, TYPE_ISAAC, mx, offx, incx); \
clRetainMemObject(mx); \
is::array y(N, TYPE_ISAAC, my, offy, incy); \
clRetainMemObject(my); \
is::scalar s(TYPE_ISAAC, dotProduct, offDP); \
clRetainMemObject(dotProduct); \
is::array x(N, TYPE_ISAAC, is::driver::Buffer(mx, false), offx, incx); \
is::array y(N, TYPE_ISAAC, is::driver::Buffer(my, false), offy, incy); \
is::scalar s(TYPE_ISAAC, is::driver::Buffer(dotProduct, false), offDP); \
execute(is::assign(s, dot(x,y)), s.context(), numCommandQueues, commandQueues, numEventsInWaitList, eventWaitList, events); \
return clblasSuccess; \
}
@@ -129,10 +119,8 @@ extern "C"
cl_mem /*scratchBuff*/, cl_uint numCommandQueues, cl_command_queue *commandQueues,\
cl_uint numEventsInWaitList, const cl_event *eventWaitList, cl_event *events)\
{\
is::array x(N, TYPE_ISAAC, mx, offx, incx);\
clRetainMemObject(mx);\
is::scalar s(TYPE_ISAAC, asum, offAsum);\
clRetainMemObject(asum);\
is::array x(N, TYPE_ISAAC, is::driver::Buffer(mx, false), offx, incx);\
is::scalar s(TYPE_ISAAC, is::driver::Buffer(asum, false), offAsum);\
execute(is::assign(s, sum(abs(x))), s.context(), numCommandQueues, commandQueues, numEventsInWaitList, eventWaitList, events);\
return clblasSuccess;\
}
@@ -156,15 +144,12 @@ extern "C"
std::swap(M, N);\
transA = (transA==clblasTrans)?clblasNoTrans:clblasTrans;\
}\
is::array A(M, N, TYPE_ISAAC, mA, offA, lda);\
clRetainMemObject(mA);\
is::array A(M, N, TYPE_ISAAC, is::driver::Buffer(mA, false), offA, lda);\
\
is::int_t sx = N, sy = M;\
if(transA) std::swap(sx, sy);\
is::array x(sx, TYPE_ISAAC, mx, offx, incx);\
clRetainMemObject(mx);\
is::array y(sy, TYPE_ISAAC, my, offy, incy);\
clRetainMemObject(my);\
is::array x(sx, TYPE_ISAAC, is::driver::Buffer(mx, false), offx, incx);\
is::array y(sy, TYPE_ISAAC, is::driver::Buffer(my, false), offy, incy);\
\
is::driver::Context const & context = A.context();\
if(transA==clblasTrans)\
@@ -204,12 +189,9 @@ extern "C"
if(transA==clblasTrans) std::swap(As1, As2);\
if(transB==clblasTrans) std::swap(Bs1, Bs2);\
/*Struct*/\
is::array A(As1, As2, TYPE_ISAAC, mA, offA, lda);\
clRetainMemObject(mA);\
is::array B(Bs1, Bs2, TYPE_ISAAC, mB, offB, ldb);\
clRetainMemObject(mB);\
is::array C(M, N, TYPE_ISAAC, mC, offC, ldc);\
clRetainMemObject(mC);\
is::array A(As1, As2, TYPE_ISAAC, is::driver::Buffer(mA, false), offA, lda);\
is::array B(Bs1, Bs2, TYPE_ISAAC, is::driver::Buffer(mB, false), offB, ldb);\
is::array C(M, N, TYPE_ISAAC, is::driver::Buffer(mC, false), offC, ldc);\
is::driver::Context const & context = C.context();\
/*Operation*/\
if((transA==clblasTrans) && (transB==clblasTrans))\

View File

@@ -45,11 +45,11 @@ void test_reduction(T epsilon, simple_vector_base<T> & cx, simple_vector_base<T
cout << endl;
#define PREFIX "[C]"
// RUN_TEST("DOT", cs+=cx[i]*cy[i], 0, cs, BLAS<T>::F(clblasSdot, clblasDdot)(N, CHANDLE(ds), 0, CHANDLE(x), x.start()[0], x.stride()[0],
// CHANDLE(y), y.start()[0], y.stride()[0],
// CHANDLE(scratch), 1, &clqueue, 0, NULL, NULL));
// RUN_TEST("ASUM", cs+=std::fabs(cx[i]), 0, cs, BLAS<T>::F(clblasSasum, clblasDasum)(N, CHANDLE(ds), 0, CHANDLE(x), x.start()[0], x.stride()[0],
// CHANDLE(scratch), 1, &clqueue, 0, NULL, NULL));
RUN_TEST("DOT", cs+=cx[i]*cy[i], 0, cs, BLAS<T>::F(clblasSdot, clblasDdot)(N, CHANDLE(ds), 0, CHANDLE(x), x.start()[0], x.stride()[0],
CHANDLE(y), y.start()[0], y.stride()[0],
CHANDLE(scratch), 1, &clqueue, 0, NULL, NULL));
RUN_TEST("ASUM", cs+=std::fabs(cx[i]), 0, cs, BLAS<T>::F(clblasSasum, clblasDasum)(N, CHANDLE(ds), 0, CHANDLE(x), x.start()[0], x.stride()[0],
CHANDLE(scratch), 1, &clqueue, 0, NULL, NULL));
#undef PREFIX
#define PREFIX "[C++]"