opencl_extensions.doxy 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2017 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,
  73. sizeof(foo), &foo,
  74. sizeof(bar), &bar,
  75. 0);
  76. if (n != 2)
  77. fprintf(stderr, "Error : %d\n", err);
  78. \endcode
  79. @name Compiling OpenCL kernels
  80. \ingroup API_OpenCL_Extensions
  81. Source codes for OpenCL kernels can be stored in a file or in a
  82. string. StarPU provides functions to build the program executable for
  83. each available OpenCL device as a cl_program object. This program
  84. executable can then be loaded within a specific queue as explained in
  85. the next section. These are only helpers, Applications can also fill a
  86. starpu_opencl_program array by hand for more advanced use (e.g.
  87. different programs on the different OpenCL devices, for relocation
  88. purpose for instance).
  89. \fn int starpu_opencl_load_opencl_from_file(const char *source_file_name, struct starpu_opencl_program *opencl_programs, const char *build_options)
  90. \ingroup API_OpenCL_Extensions
  91. Compile an OpenCL source code stored in a file.
  92. \fn int starpu_opencl_load_opencl_from_string(const char *opencl_program_source, struct starpu_opencl_program *opencl_programs, const char *build_options)
  93. \ingroup API_OpenCL_Extensions
  94. Compile an OpenCL source code stored in a string.
  95. \fn int starpu_opencl_unload_opencl(struct starpu_opencl_program *opencl_programs)
  96. \ingroup API_OpenCL_Extensions
  97. Unload an OpenCL compiled code.
  98. \fn void starpu_opencl_load_program_source(const char *source_file_name, char *located_file_name, char *located_dir_name, char *opencl_program_source)
  99. \ingroup API_OpenCL_Extensions
  100. Store the contents of the file \p source_file_name in the buffer
  101. \p opencl_program_source. The file \p source_file_name can be located in the
  102. current directory, or in the directory specified by the environment
  103. variable \ref STARPU_OPENCL_PROGRAM_DIR, or
  104. in the directory <c>share/starpu/opencl</c> of the installation
  105. directory of StarPU, or in the source directory of StarPU. When the
  106. file is found, \p located_file_name is the full name of the file as it
  107. has been located on the system, \p located_dir_name the directory
  108. where it has been located. Otherwise, they are both set to the empty
  109. string.
  110. \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)
  111. \ingroup API_OpenCL_Extensions
  112. 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.
  113. \fn int starpu_opencl_compile_opencl_from_file(const char *source_file_name, const char *build_options)
  114. \ingroup API_OpenCL_Extensions
  115. Compile the OpenCL kernel stored in the file \p source_file_name
  116. with the given options \p build_options and store the result in the
  117. directory <c>$STARPU_HOME/.starpu/opencl</c> with the same filename as
  118. \p source_file_name. The compilation is done for every OpenCL device,
  119. and the filename is suffixed with the vendor id and the device id of
  120. the OpenCL device.
  121. \fn int starpu_opencl_compile_opencl_from_string(const char *opencl_program_source, const char *file_name, const char *build_options)
  122. \ingroup API_OpenCL_Extensions
  123. Compile the OpenCL kernel in the string \p opencl_program_source
  124. with the given options \p build_options and store the result in the
  125. directory <c>$STARPU_HOME/.starpu/opencl</c> with the filename \p
  126. file_name. The compilation is done for every OpenCL device, and the
  127. filename is suffixed with the vendor id and the device id of the
  128. OpenCL device.
  129. \fn int starpu_opencl_load_binary_opencl(const char *kernel_id, struct starpu_opencl_program *opencl_programs)
  130. \ingroup API_OpenCL_Extensions
  131. Compile the binary OpenCL kernel identified with \p kernel_id.
  132. For every OpenCL device, the binary OpenCL kernel will be loaded from
  133. the file
  134. <c>$STARPU_HOME/.starpu/opencl/\<kernel_id\>.\<device_type\>.vendor_id_\<vendor_id\>_device_id_\<device_id\></c>.
  135. @name Loading OpenCL kernels
  136. \ingroup API_OpenCL_Extensions
  137. \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)
  138. \ingroup API_OpenCL_Extensions
  139. Create a kernel \p kernel for device \p devid, on its computation
  140. command queue returned in \p queue, using program \p opencl_programs
  141. and name \p kernel_name.
  142. \fn int starpu_opencl_release_kernel(cl_kernel kernel)
  143. \ingroup API_OpenCL_Extensions
  144. Release the given \p kernel, to be called after kernel execution.
  145. @name OpenCL statistics
  146. \fn int starpu_opencl_collect_stats(cl_event event)
  147. \ingroup API_OpenCL_Extensions
  148. Collect statistics on a kernel execution.
  149. After termination of the kernels, the OpenCL codelet should call this
  150. function with the event returned by \c clEnqueueNDRangeKernel(), to
  151. let StarPU collect statistics about the kernel execution (used cycles,
  152. consumed energy).
  153. @name OpenCL utilities
  154. \ingroup API_OpenCL_Extensions
  155. \fn const char *starpu_opencl_error_string(cl_int status)
  156. \ingroup API_OpenCL_Extensions
  157. Return the error message in English corresponding to \p status, an OpenCL
  158. error code.
  159. \fn void starpu_opencl_display_error(const char *func, const char *file, int line, const char *msg, cl_int status)
  160. \ingroup API_OpenCL_Extensions
  161. Given a valid error status, print the corresponding error message on
  162. \c stdout, along with the function name \p func, the filename
  163. \p file, the line number \p line and the message \p msg.
  164. \def STARPU_OPENCL_DISPLAY_ERROR(status)
  165. \ingroup API_OpenCL_Extensions
  166. Call the function starpu_opencl_display_error() with the error
  167. \p status, the current function name, current file and line number,
  168. and a empty message.
  169. \fn void starpu_opencl_report_error(const char *func, const char *file, int line, const char *msg, cl_int status)
  170. \ingroup API_OpenCL_Extensions
  171. Call the function starpu_opencl_display_error() and abort.
  172. \def STARPU_OPENCL_REPORT_ERROR(status)
  173. \ingroup API_OpenCL_Extensions
  174. Call the function starpu_opencl_report_error() with the error \p
  175. status, the current function name, current file and line number,
  176. and a empty message.
  177. \def STARPU_OPENCL_REPORT_ERROR_WITH_MSG(msg, status)
  178. \ingroup API_OpenCL_Extensions
  179. Call the function starpu_opencl_report_error() with \p msg
  180. and \p status, the current function name, current file and line number.
  181. \fn cl_int starpu_opencl_allocate_memory(int devid, cl_mem *addr, size_t size, cl_mem_flags flags)
  182. \ingroup API_OpenCL_Extensions
  183. Allocate \p size bytes of memory, stored in \p addr. \p flags must be a valid
  184. combination of \c cl_mem_flags values.
  185. \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)
  186. \ingroup API_OpenCL_Extensions
  187. Copy \p size bytes from the given \p ptr on RAM \p src_node to the
  188. given \p buffer on OpenCL \p dst_node. \p offset is the offset, in
  189. bytes, in \p buffer. if \p event is <c>NULL</c>, the copy is
  190. synchronous, i.e the queue is synchronised before returning. If not
  191. <c>NULL</c>, \p event can be used after the call to wait for this
  192. particular copy to complete. This function returns <c>CL_SUCCESS</c>
  193. if the copy was successful, or a valid OpenCL error code otherwise.
  194. The integer pointed to by \p ret is set to <c>-EAGAIN</c> if the
  195. asynchronous launch was successful, or to 0 if \p event was
  196. <c>NULL</c>.
  197. \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)
  198. \ingroup API_OpenCL_Extensions
  199. Copy \p size bytes asynchronously from the given \p buffer on OpenCL
  200. \p src_node to the given \p ptr on RAM \p dst_node. \p offset is the
  201. offset, in bytes, in \p buffer. if \p event is <c>NULL</c>, the copy
  202. is synchronous, i.e the queue is synchronised before returning. If not
  203. <c>NULL</c>, \p event can be used after the call to wait for this
  204. particular copy to complete. This function returns <c>CL_SUCCESS</c>
  205. if the copy was successful, or a valid OpenCL error code otherwise.
  206. The integer pointed to by \p ret is set to <c>-EAGAIN</c> if the
  207. asynchronous launch was successful, or to 0 if \p event was
  208. <c>NULL</c>.
  209. \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)
  210. \ingroup API_OpenCL_Extensions
  211. Copy \p size bytes asynchronously from byte offset \p src_offset of \p
  212. src on OpenCL \p src_node to byte offset \p dst_offset of \p dst on
  213. OpenCL \p dst_node. if \p event is <c>NULL</c>, the copy is
  214. synchronous, i.e. the queue is synchronised before returning. If not
  215. <c>NULL</c>, \p event can be used after the call to wait for this
  216. particular copy to complete. This function returns <c>CL_SUCCESS</c>
  217. if the copy was successful, or a valid OpenCL error code otherwise.
  218. The integer pointed to by \p ret is set to <c>-EAGAIN</c> if the
  219. asynchronous launch was successful, or to 0 if \p event was
  220. <c>NULL</c>.
  221. \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)
  222. \ingroup API_OpenCL_Extensions
  223. Copy \p size bytes from byte offset \p src_offset of \p src on \p
  224. src_node to byte offset \p dst_offset of \p dst on \p dst_node. if \p
  225. event is <c>NULL</c>, the copy is synchronous, i.e. the queue is
  226. synchronised before returning. If not <c>NULL</c>, \p event can be
  227. used after the call to wait for this particular copy to complete. The
  228. function returns <c>-EAGAIN</c> if the asynchronous launch was
  229. successfull. It returns 0 if the synchronous copy was successful, or
  230. fails otherwise.
  231. */