ViennaCL - The Vienna Computing Library  1.5.2
backend.hpp
Go to the documentation of this file.
1 #ifndef VIENNACL_OCL_BACKEND_HPP_
2 #define VIENNACL_OCL_BACKEND_HPP_
3 
4 /* =========================================================================
5  Copyright (c) 2010-2014, Institute for Microelectronics,
6  Institute for Analysis and Scientific Computing,
7  TU Wien.
8  Portions of this software are copyright by UChicago Argonne, LLC.
9 
10  -----------------
11  ViennaCL - The Vienna Computing Library
12  -----------------
13 
14  Project Head: Karl Rupp rupp@iue.tuwien.ac.at
15 
16  (A list of authors and contributors can be found in the PDF manual)
17 
18  License: MIT (X11), see file LICENSE in the base directory
19 ============================================================================= */
20 
25 #include <vector>
26 #include "viennacl/ocl/context.hpp"
27 #include "viennacl/ocl/enqueue.hpp"
28 
29 namespace viennacl
30 {
31  namespace ocl
32  {
33 
35  template <bool dummy = false> //never use parameter other than default (introduced for linkage issues only)
36  class backend
37  {
38  public:
43  static void switch_context(long i)
44  {
45  current_context_id_ = i;
46  }
47 
49  static viennacl::ocl::context & context(long id)
50  {
51  if (!initialized_[id])
52  {
53  //std::cout << "Initializing context no. " << current_context_id_ << std::endl;
54  contexts_[id].init();
55  //create one queue per device:
56  std::vector<viennacl::ocl::device> devices = contexts_[id].devices();
57  for (vcl_size_t j = 0; j<devices.size(); ++j)
58  contexts_[id].add_queue(devices[j]);
59  initialized_[id] = true;
60  /*
61  std::cout << "Context no. " << current_context_id_ << " initialized with " << devices.size() << " devices" << std::endl;
62  std::cout << "Device id: " << devices[0].id() << std::endl;
63  std::cout << "Current device id: " << contexts_[current_context_id_].current_device().id() << std::endl; */
64  }
65  return contexts_[id];
66  }
67 
70  {
71  return backend<dummy>::context(current_context_id_);
72  }
73 
76  {
77  return current_context().get_queue();
78  }
79 
85  static void setup_context(long i,
86  std::vector<cl_device_id> const & devices)
87  {
88  if (initialized_[i])
89  std::cerr << "ViennaCL: Warning in init_context(): Providing a list of devices has no effect, because context for ViennaCL is already created!" << std::endl;
90  else
91  {
92  //set devices for context:
93  for (vcl_size_t j = 0; j<devices.size(); ++j)
94  contexts_[i].add_device(devices[j]);
95  }
96  }
97 
105  static void setup_context(long i,
106  cl_context c,
107  std::vector<cl_device_id> const & devices,
108  std::map< cl_device_id, std::vector< cl_command_queue > > const & queues)
109  {
110  assert(devices.size() == queues.size() && bool("ViennaCL expects one queue per device!"));
111 
112  if (initialized_[i])
113  std::cerr << "ViennaCL: Warning in init_context(): Providing a list of devices has no effect, because context for ViennaCL is already created!" << std::endl;
114  else
115  {
116  //set devices for context:
117  for (vcl_size_t j = 0; j<devices.size(); ++j)
118  contexts_[i].add_device(devices[j]);
119 
120  //init context:
121  contexts_[i].init(c);
122 
123  //add queues:
124  typedef typename std::map< cl_device_id, std::vector< cl_command_queue > >::const_iterator queue_iterator;
125  for (queue_iterator qit = queues.begin();
126  qit != queues.end();
127  ++qit)
128  {
129  std::vector<cl_command_queue> const & queues_for_device = qit->second;
130  for (vcl_size_t j=0; j<queues_for_device.size(); ++j)
131  contexts_[i].add_queue(qit->first, queues_for_device[j]);
132  }
133 
134  initialized_[i] = true;
135  }
136  }
137 
145  static void setup_context(long i, cl_context c, std::vector<cl_device_id> const & devices, std::vector<cl_command_queue> const & queue)
146  {
147  assert(devices.size() == queue.size() && bool("ViennaCL expects one queue per device!"));
148 
149  //wrap queue vector into map
150  std::map< cl_device_id, std::vector<cl_command_queue> > queues_map;
151  for (vcl_size_t j = 0; j<devices.size(); ++j)
152  queues_map[devices[j]].push_back(queue[j]);
153 
154  setup_context(i, c, devices, queues_map);
155  }
156 
158  static void set_context_device_type(long i, cl_device_type t)
159  {
160  contexts_[i].default_device_type(t);
161  }
162 
164  static void set_context_device_num(long i, vcl_size_t num)
165  {
166  contexts_[i].default_device_num(num);
167  }
168 
170  static void set_context_platform_index(long i, vcl_size_t pf_index)
171  {
172  contexts_[i].platform_index(pf_index);
173  }
174 
175  private:
176  static long current_context_id_;
177  static std::map<long, bool> initialized_;
178  static std::map<long, viennacl::ocl::context> contexts_;
179  };
180 
181  template <bool dummy>
182  long backend<dummy>::current_context_id_ = 0;
183 
184  template <bool dummy>
185  std::map<long, bool> backend<dummy>::initialized_;
186 
187  template <bool dummy>
188  std::map<long, viennacl::ocl::context> backend<dummy>::contexts_;
189 
191 
193  {
195  }
196 
198  inline void switch_context(long i)
199  {
201  }
202 
205  {
207  }
208 
210  inline void setup_context(long i,
211  std::vector<cl_device_id> const & devices)
212  {
214  }
215 
217  inline void setup_context(long i,
219  {
220  std::vector<cl_device_id> device_id_array(1);
221  device_id_array[0] = device.id();
222  viennacl::ocl::backend<>::setup_context(i, device_id_array);
223  }
224 
226  inline void setup_context(long i,
227  cl_context c,
228  std::vector<cl_device_id> const & devices,
229  std::map< cl_device_id, std::vector<cl_command_queue> > const & queues)
230  {
231  viennacl::ocl::backend<>::setup_context(i, c, devices, queues);
232  }
233 
235  inline void setup_context(long i, cl_context c, std::vector<cl_device_id> const & devices, std::vector<cl_command_queue> const & queues)
236  {
237  viennacl::ocl::backend<>::setup_context(i, c, devices, queues);
238  }
239 
241  inline void setup_context(long i, cl_context c, cl_device_id d, cl_command_queue q)
242  {
243  std::vector<cl_device_id> devices(1);
244  std::vector<cl_command_queue> queues(1);
245  devices[0] = d;
246  queues[0] = q;
247  viennacl::ocl::backend<>::setup_context(i, c, devices, queues);
248  }
249 
251  inline void set_context_device_type(long i, cl_device_type dev_type)
252  {
254  }
255 
258  {
259  set_context_device_type(i, CL_DEVICE_TYPE_GPU);
260  }
261 
264  {
265  set_context_device_type(i, CL_DEVICE_TYPE_CPU);
266  }
267 
270  {
271  set_context_device_type(i, CL_DEVICE_TYPE_DEFAULT);
272  }
273 
276  {
277  set_context_device_type(i, CL_DEVICE_TYPE_ACCELERATOR);
278  }
279 
281  inline void set_context_device_num(long i, vcl_size_t num)
282  {
284  }
285 
286 
292  inline void set_context_platform_index(long i, vcl_size_t pf_index)
293  {
295  }
296 
298 
300  {
302  }
303 
305  inline viennacl::ocl::command_queue & get_queue(viennacl::ocl::device d, unsigned int queue_id = 0)
306  {
307  return viennacl::ocl::current_context().get_queue(d.id(), queue_id);
308  }
309 
311  inline viennacl::ocl::command_queue & get_queue(cl_device_id dev_id, unsigned int queue_id = 0)
312  {
313  return viennacl::ocl::current_context().get_queue(dev_id, queue_id);
314  }
315 
316 
318  inline viennacl::ocl::kernel & get_kernel(std::string const & prog_name, std::string const & kernel_name)
319  {
320  return viennacl::ocl::current_context().get_program(prog_name).get_kernel(kernel_name);
321  }
322 
325  {
327  }
328 
331  {
333  }
334 
335  } //ocl
336 } //viennacl
337 #endif
A tag identifying OpenCL devices as GPUs.
Definition: forwards.h:35
std::size_t vcl_size_t
Definition: forwards.h:58
viennacl::ocl::kernel & get_kernel(std::string const &prog_name, std::string const &kernel_name)
Convenience function for getting the kernel for a particular program from the current active context...
Definition: backend.hpp:318
static void set_context_device_num(long i, vcl_size_t num)
Sets the maximum number of devices per context. Ignored if a device array is provided as well...
Definition: backend.hpp:164
Represents an OpenCL kernel within ViennaCL.
Definition: kernel.hpp:59
Manages an OpenCL context and provides the respective convenience functions for creating buffers...
Definition: context.hpp:51
A tag identifying OpenCL devices as CPUs.
Definition: forwards.h:37
A class representing a compute device (e.g. a GPU)
Definition: device.hpp:49
void switch_device(vcl_size_t i)
Switches the current device to the i-th device in this context.
Definition: context.hpp:102
A class representing a command queue.
Definition: command_queue.hpp:45
static viennacl::ocl::command_queue & get_queue()
Returns the current queue for the active device in the active context.
Definition: backend.hpp:75
static void set_context_platform_index(long i, vcl_size_t pf_index)
Sets the context device type.
Definition: backend.hpp:170
viennacl::ocl::program & get_program(std::string const &name)
Returns the program with the provided name.
Definition: context.hpp:414
viennacl::ocl::device const & current_device() const
Returns the current device.
Definition: context.hpp:95
viennacl::ocl::device const & current_device()
Convenience function for returning the active device in the current context.
Definition: backend.hpp:330
void switch_device(viennacl::ocl::device &d)
Convenience function for switching the active device in the current context.
Definition: backend.hpp:324
viennacl::ocl::command_queue & get_queue()
Definition: context.hpp:249
cl_device_id id() const
Returns the OpenCL device id.
Definition: device.hpp:981
static viennacl::ocl::context & context(long id)
Returns the current active context.
Definition: backend.hpp:49
viennacl::ocl::command_queue & get_queue()
Convenience function for getting the default queue for the currently active device in the active cont...
Definition: backend.hpp:299
A tag identifying OpenCL devices as accelerators (e.g. Intel Xeon Phi)
Definition: forwards.h:39
static viennacl::ocl::context & current_context()
Returns the current active context.
Definition: backend.hpp:69
viennacl::ocl::context & current_context()
Convenience function for returning the current context.
Definition: backend.hpp:192
A backend that provides contexts for ViennaCL objects (vector, matrix, etc.)
Definition: backend.hpp:36
Represents an OpenCL context within ViennaCL.
void switch_context(long i)
Convenience function for switching the current context.
Definition: backend.hpp:198
static void switch_context(long i)
Switches the current context to the context identified by i.
Definition: backend.hpp:43
viennacl::ocl::kernel & get_kernel(std::string const &name)
Returns the kernel with the provided name.
Definition: context.hpp:638
static void setup_context(long i, cl_context c, std::vector< cl_device_id > const &devices, std::map< cl_device_id, std::vector< cl_command_queue > > const &queues)
Initializes ViennaCL with an already existing context.
Definition: backend.hpp:105
Enqueues kernels into command queues.
static void setup_context(long i, std::vector< cl_device_id > const &devices)
Sets a number of devices for the context.
Definition: backend.hpp:85
static void setup_context(long i, cl_context c, std::vector< cl_device_id > const &devices, std::vector< cl_command_queue > const &queue)
Initializes ViennaCL with an already existing context.
Definition: backend.hpp:145
static void set_context_device_type(long i, cl_device_type t)
Sets the context device type.
Definition: backend.hpp:158
void set_context_platform_index(long i, vcl_size_t pf_index)
Convenience function for setting the platform index.
Definition: backend.hpp:292
A tag denoting the default OpenCL device type (SDK-specific)
Definition: forwards.h:41
viennacl::ocl::context & get_context(long i)
Convenience function for returning the current context.
Definition: backend.hpp:204
void set_context_device_type(long i, cl_device_type dev_type)
Convenience function for setting the default device type for a context.
Definition: backend.hpp:251
void setup_context(long i, std::vector< cl_device_id > const &devices)
Convenience function for setting devices for a context.
Definition: backend.hpp:210
void set_context_device_num(long i, vcl_size_t num)
Convenience function for setting the number of default devices per context.
Definition: backend.hpp:281