1 #ifndef VIENNACL_OCL_BACKEND_HPP_
2 #define VIENNACL_OCL_BACKEND_HPP_
35 template<
bool dummy = false>
45 current_context_id_ = i;
51 if (!initialized_[
id])
53 #if defined(VIENNACL_DEBUG_ALL) || defined(VIENNACL_DEBUG_CONTEXT)
54 std::cout <<
"ViennaCL: Initializing context no. " <<
id << std::endl;
59 std::vector<viennacl::ocl::device> devices = contexts_[id].devices();
61 contexts_[
id].add_queue(devices[j]);
62 initialized_[id] =
true;
64 #if defined(VIENNACL_DEBUG_ALL) || defined(VIENNACL_DEBUG_CONTEXT)
65 std::cout <<
"ViennaCL: Context no. " <<
id <<
" initialized with " << devices.size() <<
" devices" << std::endl;
66 std::cout <<
"ViennaCL: Device id: " << devices[0].id() << std::endl;
75 #if defined(VIENNACL_DEBUG_ALL) || defined(VIENNACL_DEBUG_CONTEXT)
76 std::cout <<
"ViennaCL: Getting current_context with id " << current_context_id_ << std::endl;
78 #if defined(VIENNACL_NO_CURRENT_CONTEXT)
79 assert(
false &&
bool(
"ViennaCL: current_context called when disabled"));
96 std::vector<cl_device_id>
const & devices)
99 std::cerr <<
"ViennaCL: Warning in init_context(): Providing a list of devices has no effect, because context for ViennaCL is already created!" << std::endl;
104 contexts_[i].add_device(devices[j]);
117 std::vector<cl_device_id>
const & devices,
118 std::map< cl_device_id, std::vector< cl_command_queue > >
const & queues)
120 assert(devices.size() == queues.size() && bool(
"ViennaCL expects one queue per device!"));
123 std::cerr <<
"ViennaCL: Warning in init_context(): Providing a list of devices has no effect, because context for ViennaCL is already created!" << std::endl;
128 contexts_[i].add_device(devices[j]);
131 contexts_[i].init(c);
134 typedef typename std::map< cl_device_id, std::vector< cl_command_queue > >::const_iterator queue_iterator;
135 for (queue_iterator qit = queues.begin();
139 std::vector<cl_command_queue>
const & queues_for_device = qit->second;
140 for (
vcl_size_t j=0; j<queues_for_device.size(); ++j)
141 contexts_[i].add_queue(qit->first, queues_for_device[j]);
144 initialized_[i] =
true;
155 static void setup_context(
long i, cl_context c, std::vector<cl_device_id>
const & devices, std::vector<cl_command_queue>
const & queue)
157 assert(devices.size() == queue.size() && bool(
"ViennaCL expects one queue per device!"));
160 std::map< cl_device_id, std::vector<cl_command_queue> > queues_map;
162 queues_map[devices[j]].push_back(queue[j]);
170 #if defined(VIENNACL_DEBUG_ALL) || defined(VIENNACL_DEBUG_CONTEXT)
171 std::cout <<
"ViennaCL: Adding context '" << c.
handle() <<
"' as id " << i << std::endl;
172 std::cout <<
"ViennaCL: There are " << c.
program_num() <<
" programs" << std::endl;
175 initialized_[i] =
true;
181 contexts_[i].default_device_type(t);
187 contexts_[i].default_device_num(num);
193 contexts_[i].platform_index(pf_index);
197 static long current_context_id_;
198 static std::map<long, bool> initialized_;
199 static std::map<long, viennacl::ocl::context> contexts_;
203 long backend<dummy>::current_context_id_ = 0;
206 std::map<long, bool> backend<dummy>::initialized_;
209 std::map<long, viennacl::ocl::context> backend<dummy>::contexts_;
232 std::vector<cl_device_id>
const & devices)
241 std::vector<cl_device_id> device_id_array(1);
242 device_id_array[0] = device.
id();
249 std::vector<cl_device_id>
const & devices,
250 std::map< cl_device_id, std::vector<cl_command_queue> >
const & queues)
256 inline void setup_context(
long i, cl_context c, std::vector<cl_device_id>
const & devices, std::vector<cl_command_queue>
const & queues)
262 inline void setup_context(
long i, cl_context c, cl_device_id d, cl_command_queue q)
264 std::vector<cl_device_id> devices(1);
265 std::vector<cl_command_queue> queues(1);
A tag identifying OpenCL devices as GPUs.
viennacl::ocl::device const & current_device() const
Returns the current device.
cl_device_id id() const
Returns the OpenCL device id.
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...
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.
void switch_device(vcl_size_t i)
Switches the current device to the i-th device in this context.
static viennacl::ocl::context & current_context()
Returns the current active context.
viennacl::ocl::command_queue & get_queue()
Represents an OpenCL kernel within ViennaCL.
viennacl::ocl::program & get_program(std::string const &name)
Returns the program with the provided name.
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...
viennacl::ocl::context & current_context()
Convenience function for returning the current context.
Manages an OpenCL context and provides the respective convenience functions for creating buffers...
A tag identifying OpenCL devices as CPUs.
A class representing a compute device (e.g. a GPU)
static void switch_context(long i)
Switches the current context to the context identified by i.
A class representing a command queue.
viennacl::ocl::device const & current_device()
Convenience function for returning the active device in the current context.
vcl_size_t program_num()
Returns the number of programs within this context.
static void set_context_platform_index(long i, vcl_size_t pf_index)
Sets the context device type.
const viennacl::ocl::handle< cl_context > & handle() const
Returns the context handle.
static void add_context(long i, viennacl::ocl::context &c)
Add an existing context object to the backend.
void switch_device(viennacl::ocl::device &d)
Convenience function for switching the active device in the current context.
static void setup_context(long i, std::vector< cl_device_id > const &devices)
Sets a number of devices for the context.
viennacl::ocl::command_queue & get_queue()
Convenience function for getting the default queue for the currently active device in the active cont...
A tag identifying OpenCL devices as accelerators (e.g. Intel Xeon Phi)
A backend that provides contexts for ViennaCL objects (vector, matrix, etc.)
Represents an OpenCL context within ViennaCL.
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.
static viennacl::ocl::context & context(long id)
Returns the current active context.
void switch_context(long i)
Convenience function for switching the current context.
static viennacl::ocl::command_queue & get_queue()
Returns the current queue for the active device in the active context.
Enqueues kernels into command queues.
viennacl::ocl::kernel & get_kernel(std::string const &name)
Returns the kernel with the provided name.
void set_context_platform_index(long i, vcl_size_t pf_index)
Convenience function for setting the platform index.
A tag denoting the default OpenCL device type (SDK-specific)
static void set_context_device_type(long i, cl_device_type t)
Sets the context device type.
viennacl::ocl::context & get_context(long i)
Convenience function for returning the current context.
void set_context_device_type(long i, cl_device_type dev_type)
Convenience function for setting the default device type for a context.
void setup_context(long i, std::vector< cl_device_id > const &devices)
Convenience function for setting devices for a context.
void set_context_device_num(long i, vcl_size_t num)
Convenience function for setting the number of default devices per context.