command_queue.hpp 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008
  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. #ifndef BOOST_COMPUTE_COMMAND_QUEUE_HPP
  11. #define BOOST_COMPUTE_COMMAND_QUEUE_HPP
  12. #include <cstddef>
  13. #include <algorithm>
  14. #include <boost/assert.hpp>
  15. #include <boost/compute/config.hpp>
  16. #include <boost/compute/event.hpp>
  17. #include <boost/compute/buffer.hpp>
  18. #include <boost/compute/device.hpp>
  19. #include <boost/compute/kernel.hpp>
  20. #include <boost/compute/context.hpp>
  21. #include <boost/compute/exception.hpp>
  22. #include <boost/compute/image/image1d.hpp>
  23. #include <boost/compute/image/image2d.hpp>
  24. #include <boost/compute/image/image3d.hpp>
  25. #include <boost/compute/image/image_object.hpp>
  26. #include <boost/compute/utility/wait_list.hpp>
  27. #include <boost/compute/detail/get_object_info.hpp>
  28. #include <boost/compute/detail/assert_cl_success.hpp>
  29. #include <boost/compute/detail/diagnostic.hpp>
  30. #include <boost/compute/utility/extents.hpp>
  31. namespace boost {
  32. namespace compute {
  33. namespace detail {
  34. inline void BOOST_COMPUTE_CL_CALLBACK
  35. nullary_native_kernel_trampoline(void *user_func_ptr)
  36. {
  37. void (*user_func)();
  38. std::memcpy(&user_func, user_func_ptr, sizeof(user_func));
  39. user_func();
  40. }
  41. } // end detail namespace
  42. /// \class command_queue
  43. /// \brief A command queue.
  44. ///
  45. /// Command queues provide the interface for interacting with compute
  46. /// devices. The command_queue class provides methods to copy data to
  47. /// and from a compute device as well as execute compute kernels.
  48. ///
  49. /// Command queues are created for a compute device within a compute
  50. /// context.
  51. ///
  52. /// For example, to create a context and command queue for the default device
  53. /// on the system (this is the normal set up code used by almost all OpenCL
  54. /// programs):
  55. /// \code
  56. /// #include <boost/compute/core.hpp>
  57. ///
  58. /// // get the default compute device
  59. /// boost::compute::device device = boost::compute::system::default_device();
  60. ///
  61. /// // set up a compute context and command queue
  62. /// boost::compute::context context(device);
  63. /// boost::compute::command_queue queue(context, device);
  64. /// \endcode
  65. ///
  66. /// The default command queue for the system can be obtained with the
  67. /// system::default_queue() method.
  68. ///
  69. /// \see buffer, context, kernel
  70. class command_queue
  71. {
  72. public:
  73. enum properties {
  74. enable_profiling = CL_QUEUE_PROFILING_ENABLE,
  75. enable_out_of_order_execution = CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE
  76. #ifdef BOOST_COMPUTE_CL_VERSION_2_0
  77. ,
  78. on_device = CL_QUEUE_ON_DEVICE,
  79. on_device_default = CL_QUEUE_ON_DEVICE_DEFAULT
  80. #endif
  81. };
  82. enum map_flags {
  83. map_read = CL_MAP_READ,
  84. map_write = CL_MAP_WRITE
  85. #ifdef BOOST_COMPUTE_CL_VERSION_1_2
  86. ,
  87. map_write_invalidate_region = CL_MAP_WRITE_INVALIDATE_REGION
  88. #endif
  89. };
  90. #ifdef BOOST_COMPUTE_CL_VERSION_1_2
  91. enum mem_migration_flags {
  92. migrate_to_host = CL_MIGRATE_MEM_OBJECT_HOST,
  93. migrate_content_undefined = CL_MIGRATE_MEM_OBJECT_CONTENT_UNDEFINED
  94. };
  95. #endif // BOOST_COMPUTE_CL_VERSION_1_2
  96. /// Creates a null command queue.
  97. command_queue()
  98. : m_queue(0)
  99. {
  100. }
  101. explicit command_queue(cl_command_queue queue, bool retain = true)
  102. : m_queue(queue)
  103. {
  104. if(m_queue && retain){
  105. clRetainCommandQueue(m_queue);
  106. }
  107. }
  108. /// Creates a command queue in \p context for \p device with
  109. /// \p properties.
  110. ///
  111. /// \see_opencl_ref{clCreateCommandQueue}
  112. command_queue(const context &context,
  113. const device &device,
  114. cl_command_queue_properties properties = 0)
  115. {
  116. BOOST_ASSERT(device.id() != 0);
  117. cl_int error = 0;
  118. #ifdef BOOST_COMPUTE_CL_VERSION_2_0
  119. if (device.check_version(2, 0)){
  120. std::vector<cl_queue_properties> queue_properties;
  121. if(properties){
  122. queue_properties.push_back(CL_QUEUE_PROPERTIES);
  123. queue_properties.push_back(cl_queue_properties(properties));
  124. queue_properties.push_back(cl_queue_properties(0));
  125. }
  126. const cl_queue_properties *queue_properties_ptr =
  127. queue_properties.empty() ? 0 : &queue_properties[0];
  128. m_queue = clCreateCommandQueueWithProperties(
  129. context, device.id(), queue_properties_ptr, &error
  130. );
  131. } else
  132. #endif
  133. {
  134. // Suppress deprecated declarations warning
  135. BOOST_COMPUTE_DISABLE_DEPRECATED_DECLARATIONS();
  136. m_queue = clCreateCommandQueue(
  137. context, device.id(), properties, &error
  138. );
  139. BOOST_COMPUTE_ENABLE_DEPRECATED_DECLARATIONS();
  140. }
  141. if(!m_queue){
  142. BOOST_THROW_EXCEPTION(opencl_error(error));
  143. }
  144. }
  145. /// Creates a new command queue object as a copy of \p other.
  146. command_queue(const command_queue &other)
  147. : m_queue(other.m_queue)
  148. {
  149. if(m_queue){
  150. clRetainCommandQueue(m_queue);
  151. }
  152. }
  153. /// Copies the command queue object from \p other to \c *this.
  154. command_queue& operator=(const command_queue &other)
  155. {
  156. if(this != &other){
  157. if(m_queue){
  158. clReleaseCommandQueue(m_queue);
  159. }
  160. m_queue = other.m_queue;
  161. if(m_queue){
  162. clRetainCommandQueue(m_queue);
  163. }
  164. }
  165. return *this;
  166. }
  167. #ifndef BOOST_COMPUTE_NO_RVALUE_REFERENCES
  168. /// Move-constructs a new command queue object from \p other.
  169. command_queue(command_queue&& other) BOOST_NOEXCEPT
  170. : m_queue(other.m_queue)
  171. {
  172. other.m_queue = 0;
  173. }
  174. /// Move-assigns the command queue from \p other to \c *this.
  175. command_queue& operator=(command_queue&& other) BOOST_NOEXCEPT
  176. {
  177. if(m_queue){
  178. clReleaseCommandQueue(m_queue);
  179. }
  180. m_queue = other.m_queue;
  181. other.m_queue = 0;
  182. return *this;
  183. }
  184. #endif // BOOST_COMPUTE_NO_RVALUE_REFERENCES
  185. /// Destroys the command queue.
  186. ///
  187. /// \see_opencl_ref{clReleaseCommandQueue}
  188. ~command_queue()
  189. {
  190. if(m_queue){
  191. BOOST_COMPUTE_ASSERT_CL_SUCCESS(
  192. clReleaseCommandQueue(m_queue)
  193. );
  194. }
  195. }
  196. /// Returns the underlying OpenCL command queue.
  197. cl_command_queue& get() const
  198. {
  199. return const_cast<cl_command_queue &>(m_queue);
  200. }
  201. /// Returns the device that the command queue issues commands to.
  202. device get_device() const
  203. {
  204. return device(get_info<cl_device_id>(CL_QUEUE_DEVICE));
  205. }
  206. /// Returns the context for the command queue.
  207. context get_context() const
  208. {
  209. return context(get_info<cl_context>(CL_QUEUE_CONTEXT));
  210. }
  211. /// Returns information about the command queue.
  212. ///
  213. /// \see_opencl_ref{clGetCommandQueueInfo}
  214. template<class T>
  215. T get_info(cl_command_queue_info info) const
  216. {
  217. return detail::get_object_info<T>(clGetCommandQueueInfo, m_queue, info);
  218. }
  219. /// \overload
  220. template<int Enum>
  221. typename detail::get_object_info_type<command_queue, Enum>::type
  222. get_info() const;
  223. /// Returns the properties for the command queue.
  224. cl_command_queue_properties get_properties() const
  225. {
  226. return get_info<cl_command_queue_properties>(CL_QUEUE_PROPERTIES);
  227. }
  228. #if defined(BOOST_COMPUTE_CL_VERSION_2_1) || defined(BOOST_COMPUTE_DOXYGEN_INVOKED)
  229. /// Returns the current default device command queue for the underlying device.
  230. ///
  231. /// \opencl_version_warning{2,1}
  232. command_queue get_default_device_queue() const
  233. {
  234. return command_queue(get_info<cl_command_queue>(CL_QUEUE_DEVICE_DEFAULT));
  235. }
  236. /// Replaces the default device command queue for the underlying device
  237. /// with this command queue. Command queue must have been created
  238. /// with CL_QUEUE_ON_DEVICE flag.
  239. ///
  240. /// \see_opencl21_ref{clSetDefaultDeviceCommandQueue}
  241. ///
  242. /// \opencl_version_warning{2,1}
  243. void set_as_default_device_queue() const
  244. {
  245. cl_int ret = clSetDefaultDeviceCommandQueue(
  246. this->get_context().get(),
  247. this->get_device().get(),
  248. m_queue
  249. );
  250. if(ret != CL_SUCCESS){
  251. BOOST_THROW_EXCEPTION(opencl_error(ret));
  252. }
  253. }
  254. #endif // BOOST_COMPUTE_CL_VERSION_2_1
  255. /// Enqueues a command to read data from \p buffer to host memory.
  256. ///
  257. /// \see_opencl_ref{clEnqueueReadBuffer}
  258. ///
  259. /// \see copy()
  260. event enqueue_read_buffer(const buffer &buffer,
  261. size_t offset,
  262. size_t size,
  263. void *host_ptr,
  264. const wait_list &events = wait_list())
  265. {
  266. BOOST_ASSERT(m_queue != 0);
  267. BOOST_ASSERT(size <= buffer.size());
  268. BOOST_ASSERT(buffer.get_context() == this->get_context());
  269. BOOST_ASSERT(host_ptr != 0);
  270. event event_;
  271. cl_int ret = clEnqueueReadBuffer(
  272. m_queue,
  273. buffer.get(),
  274. CL_TRUE,
  275. offset,
  276. size,
  277. host_ptr,
  278. events.size(),
  279. events.get_event_ptr(),
  280. &event_.get()
  281. );
  282. if(ret != CL_SUCCESS){
  283. BOOST_THROW_EXCEPTION(opencl_error(ret));
  284. }
  285. return event_;
  286. }
  287. /// Enqueues a command to read data from \p buffer to host memory. The
  288. /// copy will be performed asynchronously.
  289. ///
  290. /// \see_opencl_ref{clEnqueueReadBuffer}
  291. ///
  292. /// \see copy_async()
  293. event enqueue_read_buffer_async(const buffer &buffer,
  294. size_t offset,
  295. size_t size,
  296. void *host_ptr,
  297. const wait_list &events = wait_list())
  298. {
  299. BOOST_ASSERT(m_queue != 0);
  300. BOOST_ASSERT(size <= buffer.size());
  301. BOOST_ASSERT(buffer.get_context() == this->get_context());
  302. BOOST_ASSERT(host_ptr != 0);
  303. event event_;
  304. cl_int ret = clEnqueueReadBuffer(
  305. m_queue,
  306. buffer.get(),
  307. CL_FALSE,
  308. offset,
  309. size,
  310. host_ptr,
  311. events.size(),
  312. events.get_event_ptr(),
  313. &event_.get()
  314. );
  315. if(ret != CL_SUCCESS){
  316. BOOST_THROW_EXCEPTION(opencl_error(ret));
  317. }
  318. return event_;
  319. }
  320. #if defined(BOOST_COMPUTE_CL_VERSION_1_1) || defined(BOOST_COMPUTE_DOXYGEN_INVOKED)
  321. /// Enqueues a command to read a rectangular region from \p buffer to
  322. /// host memory.
  323. ///
  324. /// \see_opencl_ref{clEnqueueReadBufferRect}
  325. ///
  326. /// \opencl_version_warning{1,1}
  327. event enqueue_read_buffer_rect(const buffer &buffer,
  328. const size_t buffer_origin[3],
  329. const size_t host_origin[3],
  330. const size_t region[3],
  331. size_t buffer_row_pitch,
  332. size_t buffer_slice_pitch,
  333. size_t host_row_pitch,
  334. size_t host_slice_pitch,
  335. void *host_ptr,
  336. const wait_list &events = wait_list())
  337. {
  338. BOOST_ASSERT(m_queue != 0);
  339. BOOST_ASSERT(buffer.get_context() == this->get_context());
  340. BOOST_ASSERT(host_ptr != 0);
  341. event event_;
  342. cl_int ret = clEnqueueReadBufferRect(
  343. m_queue,
  344. buffer.get(),
  345. CL_TRUE,
  346. buffer_origin,
  347. host_origin,
  348. region,
  349. buffer_row_pitch,
  350. buffer_slice_pitch,
  351. host_row_pitch,
  352. host_slice_pitch,
  353. host_ptr,
  354. events.size(),
  355. events.get_event_ptr(),
  356. &event_.get()
  357. );
  358. if(ret != CL_SUCCESS){
  359. BOOST_THROW_EXCEPTION(opencl_error(ret));
  360. }
  361. return event_;
  362. }
  363. /// Enqueues a command to read a rectangular region from \p buffer to
  364. /// host memory. The copy will be performed asynchronously.
  365. ///
  366. /// \see_opencl_ref{clEnqueueReadBufferRect}
  367. ///
  368. /// \opencl_version_warning{1,1}
  369. event enqueue_read_buffer_rect_async(const buffer &buffer,
  370. const size_t buffer_origin[3],
  371. const size_t host_origin[3],
  372. const size_t region[3],
  373. size_t buffer_row_pitch,
  374. size_t buffer_slice_pitch,
  375. size_t host_row_pitch,
  376. size_t host_slice_pitch,
  377. void *host_ptr,
  378. const wait_list &events = wait_list())
  379. {
  380. BOOST_ASSERT(m_queue != 0);
  381. BOOST_ASSERT(buffer.get_context() == this->get_context());
  382. BOOST_ASSERT(host_ptr != 0);
  383. event event_;
  384. cl_int ret = clEnqueueReadBufferRect(
  385. m_queue,
  386. buffer.get(),
  387. CL_FALSE,
  388. buffer_origin,
  389. host_origin,
  390. region,
  391. buffer_row_pitch,
  392. buffer_slice_pitch,
  393. host_row_pitch,
  394. host_slice_pitch,
  395. host_ptr,
  396. events.size(),
  397. events.get_event_ptr(),
  398. &event_.get()
  399. );
  400. if(ret != CL_SUCCESS){
  401. BOOST_THROW_EXCEPTION(opencl_error(ret));
  402. }
  403. return event_;
  404. }
  405. #endif // BOOST_COMPUTE_CL_VERSION_1_1
  406. /// Enqueues a command to write data from host memory to \p buffer.
  407. ///
  408. /// \see_opencl_ref{clEnqueueWriteBuffer}
  409. ///
  410. /// \see copy()
  411. event enqueue_write_buffer(const buffer &buffer,
  412. size_t offset,
  413. size_t size,
  414. const void *host_ptr,
  415. const wait_list &events = wait_list())
  416. {
  417. BOOST_ASSERT(m_queue != 0);
  418. BOOST_ASSERT(size <= buffer.size());
  419. BOOST_ASSERT(buffer.get_context() == this->get_context());
  420. BOOST_ASSERT(host_ptr != 0);
  421. event event_;
  422. cl_int ret = clEnqueueWriteBuffer(
  423. m_queue,
  424. buffer.get(),
  425. CL_TRUE,
  426. offset,
  427. size,
  428. host_ptr,
  429. events.size(),
  430. events.get_event_ptr(),
  431. &event_.get()
  432. );
  433. if(ret != CL_SUCCESS){
  434. BOOST_THROW_EXCEPTION(opencl_error(ret));
  435. }
  436. return event_;
  437. }
  438. /// Enqueues a command to write data from host memory to \p buffer.
  439. /// The copy is performed asynchronously.
  440. ///
  441. /// \see_opencl_ref{clEnqueueWriteBuffer}
  442. ///
  443. /// \see copy_async()
  444. event enqueue_write_buffer_async(const buffer &buffer,
  445. size_t offset,
  446. size_t size,
  447. const void *host_ptr,
  448. const wait_list &events = wait_list())
  449. {
  450. BOOST_ASSERT(m_queue != 0);
  451. BOOST_ASSERT(size <= buffer.size());
  452. BOOST_ASSERT(buffer.get_context() == this->get_context());
  453. BOOST_ASSERT(host_ptr != 0);
  454. event event_;
  455. cl_int ret = clEnqueueWriteBuffer(
  456. m_queue,
  457. buffer.get(),
  458. CL_FALSE,
  459. offset,
  460. size,
  461. host_ptr,
  462. events.size(),
  463. events.get_event_ptr(),
  464. &event_.get()
  465. );
  466. if(ret != CL_SUCCESS){
  467. BOOST_THROW_EXCEPTION(opencl_error(ret));
  468. }
  469. return event_;
  470. }
  471. #if defined(BOOST_COMPUTE_CL_VERSION_1_1) || defined(BOOST_COMPUTE_DOXYGEN_INVOKED)
  472. /// Enqueues a command to write a rectangular region from host memory
  473. /// to \p buffer.
  474. ///
  475. /// \see_opencl_ref{clEnqueueWriteBufferRect}
  476. ///
  477. /// \opencl_version_warning{1,1}
  478. event enqueue_write_buffer_rect(const buffer &buffer,
  479. const size_t buffer_origin[3],
  480. const size_t host_origin[3],
  481. const size_t region[3],
  482. size_t buffer_row_pitch,
  483. size_t buffer_slice_pitch,
  484. size_t host_row_pitch,
  485. size_t host_slice_pitch,
  486. void *host_ptr,
  487. const wait_list &events = wait_list())
  488. {
  489. BOOST_ASSERT(m_queue != 0);
  490. BOOST_ASSERT(buffer.get_context() == this->get_context());
  491. BOOST_ASSERT(host_ptr != 0);
  492. event event_;
  493. cl_int ret = clEnqueueWriteBufferRect(
  494. m_queue,
  495. buffer.get(),
  496. CL_TRUE,
  497. buffer_origin,
  498. host_origin,
  499. region,
  500. buffer_row_pitch,
  501. buffer_slice_pitch,
  502. host_row_pitch,
  503. host_slice_pitch,
  504. host_ptr,
  505. events.size(),
  506. events.get_event_ptr(),
  507. &event_.get()
  508. );
  509. if(ret != CL_SUCCESS){
  510. BOOST_THROW_EXCEPTION(opencl_error(ret));
  511. }
  512. return event_;
  513. }
  514. /// Enqueues a command to write a rectangular region from host memory
  515. /// to \p buffer. The copy is performed asynchronously.
  516. ///
  517. /// \see_opencl_ref{clEnqueueWriteBufferRect}
  518. ///
  519. /// \opencl_version_warning{1,1}
  520. event enqueue_write_buffer_rect_async(const buffer &buffer,
  521. const size_t buffer_origin[3],
  522. const size_t host_origin[3],
  523. const size_t region[3],
  524. size_t buffer_row_pitch,
  525. size_t buffer_slice_pitch,
  526. size_t host_row_pitch,
  527. size_t host_slice_pitch,
  528. void *host_ptr,
  529. const wait_list &events = wait_list())
  530. {
  531. BOOST_ASSERT(m_queue != 0);
  532. BOOST_ASSERT(buffer.get_context() == this->get_context());
  533. BOOST_ASSERT(host_ptr != 0);
  534. event event_;
  535. cl_int ret = clEnqueueWriteBufferRect(
  536. m_queue,
  537. buffer.get(),
  538. CL_FALSE,
  539. buffer_origin,
  540. host_origin,
  541. region,
  542. buffer_row_pitch,
  543. buffer_slice_pitch,
  544. host_row_pitch,
  545. host_slice_pitch,
  546. host_ptr,
  547. events.size(),
  548. events.get_event_ptr(),
  549. &event_.get()
  550. );
  551. if(ret != CL_SUCCESS){
  552. BOOST_THROW_EXCEPTION(opencl_error(ret));
  553. }
  554. return event_;
  555. }
  556. #endif // BOOST_COMPUTE_CL_VERSION_1_1
  557. /// Enqueues a command to copy data from \p src_buffer to
  558. /// \p dst_buffer.
  559. ///
  560. /// \see_opencl_ref{clEnqueueCopyBuffer}
  561. ///
  562. /// \see copy()
  563. event enqueue_copy_buffer(const buffer &src_buffer,
  564. const buffer &dst_buffer,
  565. size_t src_offset,
  566. size_t dst_offset,
  567. size_t size,
  568. const wait_list &events = wait_list())
  569. {
  570. BOOST_ASSERT(m_queue != 0);
  571. BOOST_ASSERT(src_offset + size <= src_buffer.size());
  572. BOOST_ASSERT(dst_offset + size <= dst_buffer.size());
  573. BOOST_ASSERT(src_buffer.get_context() == this->get_context());
  574. BOOST_ASSERT(dst_buffer.get_context() == this->get_context());
  575. event event_;
  576. cl_int ret = clEnqueueCopyBuffer(
  577. m_queue,
  578. src_buffer.get(),
  579. dst_buffer.get(),
  580. src_offset,
  581. dst_offset,
  582. size,
  583. events.size(),
  584. events.get_event_ptr(),
  585. &event_.get()
  586. );
  587. if(ret != CL_SUCCESS){
  588. BOOST_THROW_EXCEPTION(opencl_error(ret));
  589. }
  590. return event_;
  591. }
  592. #if defined(BOOST_COMPUTE_CL_VERSION_1_1) || defined(BOOST_COMPUTE_DOXYGEN_INVOKED)
  593. /// Enqueues a command to copy a rectangular region from
  594. /// \p src_buffer to \p dst_buffer.
  595. ///
  596. /// \see_opencl_ref{clEnqueueCopyBufferRect}
  597. ///
  598. /// \opencl_version_warning{1,1}
  599. event enqueue_copy_buffer_rect(const buffer &src_buffer,
  600. const buffer &dst_buffer,
  601. const size_t src_origin[3],
  602. const size_t dst_origin[3],
  603. const size_t region[3],
  604. size_t buffer_row_pitch,
  605. size_t buffer_slice_pitch,
  606. size_t host_row_pitch,
  607. size_t host_slice_pitch,
  608. const wait_list &events = wait_list())
  609. {
  610. BOOST_ASSERT(m_queue != 0);
  611. BOOST_ASSERT(src_buffer.get_context() == this->get_context());
  612. BOOST_ASSERT(dst_buffer.get_context() == this->get_context());
  613. event event_;
  614. cl_int ret = clEnqueueCopyBufferRect(
  615. m_queue,
  616. src_buffer.get(),
  617. dst_buffer.get(),
  618. src_origin,
  619. dst_origin,
  620. region,
  621. buffer_row_pitch,
  622. buffer_slice_pitch,
  623. host_row_pitch,
  624. host_slice_pitch,
  625. events.size(),
  626. events.get_event_ptr(),
  627. &event_.get()
  628. );
  629. if(ret != CL_SUCCESS){
  630. BOOST_THROW_EXCEPTION(opencl_error(ret));
  631. }
  632. return event_;
  633. }
  634. #endif // BOOST_COMPUTE_CL_VERSION_1_1
  635. #if defined(BOOST_COMPUTE_CL_VERSION_1_2) || defined(BOOST_COMPUTE_DOXYGEN_INVOKED)
  636. /// Enqueues a command to fill \p buffer with \p pattern.
  637. ///
  638. /// \see_opencl_ref{clEnqueueFillBuffer}
  639. ///
  640. /// \opencl_version_warning{1,2}
  641. ///
  642. /// \see fill()
  643. event enqueue_fill_buffer(const buffer &buffer,
  644. const void *pattern,
  645. size_t pattern_size,
  646. size_t offset,
  647. size_t size,
  648. const wait_list &events = wait_list())
  649. {
  650. BOOST_ASSERT(m_queue != 0);
  651. BOOST_ASSERT(offset + size <= buffer.size());
  652. BOOST_ASSERT(buffer.get_context() == this->get_context());
  653. event event_;
  654. cl_int ret = clEnqueueFillBuffer(
  655. m_queue,
  656. buffer.get(),
  657. pattern,
  658. pattern_size,
  659. offset,
  660. size,
  661. events.size(),
  662. events.get_event_ptr(),
  663. &event_.get()
  664. );
  665. if(ret != CL_SUCCESS){
  666. BOOST_THROW_EXCEPTION(opencl_error(ret));
  667. }
  668. return event_;
  669. }
  670. #endif // BOOST_COMPUTE_CL_VERSION_1_2
  671. /// Enqueues a command to map \p buffer into the host address space.
  672. /// Event associated with map operation is returned through
  673. /// \p map_buffer_event parameter.
  674. ///
  675. /// \see_opencl_ref{clEnqueueMapBuffer}
  676. void* enqueue_map_buffer(const buffer &buffer,
  677. cl_map_flags flags,
  678. size_t offset,
  679. size_t size,
  680. event &map_buffer_event,
  681. const wait_list &events = wait_list())
  682. {
  683. BOOST_ASSERT(m_queue != 0);
  684. BOOST_ASSERT(offset + size <= buffer.size());
  685. BOOST_ASSERT(buffer.get_context() == this->get_context());
  686. cl_int ret = 0;
  687. void *pointer = clEnqueueMapBuffer(
  688. m_queue,
  689. buffer.get(),
  690. CL_TRUE,
  691. flags,
  692. offset,
  693. size,
  694. events.size(),
  695. events.get_event_ptr(),
  696. &map_buffer_event.get(),
  697. &ret
  698. );
  699. if(ret != CL_SUCCESS){
  700. BOOST_THROW_EXCEPTION(opencl_error(ret));
  701. }
  702. return pointer;
  703. }
  704. /// \overload
  705. void* enqueue_map_buffer(const buffer &buffer,
  706. cl_map_flags flags,
  707. size_t offset,
  708. size_t size,
  709. const wait_list &events = wait_list())
  710. {
  711. event event_;
  712. return enqueue_map_buffer(buffer, flags, offset, size, event_, events);
  713. }
  714. /// Enqueues a command to map \p buffer into the host address space.
  715. /// Map operation is performed asynchronously. The pointer to the mapped
  716. /// region cannot be used until the map operation has completed.
  717. ///
  718. /// Event associated with map operation is returned through
  719. /// \p map_buffer_event parameter.
  720. ///
  721. /// \see_opencl_ref{clEnqueueMapBuffer}
  722. void* enqueue_map_buffer_async(const buffer &buffer,
  723. cl_map_flags flags,
  724. size_t offset,
  725. size_t size,
  726. event &map_buffer_event,
  727. const wait_list &events = wait_list())
  728. {
  729. BOOST_ASSERT(m_queue != 0);
  730. BOOST_ASSERT(offset + size <= buffer.size());
  731. BOOST_ASSERT(buffer.get_context() == this->get_context());
  732. cl_int ret = 0;
  733. void *pointer = clEnqueueMapBuffer(
  734. m_queue,
  735. buffer.get(),
  736. CL_FALSE,
  737. flags,
  738. offset,
  739. size,
  740. events.size(),
  741. events.get_event_ptr(),
  742. &map_buffer_event.get(),
  743. &ret
  744. );
  745. if(ret != CL_SUCCESS){
  746. BOOST_THROW_EXCEPTION(opencl_error(ret));
  747. }
  748. return pointer;
  749. }
  750. /// Enqueues a command to unmap \p buffer from the host memory space.
  751. ///
  752. /// \see_opencl_ref{clEnqueueUnmapMemObject}
  753. event enqueue_unmap_buffer(const buffer &buffer,
  754. void *mapped_ptr,
  755. const wait_list &events = wait_list())
  756. {
  757. BOOST_ASSERT(buffer.get_context() == this->get_context());
  758. return enqueue_unmap_mem_object(buffer.get(), mapped_ptr, events);
  759. }
  760. /// Enqueues a command to unmap \p mem from the host memory space.
  761. ///
  762. /// \see_opencl_ref{clEnqueueUnmapMemObject}
  763. event enqueue_unmap_mem_object(cl_mem mem,
  764. void *mapped_ptr,
  765. const wait_list &events = wait_list())
  766. {
  767. BOOST_ASSERT(m_queue != 0);
  768. event event_;
  769. cl_int ret = clEnqueueUnmapMemObject(
  770. m_queue,
  771. mem,
  772. mapped_ptr,
  773. events.size(),
  774. events.get_event_ptr(),
  775. &event_.get()
  776. );
  777. if(ret != CL_SUCCESS){
  778. BOOST_THROW_EXCEPTION(opencl_error(ret));
  779. }
  780. return event_;
  781. }
  782. /// Enqueues a command to read data from \p image to host memory.
  783. ///
  784. /// \see_opencl_ref{clEnqueueReadImage}
  785. event enqueue_read_image(const image_object& image,
  786. const size_t *origin,
  787. const size_t *region,
  788. size_t row_pitch,
  789. size_t slice_pitch,
  790. void *host_ptr,
  791. const wait_list &events = wait_list())
  792. {
  793. BOOST_ASSERT(m_queue != 0);
  794. event event_;
  795. cl_int ret = clEnqueueReadImage(
  796. m_queue,
  797. image.get(),
  798. CL_TRUE,
  799. origin,
  800. region,
  801. row_pitch,
  802. slice_pitch,
  803. host_ptr,
  804. events.size(),
  805. events.get_event_ptr(),
  806. &event_.get()
  807. );
  808. if(ret != CL_SUCCESS){
  809. BOOST_THROW_EXCEPTION(opencl_error(ret));
  810. }
  811. return event_;
  812. }
  813. /// \overload
  814. template<size_t N>
  815. event enqueue_read_image(const image_object& image,
  816. const extents<N> origin,
  817. const extents<N> region,
  818. void *host_ptr,
  819. size_t row_pitch = 0,
  820. size_t slice_pitch = 0,
  821. const wait_list &events = wait_list())
  822. {
  823. BOOST_ASSERT(image.get_context() == this->get_context());
  824. size_t origin3[3] = { 0, 0, 0 };
  825. size_t region3[3] = { 1, 1, 1 };
  826. std::copy(origin.data(), origin.data() + N, origin3);
  827. std::copy(region.data(), region.data() + N, region3);
  828. return enqueue_read_image(
  829. image, origin3, region3, row_pitch, slice_pitch, host_ptr, events
  830. );
  831. }
  832. /// Enqueues a command to write data from host memory to \p image.
  833. ///
  834. /// \see_opencl_ref{clEnqueueWriteImage}
  835. event enqueue_write_image(image_object& image,
  836. const size_t *origin,
  837. const size_t *region,
  838. const void *host_ptr,
  839. size_t input_row_pitch = 0,
  840. size_t input_slice_pitch = 0,
  841. const wait_list &events = wait_list())
  842. {
  843. BOOST_ASSERT(m_queue != 0);
  844. event event_;
  845. cl_int ret = clEnqueueWriteImage(
  846. m_queue,
  847. image.get(),
  848. CL_TRUE,
  849. origin,
  850. region,
  851. input_row_pitch,
  852. input_slice_pitch,
  853. host_ptr,
  854. events.size(),
  855. events.get_event_ptr(),
  856. &event_.get()
  857. );
  858. if(ret != CL_SUCCESS){
  859. BOOST_THROW_EXCEPTION(opencl_error(ret));
  860. }
  861. return event_;
  862. }
  863. /// \overload
  864. template<size_t N>
  865. event enqueue_write_image(image_object& image,
  866. const extents<N> origin,
  867. const extents<N> region,
  868. const void *host_ptr,
  869. const size_t input_row_pitch = 0,
  870. const size_t input_slice_pitch = 0,
  871. const wait_list &events = wait_list())
  872. {
  873. BOOST_ASSERT(image.get_context() == this->get_context());
  874. size_t origin3[3] = { 0, 0, 0 };
  875. size_t region3[3] = { 1, 1, 1 };
  876. std::copy(origin.data(), origin.data() + N, origin3);
  877. std::copy(region.data(), region.data() + N, region3);
  878. return enqueue_write_image(
  879. image, origin3, region3, host_ptr, input_row_pitch, input_slice_pitch, events
  880. );
  881. }
  882. /// Enqueues a command to map \p image into the host address space.
  883. ///
  884. /// Event associated with map operation is returned through
  885. /// \p map_image_event parameter.
  886. ///
  887. /// \see_opencl_ref{clEnqueueMapImage}
  888. void* enqueue_map_image(const image_object &image,
  889. cl_map_flags flags,
  890. const size_t *origin,
  891. const size_t *region,
  892. size_t &output_row_pitch,
  893. size_t &output_slice_pitch,
  894. event &map_image_event,
  895. const wait_list &events = wait_list())
  896. {
  897. BOOST_ASSERT(m_queue != 0);
  898. BOOST_ASSERT(image.get_context() == this->get_context());
  899. cl_int ret = 0;
  900. void *pointer = clEnqueueMapImage(
  901. m_queue,
  902. image.get(),
  903. CL_TRUE,
  904. flags,
  905. origin,
  906. region,
  907. &output_row_pitch,
  908. &output_slice_pitch,
  909. events.size(),
  910. events.get_event_ptr(),
  911. &map_image_event.get(),
  912. &ret
  913. );
  914. if(ret != CL_SUCCESS){
  915. BOOST_THROW_EXCEPTION(opencl_error(ret));
  916. }
  917. return pointer;
  918. }
  919. /// \overload
  920. void* enqueue_map_image(const image_object &image,
  921. cl_map_flags flags,
  922. const size_t *origin,
  923. const size_t *region,
  924. size_t &output_row_pitch,
  925. size_t &output_slice_pitch,
  926. const wait_list &events = wait_list())
  927. {
  928. event event_;
  929. return enqueue_map_image(
  930. image, flags, origin, region,
  931. output_row_pitch, output_slice_pitch, event_, events
  932. );
  933. }
  934. /// \overload
  935. template<size_t N>
  936. void* enqueue_map_image(image_object& image,
  937. cl_map_flags flags,
  938. const extents<N> origin,
  939. const extents<N> region,
  940. size_t &output_row_pitch,
  941. size_t &output_slice_pitch,
  942. event &map_image_event,
  943. const wait_list &events = wait_list())
  944. {
  945. BOOST_ASSERT(image.get_context() == this->get_context());
  946. size_t origin3[3] = { 0, 0, 0 };
  947. size_t region3[3] = { 1, 1, 1 };
  948. std::copy(origin.data(), origin.data() + N, origin3);
  949. std::copy(region.data(), region.data() + N, region3);
  950. return enqueue_map_image(
  951. image, flags, origin3, region3,
  952. output_row_pitch, output_slice_pitch, map_image_event, events
  953. );
  954. }
  955. /// \overload
  956. template<size_t N>
  957. void* enqueue_map_image(image_object& image,
  958. cl_map_flags flags,
  959. const extents<N> origin,
  960. const extents<N> region,
  961. size_t &output_row_pitch,
  962. size_t &output_slice_pitch,
  963. const wait_list &events = wait_list())
  964. {
  965. event event_;
  966. return enqueue_map_image(
  967. image, flags, origin, region,
  968. output_row_pitch, output_slice_pitch, event_, events
  969. );
  970. }
  971. /// Enqueues a command to map \p image into the host address space.
  972. /// Map operation is performed asynchronously. The pointer to the mapped
  973. /// region cannot be used until the map operation has completed.
  974. ///
  975. /// Event associated with map operation is returned through
  976. /// \p map_image_event parameter.
  977. ///
  978. /// \see_opencl_ref{clEnqueueMapImage}
  979. void* enqueue_map_image_async(const image_object &image,
  980. cl_map_flags flags,
  981. const size_t *origin,
  982. const size_t *region,
  983. size_t &output_row_pitch,
  984. size_t &output_slice_pitch,
  985. event &map_image_event,
  986. const wait_list &events = wait_list())
  987. {
  988. BOOST_ASSERT(m_queue != 0);
  989. BOOST_ASSERT(image.get_context() == this->get_context());
  990. cl_int ret = 0;
  991. void *pointer = clEnqueueMapImage(
  992. m_queue,
  993. image.get(),
  994. CL_FALSE,
  995. flags,
  996. origin,
  997. region,
  998. &output_row_pitch,
  999. &output_slice_pitch,
  1000. events.size(),
  1001. events.get_event_ptr(),
  1002. &map_image_event.get(),
  1003. &ret
  1004. );
  1005. if(ret != CL_SUCCESS){
  1006. BOOST_THROW_EXCEPTION(opencl_error(ret));
  1007. }
  1008. return pointer;
  1009. }
  1010. /// \overload
  1011. template<size_t N>
  1012. void* enqueue_map_image_async(image_object& image,
  1013. cl_map_flags flags,
  1014. const extents<N> origin,
  1015. const extents<N> region,
  1016. size_t &output_row_pitch,
  1017. size_t &output_slice_pitch,
  1018. event &map_image_event,
  1019. const wait_list &events = wait_list())
  1020. {
  1021. BOOST_ASSERT(image.get_context() == this->get_context());
  1022. size_t origin3[3] = { 0, 0, 0 };
  1023. size_t region3[3] = { 1, 1, 1 };
  1024. std::copy(origin.data(), origin.data() + N, origin3);
  1025. std::copy(region.data(), region.data() + N, region3);
  1026. return enqueue_map_image_async(
  1027. image, flags, origin3, region3,
  1028. output_row_pitch, output_slice_pitch, map_image_event, events
  1029. );
  1030. }
  1031. /// Enqueues a command to unmap \p image from the host memory space.
  1032. ///
  1033. /// \see_opencl_ref{clEnqueueUnmapMemObject}
  1034. event enqueue_unmap_image(const image_object &image,
  1035. void *mapped_ptr,
  1036. const wait_list &events = wait_list())
  1037. {
  1038. BOOST_ASSERT(image.get_context() == this->get_context());
  1039. return enqueue_unmap_mem_object(image.get(), mapped_ptr, events);
  1040. }
  1041. /// Enqueues a command to copy data from \p src_image to \p dst_image.
  1042. ///
  1043. /// \see_opencl_ref{clEnqueueCopyImage}
  1044. event enqueue_copy_image(const image_object& src_image,
  1045. image_object& dst_image,
  1046. const size_t *src_origin,
  1047. const size_t *dst_origin,
  1048. const size_t *region,
  1049. const wait_list &events = wait_list())
  1050. {
  1051. BOOST_ASSERT(m_queue != 0);
  1052. event event_;
  1053. cl_int ret = clEnqueueCopyImage(
  1054. m_queue,
  1055. src_image.get(),
  1056. dst_image.get(),
  1057. src_origin,
  1058. dst_origin,
  1059. region,
  1060. events.size(),
  1061. events.get_event_ptr(),
  1062. &event_.get()
  1063. );
  1064. if(ret != CL_SUCCESS){
  1065. BOOST_THROW_EXCEPTION(opencl_error(ret));
  1066. }
  1067. return event_;
  1068. }
  1069. /// \overload
  1070. template<size_t N>
  1071. event enqueue_copy_image(const image_object& src_image,
  1072. image_object& dst_image,
  1073. const extents<N> src_origin,
  1074. const extents<N> dst_origin,
  1075. const extents<N> region,
  1076. const wait_list &events = wait_list())
  1077. {
  1078. BOOST_ASSERT(src_image.get_context() == this->get_context());
  1079. BOOST_ASSERT(dst_image.get_context() == this->get_context());
  1080. BOOST_ASSERT_MSG(src_image.format() == dst_image.format(),
  1081. "Source and destination image formats must match.");
  1082. size_t src_origin3[3] = { 0, 0, 0 };
  1083. size_t dst_origin3[3] = { 0, 0, 0 };
  1084. size_t region3[3] = { 1, 1, 1 };
  1085. std::copy(src_origin.data(), src_origin.data() + N, src_origin3);
  1086. std::copy(dst_origin.data(), dst_origin.data() + N, dst_origin3);
  1087. std::copy(region.data(), region.data() + N, region3);
  1088. return enqueue_copy_image(
  1089. src_image, dst_image, src_origin3, dst_origin3, region3, events
  1090. );
  1091. }
  1092. /// Enqueues a command to copy data from \p src_image to \p dst_buffer.
  1093. ///
  1094. /// \see_opencl_ref{clEnqueueCopyImageToBuffer}
  1095. event enqueue_copy_image_to_buffer(const image_object& src_image,
  1096. memory_object& dst_buffer,
  1097. const size_t *src_origin,
  1098. const size_t *region,
  1099. size_t dst_offset,
  1100. const wait_list &events = wait_list())
  1101. {
  1102. BOOST_ASSERT(m_queue != 0);
  1103. event event_;
  1104. cl_int ret = clEnqueueCopyImageToBuffer(
  1105. m_queue,
  1106. src_image.get(),
  1107. dst_buffer.get(),
  1108. src_origin,
  1109. region,
  1110. dst_offset,
  1111. events.size(),
  1112. events.get_event_ptr(),
  1113. &event_.get()
  1114. );
  1115. if(ret != CL_SUCCESS){
  1116. BOOST_THROW_EXCEPTION(opencl_error(ret));
  1117. }
  1118. return event_;
  1119. }
  1120. /// Enqueues a command to copy data from \p src_buffer to \p dst_image.
  1121. ///
  1122. /// \see_opencl_ref{clEnqueueCopyBufferToImage}
  1123. event enqueue_copy_buffer_to_image(const memory_object& src_buffer,
  1124. image_object& dst_image,
  1125. size_t src_offset,
  1126. const size_t *dst_origin,
  1127. const size_t *region,
  1128. const wait_list &events = wait_list())
  1129. {
  1130. BOOST_ASSERT(m_queue != 0);
  1131. event event_;
  1132. cl_int ret = clEnqueueCopyBufferToImage(
  1133. m_queue,
  1134. src_buffer.get(),
  1135. dst_image.get(),
  1136. src_offset,
  1137. dst_origin,
  1138. region,
  1139. events.size(),
  1140. events.get_event_ptr(),
  1141. &event_.get()
  1142. );
  1143. if(ret != CL_SUCCESS){
  1144. BOOST_THROW_EXCEPTION(opencl_error(ret));
  1145. }
  1146. return event_;
  1147. }
  1148. #if defined(BOOST_COMPUTE_CL_VERSION_1_2) || defined(BOOST_COMPUTE_DOXYGEN_INVOKED)
  1149. /// Enqueues a command to fill \p image with \p fill_color.
  1150. ///
  1151. /// \see_opencl_ref{clEnqueueFillImage}
  1152. ///
  1153. /// \opencl_version_warning{1,2}
  1154. event enqueue_fill_image(image_object& image,
  1155. const void *fill_color,
  1156. const size_t *origin,
  1157. const size_t *region,
  1158. const wait_list &events = wait_list())
  1159. {
  1160. BOOST_ASSERT(m_queue != 0);
  1161. event event_;
  1162. cl_int ret = clEnqueueFillImage(
  1163. m_queue,
  1164. image.get(),
  1165. fill_color,
  1166. origin,
  1167. region,
  1168. events.size(),
  1169. events.get_event_ptr(),
  1170. &event_.get()
  1171. );
  1172. if(ret != CL_SUCCESS){
  1173. BOOST_THROW_EXCEPTION(opencl_error(ret));
  1174. }
  1175. return event_;
  1176. }
  1177. /// \overload
  1178. template<size_t N>
  1179. event enqueue_fill_image(image_object& image,
  1180. const void *fill_color,
  1181. const extents<N> origin,
  1182. const extents<N> region,
  1183. const wait_list &events = wait_list())
  1184. {
  1185. BOOST_ASSERT(image.get_context() == this->get_context());
  1186. size_t origin3[3] = { 0, 0, 0 };
  1187. size_t region3[3] = { 1, 1, 1 };
  1188. std::copy(origin.data(), origin.data() + N, origin3);
  1189. std::copy(region.data(), region.data() + N, region3);
  1190. return enqueue_fill_image(
  1191. image, fill_color, origin3, region3, events
  1192. );
  1193. }
  1194. /// Enqueues a command to migrate \p mem_objects.
  1195. ///
  1196. /// \see_opencl_ref{clEnqueueMigrateMemObjects}
  1197. ///
  1198. /// \opencl_version_warning{1,2}
  1199. event enqueue_migrate_memory_objects(uint_ num_mem_objects,
  1200. const cl_mem *mem_objects,
  1201. cl_mem_migration_flags flags,
  1202. const wait_list &events = wait_list())
  1203. {
  1204. BOOST_ASSERT(m_queue != 0);
  1205. event event_;
  1206. cl_int ret = clEnqueueMigrateMemObjects(
  1207. m_queue,
  1208. num_mem_objects,
  1209. mem_objects,
  1210. flags,
  1211. events.size(),
  1212. events.get_event_ptr(),
  1213. &event_.get()
  1214. );
  1215. if(ret != CL_SUCCESS){
  1216. BOOST_THROW_EXCEPTION(opencl_error(ret));
  1217. }
  1218. return event_;
  1219. }
  1220. #endif // BOOST_COMPUTE_CL_VERSION_1_2
  1221. /// Enqueues a kernel for execution.
  1222. ///
  1223. /// \see_opencl_ref{clEnqueueNDRangeKernel}
  1224. event enqueue_nd_range_kernel(const kernel &kernel,
  1225. size_t work_dim,
  1226. const size_t *global_work_offset,
  1227. const size_t *global_work_size,
  1228. const size_t *local_work_size,
  1229. const wait_list &events = wait_list())
  1230. {
  1231. BOOST_ASSERT(m_queue != 0);
  1232. BOOST_ASSERT(kernel.get_context() == this->get_context());
  1233. event event_;
  1234. cl_int ret = clEnqueueNDRangeKernel(
  1235. m_queue,
  1236. kernel,
  1237. static_cast<cl_uint>(work_dim),
  1238. global_work_offset,
  1239. global_work_size,
  1240. local_work_size,
  1241. events.size(),
  1242. events.get_event_ptr(),
  1243. &event_.get()
  1244. );
  1245. if(ret != CL_SUCCESS){
  1246. BOOST_THROW_EXCEPTION(opencl_error(ret));
  1247. }
  1248. return event_;
  1249. }
  1250. /// \overload
  1251. template<size_t N>
  1252. event enqueue_nd_range_kernel(const kernel &kernel,
  1253. const extents<N> &global_work_offset,
  1254. const extents<N> &global_work_size,
  1255. const extents<N> &local_work_size,
  1256. const wait_list &events = wait_list())
  1257. {
  1258. return enqueue_nd_range_kernel(
  1259. kernel,
  1260. N,
  1261. global_work_offset.data(),
  1262. global_work_size.data(),
  1263. local_work_size.data(),
  1264. events
  1265. );
  1266. }
  1267. /// Convenience method which calls enqueue_nd_range_kernel() with a
  1268. /// one-dimensional range.
  1269. event enqueue_1d_range_kernel(const kernel &kernel,
  1270. size_t global_work_offset,
  1271. size_t global_work_size,
  1272. size_t local_work_size,
  1273. const wait_list &events = wait_list())
  1274. {
  1275. return enqueue_nd_range_kernel(
  1276. kernel,
  1277. 1,
  1278. &global_work_offset,
  1279. &global_work_size,
  1280. local_work_size ? &local_work_size : 0,
  1281. events
  1282. );
  1283. }
  1284. /// Enqueues a kernel to execute using a single work-item.
  1285. ///
  1286. /// \see_opencl_ref{clEnqueueTask}
  1287. event enqueue_task(const kernel &kernel, const wait_list &events = wait_list())
  1288. {
  1289. BOOST_ASSERT(m_queue != 0);
  1290. BOOST_ASSERT(kernel.get_context() == this->get_context());
  1291. event event_;
  1292. // clEnqueueTask() was deprecated in OpenCL 2.0. In that case we
  1293. // just forward to the equivalent clEnqueueNDRangeKernel() call.
  1294. #ifdef BOOST_COMPUTE_CL_VERSION_2_0
  1295. size_t one = 1;
  1296. cl_int ret = clEnqueueNDRangeKernel(
  1297. m_queue, kernel, 1, 0, &one, &one,
  1298. events.size(), events.get_event_ptr(), &event_.get()
  1299. );
  1300. #else
  1301. cl_int ret = clEnqueueTask(
  1302. m_queue, kernel, events.size(), events.get_event_ptr(), &event_.get()
  1303. );
  1304. #endif
  1305. if(ret != CL_SUCCESS){
  1306. BOOST_THROW_EXCEPTION(opencl_error(ret));
  1307. }
  1308. return event_;
  1309. }
  1310. /// Enqueues a function to execute on the host.
  1311. event enqueue_native_kernel(void (BOOST_COMPUTE_CL_CALLBACK *user_func)(void *),
  1312. void *args,
  1313. size_t cb_args,
  1314. uint_ num_mem_objects,
  1315. const cl_mem *mem_list,
  1316. const void **args_mem_loc,
  1317. const wait_list &events = wait_list())
  1318. {
  1319. BOOST_ASSERT(m_queue != 0);
  1320. event event_;
  1321. cl_int ret = clEnqueueNativeKernel(
  1322. m_queue,
  1323. user_func,
  1324. args,
  1325. cb_args,
  1326. num_mem_objects,
  1327. mem_list,
  1328. args_mem_loc,
  1329. events.size(),
  1330. events.get_event_ptr(),
  1331. &event_.get()
  1332. );
  1333. if(ret != CL_SUCCESS){
  1334. BOOST_THROW_EXCEPTION(opencl_error(ret));
  1335. }
  1336. return event_;
  1337. }
  1338. /// Convenience overload for enqueue_native_kernel() which enqueues a
  1339. /// native kernel on the host with a nullary function.
  1340. event enqueue_native_kernel(void (BOOST_COMPUTE_CL_CALLBACK *user_func)(void),
  1341. const wait_list &events = wait_list())
  1342. {
  1343. return enqueue_native_kernel(
  1344. detail::nullary_native_kernel_trampoline,
  1345. reinterpret_cast<void *>(&user_func),
  1346. sizeof(user_func),
  1347. 0,
  1348. 0,
  1349. 0,
  1350. events
  1351. );
  1352. }
  1353. /// Flushes the command queue.
  1354. ///
  1355. /// \see_opencl_ref{clFlush}
  1356. void flush()
  1357. {
  1358. BOOST_ASSERT(m_queue != 0);
  1359. cl_int ret = clFlush(m_queue);
  1360. if(ret != CL_SUCCESS){
  1361. BOOST_THROW_EXCEPTION(opencl_error(ret));
  1362. }
  1363. }
  1364. /// Blocks until all outstanding commands in the queue have finished.
  1365. ///
  1366. /// \see_opencl_ref{clFinish}
  1367. void finish()
  1368. {
  1369. BOOST_ASSERT(m_queue != 0);
  1370. cl_int ret = clFinish(m_queue);
  1371. if(ret != CL_SUCCESS){
  1372. BOOST_THROW_EXCEPTION(opencl_error(ret));
  1373. }
  1374. }
  1375. /// Enqueues a barrier in the queue.
  1376. void enqueue_barrier()
  1377. {
  1378. BOOST_ASSERT(m_queue != 0);
  1379. cl_int ret = CL_SUCCESS;
  1380. #ifdef BOOST_COMPUTE_CL_VERSION_1_2
  1381. if(get_device().check_version(1, 2)){
  1382. ret = clEnqueueBarrierWithWaitList(m_queue, 0, 0, 0);
  1383. } else
  1384. #endif // BOOST_COMPUTE_CL_VERSION_1_2
  1385. {
  1386. // Suppress deprecated declarations warning
  1387. BOOST_COMPUTE_DISABLE_DEPRECATED_DECLARATIONS();
  1388. ret = clEnqueueBarrier(m_queue);
  1389. BOOST_COMPUTE_ENABLE_DEPRECATED_DECLARATIONS();
  1390. }
  1391. if(ret != CL_SUCCESS){
  1392. BOOST_THROW_EXCEPTION(opencl_error(ret));
  1393. }
  1394. }
  1395. #if defined(BOOST_COMPUTE_CL_VERSION_1_2) || defined(BOOST_COMPUTE_DOXYGEN_INVOKED)
  1396. /// Enqueues a barrier in the queue after \p events.
  1397. ///
  1398. /// \opencl_version_warning{1,2}
  1399. event enqueue_barrier(const wait_list &events)
  1400. {
  1401. BOOST_ASSERT(m_queue != 0);
  1402. event event_;
  1403. cl_int ret = CL_SUCCESS;
  1404. ret = clEnqueueBarrierWithWaitList(
  1405. m_queue, events.size(), events.get_event_ptr(), &event_.get()
  1406. );
  1407. if(ret != CL_SUCCESS){
  1408. BOOST_THROW_EXCEPTION(opencl_error(ret));
  1409. }
  1410. return event_;
  1411. }
  1412. #endif // BOOST_COMPUTE_CL_VERSION_1_2
  1413. /// Enqueues a marker in the queue and returns an event that can be
  1414. /// used to track its progress.
  1415. event enqueue_marker()
  1416. {
  1417. event event_;
  1418. cl_int ret = CL_SUCCESS;
  1419. #ifdef BOOST_COMPUTE_CL_VERSION_1_2
  1420. if(get_device().check_version(1, 2)){
  1421. ret = clEnqueueMarkerWithWaitList(m_queue, 0, 0, &event_.get());
  1422. } else
  1423. #endif
  1424. {
  1425. // Suppress deprecated declarations warning
  1426. BOOST_COMPUTE_DISABLE_DEPRECATED_DECLARATIONS();
  1427. ret = clEnqueueMarker(m_queue, &event_.get());
  1428. BOOST_COMPUTE_ENABLE_DEPRECATED_DECLARATIONS();
  1429. }
  1430. if(ret != CL_SUCCESS){
  1431. BOOST_THROW_EXCEPTION(opencl_error(ret));
  1432. }
  1433. return event_;
  1434. }
  1435. #if defined(BOOST_COMPUTE_CL_VERSION_1_2) || defined(BOOST_COMPUTE_DOXYGEN_INVOKED)
  1436. /// Enqueues a marker after \p events in the queue and returns an
  1437. /// event that can be used to track its progress.
  1438. ///
  1439. /// \opencl_version_warning{1,2}
  1440. event enqueue_marker(const wait_list &events)
  1441. {
  1442. event event_;
  1443. cl_int ret = clEnqueueMarkerWithWaitList(
  1444. m_queue, events.size(), events.get_event_ptr(), &event_.get()
  1445. );
  1446. if(ret != CL_SUCCESS){
  1447. BOOST_THROW_EXCEPTION(opencl_error(ret));
  1448. }
  1449. return event_;
  1450. }
  1451. #endif // BOOST_COMPUTE_CL_VERSION_1_2
  1452. #if defined(BOOST_COMPUTE_CL_VERSION_2_0) || defined(BOOST_COMPUTE_DOXYGEN_INVOKED)
  1453. /// Enqueues a command to copy \p size bytes of data from \p src_ptr to
  1454. /// \p dst_ptr.
  1455. ///
  1456. /// \opencl_version_warning{2,0}
  1457. ///
  1458. /// \see_opencl2_ref{clEnqueueSVMMemcpy}
  1459. event enqueue_svm_memcpy(void *dst_ptr,
  1460. const void *src_ptr,
  1461. size_t size,
  1462. const wait_list &events = wait_list())
  1463. {
  1464. event event_;
  1465. cl_int ret = clEnqueueSVMMemcpy(
  1466. m_queue,
  1467. CL_TRUE,
  1468. dst_ptr,
  1469. src_ptr,
  1470. size,
  1471. events.size(),
  1472. events.get_event_ptr(),
  1473. &event_.get()
  1474. );
  1475. if(ret != CL_SUCCESS){
  1476. BOOST_THROW_EXCEPTION(opencl_error(ret));
  1477. }
  1478. return event_;
  1479. }
  1480. /// Enqueues a command to copy \p size bytes of data from \p src_ptr to
  1481. /// \p dst_ptr. The operation is performed asynchronously.
  1482. ///
  1483. /// \opencl_version_warning{2,0}
  1484. ///
  1485. /// \see_opencl2_ref{clEnqueueSVMMemcpy}
  1486. event enqueue_svm_memcpy_async(void *dst_ptr,
  1487. const void *src_ptr,
  1488. size_t size,
  1489. const wait_list &events = wait_list())
  1490. {
  1491. event event_;
  1492. cl_int ret = clEnqueueSVMMemcpy(
  1493. m_queue,
  1494. CL_FALSE,
  1495. dst_ptr,
  1496. src_ptr,
  1497. size,
  1498. events.size(),
  1499. events.get_event_ptr(),
  1500. &event_.get()
  1501. );
  1502. if(ret != CL_SUCCESS){
  1503. BOOST_THROW_EXCEPTION(opencl_error(ret));
  1504. }
  1505. return event_;
  1506. }
  1507. /// Enqueues a command to fill \p size bytes of data at \p svm_ptr with
  1508. /// \p pattern.
  1509. ///
  1510. /// \opencl_version_warning{2,0}
  1511. ///
  1512. /// \see_opencl2_ref{clEnqueueSVMMemFill}
  1513. event enqueue_svm_fill(void *svm_ptr,
  1514. const void *pattern,
  1515. size_t pattern_size,
  1516. size_t size,
  1517. const wait_list &events = wait_list())
  1518. {
  1519. event event_;
  1520. cl_int ret = clEnqueueSVMMemFill(
  1521. m_queue,
  1522. svm_ptr,
  1523. pattern,
  1524. pattern_size,
  1525. size,
  1526. events.size(),
  1527. events.get_event_ptr(),
  1528. &event_.get()
  1529. );
  1530. if(ret != CL_SUCCESS){
  1531. BOOST_THROW_EXCEPTION(opencl_error(ret));
  1532. }
  1533. return event_;
  1534. }
  1535. /// Enqueues a command to free \p svm_ptr.
  1536. ///
  1537. /// \opencl_version_warning{2,0}
  1538. ///
  1539. /// \see_opencl2_ref{clEnqueueSVMFree}
  1540. ///
  1541. /// \see svm_free()
  1542. event enqueue_svm_free(void *svm_ptr,
  1543. const wait_list &events = wait_list())
  1544. {
  1545. event event_;
  1546. cl_int ret = clEnqueueSVMFree(
  1547. m_queue,
  1548. 1,
  1549. &svm_ptr,
  1550. 0,
  1551. 0,
  1552. events.size(),
  1553. events.get_event_ptr(),
  1554. &event_.get()
  1555. );
  1556. if(ret != CL_SUCCESS){
  1557. BOOST_THROW_EXCEPTION(opencl_error(ret));
  1558. }
  1559. return event_;
  1560. }
  1561. /// Enqueues a command to map \p svm_ptr to the host memory space.
  1562. ///
  1563. /// \opencl_version_warning{2,0}
  1564. ///
  1565. /// \see_opencl2_ref{clEnqueueSVMMap}
  1566. event enqueue_svm_map(void *svm_ptr,
  1567. size_t size,
  1568. cl_map_flags flags,
  1569. const wait_list &events = wait_list())
  1570. {
  1571. event event_;
  1572. cl_int ret = clEnqueueSVMMap(
  1573. m_queue,
  1574. CL_TRUE,
  1575. flags,
  1576. svm_ptr,
  1577. size,
  1578. events.size(),
  1579. events.get_event_ptr(),
  1580. &event_.get()
  1581. );
  1582. if(ret != CL_SUCCESS){
  1583. BOOST_THROW_EXCEPTION(opencl_error(ret));
  1584. }
  1585. return event_;
  1586. }
  1587. /// Enqueues a command to unmap \p svm_ptr from the host memory space.
  1588. ///
  1589. /// \opencl_version_warning{2,0}
  1590. ///
  1591. /// \see_opencl2_ref{clEnqueueSVMUnmap}
  1592. event enqueue_svm_unmap(void *svm_ptr,
  1593. const wait_list &events = wait_list())
  1594. {
  1595. event event_;
  1596. cl_int ret = clEnqueueSVMUnmap(
  1597. m_queue,
  1598. svm_ptr,
  1599. events.size(),
  1600. events.get_event_ptr(),
  1601. &event_.get()
  1602. );
  1603. if(ret != CL_SUCCESS){
  1604. BOOST_THROW_EXCEPTION(opencl_error(ret));
  1605. }
  1606. return event_;
  1607. }
  1608. #endif // BOOST_COMPUTE_CL_VERSION_2_0
  1609. #if defined(BOOST_COMPUTE_CL_VERSION_2_1) || defined(BOOST_COMPUTE_DOXYGEN_INVOKED)
  1610. /// Enqueues a command to indicate which device a set of ranges of SVM allocations
  1611. /// should be associated with. The pair \p svm_ptrs[i] and \p sizes[i] together define
  1612. /// the starting address and number of bytes in a range to be migrated.
  1613. ///
  1614. /// If \p sizes is empty, then that means every allocation containing any \p svm_ptrs[i]
  1615. /// is to be migrated. Also, if \p sizes[i] is zero, then the entire allocation containing
  1616. /// \p svm_ptrs[i] is migrated.
  1617. ///
  1618. /// \opencl_version_warning{2,1}
  1619. ///
  1620. /// \see_opencl21_ref{clEnqueueSVMMigrateMem}
  1621. event enqueue_svm_migrate_memory(const std::vector<const void*> &svm_ptrs,
  1622. const std::vector<size_t> &sizes,
  1623. const cl_mem_migration_flags flags = 0,
  1624. const wait_list &events = wait_list())
  1625. {
  1626. BOOST_ASSERT(svm_ptrs.size() == sizes.size() || sizes.size() == 0);
  1627. event event_;
  1628. cl_int ret = clEnqueueSVMMigrateMem(
  1629. m_queue,
  1630. static_cast<cl_uint>(svm_ptrs.size()),
  1631. const_cast<void const **>(&svm_ptrs[0]),
  1632. sizes.size() > 0 ? &sizes[0] : NULL,
  1633. flags,
  1634. events.size(),
  1635. events.get_event_ptr(),
  1636. &event_.get()
  1637. );
  1638. if(ret != CL_SUCCESS){
  1639. BOOST_THROW_EXCEPTION(opencl_error(ret));
  1640. }
  1641. return event_;
  1642. }
  1643. /// Enqueues a command to indicate which device a range of SVM allocation
  1644. /// should be associated with. The pair \p svm_ptr and \p size together define
  1645. /// the starting address and number of bytes in a range to be migrated.
  1646. ///
  1647. /// If \p size is 0, then the entire allocation containing \p svm_ptr is
  1648. /// migrated. The default value for \p size is 0.
  1649. ///
  1650. /// \opencl_version_warning{2,1}
  1651. ///
  1652. /// \see_opencl21_ref{clEnqueueSVMMigrateMem}
  1653. event enqueue_svm_migrate_memory(const void* svm_ptr,
  1654. const size_t size = 0,
  1655. const cl_mem_migration_flags flags = 0,
  1656. const wait_list &events = wait_list())
  1657. {
  1658. event event_;
  1659. cl_int ret = clEnqueueSVMMigrateMem(
  1660. m_queue,
  1661. cl_uint(1),
  1662. &svm_ptr,
  1663. &size,
  1664. flags,
  1665. events.size(),
  1666. events.get_event_ptr(),
  1667. &event_.get()
  1668. );
  1669. if(ret != CL_SUCCESS){
  1670. BOOST_THROW_EXCEPTION(opencl_error(ret));
  1671. }
  1672. return event_;
  1673. }
  1674. #endif // BOOST_COMPUTE_CL_VERSION_2_1
  1675. /// Returns \c true if the command queue is the same at \p other.
  1676. bool operator==(const command_queue &other) const
  1677. {
  1678. return m_queue == other.m_queue;
  1679. }
  1680. /// Returns \c true if the command queue is different from \p other.
  1681. bool operator!=(const command_queue &other) const
  1682. {
  1683. return m_queue != other.m_queue;
  1684. }
  1685. /// \internal_
  1686. operator cl_command_queue() const
  1687. {
  1688. return m_queue;
  1689. }
  1690. /// \internal_
  1691. bool check_device_version(int major, int minor) const
  1692. {
  1693. return get_device().check_version(major, minor);
  1694. }
  1695. private:
  1696. cl_command_queue m_queue;
  1697. };
  1698. inline buffer buffer::clone(command_queue &queue) const
  1699. {
  1700. buffer copy(get_context(), size(), get_memory_flags());
  1701. queue.enqueue_copy_buffer(*this, copy, 0, 0, size());
  1702. return copy;
  1703. }
  1704. inline image1d image1d::clone(command_queue &queue) const
  1705. {
  1706. image1d copy(
  1707. get_context(), width(), format(), get_memory_flags()
  1708. );
  1709. queue.enqueue_copy_image(*this, copy, origin(), copy.origin(), size());
  1710. return copy;
  1711. }
  1712. inline image2d image2d::clone(command_queue &queue) const
  1713. {
  1714. image2d copy(
  1715. get_context(), width(), height(), format(), get_memory_flags()
  1716. );
  1717. queue.enqueue_copy_image(*this, copy, origin(), copy.origin(), size());
  1718. return copy;
  1719. }
  1720. inline image3d image3d::clone(command_queue &queue) const
  1721. {
  1722. image3d copy(
  1723. get_context(), width(), height(), depth(), format(), get_memory_flags()
  1724. );
  1725. queue.enqueue_copy_image(*this, copy, origin(), copy.origin(), size());
  1726. return copy;
  1727. }
  1728. /// \internal_ define get_info() specializations for command_queue
  1729. BOOST_COMPUTE_DETAIL_DEFINE_GET_INFO_SPECIALIZATIONS(command_queue,
  1730. ((cl_context, CL_QUEUE_CONTEXT))
  1731. ((cl_device_id, CL_QUEUE_DEVICE))
  1732. ((uint_, CL_QUEUE_REFERENCE_COUNT))
  1733. ((cl_command_queue_properties, CL_QUEUE_PROPERTIES))
  1734. )
  1735. #ifdef BOOST_COMPUTE_CL_VERSION_2_1
  1736. BOOST_COMPUTE_DETAIL_DEFINE_GET_INFO_SPECIALIZATIONS(command_queue,
  1737. ((cl_command_queue, CL_QUEUE_DEVICE_DEFAULT))
  1738. )
  1739. #endif // BOOST_COMPUTE_CL_VERSION_2_1
  1740. } // end compute namespace
  1741. } // end boost namespace
  1742. #endif // BOOST_COMPUTE_COMMAND_QUEUE_HPP