JayBeams  0.1
Another project to have fun coding.
ut_device_selector.cpp
Go to the documentation of this file.
1 #include <jb/opencl/config.hpp>
3 
4 #include <boost/compute/system.hpp>
5 #include <boost/test/unit_test.hpp>
6 
7 /**
8  * @test Verify that we can select devices by name.
9  */
10 BOOST_AUTO_TEST_CASE(opencl_device_selector_by_name) {
11  auto devices = boost::compute::system::devices();
12  for (auto const& d : devices) {
13  BOOST_TEST_MESSAGE("searching for " << d.name());
14  boost::compute::device actual =
16 
17  BOOST_CHECK_EQUAL(d.name(), actual.name());
18  BOOST_CHECK_EQUAL(d.id(), actual.id());
19  }
20 }
21 
22 /**
23  * @test Verify that the selection with an empty name works as expected.
24  */
25 BOOST_AUTO_TEST_CASE(opencl_device_selector_empty) {
27  auto expected = boost::compute::system::default_device();
28  BOOST_TEST_MESSAGE("Default selector picked " << actual.name());
29 
30  if (expected.name().substr(0, 8) == "AMD SUMO") {
31  // AMD SUMO works so poorly that the device_selector() is
32  // hardcoded to ignore it ...
33  BOOST_CHECK_NE(expected.id(), actual.id());
34  BOOST_CHECK_NE(expected.name(), actual.name());
35  } else {
36  BOOST_CHECK_EQUAL(expected.id(), actual.id());
37  BOOST_CHECK_EQUAL(expected.name(), actual.name());
38  }
39 }
40 
41 /**
42  * @test Verify that the default selection works as expected.
43  */
44 BOOST_AUTO_TEST_CASE(opencl_device_selector_bestcpu) {
45  boost::compute::device actual;
46  try {
48  jb::opencl::config().device_name("BESTCPU"));
49  } catch (std::exception const& ex) {
50  // On some platforms (pocl) there are no CPU devices, weird.
51  BOOST_TEST_MESSAGE("No available CPU, abort test");
52  return;
53  }
54  BOOST_TEST_MESSAGE("Default selector picked " << actual.name());
55 
56  for (auto const& d : boost::compute::system::devices()) {
57  if (d.type() & boost::compute::device::cpu) {
58  BOOST_TEST_MESSAGE("checking compute unit count for " << d.name());
59  BOOST_CHECK_GE(actual.compute_units(), d.compute_units());
60  }
61  }
62 }
63 
64 /**
65  * @test Verify that the default selection works as expected.
66  */
67 BOOST_AUTO_TEST_CASE(opencl_device_selector_bestgpu) {
68  boost::compute::device actual;
69  try {
71  jb::opencl::config().device_name("BESTGPU"));
72  } catch (std::exception const& ex) {
73  // On machines without a GPU we do not want to fail the test, the
74  // expected result is that no device is found ...
75  BOOST_TEST_MESSAGE("No available GPU, abort test");
76  return;
77  }
78  BOOST_TEST_MESSAGE("Default selector picked " << actual.name());
79 
80  for (auto const& d : boost::compute::system::devices()) {
81  if (d.type() & boost::compute::device::gpu) {
82  BOOST_TEST_MESSAGE("checking compute unit count for " << d.name());
83  BOOST_CHECK_GE(actual.compute_units(), d.compute_units());
84  }
85  }
86 }
87 
88 /**
89  * @test Verify that the device selector without config works as expected.
90  */
91 BOOST_AUTO_TEST_CASE(opencl_device_selector_no_config) {
92  auto actual = jb::opencl::device_selector();
94 
95  BOOST_TEST_MESSAGE(
96  "Default device name=" << actual.name() << ", id=" << actual.id()
97  << ", type=" << actual.type());
98  if (expected.name().substr(0, 8) == "AMD SUMO") {
99  // TODO(#124) - the AMD SUMO devices do not work for us, so they
100  // are mostly disabled for the empty config (used in tests) ...
101  BOOST_CHECK_NE(expected.id(), actual.id());
102  BOOST_CHECK_NE(expected.name(), actual.name());
103  } else {
104  BOOST_CHECK_EQUAL(expected.id(), actual.id());
105  BOOST_CHECK_EQUAL(expected.name(), actual.name());
106  }
107 }
108 
109 /**
110  * @test Verify that the device selector for "SYSTEM:DEFAULT" works as expected.
111  */
112 BOOST_AUTO_TEST_CASE(opencl_device_selector_system_default) {
113  auto actual = jb::opencl::device_selector(
114  jb::opencl::config().device_name("SYSTEM:DEFAULT"));
115  auto expected = boost::compute::system::default_device();
116 
117  BOOST_CHECK_EQUAL(expected.id(), actual.id());
118  BOOST_CHECK_EQUAL(expected.name(), actual.name());
119 }
120 
121 /**
122  * @test Verify that detail::best_device() helper fails as expected.
123  */
124 BOOST_AUTO_TEST_CASE(opencl_device_selector_filter_failure) {
125  using boost::compute::device;
126  BOOST_CHECK_THROW(
128  [](device const&) { return false; }, "FAIL"),
129  std::runtime_error);
130  BOOST_CHECK_NO_THROW(jb::opencl::detail::best_device(
131  [](device const&) { return true; }, "ANY"));
132 }
Configure the OpenCL device / context options.
Definition: config.hpp:12
boost::compute::device device_selector(config const &cfg)
Select an OpenCL device matching the current configuration.
boost::compute::device best_device(Filter filter, char const *filter_name)
Return the best available device of the given type.
std::string device_name(JB_DEFAULT_device_name)
BOOST_AUTO_TEST_CASE(opencl_device_selector_by_name)