opencl_extensions.doxy 12 KB

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