From c306c95bc4384beaaac215a9877db6ad8b32dcde Mon Sep 17 00:00:00 2001 From: Denis Demidov Date: Tue, 26 Mar 2013 12:02:22 +0400 Subject: [PATCH 1/3] BOOST_COMPUTE_DEFAULT_DEVICE accepts substrings of device name BOOST_COMPUTE_DEFAULT_DEVICE="Intel" is much more convenient than BOOST_COMPUTE_DEFAULT_DEVICE="Intel(R) Core(TM) i7 CPU 920 @ 2.67GHz" --- include/boost/compute/system.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/boost/compute/system.hpp b/include/boost/compute/system.hpp index d9f894911..b5e3e092c 100644 --- a/include/boost/compute/system.hpp +++ b/include/boost/compute/system.hpp @@ -80,7 +80,7 @@ class system static device find_device(const std::string &name) { BOOST_FOREACH(const device &device, devices()){ - if(device.name() == name){ + if(device.name().find(name) != std::string::npos){ return device; } } From 160089e64f7fd4efbbdbe0ac56c1f1d9bf9d90e2 Mon Sep 17 00:00:00 2001 From: Denis Demidov Date: Fri, 12 Apr 2013 10:23:06 +0400 Subject: [PATCH 2/3] Adds support for selecting devices with environment variables boost::compute::system::default_device() supports the following environment variables: BOOST_COMPUTE_DEFAULT_DEVICE for device name BOOST_COMPUTE_DEFAULT_PLATFORM for OpenCL platform name BOOST_COMPUTE_DEFAULT_VENDOR for device vendor name If one or more of these variables is set, then device that satisfies all conditions gets selected. If such a device is unavailable, then the first available GPU is selected. If there are no GPUs in the system, then the first available CPU is selected. Otherwise, default_device() returns null device. The hello_world example is modified to use default_device() instead of default_gpu_device(). --- example/hello_world.cpp | 8 ++++---- include/boost/compute/system.hpp | 27 +++++++++++++++++++++++---- 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/example/hello_world.cpp b/example/hello_world.cpp index 492d64316..d6525f57f 100644 --- a/example/hello_world.cpp +++ b/example/hello_world.cpp @@ -15,12 +15,12 @@ int main() { - // get the default GPU device - boost::compute::device gpu = - boost::compute::system::default_gpu_device(); + // get the default device + boost::compute::device device = + boost::compute::system::default_device(); // print the GPU's name - std::cout << "hello from " << gpu.name() << std::endl; + std::cout << "hello from " << device.name() << std::endl; return 0; } diff --git a/include/boost/compute/system.hpp b/include/boost/compute/system.hpp index b5e3e092c..20da619f3 100644 --- a/include/boost/compute/system.hpp +++ b/include/boost/compute/system.hpp @@ -31,10 +31,21 @@ class system static device default_device() { // check for device from environment variable - const char *name = std::getenv("BOOST_COMPUTE_DEFAULT_DEVICE"); - if(name){ - device device = find_device(name); - if(device.id()){ + const char *name = std::getenv("BOOST_COMPUTE_DEFAULT_DEVICE"); + const char *platform = std::getenv("BOOST_COMPUTE_DEFAULT_PLATFORM"); + const char *vendor = std::getenv("BOOST_COMPUTE_DEFAULT_VENDOR"); + + if(name || platform || vendor){ + BOOST_FOREACH(const device &device, devices()){ + if (name && !matches(device.name(), name)) + continue; + + if (platform && !matches(device_platform(device).name(), platform)) + continue; + + if (vendor && !matches(device.vendor(), vendor)) + continue; + return device; } } @@ -141,6 +152,14 @@ class system clGetPlatformIDs(0, 0, &count); return static_cast(count); } + + static platform device_platform(const device &device) { + return platform( device.get_info(CL_DEVICE_PLATFORM) ); + } + + static bool matches(const std::string &str, const std::string &pattern) { + return str.find(pattern) != std::string::npos; + } }; } // end compute namespace From 2cbdd7496d3f9bc41e6ce8e265918e8a57115a33 Mon Sep 17 00:00:00 2001 From: Denis Demidov Date: Fri, 12 Apr 2013 10:42:49 +0400 Subject: [PATCH 3/3] Make system::device_platform and system::matches private --- include/boost/compute/system.hpp | 1 + 1 file changed, 1 insertion(+) diff --git a/include/boost/compute/system.hpp b/include/boost/compute/system.hpp index 20da619f3..cbc4e2994 100644 --- a/include/boost/compute/system.hpp +++ b/include/boost/compute/system.hpp @@ -153,6 +153,7 @@ class system return static_cast(count); } +private: static platform device_platform(const device &device) { return platform( device.get_info(CL_DEVICE_PLATFORM) ); }