• Main Page
  • Namespaces
  • Data Structures
  • Files
  • File List
  • Globals

/data/development/ViennaCL/dev/viennacl/ocl/handle.hpp

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

Generated on Fri Dec 30 2011 23:20:43 for ViennaCL - The Vienna Computing Library by  doxygen 1.7.1