test_command_queue.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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 TestCommandQueue
  11. #include <boost/test/unit_test.hpp>
  12. #include <iostream>
  13. #include <boost/compute/kernel.hpp>
  14. #include <boost/compute/system.hpp>
  15. #include <boost/compute/program.hpp>
  16. #include <boost/compute/command_queue.hpp>
  17. #include <boost/compute/algorithm/fill.hpp>
  18. #include <boost/compute/container/vector.hpp>
  19. #include <boost/compute/utility/dim.hpp>
  20. #include <boost/compute/utility/source.hpp>
  21. #include <boost/compute/detail/diagnostic.hpp>
  22. #include "check_macros.hpp"
  23. #include "context_setup.hpp"
  24. namespace bc = boost::compute;
  25. namespace compute = boost::compute;
  26. BOOST_AUTO_TEST_CASE(get_context)
  27. {
  28. BOOST_VERIFY(queue.get_context() == context);
  29. BOOST_VERIFY(queue.get_info<CL_QUEUE_CONTEXT>() == context.get());
  30. }
  31. BOOST_AUTO_TEST_CASE(get_device)
  32. {
  33. BOOST_VERIFY(queue.get_info<CL_QUEUE_DEVICE>() == device.get());
  34. }
  35. BOOST_AUTO_TEST_CASE(equality_operator)
  36. {
  37. compute::command_queue queue1(context, device);
  38. BOOST_CHECK(queue1 == queue1);
  39. compute::command_queue queue2 = queue1;
  40. BOOST_CHECK(queue1 == queue2);
  41. compute::command_queue queue3(context, device);
  42. BOOST_CHECK(queue1 != queue3);
  43. }
  44. BOOST_AUTO_TEST_CASE(event_profiling)
  45. {
  46. bc::command_queue queue(context, device, bc::command_queue::enable_profiling);
  47. int data[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
  48. bc::buffer buffer(context, sizeof(data));
  49. bc::event event =
  50. queue.enqueue_write_buffer_async(buffer,
  51. 0,
  52. sizeof(data),
  53. static_cast<const void *>(data));
  54. queue.finish();
  55. event.get_profiling_info<cl_ulong>(bc::event::profiling_command_queued);
  56. event.get_profiling_info<cl_ulong>(bc::event::profiling_command_submit);
  57. event.get_profiling_info<cl_ulong>(bc::event::profiling_command_start);
  58. event.get_profiling_info<cl_ulong>(bc::event::profiling_command_end);
  59. }
  60. BOOST_AUTO_TEST_CASE(kernel_profiling)
  61. {
  62. // create queue with profiling enabled
  63. boost::compute::command_queue queue(
  64. context, device, boost::compute::command_queue::enable_profiling
  65. );
  66. // input data
  67. int data[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
  68. boost::compute::buffer buffer(context, sizeof(data));
  69. // copy input data to device
  70. queue.enqueue_write_buffer(buffer, 0, sizeof(data), data);
  71. // setup kernel
  72. const char source[] =
  73. "__kernel void iscal(__global int *buffer, int alpha)\n"
  74. "{\n"
  75. " buffer[get_global_id(0)] *= alpha;\n"
  76. "}\n";
  77. boost::compute::program program =
  78. boost::compute::program::create_with_source(source, context);
  79. program.build();
  80. boost::compute::kernel kernel(program, "iscal");
  81. kernel.set_arg(0, buffer);
  82. kernel.set_arg(1, 2);
  83. // execute kernel
  84. size_t global_work_offset = 0;
  85. size_t global_work_size = 8;
  86. boost::compute::event event =
  87. queue.enqueue_nd_range_kernel(kernel,
  88. size_t(1),
  89. &global_work_offset,
  90. &global_work_size,
  91. 0);
  92. // wait until kernel is finished
  93. event.wait();
  94. // check profiling information
  95. event.get_profiling_info<cl_ulong>(bc::event::profiling_command_queued);
  96. event.get_profiling_info<cl_ulong>(bc::event::profiling_command_submit);
  97. event.get_profiling_info<cl_ulong>(bc::event::profiling_command_start);
  98. event.get_profiling_info<cl_ulong>(bc::event::profiling_command_end);
  99. // read results back to host
  100. queue.enqueue_read_buffer(buffer, 0, sizeof(data), data);
  101. // check results
  102. BOOST_CHECK_EQUAL(data[0], 2);
  103. BOOST_CHECK_EQUAL(data[1], 4);
  104. BOOST_CHECK_EQUAL(data[2], 6);
  105. BOOST_CHECK_EQUAL(data[3], 8);
  106. BOOST_CHECK_EQUAL(data[4], 10);
  107. BOOST_CHECK_EQUAL(data[5], 12);
  108. BOOST_CHECK_EQUAL(data[6], 14);
  109. BOOST_CHECK_EQUAL(data[7], 16);
  110. }
  111. BOOST_AUTO_TEST_CASE(construct_from_cl_command_queue)
  112. {
  113. // create cl_command_queue
  114. cl_command_queue cl_queue;
  115. #ifdef BOOST_COMPUTE_CL_VERSION_2_0
  116. if (device.check_version(2, 0)){ // runtime check
  117. cl_queue =
  118. clCreateCommandQueueWithProperties(context, device.id(), 0, 0);
  119. } else
  120. #endif // BOOST_COMPUTE_CL_VERSION_2_0
  121. {
  122. // Suppress deprecated declarations warning
  123. BOOST_COMPUTE_DISABLE_DEPRECATED_DECLARATIONS();
  124. cl_queue =
  125. clCreateCommandQueue(context, device.id(), 0, 0);
  126. BOOST_COMPUTE_ENABLE_DEPRECATED_DECLARATIONS();
  127. }
  128. BOOST_VERIFY(cl_queue);
  129. // create boost::compute::command_queue
  130. boost::compute::command_queue queue(cl_queue);
  131. // check queue
  132. BOOST_CHECK(queue.get_context() == context);
  133. BOOST_CHECK(cl_command_queue(queue) == cl_queue);
  134. // cleanup cl_command_queue
  135. clReleaseCommandQueue(cl_queue);
  136. }
  137. #ifdef BOOST_COMPUTE_CL_VERSION_1_1
  138. BOOST_AUTO_TEST_CASE(write_buffer_rect)
  139. {
  140. REQUIRES_OPENCL_VERSION(1, 1);
  141. // skip this test on AMD GPUs due to a buggy implementation
  142. // of the clEnqueueWriteBufferRect() function
  143. if(device.vendor() == "Advanced Micro Devices, Inc." &&
  144. device.type() & boost::compute::device::gpu){
  145. std::cerr << "skipping write_buffer_rect test on AMD GPU" << std::endl;
  146. return;
  147. }
  148. int data[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
  149. boost::compute::buffer buffer(context, 8 * sizeof(int));
  150. // copy every other value to the buffer
  151. size_t buffer_origin[] = { 0, 0, 0 };
  152. size_t host_origin[] = { 0, 0, 0 };
  153. size_t region[] = { sizeof(int), sizeof(int), 1 };
  154. queue.enqueue_write_buffer_rect(
  155. buffer,
  156. buffer_origin,
  157. host_origin,
  158. region,
  159. sizeof(int),
  160. 0,
  161. 2 * sizeof(int),
  162. 0,
  163. data
  164. );
  165. // check output values
  166. int output[4];
  167. queue.enqueue_read_buffer(buffer, 0, 4 * sizeof(int), output);
  168. BOOST_CHECK_EQUAL(output[0], 1);
  169. BOOST_CHECK_EQUAL(output[1], 3);
  170. BOOST_CHECK_EQUAL(output[2], 5);
  171. BOOST_CHECK_EQUAL(output[3], 7);
  172. }
  173. #endif // BOOST_COMPUTE_CL_VERSION_1_1
  174. static bool nullary_kernel_executed = false;
  175. static void nullary_kernel()
  176. {
  177. nullary_kernel_executed = true;
  178. }
  179. BOOST_AUTO_TEST_CASE(native_kernel)
  180. {
  181. cl_device_exec_capabilities exec_capabilities =
  182. device.get_info<CL_DEVICE_EXECUTION_CAPABILITIES>();
  183. if(!(exec_capabilities & CL_EXEC_NATIVE_KERNEL)){
  184. std::cerr << "skipping native_kernel test: "
  185. << "device does not support CL_EXEC_NATIVE_KERNEL"
  186. << std::endl;
  187. return;
  188. }
  189. compute::vector<int> vector(1000, context);
  190. compute::fill(vector.begin(), vector.end(), 42, queue);
  191. BOOST_CHECK_EQUAL(nullary_kernel_executed, false);
  192. queue.enqueue_native_kernel(&nullary_kernel);
  193. queue.finish();
  194. BOOST_CHECK_EQUAL(nullary_kernel_executed, true);
  195. }
  196. BOOST_AUTO_TEST_CASE(copy_with_wait_list)
  197. {
  198. int data1[] = { 1, 3, 5, 7 };
  199. int data2[] = { 2, 4, 6, 8 };
  200. compute::buffer buf1(context, 4 * sizeof(int));
  201. compute::buffer buf2(context, 4 * sizeof(int));
  202. compute::event write_event1 =
  203. queue.enqueue_write_buffer_async(buf1, 0, buf1.size(), data1);
  204. compute::event write_event2 =
  205. queue.enqueue_write_buffer_async(buf2, 0, buf2.size(), data2);
  206. compute::event read_event1 =
  207. queue.enqueue_read_buffer_async(buf1, 0, buf1.size(), data2, write_event1);
  208. compute::event read_event2 =
  209. queue.enqueue_read_buffer_async(buf2, 0, buf2.size(), data1, write_event2);
  210. read_event1.wait();
  211. read_event2.wait();
  212. CHECK_HOST_RANGE_EQUAL(int, 4, data1, (2, 4, 6, 8));
  213. CHECK_HOST_RANGE_EQUAL(int, 4, data2, (1, 3, 5, 7));
  214. }
  215. #ifndef BOOST_COMPUTE_NO_HDR_INITIALIZER_LIST
  216. BOOST_AUTO_TEST_CASE(enqueue_kernel_with_extents)
  217. {
  218. using boost::compute::dim;
  219. using boost::compute::uint_;
  220. const char source[] = BOOST_COMPUTE_STRINGIZE_SOURCE(
  221. __kernel void foo(__global int *output1, __global int *output2)
  222. {
  223. output1[get_global_id(0)] = get_local_id(0);
  224. output2[get_global_id(1)] = get_local_id(1);
  225. }
  226. );
  227. compute::kernel kernel =
  228. compute::kernel::create_with_source(source, "foo", context);
  229. compute::vector<uint_> output1(4, context);
  230. compute::vector<uint_> output2(4, context);
  231. kernel.set_arg(0, output1);
  232. kernel.set_arg(1, output2);
  233. queue.enqueue_nd_range_kernel(kernel, dim(0, 0), dim(4, 4), dim(1, 1));
  234. CHECK_RANGE_EQUAL(int, 4, output1, (0, 0, 0, 0));
  235. CHECK_RANGE_EQUAL(int, 4, output2, (0, 0, 0, 0));
  236. // Maximum number of work-items that can be specified in each
  237. // dimension of the work-group to clEnqueueNDRangeKernel.
  238. std::vector<size_t> max_work_item_sizes =
  239. device.get_info<CL_DEVICE_MAX_WORK_ITEM_SIZES>();
  240. if(max_work_item_sizes[0] < size_t(2)) {
  241. return;
  242. }
  243. queue.enqueue_nd_range_kernel(kernel, dim(0, 0), dim(4, 4), dim(2, 1));
  244. CHECK_RANGE_EQUAL(int, 4, output1, (0, 1, 0, 1));
  245. CHECK_RANGE_EQUAL(int, 4, output2, (0, 0, 0, 0));
  246. if(max_work_item_sizes[1] < size_t(2)) {
  247. return;
  248. }
  249. queue.enqueue_nd_range_kernel(kernel, dim(0, 0), dim(4, 4), dim(2, 2));
  250. CHECK_RANGE_EQUAL(int, 4, output1, (0, 1, 0, 1));
  251. CHECK_RANGE_EQUAL(int, 4, output2, (0, 1, 0, 1));
  252. }
  253. #endif // BOOST_COMPUTE_NO_HDR_INITIALIZER_LIST
  254. #ifdef BOOST_COMPUTE_CL_VERSION_2_1
  255. BOOST_AUTO_TEST_CASE(get_default_device_queue)
  256. {
  257. REQUIRES_OPENCL_VERSION(2, 1);
  258. boost::compute::command_queue default_device_queue(
  259. context, device,
  260. boost::compute::command_queue::on_device |
  261. boost::compute::command_queue::on_device_default |
  262. boost::compute::command_queue::enable_out_of_order_execution
  263. );
  264. BOOST_CHECK_NO_THROW(queue.get_info<CL_QUEUE_DEVICE_DEFAULT>());
  265. BOOST_CHECK_EQUAL(
  266. queue.get_default_device_queue(),
  267. default_device_queue
  268. );
  269. }
  270. BOOST_AUTO_TEST_CASE(set_as_default_device_queue)
  271. {
  272. REQUIRES_OPENCL_VERSION(2, 1);
  273. boost::compute::command_queue new_default_device_queue(
  274. context, device,
  275. boost::compute::command_queue::on_device |
  276. boost::compute::command_queue::enable_out_of_order_execution
  277. );
  278. new_default_device_queue.set_as_default_device_queue();
  279. BOOST_CHECK_EQUAL(
  280. queue.get_default_device_queue(),
  281. new_default_device_queue
  282. );
  283. }
  284. #endif
  285. BOOST_AUTO_TEST_SUITE_END()