opencl_extensions.doxy 12 KB

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