|
ViennaCL - The Vienna Computing Library
1.1.2
|
00001 /* ======================================================================= 00002 Copyright (c) 2010, Institute for Microelectronics, TU Vienna. 00003 http://www.iue.tuwien.ac.at 00004 ----------------- 00005 ViennaCL - The Vienna Computing Library 00006 ----------------- 00007 00008 authors: Karl Rupp rupp@iue.tuwien.ac.at 00009 Florian Rudolf flo.rudy+viennacl@gmail.com 00010 Josef Weinbub weinbub@iue.tuwien.ac.at 00011 00012 license: MIT (X11), see file LICENSE in the ViennaCL base directory 00013 ======================================================================= */ 00014 00015 #ifndef _VIENNACL_HANDLE_HPP_ 00016 #define _VIENNACL_HANDLE_HPP_ 00017 00018 #ifdef __APPLE__ 00019 #include <OpenCL/cl.h> 00020 #else 00021 #include <CL/cl.h> 00022 #endif 00023 00024 #include <assert.h> 00025 #include <string> 00026 #include <iostream> 00027 #include "viennacl/ocl/error.hpp" 00028 00029 namespace viennacl 00030 { 00031 namespace ocl 00032 { 00036 template<class OCL_TYPE> 00037 class handle_inc_dec_helper 00038 { 00039 typedef typename OCL_TYPE::ERROR_TEMPLATE_ARGUMENT_FOR_CLASS_INVALID ErrorType; 00040 }; 00041 00042 00043 //cl_mem: 00044 template <> 00045 struct handle_inc_dec_helper<cl_mem> 00046 { 00047 static void inc(cl_mem & something) 00048 { 00049 cl_int err = clRetainMemObject(something); 00050 VIENNACL_ERR_CHECK(err); 00051 } 00052 00053 static void dec(cl_mem & something) 00054 { 00055 #ifndef __APPLE__ 00056 cl_int err = clReleaseMemObject(something); 00057 VIENNACL_ERR_CHECK(err); 00058 #endif 00059 } 00060 }; 00061 00062 //cl_program: 00063 template <> 00064 struct handle_inc_dec_helper<cl_program> 00065 { 00066 static void inc(cl_program & something) 00067 { 00068 cl_int err = clRetainProgram(something); 00069 VIENNACL_ERR_CHECK(err); 00070 } 00071 00072 static void dec(cl_program & something) 00073 { 00074 #ifndef __APPLE__ 00075 cl_int err = clReleaseProgram(something); 00076 VIENNACL_ERR_CHECK(err); 00077 #endif 00078 } 00079 }; 00080 00081 //cl_kernel: 00082 template <> 00083 struct handle_inc_dec_helper<cl_kernel> 00084 { 00085 static void inc(cl_kernel & something) 00086 { 00087 cl_int err = clRetainKernel(something); 00088 VIENNACL_ERR_CHECK(err); 00089 } 00090 00091 static void dec(cl_kernel & something) 00092 { 00093 #ifndef __APPLE__ 00094 cl_int err = clReleaseKernel(something); 00095 VIENNACL_ERR_CHECK(err); 00096 #endif 00097 } 00098 }; 00099 00100 //cl_command_queue: 00101 template <> 00102 struct handle_inc_dec_helper<cl_command_queue> 00103 { 00104 static void inc(cl_command_queue & something) 00105 { 00106 cl_int err = clRetainCommandQueue(something); 00107 VIENNACL_ERR_CHECK(err); 00108 } 00109 00110 static void dec(cl_command_queue & something) 00111 { 00112 #ifndef __APPLE__ 00113 cl_int err = clReleaseCommandQueue(something); 00114 VIENNACL_ERR_CHECK(err); 00115 #endif 00116 } 00117 }; 00118 00119 //cl_context: 00120 template <> 00121 struct handle_inc_dec_helper<cl_context> 00122 { 00123 static void inc(cl_context & something) 00124 { 00125 cl_int err = clRetainContext(something); 00126 VIENNACL_ERR_CHECK(err); 00127 } 00128 00129 static void dec(cl_context & something) 00130 { 00131 #ifndef __APPLE__ 00132 cl_int err = clReleaseContext(something); 00133 VIENNACL_ERR_CHECK(err); 00134 #endif 00135 } 00136 }; 00137 00139 template<class OCL_TYPE> 00140 class handle 00141 { 00142 public: 00143 handle() : something(0) {} 00144 handle(const OCL_TYPE & _something) : something(_something) {} 00145 handle(const handle & h) : something(h.something) { if (something != 0) inc(); } 00146 ~handle() { if (something != 0) dec(); } 00147 handle & operator=(const handle & h) 00148 { 00149 if (something != 0) dec(); 00150 something = h.something; 00151 inc(); 00152 return *this; 00153 } 00154 handle & operator=(const OCL_TYPE & _something) 00155 { 00156 if (something != 0) dec(); 00157 something = _something; 00158 return *this; 00159 } 00160 operator OCL_TYPE() const { return something; } 00161 //const OCL_TYPE & get() const { return something; } 00162 00164 handle & swap(handle & other) 00165 { 00166 OCL_TYPE tmp = other.something; 00167 other.something = this->something; 00168 this->something = tmp; 00169 return *this; 00170 } 00171 00173 void inc() { handle_inc_dec_helper<OCL_TYPE>::inc(something); }; 00175 void dec() { handle_inc_dec_helper<OCL_TYPE>::dec(something); }; 00176 private: 00177 OCL_TYPE something; 00178 }; 00179 00180 00181 } //namespace ocl 00182 } //namespace viennacl 00183 00184 #endif
1.7.6.1