File indexing completed on 2024-11-15 09:04:32
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #ifndef BOOST_COMPUTE_SYSTEM_HPP
0012 #define BOOST_COMPUTE_SYSTEM_HPP
0013
0014 #include <string>
0015 #include <vector>
0016 #include <cstdlib>
0017
0018 #include <boost/throw_exception.hpp>
0019
0020 #include <boost/compute/cl.hpp>
0021 #include <boost/compute/device.hpp>
0022 #include <boost/compute/context.hpp>
0023 #include <boost/compute/platform.hpp>
0024 #include <boost/compute/command_queue.hpp>
0025 #include <boost/compute/detail/getenv.hpp>
0026 #include <boost/compute/exception/no_device_found.hpp>
0027
0028 namespace boost {
0029 namespace compute {
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042 class system
0043 {
0044 public:
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069
0070
0071
0072
0073
0074
0075
0076
0077
0078
0079 static device default_device()
0080 {
0081 static device default_device = find_default_device();
0082
0083 return default_device;
0084 }
0085
0086
0087
0088
0089 static device find_device(const std::string &name)
0090 {
0091 const std::vector<device> devices = system::devices();
0092 for(size_t i = 0; i < devices.size(); i++){
0093 const device& device = devices[i];
0094
0095 if(device.name() == name){
0096 return device;
0097 }
0098 }
0099
0100 BOOST_THROW_EXCEPTION(no_device_found());
0101 }
0102
0103
0104
0105
0106
0107
0108
0109
0110
0111
0112
0113 static std::vector<device> devices()
0114 {
0115 std::vector<device> devices;
0116
0117 const std::vector<platform> platforms = system::platforms();
0118 for(size_t i = 0; i < platforms.size(); i++){
0119 const std::vector<device> platform_devices = platforms[i].devices();
0120
0121 devices.insert(
0122 devices.end(), platform_devices.begin(), platform_devices.end()
0123 );
0124 }
0125
0126 return devices;
0127 }
0128
0129
0130 static size_t device_count()
0131 {
0132 size_t count = 0;
0133
0134 const std::vector<platform> platforms = system::platforms();
0135 for(size_t i = 0; i < platforms.size(); i++){
0136 count += platforms[i].device_count();
0137 }
0138
0139 return count;
0140 }
0141
0142
0143
0144
0145
0146
0147
0148
0149
0150 static context default_context()
0151 {
0152 static context default_context(default_device());
0153
0154 return default_context;
0155 }
0156
0157
0158 static command_queue& default_queue()
0159 {
0160 static command_queue queue(default_context(), default_device());
0161
0162 return queue;
0163 }
0164
0165
0166
0167
0168
0169
0170
0171
0172 static void finish()
0173 {
0174 default_queue().finish();
0175 }
0176
0177
0178
0179
0180
0181
0182
0183
0184
0185
0186 static std::vector<platform> platforms()
0187 {
0188 cl_uint count = 0;
0189 clGetPlatformIDs(0, 0, &count);
0190
0191 std::vector<platform> platforms;
0192 if(count > 0)
0193 {
0194 std::vector<cl_platform_id> platform_ids(count);
0195 clGetPlatformIDs(count, &platform_ids[0], 0);
0196
0197 for(size_t i = 0; i < platform_ids.size(); i++){
0198 platforms.push_back(platform(platform_ids[i]));
0199 }
0200 }
0201 return platforms;
0202 }
0203
0204
0205 static size_t platform_count()
0206 {
0207 cl_uint count = 0;
0208 clGetPlatformIDs(0, 0, &count);
0209 return static_cast<size_t>(count);
0210 }
0211
0212 private:
0213
0214 static device find_default_device()
0215 {
0216
0217 const std::vector<device> devices_ = devices();
0218 if(devices_.empty()){
0219 BOOST_THROW_EXCEPTION(no_device_found());
0220 }
0221
0222
0223 const char *name = detail::getenv("BOOST_COMPUTE_DEFAULT_DEVICE");
0224 const char *type = detail::getenv("BOOST_COMPUTE_DEFAULT_DEVICE_TYPE");
0225 const char *platform = detail::getenv("BOOST_COMPUTE_DEFAULT_PLATFORM");
0226 const char *vendor = detail::getenv("BOOST_COMPUTE_DEFAULT_VENDOR");
0227 const char *enforce = detail::getenv("BOOST_COMPUTE_DEFAULT_ENFORCE");
0228
0229 if(name || type || platform || vendor){
0230 for(size_t i = 0; i < devices_.size(); i++){
0231 const device& device = devices_[i];
0232 if (name && !matches(device.name(), name))
0233 continue;
0234
0235 if (type && matches(std::string("GPU"), type))
0236 if (!(device.type() & device::gpu))
0237 continue;
0238
0239 if (type && matches(std::string("CPU"), type))
0240 if (!(device.type() & device::cpu))
0241 continue;
0242
0243 if (platform && !matches(device.platform().name(), platform))
0244 continue;
0245
0246 if (vendor && !matches(device.vendor(), vendor))
0247 continue;
0248
0249 return device;
0250 }
0251
0252 if(enforce && enforce[0] == '1')
0253 BOOST_THROW_EXCEPTION(no_device_found());
0254 }
0255
0256
0257 for(size_t i = 0; i < devices_.size(); i++){
0258 const device& device = devices_[i];
0259
0260 if(device.type() & device::gpu){
0261 return device;
0262 }
0263 }
0264
0265
0266 for(size_t i = 0; i < devices_.size(); i++){
0267 const device& device = devices_[i];
0268
0269 if(device.type() & device::cpu){
0270 return device;
0271 }
0272 }
0273
0274
0275 return devices_[0];
0276 }
0277
0278
0279 static bool matches(const std::string &str, const std::string &pattern)
0280 {
0281 return str.find(pattern) != std::string::npos;
0282 }
0283 };
0284
0285 }
0286 }
0287
0288 #endif