opencl_extensions.doxy 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2018 CNRS
  4. * Copyright (C) 2009-2011,2014,2016 Université de Bordeaux
  5. * Copyright (C) 2011-2012 Inria
  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. /*! \defgroup API_OpenCL_Extensions OpenCL Extensions
  19. \def STARPU_USE_OPENCL
  20. \ingroup API_OpenCL_Extensions
  21. Defined when StarPU has been installed with
  22. OpenCL support. It should be used in your code to detect the
  23. availability of OpenCL as shown in \ref FullSourceCodeVectorScal.
  24. \def STARPU_MAXOPENCLDEVS
  25. \ingroup API_OpenCL_Extensions
  26. Define the maximum number of OpenCL devices that are
  27. supported by StarPU.
  28. \def STARPU_OPENCL_DATADIR
  29. \ingroup API_OpenCL_Extensions
  30. Define the directory in which the OpenCL codelets of the
  31. applications provided with StarPU have been installed.
  32. \struct starpu_opencl_program
  33. \ingroup API_OpenCL_Extensions
  34. Store the OpenCL programs as compiled for the different OpenCL
  35. devices.
  36. \var cl_program starpu_opencl_program::programs[STARPU_MAXOPENCLDEVS]
  37. Store each program for each OpenCL device.
  38. @name Writing OpenCL kernels
  39. \ingroup API_OpenCL_Extensions
  40. \fn void starpu_opencl_get_context(int devid, cl_context *context)
  41. \ingroup API_OpenCL_Extensions
  42. Return the OpenCL context of the device designated by \p devid
  43. in \p context.
  44. \fn void starpu_opencl_get_device(int devid, cl_device_id *device)
  45. \ingroup API_OpenCL_Extensions
  46. Return the cl_device_id corresponding to \p devid in \p device.
  47. \fn void starpu_opencl_get_queue(int devid, cl_command_queue *queue)
  48. \ingroup API_OpenCL_Extensions
  49. Return the command queue of the device designated by \p devid
  50. into \p queue.
  51. \fn void starpu_opencl_get_current_context(cl_context *context)
  52. \ingroup API_OpenCL_Extensions
  53. Return the context of the current worker.
  54. \fn void starpu_opencl_get_current_queue(cl_command_queue *queue)
  55. \ingroup API_OpenCL_Extensions
  56. Return the computation kernel command queue of the current
  57. worker.
  58. \fn int starpu_opencl_set_kernel_args(cl_int *err, cl_kernel *kernel, ...)
  59. \ingroup API_OpenCL_Extensions
  60. Set the arguments of a given kernel. The list of arguments
  61. must be given as <c>(size_t size_of_the_argument, cl_mem *
  62. pointer_to_the_argument)</c>. The last argument must be 0. Return the
  63. number of arguments that were successfully set. In case of failure,
  64. return the id of the argument that could not be set and \p err is set to
  65. the error returned by OpenCL. Otherwise, return the number of
  66. arguments that were set.
  67. Here an example:
  68. \code{.c}
  69. int n;
  70. cl_int err;
  71. cl_kernel kernel;
  72. n = starpu_opencl_set_kernel_args(&err, 2, &kernel, sizeof(foo), &foo, sizeof(bar), &bar, 0);
  73. if (n != 2)
  74. fprintf(stderr, "Error : %d\n", err);
  75. \endcode
  76. @name Compiling OpenCL kernels
  77. \ingroup API_OpenCL_Extensions
  78. Source codes for OpenCL kernels can be stored in a file or in a
  79. string. StarPU provides functions to build the program executable for
  80. each available OpenCL device as a cl_program object. This program
  81. executable can then be loaded within a specific queue as explained in
  82. the next section. These are only helpers, Applications can also fill a
  83. starpu_opencl_program array by hand for more advanced use (e.g.
  84. different programs on the different OpenCL devices, for relocation
  85. purpose for instance).
  86. \fn int starpu_opencl_load_opencl_from_file(const char *source_file_name, struct starpu_opencl_program *opencl_programs, const char *build_options)
  87. \ingroup API_OpenCL_Extensions
  88. Compile an OpenCL source code stored in a file.
  89. \fn int starpu_opencl_load_opencl_from_string(const char *opencl_program_source, struct starpu_opencl_program *opencl_programs, const char *build_options)
  90. \ingroup API_OpenCL_Extensions
  91. Compile an OpenCL source code stored in a string.
  92. \fn int starpu_opencl_unload_opencl(struct starpu_opencl_program *opencl_programs)
  93. \ingroup API_OpenCL_Extensions
  94. Unload an OpenCL compiled code.
  95. \fn void starpu_opencl_load_program_source(const char *source_file_name, char *located_file_name, char *located_dir_name, char *opencl_program_source)
  96. \ingroup API_OpenCL_Extensions
  97. Store the contents of the file \p source_file_name in the buffer
  98. \p opencl_program_source. The file \p source_file_name can be located in the
  99. current directory, or in the directory specified by the environment
  100. variable \ref STARPU_OPENCL_PROGRAM_DIR, or
  101. in the directory <c>share/starpu/opencl</c> of the installation
  102. directory of StarPU, or in the source directory of StarPU. When the
  103. file is found, \p located_file_name is the full name of the file as it
  104. has been located on the system, \p located_dir_name the directory
  105. where it has been located. Otherwise, they are both set to the empty
  106. string.
  107. \fn void starpu_opencl_load_program_source_malloc(const char *source_file_name, char **located_file_name, char **located_dir_name, char **opencl_program_source)
  108. \ingroup API_OpenCL_Extensions
  109. Similar to function starpu_opencl_load_program_source() but allocate the buffers \p located_file_name, \p located_dir_name and \p opencl_program_source.
  110. \fn int starpu_opencl_compile_opencl_from_file(const char *source_file_name, const char *build_options)
  111. \ingroup API_OpenCL_Extensions
  112. Compile the OpenCL kernel stored in the file \p source_file_name
  113. with the given options \p build_options and store the result in the
  114. directory <c>$STARPU_HOME/.starpu/opencl</c> with the same filename as
  115. \p source_file_name. The compilation is done for every OpenCL device,
  116. and the filename is suffixed with the vendor id and the device id of
  117. the OpenCL device.
  118. \fn int starpu_opencl_compile_opencl_from_string(const char *opencl_program_source, const char *file_name, const char *build_options)
  119. \ingroup API_OpenCL_Extensions
  120. Compile the OpenCL kernel in the string \p opencl_program_source
  121. with the given options \p build_options and store the result in the
  122. directory <c>$STARPU_HOME/.starpu/opencl</c> with the filename \p
  123. file_name. The compilation is done for every OpenCL device, and the
  124. filename is suffixed with the vendor id and the device id of the
  125. OpenCL device.
  126. \fn int starpu_opencl_load_binary_opencl(const char *kernel_id, struct starpu_opencl_program *opencl_programs)
  127. \ingroup API_OpenCL_Extensions
  128. Compile the binary OpenCL kernel identified with \p kernel_id.
  129. For every OpenCL device, the binary OpenCL kernel will be loaded from
  130. the file
  131. <c>$STARPU_HOME/.starpu/opencl/\<kernel_id\>.\<device_type\>.vendor_id_\<vendor_id\>_device_id_\<device_id\></c>.
  132. @name Loading OpenCL kernels
  133. \ingroup API_OpenCL_Extensions
  134. \fn int starpu_opencl_load_kernel(cl_kernel *kernel, cl_command_queue *queue, struct starpu_opencl_program *opencl_programs, const char *kernel_name, int devid)
  135. \ingroup API_OpenCL_Extensions
  136. Create a kernel \p kernel for device \p devid, on its computation
  137. command queue returned in \p queue, using program \p opencl_programs
  138. and name \p kernel_name.
  139. \fn int starpu_opencl_release_kernel(cl_kernel kernel)
  140. \ingroup API_OpenCL_Extensions
  141. Release the given \p kernel, to be called after kernel execution.
  142. @name OpenCL statistics
  143. \fn int starpu_opencl_collect_stats(cl_event event)
  144. \ingroup API_OpenCL_Extensions
  145. Collect statistics on a kernel execution.
  146. After termination of the kernels, the OpenCL codelet should call this
  147. function with the event returned by \c clEnqueueNDRangeKernel(), to
  148. let StarPU collect statistics about the kernel execution (used cycles,
  149. consumed energy).
  150. @name OpenCL utilities
  151. \ingroup API_OpenCL_Extensions
  152. \fn const char *starpu_opencl_error_string(cl_int status)
  153. \ingroup API_OpenCL_Extensions
  154. Return the error message in English corresponding to \p status, an OpenCL
  155. error code.
  156. \fn void starpu_opencl_display_error(const char *func, const char *file, int line, const char *msg, cl_int status)
  157. \ingroup API_OpenCL_Extensions
  158. Given a valid error status, print the corresponding error message on
  159. \c stdout, along with the function name \p func, the filename
  160. \p file, the line number \p line and the message \p msg.
  161. \def STARPU_OPENCL_DISPLAY_ERROR(status)
  162. \ingroup API_OpenCL_Extensions
  163. Call the function starpu_opencl_display_error() with the error
  164. \p status, the current function name, current file and line number,
  165. and a empty message.
  166. \fn void starpu_opencl_report_error(const char *func, const char *file, int line, const char *msg, cl_int status)
  167. \ingroup API_OpenCL_Extensions
  168. Call the function starpu_opencl_display_error() and abort.
  169. \def STARPU_OPENCL_REPORT_ERROR(status)
  170. \ingroup API_OpenCL_Extensions
  171. Call the function starpu_opencl_report_error() with the error \p
  172. status, the current function name, current file and line number,
  173. and a empty message.
  174. \def STARPU_OPENCL_REPORT_ERROR_WITH_MSG(msg, status)
  175. \ingroup API_OpenCL_Extensions
  176. Call the function starpu_opencl_report_error() with \p msg
  177. and \p status, the current function name, current file and line number.
  178. \fn cl_int starpu_opencl_allocate_memory(int devid, cl_mem *addr, size_t size, cl_mem_flags flags)
  179. \ingroup API_OpenCL_Extensions
  180. Allocate \p size bytes of memory, stored in \p addr. \p flags must be a valid
  181. combination of \c cl_mem_flags values.
  182. \fn 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)
  183. \ingroup API_OpenCL_Extensions
  184. Copy \p size bytes from the given \p ptr on RAM \p src_node to the
  185. given \p buffer on OpenCL \p dst_node. \p offset is the offset, in
  186. bytes, in \p buffer. if \p event is <c>NULL</c>, the copy is
  187. synchronous, i.e the queue is synchronised before returning. If not
  188. <c>NULL</c>, \p event can be used after the call to wait for this
  189. particular copy to complete. This function returns <c>CL_SUCCESS</c>
  190. if the copy was successful, or a valid OpenCL error code otherwise.
  191. The integer pointed to by \p ret is set to <c>-EAGAIN</c> if the
  192. asynchronous launch was successful, or to 0 if \p event was
  193. <c>NULL</c>.
  194. \fn 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)
  195. \ingroup API_OpenCL_Extensions
  196. Copy \p size bytes asynchronously from the given \p buffer on OpenCL
  197. \p src_node to the given \p ptr on RAM \p dst_node. \p offset is the
  198. offset, in bytes, in \p buffer. if \p event is <c>NULL</c>, the copy
  199. is synchronous, i.e the queue is synchronised before returning. If not
  200. <c>NULL</c>, \p event can be used after the call to wait for this
  201. particular copy to complete. This function returns <c>CL_SUCCESS</c>
  202. if the copy was successful, or a valid OpenCL error code otherwise.
  203. The integer pointed to by \p ret is set to <c>-EAGAIN</c> if the
  204. asynchronous launch was successful, or to 0 if \p event was
  205. <c>NULL</c>.
  206. \fn 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)
  207. \ingroup API_OpenCL_Extensions
  208. Copy \p size bytes asynchronously from byte offset \p src_offset of \p
  209. src on OpenCL \p src_node to byte offset \p dst_offset of \p dst on
  210. OpenCL \p dst_node. if \p event is <c>NULL</c>, the copy is
  211. synchronous, i.e. the queue is synchronised before returning. If not
  212. <c>NULL</c>, \p event can be used after the call to wait for this
  213. particular copy to complete. This function returns <c>CL_SUCCESS</c>
  214. if the copy was successful, or a valid OpenCL error code otherwise.
  215. The integer pointed to by \p ret is set to <c>-EAGAIN</c> if the
  216. asynchronous launch was successful, or to 0 if \p event was
  217. <c>NULL</c>.
  218. \fn 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)
  219. \ingroup API_OpenCL_Extensions
  220. Copy \p size bytes from byte offset \p src_offset of \p src on \p
  221. src_node to byte offset \p dst_offset of \p dst on \p dst_node. if \p
  222. event is <c>NULL</c>, the copy is synchronous, i.e. the queue is
  223. synchronised before returning. If not <c>NULL</c>, \p event can be
  224. used after the call to wait for this particular copy to complete. The
  225. function returns <c>-EAGAIN</c> if the asynchronous launch was
  226. successfull. It returns 0 if the synchronous copy was successful, or
  227. fails otherwise.
  228. */