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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
//---------------------------------------------------------------------------//
// Copyright (c) 2013 Kyle Lutz <kyle.r.lutz@gmail.com>
//
// Distributed under the Boost Software License, Version 1.0
// See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt
//
// See http://boostorg.github.com/compute for more information.
//---------------------------------------------------------------------------//
#ifndef BOOST_COMPUTE_SYSTEM_HPP
#define BOOST_COMPUTE_SYSTEM_HPP
#include <string>
#include <vector>
#include <cstdlib>
#include <boost/throw_exception.hpp>
#if defined(BOOST_COMPUTE_THREAD_SAFE)
# if defined(BOOST_COMPUTE_USE_CPP11)
# include <mutex>
# include <thread>
# include <atomic>
# else
# include <boost/thread/mutex.hpp>
# include <boost/thread/lock_guard.hpp>
# include <boost/atomic.hpp>
# endif
#endif
#include <boost/compute/cl.hpp>
#include <boost/compute/device.hpp>
#include <boost/compute/context.hpp>
#include <boost/compute/platform.hpp>
#include <boost/compute/command_queue.hpp>
#include <boost/compute/detail/getenv.hpp>
#include <boost/compute/exception/no_device_found.hpp>
#include <boost/compute/exception/context_error.hpp>
namespace boost {
namespace compute {
/// \class system
/// \brief Provides access to platforms and devices on the system.
///
/// The system class contains a set of static functions which provide access to
/// the OpenCL platforms and compute devices on the host system.
///
/// The default_device() convenience method automatically selects and returns
/// the "best" compute device for the system following a set of heuristics and
/// environment variables. This simplifies setup of the OpenCL enviornment.
///
/// \see platform, device, context
class system
{
public:
/// Returns the default compute device for the system.
///
/// The default device is selected based on a set of heuristics and can be
/// influenced using one of the following environment variables:
///
/// \li \c BOOST_COMPUTE_DEFAULT_DEVICE -
/// name of the compute device (e.g. "GTX TITAN")
/// \li \c BOOST_COMPUTE_DEFAULT_DEVICE_TYPE
/// type of the compute device (e.g. "GPU" or "CPU")
/// \li \c BOOST_COMPUTE_DEFAULT_PLATFORM -
/// name of the platform (e.g. "NVIDIA CUDA")
/// \li \c BOOST_COMPUTE_DEFAULT_VENDOR -
/// name of the device vendor (e.g. "NVIDIA")
/// \li \c BOOST_COMPUTE_DEFAULT_ENFORCE -
/// If this is set to "1", then throw a no_device_found() exception
/// if any of the above environment variables is set, but a matching
/// device was not found.
///
/// The default device is determined once on the first time this function
/// is called. Calling this function multiple times will always result in
/// the same device being returned.
///
/// If no OpenCL device is found on the system, a no_device_found exception
/// is thrown.
///
/// For example, to print the name of the default compute device on the
/// system:
/// \code
/// // get the default compute device
/// boost::compute::device device = boost::compute::system::default_device();
///
/// // print the name of the device
/// std::cout << "default device: " << device.name() << std::endl;
/// \endcode
static device default_device()
{
return init_default_device();
}
/// Returns the device with \p name.
///
/// \throws no_device_found if no device with \p name is found.
static device find_device(const std::string &name)
{
const std::vector<device> devices = system::devices();
for(size_t i = 0; i < devices.size(); i++){
const device& device = devices[i];
if(device.name() == name){
return device;
}
}
BOOST_THROW_EXCEPTION(no_device_found());
}
/// Returns a vector containing all of the compute devices on
/// the system.
///
/// For example, to print out the name of each OpenCL-capable device
/// available on the system:
/// \code
/// for(const auto &device : boost::compute::system::devices()){
/// std::cout << device.name() << std::endl;
/// }
/// \endcode
static std::vector<device> devices()
{
std::vector<device> devices;
const std::vector<platform> platforms = system::platforms();
for(size_t i = 0; i < platforms.size(); i++){
const std::vector<device> platform_devices = platforms[i].devices();
devices.insert(
devices.end(), platform_devices.begin(), platform_devices.end()
);
}
return devices;
}
/// Returns the number of compute devices on the system.
static size_t device_count()
{
size_t count = 0;
const std::vector<platform> platforms = system::platforms();
for(size_t i = 0; i < platforms.size(); i++){
count += platforms[i].device_count();
}
return count;
}
/// Returns the default context for the system.
///
/// The default context is created for the default device on the system
/// (as returned by default_device()).
///
/// The default context is created once on the first time this function is
/// called. Calling this function multiple times will always result in the
/// same context object being returned.
static context default_context()
{
return init_default_context();
}
/// Returns the default command queue for the system.
///
/// If user-provided command queue is given, the system-wide default context
/// and default device will be set up appropriately so that the default queue
/// matches the default context and device.
///
/// If the OpenCL context and device associated with user-provided command queue
/// does not match the default context and device that have already been set,
/// a set_default_queue_error exception is thrown. For example:
///
/// \snippet test/test_attach_user_queue_error.cpp queue_mismatch
///
/// The default queue is created once on the first time this function is
/// called. Calling this function multiple times will always result in the
/// same command queue object being returned.
static command_queue& default_queue(const command_queue &user_queue = command_queue())
{
return init_default_queue(user_queue);
}
/// Blocks until all outstanding computations on the default
/// command queue are complete.
///
/// This is equivalent to:
/// \code
/// system::default_queue().finish();
/// \endcode
static void finish()
{
default_queue().finish();
}
/// Returns a vector containing each of the OpenCL platforms on the system.
///
/// For example, to print out the name of each OpenCL platform present on
/// the system:
/// \code
/// for(const auto &platform : boost::compute::system::platforms()){
/// std::cout << platform.name() << std::endl;
/// }
/// \endcode
static std::vector<platform> platforms()
{
cl_uint count = 0;
clGetPlatformIDs(0, 0, &count);
std::vector<platform> platforms;
if(count > 0)
{
std::vector<cl_platform_id> platform_ids(count);
clGetPlatformIDs(count, &platform_ids[0], 0);
for(size_t i = 0; i < platform_ids.size(); i++){
platforms.push_back(platform(platform_ids[i]));
}
}
return platforms;
}
/// Returns the number of compute platforms on the system.
static size_t platform_count()
{
cl_uint count = 0;
clGetPlatformIDs(0, 0, &count);
return static_cast<size_t>(count);
}
private:
/// \internal_
static device find_default_device()
{
// get a list of all devices on the system
const std::vector<device> devices_ = devices();
if(devices_.empty()){
BOOST_THROW_EXCEPTION(no_device_found());
}
// check for device from environment variable
const char *name = detail::getenv("BOOST_COMPUTE_DEFAULT_DEVICE");
const char *type = detail::getenv("BOOST_COMPUTE_DEFAULT_DEVICE_TYPE");
const char *platform = detail::getenv("BOOST_COMPUTE_DEFAULT_PLATFORM");
const char *vendor = detail::getenv("BOOST_COMPUTE_DEFAULT_VENDOR");
const char *enforce = detail::getenv("BOOST_COMPUTE_DEFAULT_ENFORCE");
if(name || type || platform || vendor){
for(size_t i = 0; i < devices_.size(); i++){
const device& device = devices_[i];
if (name && !matches(device.name(), name))
continue;
if (type && matches(std::string("GPU"), type))
if (!(device.type() & device::gpu))
continue;
if (type && matches(std::string("CPU"), type))
if (!(device.type() & device::cpu))
continue;
if (platform && !matches(device.platform().name(), platform))
continue;
if (vendor && !matches(device.vendor(), vendor))
continue;
return device;
}
if(enforce && enforce[0] == '1')
BOOST_THROW_EXCEPTION(no_device_found());
}
// find the first gpu device
for(size_t i = 0; i < devices_.size(); i++){
const device& device = devices_[i];
if(device.type() & device::gpu){
return device;
}
}
// find the first cpu device
for(size_t i = 0; i < devices_.size(); i++){
const device& device = devices_[i];
if(device.type() & device::cpu){
return device;
}
}
// return the first device found
return devices_[0];
}
/// \internal_
static bool matches(const std::string &str, const std::string &pattern)
{
return str.find(pattern) != std::string::npos;
}
/// \internal_
static device init_default_device(const device &user_device = device())
{
static device default_device;
#ifdef BOOST_COMPUTE_THREAD_SAFE
#ifdef BOOST_COMPUTE_USE_CPP11
using namespace std;
#else
using namespace boost;
#endif
static atomic<bool> is_init;
static mutex init_mutex;
bool is_init_value = is_init.load(memory_order_consume);
if (!is_init_value)
{
lock_guard<mutex> lock(init_mutex);
is_init_value = is_init.load(memory_order_consume);
if (!is_init_value)
{
default_device = user_device.get() ?
user_device : find_default_device();
is_init.store(true, memory_order_release);
}
}
#else // BOOST_COMPUTE_THREAD_SAFE
if (!default_device.get())
{
default_device = user_device.get() ?
user_device : find_default_device();
}
#endif // BOOST_COMPUTE_THREAD_SAFE
return default_device;
}
/// \internal_
static context init_default_context(const context &user_context = context())
{
static context default_context;
#ifdef BOOST_COMPUTE_THREAD_SAFE
#ifdef BOOST_COMPUTE_USE_CPP11
using namespace std;
#else
using namespace boost;
#endif
static atomic<bool> is_init;
static mutex init_mutex;
bool is_init_value = is_init.load(memory_order_consume);
if (!is_init_value)
{
lock_guard<mutex> lock(init_mutex);
is_init_value = is_init.load(memory_order_consume);
if (!is_init_value)
{
default_context = user_context.get() ?
user_context : context(default_device());
is_init.store(true, memory_order_release);
}
}
#else // BOOST_COMPUTE_THREAD_SAFE
if (!default_context.get())
{
default_context = user_context.get() ?
user_context : context(default_device());
}
#endif // BOOST_COMPUTE_THREAD_SAFE
return default_context;
}
/// \internal_
static void init_default_device_and_context(const command_queue &user_queue)
{
device user_device = user_queue.get_device();
context user_context = user_queue.get_context();
if ( (user_device != init_default_device(user_device)) ||
(user_context != init_default_context(user_context)) )
{
// Try invoking default_queue() before anything else
BOOST_THROW_EXCEPTION(set_default_queue_error());
}
}
/// \internal_
static command_queue& init_default_queue(const command_queue &user_queue = command_queue())
{
static command_queue default_queue;
#ifdef BOOST_COMPUTE_THREAD_SAFE
#ifdef BOOST_COMPUTE_USE_CPP11
using namespace std;
#else
using namespace boost;
#endif
static atomic<bool> is_init;
static mutex init_mutex;
bool is_init_value = is_init.load(memory_order_consume);
if (!is_init_value)
{
lock_guard<mutex> lock(init_mutex);
is_init_value = is_init.load(memory_order_consume);
if (!is_init_value)
{
if (user_queue.get())
init_default_device_and_context(user_queue);
default_queue = user_queue.get() ?
user_queue :
command_queue(default_context(), default_device());
is_init.store(true, memory_order_release);
}
}
#else // BOOST_COMPUTE_THREAD_SAFE
if (!default_queue.get())
{
if (user_queue.get())
init_default_device_and_context(user_queue);
default_queue = user_queue.get() ?
user_queue :
command_queue(default_context(), default_device());
}
#endif // BOOST_COMPUTE_THREAD_SAFE
else
{
BOOST_ASSERT_MSG(user_queue.get() == 0,
"Default command queue has already been set.");
}
return default_queue;
}
};
} // end compute namespace
} // end boost namespace
#endif // BOOST_COMPUTE_SYSTEM_HPP