All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
backend.hpp
Go to the documentation of this file.
#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
============================================================================= */
#include <vector>
#include "viennacl/ocl/context.hpp"
#include "viennacl/ocl/enqueue.hpp"
namespace viennacl
{
namespace ocl
{
template<bool dummy = false> //never use parameter other than default (introduced for linkage issues only)
class backend
{
public:
static void switch_context(long i)
{
current_context_id_ = i;
}
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];
}
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_);
}
static viennacl::ocl::command_queue & get_queue()
{
return current_context().get_queue();
}
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]);
}
}
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;
}
}
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);
}
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;
}
static void set_context_device_type(long i, cl_device_type t)
{
contexts_[i].default_device_type(t);
}
static void set_context_device_num(long i, vcl_size_t num)
{
contexts_[i].default_device_num(num);
}
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_;
inline viennacl::ocl::context & current_context()
{
return viennacl::ocl::backend<>::current_context();
}
inline void switch_context(long i)
{
viennacl::ocl::backend<>::switch_context(i);
}
inline viennacl::ocl::context & get_context(long i)
{
return viennacl::ocl::backend<>::context(i);
}
inline void setup_context(long i,
std::vector<cl_device_id> const & devices)
{
viennacl::ocl::backend<>::setup_context(i, devices);
}
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);
}
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);
}
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);
}
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);
}
inline void set_context_device_type(long i, cl_device_type dev_type)
{
viennacl::ocl::backend<>::set_context_device_type(i, dev_type);
}
inline void set_context_device_type(long i, viennacl::ocl::gpu_tag)
{
set_context_device_type(i, CL_DEVICE_TYPE_GPU);
}
inline void set_context_device_type(long i, viennacl::ocl::cpu_tag)
{
set_context_device_type(i, CL_DEVICE_TYPE_CPU);
}
inline void set_context_device_type(long i, viennacl::ocl::default_tag)
{
set_context_device_type(i, CL_DEVICE_TYPE_DEFAULT);
}
inline void set_context_device_type(long i, viennacl::ocl::accelerator_tag)
{
set_context_device_type(i, CL_DEVICE_TYPE_ACCELERATOR);
}
inline void set_context_device_num(long i, vcl_size_t num)
{
viennacl::ocl::backend<>::set_context_device_num(i, num);
}
inline void set_context_platform_index(long i, vcl_size_t pf_index)
{
viennacl::ocl::backend<>::set_context_platform_index(i, pf_index);
}
inline viennacl::ocl::command_queue & get_queue()
{
return viennacl::ocl::current_context().get_queue();
}
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);
}
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);
}
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);
}
inline void switch_device(viennacl::ocl::device & d)
{
viennacl::ocl::current_context().switch_device(d);
}
inline viennacl::ocl::device const & current_device()
{
return viennacl::ocl::current_context().current_device();
}
} //ocl
} //viennacl
#endif
viennacl::ocl::gpu_tag
A tag identifying OpenCL devices as GPUs.
Definition: forwards.h:35
viennacl::ocl::context::current_device
viennacl::ocl::device const & current_device() const
Returns the current device.
Definition: context.hpp:112
viennacl::ocl::device::id
cl_device_id id() const
Returns the OpenCL device id.
Definition: device.hpp:981
viennacl::ocl::get_kernel
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:339
viennacl::ocl::backend::setup_context
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:155
viennacl::ocl::context::switch_device
void switch_device(vcl_size_t i)
Switches the current device to the i-th device in this context.
Definition: context.hpp:119
viennacl::ocl::backend::current_context
static viennacl::ocl::context & current_context()
Returns the current active context.
Definition: backend.hpp:73
viennacl::ocl::context::get_queue
viennacl::ocl::command_queue & get_queue()
Definition: context.hpp:266
viennacl::ocl::kernel
Represents an OpenCL kernel within ViennaCL.
Definition: kernel.hpp:58
viennacl::ocl::context::get_program
viennacl::ocl::program & get_program(std::string const &name)
Returns the program with the provided name.
Definition: context.hpp:532
viennacl::ocl::backend::set_context_device_num
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:185
viennacl::ocl::current_context
viennacl::ocl::context & current_context()
Convenience function for returning the current context.
Definition: backend.hpp:213
viennacl::ocl::context
Manages an OpenCL context and provides the respective convenience functions for creating buffers...
Definition: context.hpp:55
viennacl::ocl::cpu_tag
A tag identifying OpenCL devices as CPUs.
Definition: forwards.h:37
viennacl::ocl::device
A class representing a compute device (e.g. a GPU)
Definition: device.hpp:49
viennacl::ocl::backend::switch_context
static void switch_context(long i)
Switches the current context to the context identified by i.
Definition: backend.hpp:43
viennacl::ocl::command_queue
A class representing a command queue.
Definition: command_queue.hpp:45
viennacl::ocl::current_device
viennacl::ocl::device const & current_device()
Convenience function for returning the active device in the current context.
Definition: backend.hpp:351
viennacl::ocl::context::program_num
vcl_size_t program_num()
Returns the number of programs within this context.
Definition: context.hpp:602
viennacl::ocl::backend::set_context_platform_index
static void set_context_platform_index(long i, vcl_size_t pf_index)
Sets the context device type.
Definition: backend.hpp:191
viennacl::ocl::context::handle
const viennacl::ocl::handle< cl_context > & handle() const
Returns the context handle.
Definition: context.hpp:611
viennacl::ocl::backend::add_context
static void add_context(long i, viennacl::ocl::context &c)
Add an existing context object to the backend.
Definition: backend.hpp:168
viennacl::ocl::switch_device
void switch_device(viennacl::ocl::device &d)
Convenience function for switching the active device in the current context.
Definition: backend.hpp:345
viennacl::ocl::backend::setup_context
static void setup_context(long i, std::vector< cl_device_id > const &devices)
Sets a number of devices for the context.
Definition: backend.hpp:95
viennacl::ocl::get_queue
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:320
viennacl::ocl::accelerator_tag
A tag identifying OpenCL devices as accelerators (e.g. Intel Xeon Phi)
Definition: forwards.h:39
viennacl::ocl::backend
A backend that provides contexts for ViennaCL objects (vector, matrix, etc.)
Definition: backend.hpp:36
viennacl::vcl_size_t
std::size_t vcl_size_t
Definition: forwards.h:75
context.hpp
Represents an OpenCL context within ViennaCL.
viennacl::ocl::backend::setup_context
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:115
viennacl::ocl::backend::context
static viennacl::ocl::context & context(long id)
Returns the current active context.
Definition: backend.hpp:49
viennacl::ocl::switch_context
void switch_context(long i)
Convenience function for switching the current context.
Definition: backend.hpp:219
viennacl::ocl::backend::get_queue
static viennacl::ocl::command_queue & get_queue()
Returns the current queue for the active device in the active context.
Definition: backend.hpp:85
enqueue.hpp
Enqueues kernels into command queues.
viennacl::ocl::program::get_kernel
viennacl::ocl::kernel & get_kernel(std::string const &name)
Returns the kernel with the provided name.
Definition: context.hpp:773
viennacl::ocl::set_context_platform_index
void set_context_platform_index(long i, vcl_size_t pf_index)
Convenience function for setting the platform index.
Definition: backend.hpp:313
viennacl::ocl::default_tag
A tag denoting the default OpenCL device type (SDK-specific)
Definition: forwards.h:41
viennacl::ocl::backend::set_context_device_type
static void set_context_device_type(long i, cl_device_type t)
Sets the context device type.
Definition: backend.hpp:179
viennacl::ocl::get_context
viennacl::ocl::context & get_context(long i)
Convenience function for returning the current context.
Definition: backend.hpp:225
viennacl::ocl::set_context_device_type
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:272
viennacl::ocl::setup_context
void setup_context(long i, std::vector< cl_device_id > const &devices)
Convenience function for setting devices for a context.
Definition: backend.hpp:231
viennacl::ocl::set_context_device_num
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:302