starpu_opencl.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2021 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
  4. *
  5. * StarPU is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation; either version 2.1 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * StarPU is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. *
  14. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  15. */
  16. #ifndef __STARPU_OPENCL_H__
  17. #define __STARPU_OPENCL_H__
  18. #include <starpu_config.h>
  19. #ifdef STARPU_USE_OPENCL
  20. #ifndef CL_TARGET_OPENCL_VERSION
  21. #define CL_TARGET_OPENCL_VERSION 100
  22. #endif
  23. #ifdef __APPLE__
  24. #include <OpenCL/cl.h>
  25. #else
  26. #include <CL/cl.h>
  27. #endif
  28. #include <assert.h>
  29. #ifdef __cplusplus
  30. extern "C"
  31. {
  32. #endif
  33. /**
  34. @defgroup API_OpenCL_Extensions OpenCL Extensions
  35. @{
  36. */
  37. /**
  38. Store the OpenCL programs as compiled for the different OpenCL
  39. devices.
  40. */
  41. struct starpu_opencl_program
  42. {
  43. /** Store each program for each OpenCL device. */
  44. cl_program programs[STARPU_MAXOPENCLDEVS];
  45. };
  46. /**
  47. @name Writing OpenCL kernels
  48. @{
  49. */
  50. /**
  51. Return the OpenCL context of the device designated by \p devid
  52. in \p context.
  53. */
  54. void starpu_opencl_get_context(int devid, cl_context *context);
  55. /**
  56. Return the cl_device_id corresponding to \p devid in \p device.
  57. */
  58. void starpu_opencl_get_device(int devid, cl_device_id *device);
  59. /**
  60. Return the command queue of the device designated by \p devid
  61. into \p queue.
  62. */
  63. void starpu_opencl_get_queue(int devid, cl_command_queue *queue);
  64. /**
  65. Return the context of the current worker.
  66. */
  67. void starpu_opencl_get_current_context(cl_context *context);
  68. /**
  69. Return the computation kernel command queue of the current
  70. worker.
  71. */
  72. void starpu_opencl_get_current_queue(cl_command_queue *queue);
  73. /**
  74. Set the arguments of a given kernel. The list of arguments
  75. must be given as <c>(size_t size_of_the_argument, cl_mem *
  76. pointer_to_the_argument)</c>. The last argument must be 0. Return the
  77. number of arguments that were successfully set. In case of failure,
  78. return the id of the argument that could not be set and \p err is set to
  79. the error returned by OpenCL. Otherwise, return the number of
  80. arguments that were set.
  81. Here an example:
  82. \code{.c}
  83. int n;
  84. cl_int err;
  85. cl_kernel kernel;
  86. n = starpu_opencl_set_kernel_args(&err, 2, &kernel, sizeof(foo), &foo, sizeof(bar), &bar, 0);
  87. if (n != 2) fprintf(stderr, "Error : %d\n", err);
  88. \endcode
  89. */
  90. int starpu_opencl_set_kernel_args(cl_int *err, cl_kernel *kernel, ...);
  91. /** @} */
  92. /**
  93. @name Compiling OpenCL kernels
  94. Source codes for OpenCL kernels can be stored in a file or in a
  95. string. StarPU provides functions to build the program executable for
  96. each available OpenCL device as a cl_program object. This program
  97. executable can then be loaded within a specific queue as explained in
  98. the next section. These are only helpers, Applications can also fill a
  99. starpu_opencl_program array by hand for more advanced use (e.g.
  100. different programs on the different OpenCL devices, for relocation
  101. purpose for instance).
  102. @{
  103. */
  104. /**
  105. Store the contents of the file \p source_file_name in the buffer
  106. \p opencl_program_source. The file \p source_file_name can be located in the
  107. current directory, or in the directory specified by the environment
  108. variable \ref STARPU_OPENCL_PROGRAM_DIR, or
  109. in the directory <c>share/starpu/opencl</c> of the installation
  110. directory of StarPU, or in the source directory of StarPU. When the
  111. file is found, \p located_file_name is the full name of the file as it
  112. has been located on the system, \p located_dir_name the directory
  113. where it has been located. Otherwise, they are both set to the empty
  114. string.
  115. */
  116. void starpu_opencl_load_program_source(const char *source_file_name, char *located_file_name, char *located_dir_name, char *opencl_program_source);
  117. /**
  118. Similar to function starpu_opencl_load_program_source() but
  119. allocate the buffers \p located_file_name, \p located_dir_name and
  120. \p opencl_program_source.
  121. */
  122. void starpu_opencl_load_program_source_malloc(const char *source_file_name, char **located_file_name, char **located_dir_name, char **opencl_program_source);
  123. /**
  124. Compile the OpenCL kernel stored in the file \p source_file_name
  125. with the given options \p build_options and store the result in the
  126. directory <c>$STARPU_HOME/.starpu/opencl</c> with the same filename as
  127. \p source_file_name. The compilation is done for every OpenCL device,
  128. and the filename is suffixed with the vendor id and the device id of
  129. the OpenCL device.
  130. */
  131. int starpu_opencl_compile_opencl_from_file(const char *source_file_name, const char *build_options);
  132. /**
  133. Compile the OpenCL kernel in the string \p opencl_program_source
  134. with the given options \p build_options and store the result in the
  135. directory <c>$STARPU_HOME/.starpu/opencl</c> with the filename \p
  136. file_name. The compilation is done for every OpenCL device, and the
  137. filename is suffixed with the vendor id and the device id of the
  138. OpenCL device.
  139. */
  140. int starpu_opencl_compile_opencl_from_string(const char *opencl_program_source, const char *file_name, const char *build_options);
  141. /**
  142. Compile the binary OpenCL kernel identified with \p kernel_id.
  143. For every OpenCL device, the binary OpenCL kernel will be loaded from
  144. the file
  145. <c>$STARPU_HOME/.starpu/opencl/\<kernel_id\>.\<device_type\>.vendor_id_\<vendor_id\>_device_id_\<device_id\></c>.
  146. */
  147. int starpu_opencl_load_binary_opencl(const char *kernel_id, struct starpu_opencl_program *opencl_programs);
  148. /**
  149. Compile an OpenCL source code stored in a file.
  150. */
  151. int starpu_opencl_load_opencl_from_file(const char *source_file_name, struct starpu_opencl_program *opencl_programs, const char *build_options);
  152. /**
  153. Compile an OpenCL source code stored in a string.
  154. */
  155. int starpu_opencl_load_opencl_from_string(const char *opencl_program_source, struct starpu_opencl_program *opencl_programs, const char *build_options);
  156. /**
  157. Unload an OpenCL compiled code.
  158. */
  159. int starpu_opencl_unload_opencl(struct starpu_opencl_program *opencl_programs);
  160. /** @} */
  161. /**
  162. @name Loading OpenCL kernels
  163. @{
  164. */
  165. /**
  166. Create a kernel \p kernel for device \p devid, on its computation
  167. command queue returned in \p queue, using program \p opencl_programs
  168. and name \p kernel_name.
  169. */
  170. int starpu_opencl_load_kernel(cl_kernel *kernel, cl_command_queue *queue, struct starpu_opencl_program *opencl_programs, const char *kernel_name, int devid);
  171. /**
  172. Release the given \p kernel, to be called after kernel execution.
  173. */
  174. int starpu_opencl_release_kernel(cl_kernel kernel);
  175. /** @} */
  176. /**
  177. @name OpenCL Statistics
  178. @{
  179. */
  180. /**
  181. Collect statistics on a kernel execution.
  182. After termination of the kernels, the OpenCL codelet should call this
  183. function with the event returned by \c clEnqueueNDRangeKernel(), to
  184. let StarPU collect statistics about the kernel execution (used cycles,
  185. consumed energy).
  186. */
  187. int starpu_opencl_collect_stats(cl_event event);
  188. /** @} */
  189. /**
  190. @name OpenCL Utilities
  191. @{
  192. */
  193. /**
  194. Return the error message in English corresponding to \p status, an OpenCL
  195. error code.
  196. */
  197. const char *starpu_opencl_error_string(cl_int status);
  198. /**
  199. Given a valid error status, print the corresponding error message on
  200. \c stdout, along with the function name \p func, the filename
  201. \p file, the line number \p line and the message \p msg.
  202. */
  203. void starpu_opencl_display_error(const char *func, const char *file, int line, const char *msg, cl_int status);
  204. /**
  205. Call the function starpu_opencl_display_error() with the error
  206. \p status, the current function name, current file and line number,
  207. and a empty message.
  208. */
  209. #define STARPU_OPENCL_DISPLAY_ERROR(status) starpu_opencl_display_error(__starpu_func__, __FILE__, __LINE__, NULL, status)
  210. /**
  211. Call the function starpu_opencl_display_error() and abort.
  212. */
  213. static __starpu_inline void starpu_opencl_report_error(const char *func, const char *file, int line, const char *msg, cl_int status)
  214. {
  215. starpu_opencl_display_error(func, file, line, msg, status);
  216. assert(0);
  217. }
  218. /**
  219. Call the function starpu_opencl_report_error() with the error \p
  220. status, the current function name, current file and line number,
  221. and a empty message.
  222. */
  223. #define STARPU_OPENCL_REPORT_ERROR(status) starpu_opencl_report_error(__starpu_func__, __FILE__, __LINE__, NULL, status)
  224. /**
  225. Call the function starpu_opencl_report_error() with \p msg
  226. and \p status, the current function name, current file and line number.
  227. */
  228. #define STARPU_OPENCL_REPORT_ERROR_WITH_MSG(msg, status) starpu_opencl_report_error(__starpu_func__, __FILE__, __LINE__, msg, status)
  229. /**
  230. Allocate \p size bytes of memory, stored in \p addr. \p flags must be a valid
  231. combination of \c cl_mem_flags values.
  232. */
  233. cl_int starpu_opencl_allocate_memory(int devid, cl_mem *addr, size_t size, cl_mem_flags flags);
  234. /**
  235. Copy \p size bytes from the given \p ptr on RAM \p src_node to the
  236. given \p buffer on OpenCL \p dst_node. \p offset is the offset, in
  237. bytes, in \p buffer. if \p event is <c>NULL</c>, the copy is
  238. synchronous, i.e the queue is synchronised before returning. If not
  239. <c>NULL</c>, \p event can be used after the call to wait for this
  240. particular copy to complete. This function returns <c>CL_SUCCESS</c>
  241. if the copy was successful, or a valid OpenCL error code otherwise.
  242. The integer pointed to by \p ret is set to <c>-EAGAIN</c> if the
  243. asynchronous launch was successful, or to 0 if \p event was
  244. <c>NULL</c>.
  245. */
  246. cl_int starpu_opencl_copy_ram_to_opencl(void *ptr, unsigned src_node, cl_mem buffer, unsigned dst_node, size_t size, size_t offset, cl_event *event, int *ret);
  247. /**
  248. Copy \p size bytes asynchronously from the given \p buffer on OpenCL
  249. \p src_node to the given \p ptr on RAM \p dst_node. \p offset is the
  250. offset, in bytes, in \p buffer. if \p event is <c>NULL</c>, the copy
  251. is synchronous, i.e the queue is synchronised before returning. If not
  252. <c>NULL</c>, \p event can be used after the call to wait for this
  253. particular copy to complete. This function returns <c>CL_SUCCESS</c>
  254. if the copy was successful, or a valid OpenCL error code otherwise.
  255. The integer pointed to by \p ret is set to <c>-EAGAIN</c> if the
  256. asynchronous launch was successful, or to 0 if \p event was
  257. <c>NULL</c>.
  258. */
  259. cl_int starpu_opencl_copy_opencl_to_ram(cl_mem buffer, unsigned src_node, void *ptr, unsigned dst_node, size_t size, size_t offset, cl_event *event, int *ret);
  260. /**
  261. Copy \p size bytes asynchronously from byte offset \p src_offset of \p
  262. src on OpenCL \p src_node to byte offset \p dst_offset of \p dst on
  263. OpenCL \p dst_node. if \p event is <c>NULL</c>, the copy is
  264. synchronous, i.e. the queue is synchronised before returning. If not
  265. <c>NULL</c>, \p event can be used after the call to wait for this
  266. particular copy to complete. This function returns <c>CL_SUCCESS</c>
  267. if the copy was successful, or a valid OpenCL error code otherwise.
  268. The integer pointed to by \p ret is set to <c>-EAGAIN</c> if the
  269. asynchronous launch was successful, or to 0 if \p event was
  270. <c>NULL</c>.
  271. */
  272. cl_int starpu_opencl_copy_opencl_to_opencl(cl_mem src, unsigned src_node, size_t src_offset, cl_mem dst, unsigned dst_node, size_t dst_offset, size_t size, cl_event *event, int *ret);
  273. /**
  274. Copy \p size bytes from byte offset \p src_offset of \p src on \p
  275. src_node to byte offset \p dst_offset of \p dst on \p dst_node. if \p
  276. event is <c>NULL</c>, the copy is synchronous, i.e. the queue is
  277. synchronised before returning. If not <c>NULL</c>, \p event can be
  278. used after the call to wait for this particular copy to complete. The
  279. function returns <c>-EAGAIN</c> if the asynchronous launch was
  280. successfull. It returns 0 if the synchronous copy was successful, or
  281. fails otherwise.
  282. */
  283. cl_int starpu_opencl_copy_async_sync(uintptr_t src, size_t src_offset, unsigned src_node, uintptr_t dst, size_t dst_offset, unsigned dst_node, size_t size, cl_event *event);
  284. /** @} */
  285. /** @} */
  286. #ifdef __cplusplus
  287. }
  288. #endif
  289. #endif /* STARPU_USE_OPENCL */
  290. #endif /* __STARPU_OPENCL_H__ */