46 #define __CL_ENABLE_EXCEPTIONS
48 #undef __CL_ENABLE_EXCEPTIONS
50 #include <pvsutil/Assert.h>
51 #include <pvsutil/Logger.h>
53 #include "SkelCL/detail/DeviceList.h"
55 #include "SkelCL/detail/Device.h"
56 #include "SkelCL/detail/DeviceID.h"
57 #include "SkelCL/detail/DeviceProperties.h"
58 #include "SkelCL/detail/PlatformID.h"
59 #include "SkelCL/detail/skelclDll.h"
65 SKELCL_DLL DeviceList globalDeviceList;
67 DeviceList::DeviceList()
72 DeviceList::DeviceList(std::initializer_list<std::shared_ptr<Device>> list)
73 : _devices(list.begin(), list.end())
77 bool DeviceList::operator==(
const DeviceList& rhs)
const
79 return _devices == rhs._devices;
84 ASSERT(_devices.empty());
86 std::vector<cl::Platform> platforms;
87 cl::Platform::get(&platforms);
88 ASSERT(platforms.size() > 0);
90 LOG_INFO(platforms.size(),
" OpenCL platform(s) found");
97 std::vector<cl::Device> devices;
98 platform.getDevices(CL_DEVICE_TYPE_ALL, &devices);
100 LOG_INFO(devices.size(),
" device(s) for OpenCL platform `",
101 platform.getInfo<CL_PLATFORM_NAME>(),
"' found");
103 for (
auto&
device : devices) {
105 if (!properties.matchAndTake(
device)) {
106 LOG_INFO(
"Skip device `",
device.getInfo<CL_DEVICE_NAME>(),
107 "' not machting given criteria for device selection.");
111 _devices.push_back( std::make_shared<Device>(
device,
118 }
catch (cl::Error& err) {
119 ABORT_WITH_ERROR(err);
121 ASSERT_MESSAGE(!_devices.empty(),
"None OpenCL device was selected.");
122 LOG_INFO(
"Using ", _devices.size(),
" OpenCL device(s) in total");
127 ASSERT(_devices.empty());
129 std::vector<cl::Platform> platforms;
130 cl::Platform::get(&platforms);
131 if (platforms.size() <= pID.id()) {
132 throw std::invalid_argument(
"Given PlatformID is invalid");
135 auto&
platform = platforms[pID.id()];
137 std::vector<cl::Device> devices;
138 platform.getDevices(CL_DEVICE_TYPE_ALL, &devices);
139 if (devices.size() <= dID.id()) {
140 throw std::invalid_argument(
"Given DeviceID is invalid");
143 auto&
device = devices[dID.id()];
145 _devices.push_back( std::make_shared<Device>(
device,
150 }
catch (cl::Error& err) {
151 ABORT_WITH_ERROR(err);
152 }
catch (std::invalid_argument& ia) {
153 LOG_ERROR(
"Invalid arguments provided: ", ia.what());
156 LOG_INFO(
"Using ", _devices.size(),
" OpenCL device(s) in total");
159 void DeviceList::clear()
164 void DeviceList::barrier()
const
167 std::for_each( _devices.begin(), _devices.end(),
168 std::mem_fn(&Device::wait) );
169 }
catch (cl::Error& err) {
170 ABORT_WITH_ERROR(err);
172 LOG_DEBUG_INFO(
"Finished waiting for ", _devices.size(),
" devices");
175 DeviceList::const_iterator DeviceList::begin()
const
177 return _devices.begin();
180 DeviceList::const_iterator DeviceList::end()
const
182 return _devices.end();
185 DeviceList::const_reverse_iterator DeviceList::rbegin()
const
187 return _devices.rbegin();
190 DeviceList::const_reverse_iterator DeviceList::rend()
const
192 return _devices.rend();
195 DeviceList::size_type DeviceList::size()
const
197 return _devices.size();
200 bool DeviceList::empty()
const
202 return _devices.empty();
205 DeviceList::const_reference DeviceList::operator[](size_type n)
const
207 return _devices.operator[](n);
210 DeviceList::const_reference DeviceList::at(size_type n)
const
212 return _devices.at(n);
215 DeviceList::const_reference DeviceList::front()
const
217 return _devices.front();
220 DeviceList::const_reference DeviceList::back()
const
222 return _devices.back();
SKELCL_DLL void init(detail::DeviceProperties properties=allDevices())
Initializes the SkelCL library. This function (or another init function) has to be called prior to ev...
SKELCL_DLL detail::DeviceID device(size_t dID)
Creates an OpenCL device ID to be used as parameter of the init(detail::PlatformID, detail::DeviceID) function.
SKELCL_DLL detail::PlatformID platform(size_t pID)
Creates an OpenCL platform ID to be used as parameter of the init(detail::PlatformID, detail::DeviceID) function.