starpu_opencl.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2014,2018 Université de Bordeaux
  4. * Copyright (C) 2011,2012 Inria
  5. * Copyright (C) 2010-2013,2015-2017,2019 CNRS
  6. *
  7. * StarPU is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation; either version 2.1 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * StarPU is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. *
  16. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  17. */
  18. #ifndef __STARPU_OPENCL_H__
  19. #define __STARPU_OPENCL_H__
  20. /** @defgroup API_OpenCL_Extensions OpenCL Extensions
  21. *
  22. * @{
  23. */
  24. #include <starpu_config.h>
  25. #ifdef STARPU_USE_OPENCL
  26. #ifndef CL_TARGET_OPENCL_VERSION
  27. #define CL_TARGET_OPENCL_VERSION 100
  28. #endif
  29. #ifdef __APPLE__
  30. #include <OpenCL/cl.h>
  31. #else
  32. #include <CL/cl.h>
  33. #endif
  34. #include <assert.h>
  35. #ifdef __cplusplus
  36. extern "C"
  37. {
  38. #endif
  39. /**
  40. Store the OpenCL programs as compiled for the different OpenCL
  41. devices.
  42. */
  43. struct starpu_opencl_program
  44. {
  45. /** Store each program for each OpenCL device. */
  46. cl_program programs[STARPU_MAXOPENCLDEVS];
  47. };
  48. /** @name Writing OpenCL kernels
  49. @{
  50. */
  51. /**
  52. Return the OpenCL context of the device designated by \p devid
  53. in \p context.
  54. */
  55. void starpu_opencl_get_context(int devid, cl_context *context);
  56. /**
  57. Return the cl_device_id corresponding to \p devid in \p device.
  58. */
  59. void starpu_opencl_get_device(int devid, cl_device_id *device);
  60. /**
  61. Return the command queue of the device designated by \p devid
  62. into \p queue.
  63. */
  64. void starpu_opencl_get_queue(int devid, cl_command_queue *queue);
  65. /**
  66. Return the context of the current worker.
  67. */
  68. void starpu_opencl_get_current_context(cl_context *context);
  69. /**
  70. Return the computation kernel command queue of the current
  71. worker.
  72. */
  73. void starpu_opencl_get_current_queue(cl_command_queue *queue);
  74. /**
  75. Set the arguments of a given kernel. The list of arguments
  76. must be given as <c>(size_t size_of_the_argument, cl_mem *
  77. pointer_to_the_argument)</c>. The last argument must be 0. Return the
  78. number of arguments that were successfully set. In case of failure,
  79. return the id of the argument that could not be set and \p err is set to
  80. the error returned by OpenCL. Otherwise, return the number of
  81. arguments that were set.
  82. Here an example:
  83. \code{.c}
  84. int n;
  85. cl_int err;
  86. cl_kernel kernel;
  87. n = starpu_opencl_set_kernel_args(&err, 2, &kernel, sizeof(foo), &foo, sizeof(bar), &bar, 0);
  88. if (n != 2) fprintf(stderr, "Error : %d\n", err);
  89. \endcode
  90. */
  91. int starpu_opencl_set_kernel_args(cl_int *err, cl_kernel *kernel, ...);
  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. /** @name Loading OpenCL kernels
  162. @{
  163. */
  164. /**
  165. Create a kernel \p kernel for device \p devid, on its computation
  166. command queue returned in \p queue, using program \p opencl_programs
  167. and name \p kernel_name.
  168. */
  169. int starpu_opencl_load_kernel(cl_kernel *kernel, cl_command_queue *queue, struct starpu_opencl_program *opencl_programs, const char *kernel_name, int devid);
  170. /**
  171. Release the given \p kernel, to be called after kernel execution.
  172. */
  173. int starpu_opencl_release_kernel(cl_kernel kernel);
  174. /** @} */
  175. /** @name OpenCL Statistics
  176. @{
  177. */
  178. /**
  179. Collect statistics on a kernel execution.
  180. After termination of the kernels, the OpenCL codelet should call this
  181. function with the event returned by \c clEnqueueNDRangeKernel(), to
  182. let StarPU collect statistics about the kernel execution (used cycles,
  183. consumed energy).
  184. */
  185. int starpu_opencl_collect_stats(cl_event event);
  186. /** @} */
  187. /** @name OpenCL Utilities
  188. @{
  189. */
  190. /**
  191. Return the error message in English corresponding to \p status, an OpenCL
  192. error code.
  193. */
  194. const char *starpu_opencl_error_string(cl_int status);
  195. /**
  196. Given a valid error status, print the corresponding error message on
  197. \c stdout, along with the function name \p func, the filename
  198. \p file, the line number \p line and the message \p msg.
  199. */
  200. void starpu_opencl_display_error(const char *func, const char *file, int line, const char *msg, cl_int status);
  201. /**
  202. Call the function starpu_opencl_display_error() with the error
  203. \p status, the current function name, current file and line number,
  204. and a empty message.
  205. */
  206. #define STARPU_OPENCL_DISPLAY_ERROR(status) starpu_opencl_display_error(__starpu_func__, __FILE__, __LINE__, NULL, status)
  207. /**
  208. Call the function starpu_opencl_display_error() and abort.
  209. */
  210. static __starpu_inline void starpu_opencl_report_error(const char *func, const char *file, int line, const char *msg, cl_int status)
  211. {
  212. starpu_opencl_display_error(func, file, line, msg, status);
  213. assert(0);
  214. }
  215. /**
  216. Call the function starpu_opencl_report_error() with the error \p
  217. status, the current function name, current file and line number,
  218. and a empty message.
  219. */
  220. #define STARPU_OPENCL_REPORT_ERROR(status) starpu_opencl_report_error(__starpu_func__, __FILE__, __LINE__, NULL, status)
  221. /**
  222. Call the function starpu_opencl_report_error() with \p msg
  223. and \p status, the current function name, current file and line number.
  224. */
  225. #define STARPU_OPENCL_REPORT_ERROR_WITH_MSG(msg, status) starpu_opencl_report_error(__starpu_func__, __FILE__, __LINE__, msg, status)
  226. /**
  227. Allocate \p size bytes of memory, stored in \p addr. \p flags must be a valid
  228. combination of \c cl_mem_flags values.
  229. */
  230. cl_int starpu_opencl_allocate_memory(int devid, cl_mem *addr, size_t size, cl_mem_flags flags);
  231. /**
  232. Copy \p size bytes from the given \p ptr on RAM \p src_node to the
  233. given \p buffer on OpenCL \p dst_node. \p offset is the offset, in
  234. bytes, in \p buffer. if \p event is <c>NULL</c>, the copy is
  235. synchronous, i.e the queue is synchronised before returning. If not
  236. <c>NULL</c>, \p event can be used after the call to wait for this
  237. particular copy to complete. This function returns <c>CL_SUCCESS</c>
  238. if the copy was successful, or a valid OpenCL error code otherwise.
  239. The integer pointed to by \p ret is set to <c>-EAGAIN</c> if the
  240. asynchronous launch was successful, or to 0 if \p event was
  241. <c>NULL</c>.
  242. */
  243. 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);
  244. /**
  245. Copy \p size bytes asynchronously from the given \p buffer on OpenCL
  246. \p src_node to the given \p ptr on RAM \p dst_node. \p offset is the
  247. offset, in bytes, in \p buffer. if \p event is <c>NULL</c>, the copy
  248. is synchronous, i.e the queue is synchronised before returning. If not
  249. <c>NULL</c>, \p event can be used after the call to wait for this
  250. particular copy to complete. This function returns <c>CL_SUCCESS</c>
  251. if the copy was successful, or a valid OpenCL error code otherwise.
  252. The integer pointed to by \p ret is set to <c>-EAGAIN</c> if the
  253. asynchronous launch was successful, or to 0 if \p event was
  254. <c>NULL</c>.
  255. */
  256. 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);
  257. /**
  258. Copy \p size bytes asynchronously from byte offset \p src_offset of \p
  259. src on OpenCL \p src_node to byte offset \p dst_offset of \p dst on
  260. OpenCL \p dst_node. if \p event is <c>NULL</c>, the copy is
  261. synchronous, i.e. the queue is synchronised before returning. If not
  262. <c>NULL</c>, \p event can be used after the call to wait for this
  263. particular copy to complete. This function returns <c>CL_SUCCESS</c>
  264. if the copy was successful, or a valid OpenCL error code otherwise.
  265. The integer pointed to by \p ret is set to <c>-EAGAIN</c> if the
  266. asynchronous launch was successful, or to 0 if \p event was
  267. <c>NULL</c>.
  268. */
  269. 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);
  270. /**
  271. Copy \p size bytes from byte offset \p src_offset of \p src on \p
  272. src_node to byte offset \p dst_offset of \p dst on \p dst_node. if \p
  273. event is <c>NULL</c>, the copy is synchronous, i.e. the queue is
  274. synchronised before returning. If not <c>NULL</c>, \p event can be
  275. used after the call to wait for this particular copy to complete. The
  276. function returns <c>-EAGAIN</c> if the asynchronous launch was
  277. successfull. It returns 0 if the synchronous copy was successful, or
  278. fails otherwise.
  279. */
  280. 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);
  281. /** @} */
  282. #ifdef __cplusplus
  283. }
  284. #endif
  285. #endif /* STARPU_USE_OPENCL */
  286. /** @} */
  287. #endif /* __STARPU_OPENCL_H__ */