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-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-11-27 02:09:15 -05:00
|
|
|
|
|
|
|
|
2015-08-25 12:41:21 -04:00
|
|
|
//CUDA
|
2015-04-29 15:50:57 -04:00
|
|
|
template<class CLType, class CUType>
|
2016-04-02 18:19:33 -04:00
|
|
|
void Handle<CLType, CUType>::_delete(CUcontext x) { check_destruction(dispatch::cuCtxDestroy(x)); }
|
2015-04-29 15:50:57 -04:00
|
|
|
|
|
|
|
template<class CLType, class CUType>
|
2016-10-04 04:20:07 -04:00
|
|
|
void Handle<CLType, CUType>::_delete(CUdeviceptr x) { check_destruction(dispatch::cuMemFree(x)); }
|
2015-04-29 15:50:57 -04:00
|
|
|
|
|
|
|
template<class CLType, class CUType>
|
2016-04-02 18:19:33 -04:00
|
|
|
void Handle<CLType, CUType>::_delete(CUstream x) { check_destruction(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>
|
2016-10-04 04:20:07 -04:00
|
|
|
void Handle<CLType, CUType>::_delete(CUevent x) { check_destruction(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>
|
2016-10-04 04:20:07 -04:00
|
|
|
void Handle<CLType, CUType>::_delete(CUmodule x) { check_destruction(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>
|
2016-10-04 04:20:07 -04:00
|
|
|
void Handle<CLType, CUType>::release(cl_context x) { dispatch::clReleaseContext(x); }
|
2015-07-26 18:30:06 -07:00
|
|
|
|
|
|
|
template<class CLType, class CUType>
|
2016-10-04 04:20:07 -04:00
|
|
|
void Handle<CLType, CUType>::release(cl_mem x) { dispatch::clReleaseMemObject(x); }
|
2015-07-26 18:30:06 -07:00
|
|
|
|
|
|
|
template<class CLType, class CUType>
|
2016-10-04 04:20:07 -04:00
|
|
|
void Handle<CLType, CUType>::release(cl_command_queue x) { dispatch::clReleaseCommandQueue(x); }
|
2015-07-26 18:30:06 -07:00
|
|
|
|
|
|
|
template<class CLType, class CUType>
|
2016-10-06 15:28:36 -04:00
|
|
|
void Handle<CLType, CUType>::release(cl_device_id /*x*/) { /*dispatch::clReleaseDevice(x);*/ }
|
2015-07-26 18:30:06 -07:00
|
|
|
|
|
|
|
template<class CLType, class CUType>
|
2016-10-04 04:20:07 -04:00
|
|
|
void Handle<CLType, CUType>::release(cl_event x) { dispatch::clReleaseEvent(x); }
|
2015-07-26 18:30:06 -07:00
|
|
|
|
|
|
|
template<class CLType, class CUType>
|
2016-10-04 04:20:07 -04:00
|
|
|
void Handle<CLType, CUType>::release(cl_kernel x) { dispatch::clReleaseKernel(x); }
|
2015-07-26 18:30:06 -07:00
|
|
|
|
|
|
|
template<class CLType, class CUType>
|
2016-10-04 04:20:07 -04:00
|
|
|
void Handle<CLType, CUType>::release(cl_program x) { 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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-02 18:19:33 -04:00
|
|
|
template<class CLType, class CUType>
|
|
|
|
backend_type Handle<CLType, CUType>::backend() const
|
|
|
|
{ return backend_; }
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-04-02 18:19:33 -04:00
|
|
|
template<class CLType, class CUType>
|
|
|
|
bool Handle<CLType, CUType>::operator!=(Handle const & other) const
|
|
|
|
{ return !((*this)==other); }
|
|
|
|
|
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
|
|
|
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
|
|
|
|
|
|
|
}
|
|
|
|
}
|