test_device.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. //---------------------------------------------------------------------------//
  2. // Copyright (c) 2013 Kyle Lutz <kyle.r.lutz@gmail.com>
  3. //
  4. // Distributed under the Boost Software License, Version 1.0
  5. // See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt
  7. //
  8. // See http://boostorg.github.com/compute for more information.
  9. //---------------------------------------------------------------------------//
  10. #define BOOST_TEST_MODULE TestDevice
  11. #include <boost/test/unit_test.hpp>
  12. #include <iostream>
  13. #include <boost/compute/device.hpp>
  14. #include <boost/compute/system.hpp>
  15. #include <boost/compute/detail/nvidia_compute_capability.hpp>
  16. #include "opencl_version_check.hpp"
  17. BOOST_AUTO_TEST_CASE(null_device)
  18. {
  19. boost::compute::device null;
  20. BOOST_CHECK(null.id() == cl_device_id());
  21. BOOST_CHECK(null.get() == cl_device_id());
  22. }
  23. BOOST_AUTO_TEST_CASE(default_device_doctest)
  24. {
  25. //! [default_gpu]
  26. boost::compute::device gpu = boost::compute::system::default_device();
  27. //! [default_gpu]
  28. BOOST_CHECK(gpu.id());
  29. }
  30. BOOST_AUTO_TEST_CASE(device_platform)
  31. {
  32. boost::compute::platform p = boost::compute::system::platforms().at(0);
  33. BOOST_CHECK(p == p.devices().at(0).platform());
  34. }
  35. BOOST_AUTO_TEST_CASE(get_device_name)
  36. {
  37. boost::compute::device gpu = boost::compute::system::default_device();
  38. if(gpu.id()){
  39. BOOST_CHECK(!gpu.name().empty());
  40. }
  41. }
  42. BOOST_AUTO_TEST_CASE(equality_operator)
  43. {
  44. boost::compute::device device1 = boost::compute::system::default_device();
  45. BOOST_CHECK(device1 == device1);
  46. boost::compute::device device2 = device1;
  47. BOOST_CHECK(device1 == device2);
  48. }
  49. BOOST_AUTO_TEST_CASE(get_max_work_item_sizes)
  50. {
  51. boost::compute::device device = boost::compute::system::default_device();
  52. std::vector<size_t> max_work_item_sizes =
  53. device.get_info<std::vector<size_t> >(CL_DEVICE_MAX_WORK_ITEM_SIZES);
  54. BOOST_CHECK_GE(max_work_item_sizes.size(), size_t(3));
  55. BOOST_CHECK_GE(max_work_item_sizes[0], size_t(1));
  56. BOOST_CHECK_GE(max_work_item_sizes[1], size_t(1));
  57. BOOST_CHECK_GE(max_work_item_sizes[2], size_t(1));
  58. }
  59. #ifdef BOOST_COMPUTE_CL_VERSION_1_2
  60. // returns true if the device supports the partitioning type
  61. bool supports_partition_type(const boost::compute::device &device,
  62. cl_device_partition_property type)
  63. {
  64. const std::vector<cl_device_partition_property> properties =
  65. device.get_info<std::vector<cl_device_partition_property> >(
  66. CL_DEVICE_PARTITION_PROPERTIES
  67. );
  68. return std::find(properties.begin(),
  69. properties.end(),
  70. type) != properties.end();
  71. }
  72. BOOST_AUTO_TEST_CASE(partition_device_equally)
  73. {
  74. // get default device and ensure it has at least two compute units
  75. boost::compute::device device = boost::compute::system::default_device();
  76. REQUIRES_OPENCL_VERSION(1,2);
  77. if(device.compute_units() < 2){
  78. std::cout << "skipping test: "
  79. << "device does not have enough compute units"
  80. << std::endl;
  81. return;
  82. }
  83. // check that the device supports partitioning equally
  84. if(!supports_partition_type(device, CL_DEVICE_PARTITION_EQUALLY)){
  85. std::cout << "skipping test: "
  86. << "device does not support CL_DEVICE_PARTITION_EQUALLY"
  87. << std::endl;
  88. return;
  89. }
  90. // ensure device is not a sub-device
  91. BOOST_CHECK(device.is_subdevice() == false);
  92. // partition default device into sub-devices with one compute unit each
  93. std::vector<boost::compute::device>
  94. sub_devices = device.partition_equally(1);
  95. BOOST_CHECK_EQUAL(sub_devices.size(), size_t(device.compute_units()));
  96. // verify each of the sub-devices
  97. for(size_t i = 0; i < sub_devices.size(); i++){
  98. const boost::compute::device &sub_device = sub_devices[i];
  99. // ensure parent device id is correct
  100. cl_device_id parent_id =
  101. sub_device.get_info<cl_device_id>(CL_DEVICE_PARENT_DEVICE);
  102. BOOST_CHECK(parent_id == device.id());
  103. // ensure device is a sub-device
  104. BOOST_CHECK(sub_device.is_subdevice() == true);
  105. // check number of compute units
  106. BOOST_CHECK_EQUAL(sub_device.compute_units(), size_t(1));
  107. }
  108. }
  109. // used to sort devices by number of compute units
  110. bool compare_compute_units(const boost::compute::device &a,
  111. const boost::compute::device &b)
  112. {
  113. return a.compute_units() < b.compute_units();
  114. }
  115. BOOST_AUTO_TEST_CASE(partition_by_counts)
  116. {
  117. // get default device and ensure it has at least four compute units
  118. boost::compute::device device = boost::compute::system::default_device();
  119. REQUIRES_OPENCL_VERSION(1,2);
  120. if(device.compute_units() < 4){
  121. std::cout << "skipping test: "
  122. << "device does not have enough compute units"
  123. << std::endl;
  124. return;
  125. }
  126. // check that the device supports partitioning by counts
  127. if(!supports_partition_type(device, CL_DEVICE_PARTITION_BY_COUNTS)){
  128. std::cout << "skipping test: "
  129. << "device does not support CL_DEVICE_PARTITION_BY_COUNTS"
  130. << std::endl;
  131. return;
  132. }
  133. // ensure device is not a sub-device
  134. BOOST_CHECK(device.is_subdevice() == false);
  135. // create vector of sub-device compute unit counts
  136. std::vector<size_t> counts;
  137. counts.push_back(2);
  138. counts.push_back(1);
  139. counts.push_back(1);
  140. // partition default device into sub-devices according to counts
  141. std::vector<boost::compute::device>
  142. sub_devices = device.partition_by_counts(counts);
  143. BOOST_CHECK_EQUAL(sub_devices.size(), size_t(3));
  144. // sort sub-devices by number of compute units (see issue #185)
  145. std::sort(sub_devices.begin(), sub_devices.end(), compare_compute_units);
  146. // verify each of the sub-devices
  147. BOOST_CHECK_EQUAL(sub_devices[0].compute_units(), size_t(1));
  148. BOOST_CHECK_EQUAL(sub_devices[1].compute_units(), size_t(1));
  149. BOOST_CHECK_EQUAL(sub_devices[2].compute_units(), size_t(2));
  150. }
  151. BOOST_AUTO_TEST_CASE(partition_by_affinity_domain)
  152. {
  153. // get default device and ensure it has at least two compute units
  154. boost::compute::device device = boost::compute::system::default_device();
  155. REQUIRES_OPENCL_VERSION(1,2);
  156. if(device.compute_units() < 2){
  157. std::cout << "skipping test: "
  158. << "device does not have enough compute units"
  159. << std::endl;
  160. return;
  161. }
  162. // check that the device supports splitting by affinity domains
  163. if(!supports_partition_type(device, CL_DEVICE_AFFINITY_DOMAIN_NEXT_PARTITIONABLE)){
  164. std::cout << "skipping test: "
  165. << "device does not support partitioning by affinity domain"
  166. << std::endl;
  167. return;
  168. }
  169. // ensure device is not a sub-device
  170. BOOST_CHECK(device.is_subdevice() == false);
  171. std::vector<boost::compute::device> sub_devices =
  172. device.partition_by_affinity_domain(
  173. CL_DEVICE_AFFINITY_DOMAIN_NEXT_PARTITIONABLE);
  174. BOOST_CHECK(sub_devices.size() > 0);
  175. BOOST_CHECK(sub_devices[0].is_subdevice() == true);
  176. }
  177. #endif // BOOST_COMPUTE_CL_VERSION_1_2
  178. BOOST_AUTO_TEST_CASE(nvidia_compute_capability)
  179. {
  180. boost::compute::device device = boost::compute::system::default_device();
  181. int major, minor;
  182. boost::compute::detail::get_nvidia_compute_capability(device, major, minor);
  183. boost::compute::detail::check_nvidia_compute_capability(device, 3, 0);
  184. }
  185. BOOST_AUTO_TEST_CASE(get_info_specializations)
  186. {
  187. boost::compute::device device = boost::compute::system::default_device();
  188. std::cout << device.get_info<CL_DEVICE_NAME>() << std::endl;
  189. }
  190. #ifdef BOOST_COMPUTE_CL_VERSION_2_1
  191. BOOST_AUTO_TEST_CASE(get_host_timer)
  192. {
  193. boost::compute::device device = boost::compute::system::default_device();
  194. REQUIRES_OPENCL_VERSION(2, 1);
  195. BOOST_CHECK(device.get_host_timer() != 0);
  196. #ifndef BOOST_COMPUTE_NO_HDR_CHRONO
  197. typedef std::chrono::milliseconds stdms;
  198. BOOST_CHECK(device.get_host_timer<stdms>().count() != 0);
  199. #endif
  200. #ifndef BOOST_COMPUTE_NO_BOOST_CHRONO
  201. typedef boost::chrono::milliseconds bms;
  202. BOOST_CHECK(device.get_host_timer<bms>().count() != 0);
  203. #endif
  204. }
  205. BOOST_AUTO_TEST_CASE(get_device_and_host_timer)
  206. {
  207. boost::compute::device device = boost::compute::system::default_device();
  208. REQUIRES_OPENCL_VERSION(2, 1);
  209. typedef std::pair<boost::compute::ulong_, boost::compute::ulong_> dah_timer;
  210. dah_timer timer;
  211. BOOST_CHECK_NO_THROW(timer = device.get_device_and_host_timer());
  212. BOOST_CHECK(timer.first != 0);
  213. BOOST_CHECK(timer.second != 0);
  214. #ifndef BOOST_COMPUTE_NO_HDR_CHRONO
  215. typedef std::chrono::milliseconds stdms;
  216. BOOST_CHECK(device.get_device_and_host_timer<stdms>().first.count() != 0);
  217. BOOST_CHECK(device.get_device_and_host_timer<stdms>().second.count() != 0);
  218. #endif
  219. #ifndef BOOST_COMPUTE_NO_BOOST_CHRONO
  220. typedef boost::chrono::milliseconds bms;
  221. BOOST_CHECK(device.get_device_and_host_timer<bms>().first.count() != 0);
  222. BOOST_CHECK(device.get_device_and_host_timer<bms>().second.count() != 0);
  223. #endif
  224. }
  225. BOOST_AUTO_TEST_CASE(get_info_opencl21_queries)
  226. {
  227. boost::compute::device device = boost::compute::system::default_device();
  228. REQUIRES_OPENCL_VERSION(2, 1);
  229. BOOST_CHECK(!device.get_info<CL_DEVICE_IL_VERSION>().empty());
  230. BOOST_CHECK(device.get_info<CL_DEVICE_MAX_NUM_SUB_GROUPS>() > 0);
  231. BOOST_CHECK_NO_THROW(
  232. device.get_info<CL_DEVICE_SUB_GROUP_INDEPENDENT_FORWARD_PROGRESS>()
  233. );
  234. }
  235. #endif // BOOST_COMPUTE_CL_VERSION_2_1