From 178676df4fb2418cd214aa0b53de336dcd15b8fc Mon Sep 17 00:00:00 2001 From: Kyle Lutz Date: Fri, 10 May 2013 22:49:05 -0400 Subject: [PATCH] Refactor the system::default_device() method This refactors the system::default_device() method. Now, the default compute device for the system is only found once and stored in a static variable. This eliminates many redundant calls to clGetPlatformIDs() and clGetDeviceIDs(). Also, the default_cpu_device() and default_gpu_device() methods have been removed and their usages replaced with default_device(). --- doc/reference/command_queue.xml | 16 +++--- doc/reference/device.xml | 15 ++--- doc/reference/system.xml | 22 -------- doc/tutorial.qbk | 2 +- example/simple_kernel.cpp | 4 +- example/vector_addition.cpp | 4 +- include/boost/compute/system.hpp | 96 ++++++++++++++------------------ test/test_device.cpp | 20 +------ 8 files changed, 61 insertions(+), 118 deletions(-) diff --git a/doc/reference/command_queue.xml b/doc/reference/command_queue.xml index 966676c2..f46708f9 100644 --- a/doc/reference/command_queue.xml +++ b/doc/reference/command_queue.xml @@ -16,17 +16,17 @@ Command queues are created for one or more compute devices within a - compute context. For example, to create a queue for the default GPU - on the system: + compute context. For example, to create a queue for the default + compute device on the system: -// get the default GPU device -boost::compute::device gpu = boost::compute::system::default_gpu_device(); +// get the default compute device +boost::compute::device device = boost::compute::system::default_device(); -// create a context for the GPU device -boost::compute::context context(gpu); +// create a context for the device +boost::compute::context context(device); -// create a command queue for the GPU device -boost::compute::command_queue queue(context, gpu); +// create a command queue for the device +boost::compute::command_queue queue(context, device); diff --git a/doc/reference/device.xml b/doc/reference/device.xml index 1bff02a8..b57d391f 100644 --- a/doc/reference/device.xml +++ b/doc/reference/device.xml @@ -14,19 +14,14 @@ the platform::devices() method. - A default device can be easily created with the one of the - following convenience methods: - - system::default_device() - system::default_cpu_device() - system::default_gpu_device() - + A default device can be easily created with the + system::default_device() method. - For example, to get the default GPU device on the system: + For example, to get the default compute device on the system: -// get the default GPU device -boost::compute::device gpu = boost::compute::system::default_gpu_device(); +// get the default compute device +boost::compute::device device = boost::compute::system::default_device(); diff --git a/doc/reference/system.xml b/doc/reference/system.xml index 3c9fab82..d4d64692 100644 --- a/doc/reference/system.xml +++ b/doc/reference/system.xml @@ -30,28 +30,6 @@ - - - device - - - - Returns the default GPU compute device for the system. - - - - - - - device - - - - Returns the default CPU compute device for the system. - - - - context diff --git a/doc/tutorial.qbk b/doc/tutorial.qbk index 517817f2..616842aa 100644 --- a/doc/tutorial.qbk +++ b/doc/tutorial.qbk @@ -3,7 +3,7 @@ [section Hello World] The hello world example gives a simple application that prints the name of -the default GPU compute device on the system. +the default compute device on the system. Compute devices are represented with the [classref boost::compute::device device] class. diff --git a/example/simple_kernel.cpp b/example/simple_kernel.cpp index c4063cff..2f92807f 100644 --- a/example/simple_kernel.cpp +++ b/example/simple_kernel.cpp @@ -15,9 +15,9 @@ // setup and run a simple vector addition kernel on the GPU int main() { - // get the default GPU device + // get the default device boost::compute::device gpu = - boost::compute::system::default_gpu_device(); + boost::compute::system::default_device(); // create a context for the device boost::compute::context context(gpu); diff --git a/example/vector_addition.cpp b/example/vector_addition.cpp index 310cf968..85017d1c 100644 --- a/example/vector_addition.cpp +++ b/example/vector_addition.cpp @@ -15,9 +15,9 @@ // implementation to add two vectors on the GPU int main() { - // get the default GPU device + // get the default device boost::compute::device gpu = - boost::compute::system::default_gpu_device(); + boost::compute::system::default_device(); // create a context for the device boost::compute::context context(gpu); diff --git a/include/boost/compute/system.hpp b/include/boost/compute/system.hpp index 55461fab..d328939e 100644 --- a/include/boost/compute/system.hpp +++ b/include/boost/compute/system.hpp @@ -30,62 +30,9 @@ class system public: static device default_device() { - // check for device from environment variable - 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"); + static device default_device = find_default_device(); - 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; - } - } - - // check for a gpu device - device gpu = default_gpu_device(); - if(gpu.id() != 0){ - return gpu; - } - - // check for a cpu device - device cpu = default_cpu_device(); - if(cpu.id() != 0){ - return cpu; - } - - // return a null device - return device(); - } - - static device default_cpu_device() - { - BOOST_FOREACH(const device &device, devices()){ - if(device.type() == device::cpu){ - return device; - } - } - - return device(); - } - - static device default_gpu_device() - { - BOOST_FOREACH(const device &device, devices()){ - if(device.type() == device::gpu){ - return device; - } - } - - return device(); + return default_device; } static device find_device(const std::string &name) @@ -154,6 +101,45 @@ public: } private: + static device find_default_device() + { + // check for device from environment variable + 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; + } + } + + // find the first gpu device + BOOST_FOREACH(const device &device, devices()){ + if(device.type() == device::gpu){ + return device; + } + } + + // find the first cpu device + BOOST_FOREACH(const device &device, devices()){ + if(device.type() == device::cpu){ + return device; + } + } + + return device(); + } + static platform device_platform(const device &device) { return platform(device.get_info(CL_DEVICE_PLATFORM)); diff --git a/test/test_device.cpp b/test/test_device.cpp index 01195a97..d4a89094 100644 --- a/test/test_device.cpp +++ b/test/test_device.cpp @@ -22,25 +22,9 @@ BOOST_AUTO_TEST_CASE(null_device) BOOST_CHECK(null.id() == cl_device_id()); } -BOOST_AUTO_TEST_CASE(get_gpu_type) +BOOST_AUTO_TEST_CASE(get_device_name) { - boost::compute::device gpu = boost::compute::system::default_gpu_device(); - if(gpu.id()){ - BOOST_CHECK(gpu.type() == boost::compute::device::gpu); - } -} - -BOOST_AUTO_TEST_CASE(get_cpu_type) -{ - boost::compute::device cpu = boost::compute::system::default_cpu_device(); - if(cpu.id()){ - BOOST_CHECK(cpu.type() == boost::compute::device::cpu); - } -} - -BOOST_AUTO_TEST_CASE(get_gpu_name) -{ - boost::compute::device gpu = boost::compute::system::default_gpu_device(); + boost::compute::device gpu = boost::compute::system::default_device(); if(gpu.id()){ BOOST_CHECK(!gpu.name().empty()); }