opencl_extensions.doxy 12 KB

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