/* 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. */ #include #include #include "isaac/driver/handle.h" namespace isaac { namespace driver { //CUDA template void Handle::_delete(CUcontext x) { check_destruction(dispatch::cuCtxDestroy(x)); } template void Handle::_delete(CUdeviceptr x) { check_destruction(dispatch::cuMemFree(x)); } template void Handle::_delete(CUstream x) { check_destruction(dispatch::cuStreamDestroy(x)); } template void Handle::_delete(CUdevice) { } template void Handle::_delete(CUevent x) { check_destruction(dispatch::cuEventDestroy(x)); } template void Handle::_delete(CUfunction) { } template void Handle::_delete(CUmodule x) { check_destruction(dispatch::cuModuleUnload(x)); } template void Handle::_delete(cu_event_t x) { _delete(x.first); _delete(x.second); } //OpenCL template void Handle::release(cl_context x) { dispatch::clReleaseContext(x); } template void Handle::release(cl_mem x) { dispatch::clReleaseMemObject(x); } template void Handle::release(cl_command_queue x) { dispatch::clReleaseCommandQueue(x); } template void Handle::release(cl_device_id /*x*/) { /*dispatch::clReleaseDevice(x);*/ } template void Handle::release(cl_event x) { dispatch::clReleaseEvent(x); } template void Handle::release(cl_kernel x) { dispatch::clReleaseKernel(x); } template void Handle::release(cl_program x) { dispatch::clReleaseProgram(x); } template Handle::Handle(backend_type backend, bool take_ownership): backend_(backend), has_ownership_(take_ownership) { switch(backend_) { case CUDA: cu_.reset(new CUType()); case OPENCL: cl_.reset(new CLType()); } } template backend_type Handle::backend() const { return backend_; } template bool Handle::operator==(Handle const & other) const { if(backend_==CUDA && other.backend_==CUDA) return cu()==other.cu(); if(backend_==OPENCL && other.backend_==OPENCL) return cl()==other.cl(); return false; } template bool Handle::operator!=(Handle const & other) const { return !((*this)==other); } template bool Handle::operator<(Handle const & other) const { if(backend_==CUDA && other.backend_==CUDA) return (*cu_)<(*other.cu_); if(backend_==OPENCL && other.backend_==OPENCL) return (*cl_)<(*other.cl_); if(backend_==CUDA && other.backend_==OPENCL) return true; return false; } template Handle::~Handle() { if(backend_==CUDA && has_ownership_ && cu_ && cu_.unique() && *cu_){ _delete(*cu_); } if(backend_==OPENCL && has_ownership_ && cl_ && cl_.unique() && *cl_) release(*cl_); } template CLType & Handle::cl() { assert(backend_==OPENCL); return *cl_; } template CLType const & Handle::cl() const { assert(backend_==OPENCL); return *cl_; } template CUType & Handle::cu() { assert(backend_==CUDA); return *cu_; } template CUType const & Handle::cu() const { assert(backend_==CUDA); return *cu_; } template class Handle; template class Handle; template class Handle; template class Handle; template class Handle; template class Handle; template class Handle; } }