ViennaCL - The Vienna Computing Library  1.2.0
handle.hpp
Go to the documentation of this file.
1 #ifndef VIENNACL_OCL_HANDLE_HPP_
2 #define VIENNACL_OCL_HANDLE_HPP_
3 
4 /* =========================================================================
5  Copyright (c) 2010-2011, Institute for Microelectronics,
6  Institute for Analysis and Scientific Computing,
7  TU Wien.
8 
9  -----------------
10  ViennaCL - The Vienna Computing Library
11  -----------------
12 
13  Project Head: Karl Rupp rupp@iue.tuwien.ac.at
14 
15  (A list of authors and contributors can be found in the PDF manual)
16 
17  License: MIT (X11), see file LICENSE in the base directory
18 ============================================================================= */
19 
24 #ifdef __APPLE__
25 #include <OpenCL/cl.h>
26 #else
27 #include <CL/cl.h>
28 #endif
29 
30 #include <assert.h>
31 #include <string>
32 #include <iostream>
33 #include "viennacl/ocl/error.hpp"
34 
35 namespace viennacl
36 {
37  namespace ocl
38  {
42  template<class OCL_TYPE>
44  {
45  typedef typename OCL_TYPE::ERROR_TEMPLATE_ARGUMENT_FOR_CLASS_INVALID ErrorType;
46  };
47 
48 
49  //cl_mem:
50  template <>
51  struct handle_inc_dec_helper<cl_mem>
52  {
53  static void inc(cl_mem & something)
54  {
55  cl_int err = clRetainMemObject(something);
56  VIENNACL_ERR_CHECK(err);
57  }
58 
59  static void dec(cl_mem & something)
60  {
61  #ifndef __APPLE__
62  cl_int err = clReleaseMemObject(something);
63  VIENNACL_ERR_CHECK(err);
64  #endif
65  }
66  };
67 
68  //cl_program:
69  template <>
70  struct handle_inc_dec_helper<cl_program>
71  {
72  static void inc(cl_program & something)
73  {
74  cl_int err = clRetainProgram(something);
75  VIENNACL_ERR_CHECK(err);
76  }
77 
78  static void dec(cl_program & something)
79  {
80  #ifndef __APPLE__
81  cl_int err = clReleaseProgram(something);
82  VIENNACL_ERR_CHECK(err);
83  #endif
84  }
85  };
86 
87  //cl_kernel:
88  template <>
89  struct handle_inc_dec_helper<cl_kernel>
90  {
91  static void inc(cl_kernel & something)
92  {
93  cl_int err = clRetainKernel(something);
94  VIENNACL_ERR_CHECK(err);
95  }
96 
97  static void dec(cl_kernel & something)
98  {
99  #ifndef __APPLE__
100  cl_int err = clReleaseKernel(something);
101  VIENNACL_ERR_CHECK(err);
102  #endif
103  }
104  };
105 
106  //cl_command_queue:
107  template <>
108  struct handle_inc_dec_helper<cl_command_queue>
109  {
110  static void inc(cl_command_queue & something)
111  {
112  cl_int err = clRetainCommandQueue(something);
113  VIENNACL_ERR_CHECK(err);
114  }
115 
116  static void dec(cl_command_queue & something)
117  {
118  #ifndef __APPLE__
119  cl_int err = clReleaseCommandQueue(something);
120  VIENNACL_ERR_CHECK(err);
121  #endif
122  }
123  };
124 
125  //cl_context:
126  template <>
127  struct handle_inc_dec_helper<cl_context>
128  {
129  static void inc(cl_context & something)
130  {
131  cl_int err = clRetainContext(something);
132  VIENNACL_ERR_CHECK(err);
133  }
134 
135  static void dec(cl_context & something)
136  {
137  #ifndef __APPLE__
138  cl_int err = clReleaseContext(something);
139  VIENNACL_ERR_CHECK(err);
140  #endif
141  }
142  };
143 
145  template<class OCL_TYPE>
146  class handle
147  {
148  public:
149  handle() : something(0) {}
150  handle(const OCL_TYPE & _something) : something(_something) {}
151  handle(const handle & h) : something(h.something) { if (something != 0) inc(); }
152  ~handle() { if (something != 0) dec(); }
153  handle & operator=(const handle & h)
154  {
155  if (something != 0) dec();
156  something = h.something;
157  inc();
158  return *this;
159  }
160  handle & operator=(const OCL_TYPE & _something)
161  {
162  if (something != 0) dec();
163  something = _something;
164  return *this;
165  }
166  operator OCL_TYPE() const { return something; }
167  //const OCL_TYPE & get() const { return something; }
168 
170  handle & swap(handle & other)
171  {
172  OCL_TYPE tmp = other.something;
173  other.something = this->something;
174  this->something = tmp;
175  return *this;
176  }
177 
182  private:
183  OCL_TYPE something;
184  };
185 
186 
187  } //namespace ocl
188 } //namespace viennacl
189 
190 #endif