opencl_extensions.doxy 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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 Centre National de la Recherche Scientifique
  5. * Copyright (C) 2011, 2012 Institut National de Recherche en Informatique et Automatique
  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 int starpu_opencl_compile_opencl_from_file(const char *source_file_name, const char *build_options)
  101. \ingroup API_OpenCL_Extensions
  102. Compile the OpenCL kernel stored in the file \p source_file_name
  103. with the given options \p build_options and stores the result in the
  104. directory <c>$STARPU_HOME/.starpu/opencl</c> with the same filename as
  105. \p source_file_name. The compilation is done for every OpenCL device,
  106. and the filename is suffixed with the vendor id and the device id of
  107. the OpenCL device.
  108. \fn int starpu_opencl_compile_opencl_from_string(const char *opencl_program_source, const char *file_name, const char *build_options)
  109. \ingroup API_OpenCL_Extensions
  110. Compile the OpenCL kernel in the string \p opencl_program_source
  111. with the given options \p build_options and stores the result in the
  112. directory <c>$STARPU_HOME/.starpu/opencl</c> with the filename \p
  113. file_name. The compilation is done for every OpenCL device, and the
  114. filename is suffixed with the vendor id and the device id of the
  115. OpenCL device.
  116. \fn int starpu_opencl_load_binary_opencl(const char *kernel_id, struct starpu_opencl_program *opencl_programs)
  117. \ingroup API_OpenCL_Extensions
  118. Compile the binary OpenCL kernel identified with \p kernel_id.
  119. For every OpenCL device, the binary OpenCL kernel will be loaded from
  120. the file
  121. <c>$STARPU_HOME/.starpu/opencl/\<kernel_id\>.\<device_type\>.vendor_id_\<vendor_id\>_device_id_\<device_id\></c>.
  122. @name Loading OpenCL kernels
  123. \ingroup API_OpenCL_Extensions
  124. \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)
  125. \ingroup API_OpenCL_Extensions
  126. Create a kernel \p kernel for device \p devid, on its computation
  127. command queue returned in \p queue, using program \p opencl_programs
  128. and name \p kernel_name.
  129. \fn int starpu_opencl_release_kernel(cl_kernel kernel)
  130. \ingroup API_OpenCL_Extensions
  131. Release the given \p kernel, to be called after kernel execution.
  132. @name OpenCL statistics
  133. \fn int starpu_opencl_collect_stats(cl_event event)
  134. \ingroup API_OpenCL_Extensions
  135. This function allows to collect statistics on a kernel execution.
  136. After termination of the kernels, the OpenCL codelet should call this
  137. function to pass it the even returned by clEnqueueNDRangeKernel, to
  138. let StarPU collect statistics about the kernel execution (used cycles,
  139. consumed power).
  140. @name OpenCL utilities
  141. \ingroup API_OpenCL_Extensions
  142. \fn const char *starpu_opencl_error_string(cl_int status)
  143. \ingroup API_OpenCL_Extensions
  144. Return the error message in English corresponding to \p status, an OpenCL
  145. error code.
  146. \fn void starpu_opencl_display_error(const char *func, const char *file, int line, const char *msg, cl_int status)
  147. \ingroup API_OpenCL_Extensions
  148. Given a valid error status, prints the corresponding error message on
  149. stdout, along with the given function name \p func, the given filename
  150. \p file, the given line number \p line and the given message \p msg.
  151. \def STARPU_OPENCL_DISPLAY_ERROR(status)
  152. \ingroup API_OpenCL_Extensions
  153. Call the function starpu_opencl_display_error() with the given error
  154. \p status, the current function name, current file and line number,
  155. and a empty message.
  156. \fn void starpu_opencl_report_error(const char *func, const char *file, int line, const char *msg, cl_int status)
  157. \ingroup API_OpenCL_Extensions
  158. Call the function starpu_opencl_display_error() and abort.
  159. \def STARPU_OPENCL_REPORT_ERROR(status)
  160. \ingroup API_OpenCL_Extensions
  161. Call the function starpu_opencl_report_error() with the given error \p
  162. status, with the current function name, current file and line number,
  163. and a empty message.
  164. \def STARPU_OPENCL_REPORT_ERROR_WITH_MSG(msg, status)
  165. \ingroup API_OpenCL_Extensions
  166. Call the function starpu_opencl_report_error() with the given \p msg
  167. and the given error \p status, with the current function name, current
  168. file and line number.
  169. \fn cl_int starpu_opencl_allocate_memory(int devid, cl_mem *addr, size_t size, cl_mem_flags flags)
  170. \ingroup API_OpenCL_Extensions
  171. Allocate \p size bytes of memory, stored in \p addr. \p flags must be a valid
  172. combination of cl_mem_flags values.
  173. \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)
  174. \ingroup API_OpenCL_Extensions
  175. Copy \p size bytes from the given \p ptr on RAM \p src_node to the
  176. given \p buffer on OpenCL \p dst_node. \p offset is the offset, in
  177. bytes, in \p buffer. if \p event is <c>NULL</c>, the copy is
  178. synchronous, i.e the queue is synchronised before returning. If not
  179. <c>NULL</c>, \p event can be used after the call to wait for this
  180. particular copy to complete. This function returns <c>CL_SUCCESS</c>
  181. if the copy was successful, or a valid OpenCL error code otherwise.
  182. The integer pointed to by \p ret is set to <c>-EAGAIN</c> if the
  183. asynchronous launch was successful, or to 0 if \p event was
  184. <c>NULL</c>.
  185. \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)
  186. \ingroup API_OpenCL_Extensions
  187. Copy \p size bytes asynchronously from the given \p buffer on OpenCL
  188. \p src_node to the given \p ptr on RAM \p dst_node. \p offset is the
  189. offset, in bytes, in \p buffer. if \p event is <c>NULL</c>, the copy
  190. is 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_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)
  198. \ingroup API_OpenCL_Extensions
  199. Copy \p size bytes asynchronously from byte offset \p src_offset of \p
  200. src on OpenCL \p src_node to byte offset \p dst_offset of \p dst on
  201. OpenCL \p dst_node. if \p event is <c>NULL</c>, the copy is
  202. 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_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)
  210. \ingroup API_OpenCL_Extensions
  211. Copy \p size bytes from byte offset \p src_offset of \p src on \p
  212. src_node to byte offset \p dst_offset of \p dst on \p dst_node. if \p
  213. event is <c>NULL</c>, the copy is synchronous, i.e. the queue is
  214. synchronised before returning. If not <c>NULL</c>, \p event can be
  215. used after the call to wait for this particular copy to complete. The
  216. function returns <c>-EAGAIN</c> if the asynchronous launch was
  217. successfull. It returns 0 if the synchronous copy was successful, or
  218. fails otherwise.
  219. */