2015-08-26 14:12:50 -04:00
|
|
|
#include <cassert>
|
2015-04-29 15:50:57 -04:00
|
|
|
#include <memory>
|
|
|
|
|
2015-08-26 14:12:50 -04:00
|
|
|
#include "isaac/driver/handle.h"
|
|
|
|
|
2015-04-29 15:50:57 -04:00
|
|
|
namespace isaac
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace driver
|
|
|
|
{
|
|
|
|
|
2015-08-25 12:41:21 -04:00
|
|
|
//CUDA
|
2015-04-29 15:50:57 -04:00
|
|
|
template<class CLType, class CUType>
|
2015-08-25 12:41:21 -04:00
|
|
|
void Handle<CLType, CUType>::_delete(CUcontext x) { cuda::check(dispatch::cuCtxDestroy(x)); }
|
2015-04-29 15:50:57 -04:00
|
|
|
|
|
|
|
template<class CLType, class CUType>
|
2015-08-25 12:41:21 -04:00
|
|
|
void Handle<CLType, CUType>::_delete(CUdeviceptr x) { cuda::check(dispatch::dispatch::cuMemFree(x)); }
|
2015-04-29 15:50:57 -04:00
|
|
|
|
|
|
|
template<class CLType, class CUType>
|
2015-08-25 12:41:21 -04:00
|
|
|
void Handle<CLType, CUType>::_delete(CUstream x) { cuda::check(dispatch::cuStreamDestroy(x)); }
|
2015-04-29 15:50:57 -04:00
|
|
|
|
|
|
|
template<class CLType, class CUType>
|
2015-08-25 12:41:21 -04:00
|
|
|
void Handle<CLType, CUType>::_delete(CUdevice) { }
|
2015-04-29 15:50:57 -04:00
|
|
|
|
|
|
|
template<class CLType, class CUType>
|
2015-08-25 12:41:21 -04:00
|
|
|
void Handle<CLType, CUType>::_delete(CUevent x) { cuda::check(dispatch::dispatch::cuEventDestroy(x)); }
|
2015-04-29 15:50:57 -04:00
|
|
|
|
|
|
|
template<class CLType, class CUType>
|
|
|
|
void Handle<CLType, CUType>::_delete(CUfunction) { }
|
|
|
|
|
|
|
|
template<class CLType, class CUType>
|
2015-08-25 12:41:21 -04:00
|
|
|
void Handle<CLType, CUType>::_delete(CUmodule x) { cuda::check(dispatch::dispatch::cuModuleUnload(x)); }
|
2015-04-29 15:50:57 -04:00
|
|
|
|
|
|
|
template<class CLType, class CUType>
|
2015-08-21 13:06:20 -04:00
|
|
|
void Handle<CLType, CUType>::_delete(cu_event_t x) { _delete(x.first); _delete(x.second); }
|
2015-04-29 15:50:57 -04:00
|
|
|
|
2015-08-25 12:41:21 -04:00
|
|
|
//OpenCL
|
2015-07-26 18:30:06 -07:00
|
|
|
template<class CLType, class CUType>
|
2015-08-25 12:41:21 -04:00
|
|
|
void Handle<CLType, CUType>::release(cl_context x) { ocl::check(dispatch::clReleaseContext(x)); }
|
2015-07-26 18:30:06 -07:00
|
|
|
|
|
|
|
template<class CLType, class CUType>
|
2015-08-25 12:41:21 -04:00
|
|
|
void Handle<CLType, CUType>::release(cl_mem x) { ocl::check(dispatch::clReleaseMemObject(x)); }
|
2015-07-26 18:30:06 -07:00
|
|
|
|
|
|
|
template<class CLType, class CUType>
|
2015-08-25 12:41:21 -04:00
|
|
|
void Handle<CLType, CUType>::release(cl_command_queue x) { ocl::check(dispatch::clReleaseCommandQueue(x)); }
|
2015-07-26 18:30:06 -07:00
|
|
|
|
|
|
|
template<class CLType, class CUType>
|
2015-08-25 12:41:21 -04:00
|
|
|
void Handle<CLType, CUType>::release(cl_device_id x) { ocl::check(dispatch::clReleaseDevice(x)); }
|
2015-07-26 18:30:06 -07:00
|
|
|
|
|
|
|
template<class CLType, class CUType>
|
2015-08-25 12:41:21 -04:00
|
|
|
void Handle<CLType, CUType>::release(cl_event x) { ocl::check(dispatch::clReleaseEvent(x)); }
|
2015-07-26 18:30:06 -07:00
|
|
|
|
|
|
|
template<class CLType, class CUType>
|
2015-08-25 12:41:21 -04:00
|
|
|
void Handle<CLType, CUType>::release(cl_kernel x) { ocl::check(dispatch::clReleaseKernel(x)); }
|
2015-07-26 18:30:06 -07:00
|
|
|
|
|
|
|
template<class CLType, class CUType>
|
2015-08-25 12:41:21 -04:00
|
|
|
void Handle<CLType, CUType>::release(cl_program x) { ocl::check(dispatch::clReleaseProgram(x)); }
|
2015-07-26 18:30:06 -07:00
|
|
|
|
2015-04-29 15:50:57 -04:00
|
|
|
template<class CLType, class CUType>
|
2015-07-26 21:13:28 -07:00
|
|
|
Handle<CLType, CUType>::Handle(backend_type backend, bool take_ownership): backend_(backend), has_ownership_(take_ownership)
|
2015-04-29 15:50:57 -04:00
|
|
|
{
|
|
|
|
switch(backend_)
|
|
|
|
{
|
2015-07-23 09:39:13 -07:00
|
|
|
case CUDA: cu_.reset(new CUType());
|
|
|
|
case OPENCL: cl_.reset(new CLType());
|
2015-04-29 15:50:57 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class CLType, class CUType>
|
|
|
|
bool Handle<CLType, CUType>::operator==(Handle const & other) const
|
|
|
|
{
|
|
|
|
if(backend_==CUDA && other.backend_==CUDA)
|
2015-07-23 09:39:13 -07:00
|
|
|
return cu()==other.cu();
|
2015-04-29 15:50:57 -04:00
|
|
|
if(backend_==OPENCL && other.backend_==OPENCL)
|
2015-07-25 21:00:18 -07:00
|
|
|
return cl()==other.cl();
|
2015-04-29 15:50:57 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class CLType, class CUType>
|
|
|
|
bool Handle<CLType, CUType>::operator<(Handle const & other) const
|
|
|
|
{
|
|
|
|
if(backend_==CUDA && other.backend_==CUDA)
|
2015-07-23 09:39:13 -07:00
|
|
|
return (*cu_)<(*other.cu_);
|
2015-04-29 15:50:57 -04:00
|
|
|
if(backend_==OPENCL && other.backend_==OPENCL)
|
2015-07-25 21:00:18 -07:00
|
|
|
return (*cl_)<(*other.cl_);
|
2015-04-29 15:50:57 -04:00
|
|
|
if(backend_==CUDA && other.backend_==OPENCL)
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class CLType, class CUType>
|
|
|
|
Handle<CLType, CUType>::~Handle()
|
|
|
|
{
|
2015-08-25 12:41:21 -04:00
|
|
|
if(backend_==CUDA && has_ownership_ && cu_ && cu_.unique() && *cu_){
|
2015-07-26 18:30:06 -07:00
|
|
|
_delete(*cu_);
|
2015-08-21 13:06:20 -04:00
|
|
|
}
|
2015-08-25 12:41:21 -04:00
|
|
|
if(backend_==OPENCL && has_ownership_ && cl_ && cl_.unique() && *cl_)
|
2015-07-26 18:30:06 -07:00
|
|
|
release(*cl_);
|
2015-04-29 15:50:57 -04:00
|
|
|
}
|
|
|
|
|
2015-07-23 09:39:13 -07:00
|
|
|
template<class CLType, class CUType>
|
|
|
|
CLType & Handle<CLType, CUType>::cl()
|
2015-08-26 14:12:50 -04:00
|
|
|
{
|
|
|
|
assert(backend_==OPENCL);
|
|
|
|
return *cl_;
|
|
|
|
}
|
2015-04-29 15:50:57 -04:00
|
|
|
|
2015-07-23 09:39:13 -07:00
|
|
|
template<class CLType, class CUType>
|
|
|
|
CLType const & Handle<CLType, CUType>::cl() const
|
2015-08-26 14:12:50 -04:00
|
|
|
{
|
|
|
|
assert(backend_==OPENCL);
|
|
|
|
return *cl_;
|
|
|
|
}
|
2015-04-29 15:50:57 -04:00
|
|
|
|
2015-07-23 09:39:13 -07:00
|
|
|
template<class CLType, class CUType>
|
|
|
|
CUType & Handle<CLType, CUType>::cu()
|
|
|
|
{
|
2015-08-26 14:12:50 -04:00
|
|
|
assert(backend_==CUDA);
|
2015-07-23 09:39:13 -07:00
|
|
|
return *cu_;
|
|
|
|
}
|
|
|
|
|
2015-08-20 21:24:41 -04:00
|
|
|
template<class CLType, class CUType>
|
|
|
|
CUType const & Handle<CLType, CUType>::cu() const
|
|
|
|
{
|
2015-08-26 14:12:50 -04:00
|
|
|
assert(backend_==CUDA);
|
2015-08-20 21:24:41 -04:00
|
|
|
return *cu_;
|
|
|
|
}
|
|
|
|
|
2015-07-25 21:00:18 -07:00
|
|
|
template class Handle<cl_mem, CUdeviceptr>;
|
|
|
|
template class Handle<cl_command_queue, CUstream>;
|
|
|
|
template class Handle<cl_context, CUcontext>;
|
|
|
|
template class Handle<cl_device_id, CUdevice>;
|
2015-08-21 13:06:20 -04:00
|
|
|
template class Handle<cl_event, cu_event_t>;
|
2015-07-25 21:00:18 -07:00
|
|
|
template class Handle<cl_kernel, CUfunction>;
|
|
|
|
template class Handle<cl_program, CUmodule>;
|
2015-04-29 15:50:57 -04:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|