HComputeDeviceExt.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /*****************************************************************************
  2. * HComputeDeviceExt.h
  3. *****************************************************************************
  4. *
  5. * Project: Halcon/libhalcon
  6. * Description: External Compute Device Interface
  7. *
  8. * (c) 2011-2020 by MVTec Software GmbH
  9. * www.mvtec.com
  10. *
  11. *****************************************************************************/
  12. #ifndef HCOMPUTEDEVICEEXT_H
  13. #define HCOMPUTEDEVICEEXT_H
  14. #include "hlib/DBNew.h"
  15. #ifndef HC_NO_COMPUTE_DEVICES
  16. # ifdef __APPLE__
  17. # include "OpenCL/opencl.h"
  18. # else
  19. # include "CL/opencl.h"
  20. # endif
  21. # if defined(__cplusplus)
  22. extern "C" {
  23. # endif
  24. /*****************************************************************************/
  25. /** HXPkgCDInitDevice: initiales a compute device
  26. *****************************************************************************
  27. * Effectively does nothing if the device had been activated before
  28. *
  29. * \pi proc_id HALCON procedure handle
  30. * \pi device_kind currently only COMPUTE_DEVICE_OpenCL is supported
  31. *****************************************************************************/
  32. extern HLibExport Herror HXPkgCDInitDevice(Hproc_handle proc_id,
  33. INT4 device_kind);
  34. /*****************************************************************************/
  35. /** HXPkgCDDeviceActive: tests if a device is active for the current thread
  36. *****************************************************************************
  37. * \pi proc_id HALCON procedure handle
  38. * \pi device_kind currently only COMPUTE_DEVICE_OpenCL is supported
  39. *
  40. * \ret TRUE if a device is active for the current thread
  41. *****************************************************************************/
  42. extern HLibExport HBOOL HXPkgCDDeviceActive(Hproc_handle proc_id,
  43. INT4 device_kind);
  44. /*****************************************************************************/
  45. /** HXPkgCDActiveDeviceKind: returns the kind of the currently active device
  46. *****************************************************************************
  47. * \pi proc_id HALCON procedure handle
  48. * \po device_kind COMPUTE_DEVICE_OpenCL or COMPUTE_DEVICE_NONE
  49. *****************************************************************************/
  50. extern HLibExport Herror HXPkgCDActiveDeviceKind(Hproc_handle proc_id,
  51. INT4* device_kind);
  52. /*****************************************************************************/
  53. /** HXPkgCDTransferToBuffer: transfer an image object to a device buffer
  54. *****************************************************************************
  55. * Is used to transfer input image objects onto the device. May allocate
  56. * memory on the device. If the image object already is on the device,
  57. * HXPkgCDTransferToBuffer does not transfer it again.
  58. *
  59. * \pi proc_id HALCON procedure handle
  60. * \pi image_key key of the image object to be transferred
  61. * \po device_ptr pointer to the device memory of image_key
  62. *****************************************************************************/
  63. extern HLibExport Herror HXPkgCDTransferToBuffer(Hproc_handle proc_id,
  64. Hkey image_key,
  65. HCDPtr* device_ptr);
  66. /*****************************************************************************/
  67. /** HXPkgCDTransferToImage: transfer an image object to a device image
  68. *****************************************************************************
  69. * Is used to transfer input image objects onto the device. May allocate
  70. * memory on the device. If the image object already is on the device,
  71. * HXPkgCDTransferToImage does not transfer it again.
  72. *
  73. * \pi proc_id HALCON procedure handle
  74. * \pi image_key key of the image object to be transferred
  75. * \po device_ptr pointer to the device memory of image_key
  76. *****************************************************************************/
  77. extern HLibExport Herror HXPkgCDTransferToImage(Hproc_handle proc_id,
  78. Hkey image_key,
  79. HCDPtr* device_ptr);
  80. /*****************************************************************************/
  81. /** HXPkgCDAllocBuffer: allocates device memory for an output object
  82. *****************************************************************************
  83. * Is used to allocate memory for an output image object on the device.
  84. *
  85. * \pi proc_id HALCON procedure handle
  86. * \pi image_key key of the output image object
  87. * \po device_ptr pointer to the device memory of image_key
  88. *****************************************************************************/
  89. extern HLibExport Herror HXPkgCDAllocBuffer(Hproc_handle proc_id,
  90. Hkey image_key,
  91. HCDPtr* device_ptr);
  92. /*****************************************************************************/
  93. /** HXPkgCDAttachComputeEvent: attaches an event to an output object
  94. *****************************************************************************
  95. * To allow for asynchronous execution of compute device operations
  96. * HXPkgCDAttachComputeEvent must be used to attach an event to an output
  97. * object such that a subsequent operator that depends on the results of
  98. * the compute device operation, can wait for this event
  99. *
  100. * \pi proc_id HALCON procedure handle
  101. * \pi image_key key of the output image object
  102. *****************************************************************************/
  103. extern HLibExport Herror HXPkgCDAttachComputeEvent(Hproc_handle proc_id,
  104. Hkey image_key);
  105. /*****************************************************************************/
  106. /** HXPkgCDGetOpenCLContext: returns the OpenCL context
  107. *****************************************************************************
  108. * \pi proc_id HALCON procedure handle
  109. * \po context OpenCL context
  110. *****************************************************************************/
  111. extern HLibExport Herror HXPkgCDGetOpenCLContext(Hproc_handle proc_id,
  112. cl_context* context);
  113. /*****************************************************************************/
  114. /** HXPkgCDGetOpenCLCommandQueue: returns the OpenCL command queue
  115. *****************************************************************************
  116. * \pi proc_id HALCON procedure handle
  117. * \po queue OpenCL command queue
  118. *****************************************************************************/
  119. extern HLibExport Herror HXPkgCDGetOpenCLCommandQueue(Hproc_handle proc_id,
  120. cl_command_queue* queue);
  121. /*****************************************************************************/
  122. /** HXPkgCDKernelFileOpenCL: add a new OpenCL source file
  123. *****************************************************************************
  124. * HXPkgCDKernelFileOpenCL must be called in HXPkgMain such that it is
  125. * executed before any HALCON operator call.
  126. *
  127. * The kernel source file must have the extension .cl. file_name must be
  128. * passed without extension.
  129. *
  130. * You can pass NULL for path. In this case the source file must be located
  131. * in the subfolder opencl_kernel within the extension package folder.
  132. *
  133. * \pi proc_id HALCON procedure handle
  134. * \pi file_name name of the source file
  135. * \pi path path to the source file
  136. * \pi kernel_names names of __kernel functions in the source file
  137. * \pi num_kernel number of kernels in the source file
  138. * \pi operator_names name of the operator a kernel is assigned to
  139. * (used in init_compute_device)
  140. * \po kernel_ids identifiers that can later be passed to
  141. * HXPkgCDGetKernelOpenCL
  142. *****************************************************************************/
  143. extern HLibExport Herror HXPkgCDKernelFileOpenCL(
  144. Hproc_handle proc_id, char* file_name, char* path, char** kernel_names,
  145. INT4 num_kernel, char** operator_names, INT4* kernel_ids);
  146. /*****************************************************************************/
  147. /** HXPkgCDGetKernelOpenCL: returns a handle to an OpenCL kernel
  148. *****************************************************************************
  149. * If the kernel has not been compiled before (e.g. by init_compute_device)
  150. * it is done on demand.
  151. *
  152. * \pi proc_id HALCON procedure handle
  153. * \pi kernel_id as obtained by HXPkgCDKernelFileOpenCL
  154. * \po kernel handle to the OpenCL kernel
  155. *****************************************************************************/
  156. extern HLibExport Herror HXPkgCDGetKernelOpenCL(Hproc_handle proc_id,
  157. INT4 kernel_id,
  158. cl_kernel* kernel);
  159. /*****************************************************************************/
  160. /** HXPkgCDAllocDeviceBufferOpenCL: allocate a device buffer
  161. *****************************************************************************
  162. * Uses the HALCON device buffer cache
  163. *
  164. * \pi proc_id HALCON procedure handle
  165. * \po ptr pointer to the allocated device buffer
  166. * \pi size requested size
  167. *****************************************************************************/
  168. extern HLibExport Herror HXPkgCDAllocDeviceBufferOpenCL(Hproc_handle proc_id,
  169. HCDPtr* ptr,
  170. UINT4_8 size);
  171. /*****************************************************************************/
  172. /** HXPkgCDAllocConstantDeviceBufferOpenCL: allocate a constant device buffer
  173. *****************************************************************************
  174. * Uses the HALCON device buffer cache. The only difference to
  175. * HXPkgCDAllocDeviceBufferOpenCL is that the size of the returned memory block
  176. * is smaller than the maximum size of constant buffers.
  177. *
  178. * \pi proc_id HALCON procedure handle
  179. * \po ptr pointer to the allocated device buffer
  180. * \pi size requested size
  181. *****************************************************************************/
  182. extern HLibExport Herror HXPkgCDAllocConstantDeviceBufferOpenCL(
  183. Hproc_handle proc_id, HCDPtr* ptr, UINT4_8 size);
  184. /*****************************************************************************/
  185. /** HXPkgCDFreeDeviceBufferOpenCL: free a device buffer
  186. *****************************************************************************
  187. * Free blocks obtained by HXPkgCDAllocDeviceBufferOpenCL or
  188. * HXPkgCDAllocConstantDeviceBufferOpenCL.
  189. *
  190. * \pi proc_id HALCON procedure handle
  191. * \po ptr pointer to the device buffer
  192. *****************************************************************************/
  193. extern HLibExport Herror HXPkgCDFreeDeviceBufferOpenCL(Hproc_handle proc_id,
  194. HCDPtr ptr);
  195. /*****************************************************************************/
  196. /** HXPkgCDAllocLocalDeviceBufferOpenCL: allocate a local device buffer
  197. *****************************************************************************
  198. * A device buffer allocated by HXPkgCDAllocLocalDeviceBufferOpenCL is freed
  199. * automatically if the output image object assigned to the current operation
  200. * (via HXPkgAttachComputeEvent) is freed or transferred to the host.
  201. *
  202. * This allows for the usage of temporary buffers in asynchronously executed
  203. * compute device code.
  204. *
  205. * \pi proc_id HALCON procedure handle
  206. * \po ptr pointer to the allocated device buffer
  207. * \pi size requested size
  208. *****************************************************************************/
  209. extern HLibExport Herror HXPkgCDAllocLocalDeviceBufferOpenCL(
  210. Hproc_handle proc_id, HCDPtr* ptr, UINT4_8 size);
  211. /*****************************************************************************/
  212. /** HXPkgCDAllocLocalConstantDeviceBufferOpenCL
  213. *****************************************************************************
  214. * The only difference to HXPkgCDAllocLocalDeviceBufferOpenCL is that the
  215. * size of the returned memory block is smaller than the maximum size of
  216. * constant buffers.
  217. *
  218. * \pi proc_id HALCON procedure handle
  219. * \po ptr pointer to the allocated device buffer
  220. * \pi size requested size
  221. *****************************************************************************/
  222. extern HLibExport Herror HXPkgCDAllocLocalConstantDeviceBufferOpenCL(
  223. Hproc_handle proc_id, HCDPtr* ptr, UINT4_8 size);
  224. /*****************************************************************************/
  225. /** HXPkgCDAllocDeviceImageOpenCL: allocate a device image
  226. *****************************************************************************
  227. * Uses the HALCON device image cache
  228. *
  229. * \pi proc_id HALCON procedure handle
  230. * \po ptr pointer to the allocated device image
  231. * \pi kind of the image
  232. * \pi width of the image
  233. * \pi height of the image
  234. *****************************************************************************/
  235. extern HLibExport Herror HXPkgCDAllocDeviceImageOpenCL(Hproc_handle proc_id,
  236. HCDPtr* ptr, HINT kind,
  237. HIMGDIM width,
  238. HIMGDIM height);
  239. /*****************************************************************************/
  240. /** HXPkgCDFreeDeviceImageOpenCL: free a device image
  241. *****************************************************************************
  242. * Free blocks obtained by HXPkgCDAllocDeviceImageOpenCL .
  243. *
  244. * \pi proc_id HALCON procedure handle
  245. * \po ptr pointer to the device image
  246. *****************************************************************************/
  247. extern HLibExport Herror HXPkgCDFreeDeviceImageOpenCL(Hproc_handle proc_id,
  248. HCDPtr ptr);
  249. /*****************************************************************************/
  250. /** HXPkgCDAllocPinnedOpenCL: allocate a pinned block in host memory
  251. *****************************************************************************
  252. * Uses the HALCON pinned memory cache.
  253. *
  254. * \pi proc_id HALCON procedure handle
  255. * \pi size required size
  256. * \po ptr_p pointer to the pinned memory block
  257. * \po pinned_handle handle to the pinned memory block
  258. *****************************************************************************/
  259. extern HLibExport Herror HXPkgCDAllocPinnedOpenCL(Hproc_handle proc_id,
  260. size_t size, void* ptr_p,
  261. HCDPinnedPtr* pinned_handle);
  262. /*****************************************************************************/
  263. /** HXPkgCDFreePinnedOpenCL: free a pinned memory block
  264. *****************************************************************************
  265. * \pi proc_id HALCON procedure handle
  266. * \po ptr pointer to the pinned memory block
  267. * \po pinned_handle handle to the pinned memory block
  268. *****************************************************************************/
  269. extern HLibExport Herror HXPkgCDFreePinnedOpenCL(Hproc_handle proc_id,
  270. void* ptr,
  271. HCDPinnedPtr pinned_handle);
  272. /*****************************************************************************/
  273. /** HXPkgCDAllocLocalPinnedOpenCL: allocate a local pinned block in host memory
  274. *****************************************************************************
  275. * A pinned memory lblock allocated by HXPkgCDAllocLocalPinnedOpenCL is freed
  276. * automatically if the output image object assigned to the current operation
  277. * (via HXPkgAttachComputeEvent) is freed or transferred to the host.
  278. *
  279. * \pi proc_id HALCON procedure handle
  280. * \pi size required size
  281. * \po ptr_p pointer to the pinned memory block
  282. * \po pinned_handle handle to the pinned memory block
  283. *****************************************************************************/
  284. extern HLibExport Herror
  285. HXPkgCDAllocLocalPinnedOpenCL(Hproc_handle proc_id, size_t size, void* ptr_p,
  286. HCDPinnedPtr* pinned_handle);
  287. /*****************************************************************************/
  288. /** HXPkgCDGetDevicePtr: obtain the device pointer of an image object
  289. *****************************************************************************
  290. * ptr is removed from the HALCON device buffer/image cache and must be
  291. * retained before the image object refrenced by image_key is freed.
  292. *
  293. * \pi proc_id HALCON procedure handle
  294. * \pi image_key image object
  295. * \po ptr device pointer of the image object
  296. *****************************************************************************/
  297. extern HLibExport Herror HXPkgCDGetDevicePtr(Hproc_handle proc_id,
  298. Hkey image_key, HCDPtr* ptr);
  299. /*****************************************************************************/
  300. /** HXPkgCDSetDevicePtr: set the device pointer of an image object
  301. *****************************************************************************
  302. * ptr is retained by HXPkgCDSetDevicePtr.
  303. *
  304. * \pi proc_id HALCON procedure handle
  305. * \pi image_key image object
  306. * \pi mem_handle OpenCL memory object
  307. * \pi mem_kind HCD_BUFFER or HCD_IMAGE
  308. *****************************************************************************/
  309. extern HLibExport Herror HXPkgCDSetDevicePtr(Hproc_handle proc_id,
  310. Hkey image_key, cl_mem mem_handle,
  311. INT4 mem_kind);
  312. # if defined(__cplusplus)
  313. }
  314. # endif
  315. #endif
  316. #endif