/usr/include/viennacl/ocl/backend.hpp is in libviennacl-dev 1.7.1+dfsg1-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 | #ifndef VIENNACL_OCL_BACKEND_HPP_
#define VIENNACL_OCL_BACKEND_HPP_
/* =========================================================================
Copyright (c) 2010-2016, Institute for Microelectronics,
Institute for Analysis and Scientific Computing,
TU Wien.
Portions of this software are copyright by UChicago Argonne, LLC.
-----------------
ViennaCL - The Vienna Computing Library
-----------------
Project Head: Karl Rupp rupp@iue.tuwien.ac.at
(A list of authors and contributors can be found in the manual)
License: MIT (X11), see file LICENSE in the base directory
============================================================================= */
/** @file viennacl/ocl/backend.hpp
@brief Implementations of the OpenCL backend, where all contexts are stored in.
*/
#include <vector>
#include "viennacl/ocl/context.hpp"
#include "viennacl/ocl/enqueue.hpp"
namespace viennacl
{
namespace ocl
{
/** @brief A backend that provides contexts for ViennaCL objects (vector, matrix, etc.) */
template<bool dummy = false> //never use parameter other than default (introduced for linkage issues only)
class backend
{
public:
/** @brief Switches the current context to the context identified by i
*
* @param i ID of the new active context
*/
static void switch_context(long i)
{
current_context_id_ = i;
}
/** @brief Returns the current active context */
static viennacl::ocl::context & context(long id)
{
if (!initialized_[id])
{
#if defined(VIENNACL_DEBUG_ALL) || defined(VIENNACL_DEBUG_CONTEXT)
std::cout << "ViennaCL: Initializing context no. " << id << std::endl;
#endif
contexts_[id].init();
//create one queue per device:
std::vector<viennacl::ocl::device> devices = contexts_[id].devices();
for (vcl_size_t j = 0; j<devices.size(); ++j)
contexts_[id].add_queue(devices[j]);
initialized_[id] = true;
#if defined(VIENNACL_DEBUG_ALL) || defined(VIENNACL_DEBUG_CONTEXT)
std::cout << "ViennaCL: Context no. " << id << " initialized with " << devices.size() << " devices" << std::endl;
std::cout << "ViennaCL: Device id: " << devices[0].id() << std::endl;
#endif
}
return contexts_[id];
}
/** @brief Returns the current active context */
static viennacl::ocl::context & current_context()
{
#if defined(VIENNACL_DEBUG_ALL) || defined(VIENNACL_DEBUG_CONTEXT)
std::cout << "ViennaCL: Getting current_context with id " << current_context_id_ << std::endl;
#endif
#if defined(VIENNACL_NO_CURRENT_CONTEXT)
assert(false && bool("ViennaCL: current_context called when disabled"));
#endif
return backend<dummy>::context(current_context_id_);
}
/** @brief Returns the current queue for the active device in the active context */
static viennacl::ocl::command_queue & get_queue()
{
return current_context().get_queue();
}
/** @brief Sets a number of devices for the context.
*
* @param i ID of the context to be set up
* @param devices A vector of OpenCL device-IDs that should be added to the context
*/
static void setup_context(long i,
std::vector<cl_device_id> const & devices)
{
if (initialized_[i])
std::cerr << "ViennaCL: Warning in init_context(): Providing a list of devices has no effect, because context for ViennaCL is already created!" << std::endl;
else
{
//set devices for context:
for (vcl_size_t j = 0; j<devices.size(); ++j)
contexts_[i].add_device(devices[j]);
}
}
/** @brief Initializes ViennaCL with an already existing context
*
* @param i ID of the context to be set up
* @param c The OpenCL handle of the existing context
* @param devices A vector of OpenCL device-IDs that should be added to the context
* @param queues A map of queues for each device
*/
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)
{
assert(devices.size() == queues.size() && bool("ViennaCL expects one queue per device!"));
if (initialized_[i])
std::cerr << "ViennaCL: Warning in init_context(): Providing a list of devices has no effect, because context for ViennaCL is already created!" << std::endl;
else
{
//set devices for context:
for (vcl_size_t j = 0; j<devices.size(); ++j)
contexts_[i].add_device(devices[j]);
//init context:
contexts_[i].init(c);
//add queues:
typedef typename std::map< cl_device_id, std::vector< cl_command_queue > >::const_iterator queue_iterator;
for (queue_iterator qit = queues.begin();
qit != queues.end();
++qit)
{
std::vector<cl_command_queue> const & queues_for_device = qit->second;
for (vcl_size_t j=0; j<queues_for_device.size(); ++j)
contexts_[i].add_queue(qit->first, queues_for_device[j]);
}
initialized_[i] = true;
}
}
/** @brief Initializes ViennaCL with an already existing context
*
* @param i ID of the context to be set up
* @param c The OpenCL handle of the existing context
* @param devices A vector of OpenCL device-IDs that should be added to the context
* @param queue One queue per device
*/
static void setup_context(long i, cl_context c, std::vector<cl_device_id> const & devices, std::vector<cl_command_queue> const & queue)
{
assert(devices.size() == queue.size() && bool("ViennaCL expects one queue per device!"));
//wrap queue vector into map
std::map< cl_device_id, std::vector<cl_command_queue> > queues_map;
for (vcl_size_t j = 0; j<devices.size(); ++j)
queues_map[devices[j]].push_back(queue[j]);
setup_context(i, c, devices, queues_map);
}
/** @brief Add an existing context object to the backend */
static void add_context(long i, viennacl::ocl::context& c)
{
#if defined(VIENNACL_DEBUG_ALL) || defined(VIENNACL_DEBUG_CONTEXT)
std::cout << "ViennaCL: Adding context '" << c.handle() << "' as id " << i << std::endl;
std::cout << "ViennaCL: There are " << c.program_num() << " programs" << std::endl;
#endif
contexts_[i] = c;
initialized_[i] = true;
}
/** @brief Sets the context device type */
static void set_context_device_type(long i, cl_device_type t)
{
contexts_[i].default_device_type(t);
}
/** @brief Sets the maximum number of devices per context. Ignored if a device array is provided as well. */
static void set_context_device_num(long i, vcl_size_t num)
{
contexts_[i].default_device_num(num);
}
/** @brief Sets the context device type */
static void set_context_platform_index(long i, vcl_size_t pf_index)
{
contexts_[i].platform_index(pf_index);
}
private:
static long current_context_id_;
static std::map<long, bool> initialized_;
static std::map<long, viennacl::ocl::context> contexts_;
};
template<bool dummy>
long backend<dummy>::current_context_id_ = 0;
template<bool dummy>
std::map<long, bool> backend<dummy>::initialized_;
template<bool dummy>
std::map<long, viennacl::ocl::context> backend<dummy>::contexts_;
////////////////////// current context //////////////////
/** @brief Convenience function for returning the current context */
inline viennacl::ocl::context & current_context()
{
return viennacl::ocl::backend<>::current_context();
}
/** @brief Convenience function for switching the current context */
inline void switch_context(long i)
{
viennacl::ocl::backend<>::switch_context(i);
}
/** @brief Convenience function for returning the current context */
inline viennacl::ocl::context & get_context(long i)
{
return viennacl::ocl::backend<>::context(i);
}
/** @brief Convenience function for setting devices for a context */
inline void setup_context(long i,
std::vector<cl_device_id> const & devices)
{
viennacl::ocl::backend<>::setup_context(i, devices);
}
/** @brief Convenience function for setting devices for a context */
inline void setup_context(long i,
viennacl::ocl::device const & device)
{
std::vector<cl_device_id> device_id_array(1);
device_id_array[0] = device.id();
viennacl::ocl::backend<>::setup_context(i, device_id_array);
}
/** @brief Convenience function for setting up a context in ViennaCL from an existing OpenCL context */
inline 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)
{
viennacl::ocl::backend<>::setup_context(i, c, devices, queues);
}
/** @brief Convenience function for setting up a context in ViennaCL from an existing OpenCL context */
inline void setup_context(long i, cl_context c, std::vector<cl_device_id> const & devices, std::vector<cl_command_queue> const & queues)
{
viennacl::ocl::backend<>::setup_context(i, c, devices, queues);
}
/** @brief Convenience function for setting up a context in ViennaCL from an existing OpenCL context */
inline void setup_context(long i, cl_context c, cl_device_id d, cl_command_queue q)
{
std::vector<cl_device_id> devices(1);
std::vector<cl_command_queue> queues(1);
devices[0] = d;
queues[0] = q;
viennacl::ocl::backend<>::setup_context(i, c, devices, queues);
}
/** @brief Convenience function for setting the default device type for a context */
inline void set_context_device_type(long i, cl_device_type dev_type)
{
viennacl::ocl::backend<>::set_context_device_type(i, dev_type);
}
/** @brief Convenience function for setting the default device type for a context to GPUs */
inline void set_context_device_type(long i, viennacl::ocl::gpu_tag)
{
set_context_device_type(i, CL_DEVICE_TYPE_GPU);
}
/** @brief Convenience function for setting the default device type for a context to CPUs */
inline void set_context_device_type(long i, viennacl::ocl::cpu_tag)
{
set_context_device_type(i, CL_DEVICE_TYPE_CPU);
}
/** @brief Convenience function for setting the default device type for a context to the default OpenCL device type */
inline void set_context_device_type(long i, viennacl::ocl::default_tag)
{
set_context_device_type(i, CL_DEVICE_TYPE_DEFAULT);
}
/** @brief Convenience function for setting the default device type for a context to accelerators */
inline void set_context_device_type(long i, viennacl::ocl::accelerator_tag)
{
set_context_device_type(i, CL_DEVICE_TYPE_ACCELERATOR);
}
/** @brief Convenience function for setting the number of default devices per context */
inline void set_context_device_num(long i, vcl_size_t num)
{
viennacl::ocl::backend<>::set_context_device_num(i, num);
}
/** @brief Convenience function for setting the platform index
*
* @param i Context ID
* @param pf_index The platform index as returned by clGetPlatformIDs(). This is not the ID of type cl_platform_id!
*/
inline void set_context_platform_index(long i, vcl_size_t pf_index)
{
viennacl::ocl::backend<>::set_context_platform_index(i, pf_index);
}
///////////////////////// get queues ///////////////////
/** @brief Convenience function for getting the default queue for the currently active device in the active context */
inline viennacl::ocl::command_queue & get_queue()
{
return viennacl::ocl::current_context().get_queue();
}
/** @brief Convenience function for getting the queue for a particular device in the current active context */
inline viennacl::ocl::command_queue & get_queue(viennacl::ocl::device d, unsigned int queue_id = 0)
{
return viennacl::ocl::current_context().get_queue(d.id(), queue_id);
}
/** @brief Convenience function for getting the queue for a particular device in the current active context */
inline viennacl::ocl::command_queue & get_queue(cl_device_id dev_id, unsigned int queue_id = 0)
{
return viennacl::ocl::current_context().get_queue(dev_id, queue_id);
}
/** @brief Convenience function for getting the kernel for a particular program from the current active context */
inline viennacl::ocl::kernel & get_kernel(std::string const & prog_name, std::string const & kernel_name)
{
return viennacl::ocl::current_context().get_program(prog_name).get_kernel(kernel_name);
}
/** @brief Convenience function for switching the active device in the current context */
inline void switch_device(viennacl::ocl::device & d)
{
viennacl::ocl::current_context().switch_device(d);
}
/** @brief Convenience function for returning the active device in the current context */
inline viennacl::ocl::device const & current_device()
{
return viennacl::ocl::current_context().current_device();
}
} //ocl
} //viennacl
#endif
|