/* * Copyright (c) 2015, PHILIPPE TILLET. All rights reserved. * * This file is part of ISAAC. * * ISAAC is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, * MA 02110-1301 USA */ #include #include #include "isaac/driver/handle.h" namespace isaac { namespace driver { //CUDA template void Handle::_delete(CUcontext x) { cuda::check_destruction(dispatch::cuCtxDestroy(x)); } template void Handle::_delete(CUdeviceptr x) { cuda::check_destruction(dispatch::dispatch::cuMemFree(x)); } template void Handle::_delete(CUstream x) { cuda::check_destruction(dispatch::cuStreamDestroy(x)); } template void Handle::_delete(CUdevice) { } template void Handle::_delete(CUevent x) { cuda::check_destruction(dispatch::dispatch::cuEventDestroy(x)); } template void Handle::_delete(CUfunction) { } template void Handle::_delete(CUmodule x) { cuda::check_destruction(dispatch::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) { ocl::check(dispatch::clReleaseContext(x)); } template void Handle::release(cl_mem x) { ocl::check(dispatch::clReleaseMemObject(x)); } template void Handle::release(cl_command_queue x) { ocl::check(dispatch::clReleaseCommandQueue(x)); } template void Handle::release(cl_device_id x) { ocl::check(dispatch::clReleaseDevice(x)); } template void Handle::release(cl_event x) { ocl::check(dispatch::clReleaseEvent(x)); } template void Handle::release(cl_kernel x) { ocl::check(dispatch::clReleaseKernel(x)); } template void Handle::release(cl_program x) { ocl::check(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 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 { 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; } }