2017-01-17 20:25:28 -05:00
|
|
|
/* Copyright 2015-2017 Philippe Tillet
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
* a copy of this software and associated documentation files
|
|
|
|
* (the "Software"), to deal in the Software without restriction,
|
|
|
|
* including without limitation the rights to use, copy, modify, merge,
|
|
|
|
* publish, distribute, sublicense, and/or sell copies of the Software,
|
|
|
|
* and to permit persons to whom the Software is furnished to do so,
|
|
|
|
* subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be
|
|
|
|
* included in all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
|
|
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
|
|
|
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
|
|
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
|
|
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*/
|
2015-12-21 17:04:09 -05:00
|
|
|
|
2015-07-25 21:00:18 -07:00
|
|
|
#include <iostream>
|
|
|
|
|
2015-07-31 00:41:03 -07:00
|
|
|
#include "isaac/driver/backend.h"
|
2015-04-29 15:50:57 -04:00
|
|
|
#include "isaac/driver/command_queue.h"
|
2015-06-28 17:53:16 -07:00
|
|
|
#include "isaac/driver/common.h"
|
2015-04-29 15:50:57 -04:00
|
|
|
#include "isaac/driver/context.h"
|
|
|
|
#include "isaac/driver/device.h"
|
|
|
|
#include "isaac/driver/event.h"
|
|
|
|
#include "isaac/driver/kernel.h"
|
|
|
|
#include "isaac/driver/ndrange.h"
|
|
|
|
#include "isaac/driver/buffer.h"
|
2015-07-25 21:00:18 -07:00
|
|
|
|
|
|
|
#include "helpers/ocl/infos.hpp"
|
|
|
|
|
2015-04-29 15:50:57 -04:00
|
|
|
namespace isaac
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace driver
|
|
|
|
{
|
|
|
|
|
2015-08-03 20:20:27 -07:00
|
|
|
CommandQueue::CommandQueue(cl_command_queue const & queue, bool take_ownership) : backend_(OPENCL), context_(backend::contexts::import(ocl::info<CL_QUEUE_CONTEXT>(queue))), device_(ocl::info<CL_QUEUE_DEVICE>(queue), false), h_(backend_, take_ownership)
|
2015-06-23 09:38:34 -07:00
|
|
|
{
|
2015-07-23 09:39:13 -07:00
|
|
|
h_.cl() = queue;
|
2015-06-23 09:38:34 -07:00
|
|
|
}
|
|
|
|
|
2015-08-03 20:20:27 -07:00
|
|
|
CommandQueue::CommandQueue(Context const & context, Device const & device, cl_command_queue_properties properties): backend_(device.backend_), context_(context), device_(device), h_(backend_, true)
|
2015-04-29 15:50:57 -04:00
|
|
|
{
|
|
|
|
switch(backend_)
|
|
|
|
{
|
2015-06-28 17:53:16 -07:00
|
|
|
case CUDA:
|
2015-11-21 13:57:05 -05:00
|
|
|
{
|
2016-10-04 04:20:07 -04:00
|
|
|
dispatch::cuStreamCreate(&h_.cu(), 0);
|
2015-06-28 17:53:16 -07:00
|
|
|
break;
|
2015-11-21 13:57:05 -05:00
|
|
|
}
|
2015-08-25 12:41:21 -04:00
|
|
|
|
2015-06-28 17:53:16 -07:00
|
|
|
case OPENCL:
|
2015-07-25 21:00:18 -07:00
|
|
|
{
|
2015-06-28 17:53:16 -07:00
|
|
|
cl_int err;
|
2015-08-25 12:41:21 -04:00
|
|
|
h_.cl() = dispatch::clCreateCommandQueue(context.h_.cl(), device.h_.cl(), properties, &err);
|
2016-04-02 18:19:33 -04:00
|
|
|
check(err);
|
2015-06-28 17:53:16 -07:00
|
|
|
break;
|
2015-07-25 21:00:18 -07:00
|
|
|
}
|
2015-11-21 13:57:05 -05:00
|
|
|
|
|
|
|
default:
|
|
|
|
throw;
|
2015-04-29 15:50:57 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-21 18:43:05 -05:00
|
|
|
backend_type CommandQueue::backend() const
|
|
|
|
{
|
|
|
|
return backend_;
|
|
|
|
}
|
|
|
|
|
2015-04-29 15:50:57 -04:00
|
|
|
Context const & CommandQueue::context() const
|
2015-08-12 00:46:51 -07:00
|
|
|
{
|
|
|
|
return context_;
|
|
|
|
}
|
2015-04-29 15:50:57 -04:00
|
|
|
|
|
|
|
Device const & CommandQueue::device() const
|
2015-08-12 00:46:51 -07:00
|
|
|
{
|
|
|
|
return device_;
|
|
|
|
}
|
2015-04-29 15:50:57 -04:00
|
|
|
|
|
|
|
void CommandQueue::synchronize()
|
|
|
|
{
|
|
|
|
switch(backend_)
|
|
|
|
{
|
2016-10-04 04:20:07 -04:00
|
|
|
case CUDA: dispatch::cuStreamSynchronize(h_.cu()); break;
|
|
|
|
case OPENCL: dispatch::clFinish(h_.cl()); break;
|
2015-04-29 15:50:57 -04:00
|
|
|
default: throw;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-21 18:43:05 -05:00
|
|
|
void CommandQueue::enqueue(Kernel const & kernel, NDRange global, driver::NDRange local, std::vector<Event> const *, Event* event)
|
2015-04-29 15:50:57 -04:00
|
|
|
{
|
|
|
|
switch(backend_)
|
|
|
|
{
|
|
|
|
case CUDA:
|
2015-12-21 18:43:05 -05:00
|
|
|
if(event)
|
2016-10-04 04:20:07 -04:00
|
|
|
dispatch::cuEventRecord(event->h_.cu().first, h_.cu());
|
2015-12-21 18:43:05 -05:00
|
|
|
|
2016-10-04 04:20:07 -04:00
|
|
|
dispatch::cuLaunchKernel(kernel.h_.cu(), global[0]/local[0], global[1]/local[1], global[2]/local[2],
|
|
|
|
local[0], local[1], local[2], 0, h_.cu(),(void**)&kernel.cu_params_[0], NULL);
|
2015-12-21 18:43:05 -05:00
|
|
|
|
|
|
|
if(event)
|
2016-10-04 04:20:07 -04:00
|
|
|
dispatch::cuEventRecord(event->h_.cu().second, h_.cu());
|
2015-04-29 15:50:57 -04:00
|
|
|
break;
|
|
|
|
case OPENCL:
|
2016-10-04 04:20:07 -04:00
|
|
|
dispatch::clEnqueueNDRangeKernel(h_.cl(), kernel.h_.cl(), global.dimension(), NULL, (const size_t *)global, (const size_t *) local, 0, NULL, event?&event->h_.cl():NULL);
|
2015-04-29 15:50:57 -04:00
|
|
|
break;
|
|
|
|
default: throw;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CommandQueue::write(Buffer const & buffer, bool blocking, std::size_t offset, std::size_t size, void const* ptr)
|
|
|
|
{
|
|
|
|
switch(backend_)
|
|
|
|
{
|
|
|
|
case CUDA:
|
|
|
|
if(blocking)
|
2016-10-04 04:20:07 -04:00
|
|
|
dispatch::cuMemcpyHtoD(buffer.h_.cu() + offset, ptr, size);
|
2015-04-29 15:50:57 -04:00
|
|
|
else
|
2016-10-04 04:20:07 -04:00
|
|
|
dispatch::cuMemcpyHtoDAsync(buffer.h_.cu() + offset, ptr, size, h_.cu());
|
2015-04-29 15:50:57 -04:00
|
|
|
break;
|
|
|
|
case OPENCL:
|
2016-10-04 04:20:07 -04:00
|
|
|
dispatch::clEnqueueWriteBuffer(h_.cl(), buffer.h_.cl(), blocking?CL_TRUE:CL_FALSE, offset, size, ptr, 0, NULL, NULL);
|
2015-04-29 15:50:57 -04:00
|
|
|
break;
|
|
|
|
default: throw;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CommandQueue::read(Buffer const & buffer, bool blocking, std::size_t offset, std::size_t size, void* ptr)
|
|
|
|
{
|
|
|
|
switch(backend_)
|
|
|
|
{
|
|
|
|
case CUDA:
|
|
|
|
if(blocking)
|
2016-10-04 04:20:07 -04:00
|
|
|
dispatch::cuMemcpyDtoH(ptr, buffer.h_.cu() + offset, size);
|
2015-04-29 15:50:57 -04:00
|
|
|
else
|
2016-10-04 04:20:07 -04:00
|
|
|
dispatch::cuMemcpyDtoHAsync(ptr, buffer.h_.cu() + offset, size, h_.cu());
|
2015-04-29 15:50:57 -04:00
|
|
|
break;
|
|
|
|
case OPENCL:
|
2016-10-04 04:20:07 -04:00
|
|
|
dispatch::clEnqueueReadBuffer(h_.cl(), buffer.h_.cl(), blocking?CL_TRUE:CL_FALSE, offset, size, ptr, 0, NULL, NULL);
|
2015-04-29 15:50:57 -04:00
|
|
|
break;
|
|
|
|
default: throw;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-31 01:29:31 -04:00
|
|
|
CommandQueue::handle_type const & CommandQueue::handle() const
|
|
|
|
{ return h_; }
|
2015-04-29 15:50:57 -04:00
|
|
|
|
2016-05-31 01:29:31 -04:00
|
|
|
CommandQueue::handle_type & CommandQueue::handle()
|
2015-04-29 15:50:57 -04:00
|
|
|
{ return h_; }
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|