c-extensions.texi 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. @c -*-texinfo-*-
  2. @c This file is part of the StarPU Handbook.
  3. @c Copyright (C) 2011, 2012 Institut National de Recherche en Informatique et Automatique
  4. @c See the file starpu.texi for copying conditions.
  5. @cindex C extensions
  6. @cindex GCC plug-in
  7. When GCC plug-in support is available, StarPU builds a plug-in for the
  8. GNU Compiler Collection (GCC), which defines extensions to languages of
  9. the C family (C, C++, Objective-C) that make it easier to write StarPU
  10. code@footnote{This feature is only available for GCC 4.5 and later. It
  11. can be disabled by configuring with @code{--disable-gcc-extensions}.}.
  12. Those extensions include syntactic sugar for defining
  13. tasks and their implementations, invoking a task, and manipulating data
  14. buffers. Use of these extensions can be made conditional on the
  15. availability of the plug-in, leading to valid C sequential code when the
  16. plug-in is not used (@pxref{Conditional Extensions}).
  17. This section does not require detailed knowledge of the StarPU library.
  18. Note: as of StarPU @value{VERSION}, this is still an area under
  19. development and subject to change.
  20. @menu
  21. * Defining Tasks:: Defining StarPU tasks
  22. * Registered Data Buffers:: Manipulating data buffers
  23. * Conditional Extensions:: Using C extensions only when available
  24. @end menu
  25. @node Defining Tasks
  26. @section Defining Tasks
  27. @cindex task
  28. @cindex task implementation
  29. The StarPU GCC plug-in views @dfn{tasks} as ``extended'' C functions:
  30. @enumerate
  31. @item
  32. tasks may have several implementations---e.g., one for CPUs, one written
  33. in OpenCL, one written in CUDA;
  34. @item
  35. tasks may have several implementations of the same target---e.g.,
  36. several CPU implementations;
  37. @item
  38. when a task is invoked, it may run in parallel, and StarPU is free to
  39. choose any of its implementations.
  40. @end enumerate
  41. Tasks and their implementations must be @emph{declared}. These
  42. declarations are annotated with @dfn{attributes} (@pxref{Attribute
  43. Syntax, attributes in GNU C,, gcc, Using the GNU Compiler Collection
  44. (GCC)}): the declaration of a task is a regular C function declaration
  45. with an additional @code{task} attribute, and task implementations are
  46. declared with a @code{task_implementation} attribute.
  47. The following function attributes are provided:
  48. @table @code
  49. @item task
  50. @cindex @code{task} attribute
  51. Declare the given function as a StarPU task. Its return type must be
  52. @code{void}, and it must not be defined---instead, a definition will
  53. automatically be provided by the compiler.
  54. Under the hood, declaring a task leads to the declaration of the
  55. corresponding @code{codelet} (@pxref{Codelet and Tasks}). If one or
  56. more task implementations are declared in the same compilation unit,
  57. then the codelet and the function itself are also defined; they inherit
  58. the scope of the task.
  59. Scalar arguments to the task are passed by value and copied to the
  60. target device if need be---technically, they are passed as the
  61. @code{cl_arg} buffer (@pxref{Codelets and Tasks, @code{cl_arg}}).
  62. @cindex @code{output} type attribute
  63. Pointer arguments are assumed to be registered data buffers---the
  64. @code{buffers} argument of a task (@pxref{Codelets and Tasks,
  65. @code{buffers}}); @code{const}-qualified pointer arguments are viewed as
  66. read-only buffers (@code{STARPU_R}), and non-@code{const}-qualified
  67. buffers are assumed to be used read-write (@code{STARPU_RW}). In
  68. addition, the @code{output} type attribute can be as a type qualifier
  69. for output pointer or array parameters (@code{STARPU_W}).
  70. @item task_implementation (@var{target}, @var{task})
  71. @cindex @code{task_implementation} attribute
  72. Declare the given function as an implementation of @var{task} to run on
  73. @var{target}. @var{target} must be a string, currently one of
  74. @code{"cpu"}, @code{"opencl"}, or @code{"cuda"}.
  75. @c FIXME: Update when OpenCL support is ready.
  76. @end table
  77. Here is an example:
  78. @cartouche
  79. @smallexample
  80. #define __output __attribute__ ((output))
  81. static void matmul (const float *A, const float *B,
  82. __output float *C,
  83. size_t nx, size_t ny, size_t nz)
  84. __attribute__ ((task));
  85. static void matmul_cpu (const float *A, const float *B,
  86. __output float *C,
  87. size_t nx, size_t ny, size_t nz)
  88. __attribute__ ((task_implementation ("cpu", matmul)));
  89. static void
  90. matmul_cpu (const float *A, const float *B, __output float *C,
  91. size_t nx, size_t ny, size_t nz)
  92. @{
  93. size_t i, j, k;
  94. for (j = 0; j < ny; j++)
  95. for (i = 0; i < nx; i++)
  96. @{
  97. for (k = 0; k < nz; k++)
  98. C[j * nx + i] += A[j * nz + k] * B[k * nx + i];
  99. @}
  100. @}
  101. @end smallexample
  102. @end cartouche
  103. @noindent
  104. A @code{matmult} task is defined; it has only one implementation,
  105. @code{matmult_cpu}, which runs on the CPU. Variables @var{A} and
  106. @var{B} are input buffers, whereas @var{C} is considered an input/output
  107. buffer.
  108. CUDA and OpenCL implementations can be declared in a similar way:
  109. @cartouche
  110. @smallexample
  111. static void matmul_cuda (const float *A, const float *B, float *C,
  112. size_t nx, size_t ny, size_t nz)
  113. __attribute__ ((task_implementation ("cuda", matmul)));
  114. static void matmul_opencl (const float *A, const float *B, float *C,
  115. size_t nx, size_t ny, size_t nz)
  116. __attribute__ ((task_implementation ("opencl", matmul)));
  117. @end smallexample
  118. @end cartouche
  119. @noindent
  120. The CUDA and OpenCL implementations typically either invoke a kernel
  121. written in CUDA or OpenCL (for similar code, @pxref{CUDA Kernel}, and
  122. @pxref{OpenCL Kernel}), or call a library function that uses CUDA or
  123. OpenCL under the hood, such as CUBLAS functions:
  124. @cartouche
  125. @smallexample
  126. static void
  127. matmul_cuda (const float *A, const float *B, float *C,
  128. size_t nx, size_t ny, size_t nz)
  129. @{
  130. cublasSgemm ('n', 'n', nx, ny, nz,
  131. 1.0f, A, 0, B, 0,
  132. 0.0f, C, 0);
  133. cudaStreamSynchronize (starpu_cuda_get_local_stream ());
  134. @}
  135. @end smallexample
  136. @end cartouche
  137. A task can be invoked like a regular C function:
  138. @cartouche
  139. @smallexample
  140. matmul (&A[i * zdim * bydim + k * bzdim * bydim],
  141. &B[k * xdim * bzdim + j * bxdim * bzdim],
  142. &C[i * xdim * bydim + j * bxdim * bydim],
  143. bxdim, bydim, bzdim);
  144. @end smallexample
  145. @end cartouche
  146. @noindent
  147. This leads to an @dfn{asynchronous invocation}, whereby @code{matmult}'s
  148. implementation may run in parallel with the continuation of the caller.
  149. The next section describes how memory buffers must be handled in
  150. StarPU-GCC code.
  151. @node Registered Data Buffers
  152. @section Registered Data Buffers
  153. Data buffers such as matrices and vectors that are to be passed to tasks
  154. must be @dfn{registered}. Registration allows StarPU to handle data
  155. transfers among devices---e.g., transferring an input buffer from the
  156. CPU's main memory to a task scheduled to run a GPU (@pxref{StarPU Data
  157. Management Library}).
  158. The following pragmas are provided:
  159. @table @code
  160. @item #pragma starpu register @var{ptr} [@var{size}]
  161. Register @var{ptr} as a @var{size}-element buffer. When @var{ptr} has
  162. an array type whose size is known, @var{size} may be omitted.
  163. @item #pragma starpu unregister @var{ptr}
  164. Unregister the previously-registered memory area pointed to by
  165. @var{ptr}. As a side-effect, @var{ptr} points to a valid copy in main
  166. memory.
  167. @item #pragma starpu acquire @var{ptr}
  168. Acquire in main memory an up-to-date copy of the previously-registered
  169. memory area pointed to by @var{ptr}, for read-write access.
  170. @item #pragma starpu release @var{ptr}
  171. Release the previously-register memory area pointed to by @var{ptr},
  172. making it available to the tasks.
  173. @end table
  174. As a substitute for the @code{register} and @code{unregister} pragmas,
  175. the @code{heap_allocated} variable attribute offers a higher-level
  176. mechanism:
  177. @table @code
  178. @item heap_allocated
  179. @cindex @code{heap_allocated} attribute
  180. This attributes applies to local variables with an array type. Its
  181. effect is to automatically allocate and register the array's storage on
  182. the heap, using @code{starpu_malloc} under the hood (@pxref{Basic Data
  183. Library API, starpu_malloc}). The heap-allocated array is automatically
  184. freed and unregistered when the variable's scope is left, as with
  185. automatic variables@footnote{This is achieved by using the
  186. @code{cleanup} attribute (@pxref{Variable Attributes,,, gcc, Using the
  187. GNU Compiler Collection (GCC)})}.
  188. @end table
  189. @noindent
  190. The following example illustrates use of the @code{heap_allocated}
  191. attribute:
  192. @example
  193. extern void cholesky(unsigned nblocks, unsigned size,
  194. float mat[nblocks][nblocks][size])
  195. __attribute__ ((task));
  196. int
  197. main (int argc, char *argv[])
  198. @{
  199. #pragma starpu initialize
  200. /* ... */
  201. int nblocks, size;
  202. parse_args (&nblocks, &size);
  203. /* Allocate an array of the required size on the heap,
  204. and register it. */
  205. float matrix[nblocks][nblocks][size]
  206. __attribute__ ((heap_allocated));
  207. cholesky (nblocks, size, matrix);
  208. #pragma starpu shutdown
  209. /* MATRIX is automatically freed upon return. */
  210. return EXIT_SUCCESS;
  211. @}
  212. @end example
  213. @node Conditional Extensions
  214. @section Using C Extensions Conditionally
  215. The C extensions described in this chapter are only available when GCC
  216. and its StarPU plug-in are in use. Yet, it is possible to make use of
  217. these extensions when they are available---leading to hybrid CPU/GPU
  218. code---and discard them when they are not available---leading to valid
  219. sequential code.
  220. To that end, the GCC plug-in defines a C preprocessor macro when it is
  221. being used:
  222. @defmac STARPU_GCC_PLUGIN
  223. Defined for code being compiled with the StarPU GCC plug-in. When
  224. defined, this macro expands to an integer denoting the version of the
  225. supported C extensions.
  226. @end defmac
  227. The code below illustrates how to define a task and its implementations
  228. in a way that allows it to be compiled without the GCC plug-in:
  229. @cartouche
  230. @smallexample
  231. /* The macros below abstract over the attributes specific to
  232. StarPU-GCC and the name of the CPU implementation. */
  233. #ifdef STARPU_GCC_PLUGIN
  234. # define __task __attribute__ ((task))
  235. # define CPU_TASK_IMPL(task) task ## _cpu
  236. #else
  237. # define __task
  238. # define CPU_TASK_IMPL(task) task
  239. #endif
  240. #include <stdlib.h>
  241. static void matmul (const float *A, const float *B, float *C,
  242. size_t nx, size_t ny, size_t nz) __task;
  243. #ifdef STARPU_GCC_PLUGIN
  244. static void matmul_cpu (const float *A, const float *B, float *C,
  245. size_t nx, size_t ny, size_t nz)
  246. __attribute__ ((task_implementation ("cpu", matmul)));
  247. #endif
  248. static void
  249. CPU_TASK_IMPL (matmul) (const float *A, const float *B, float *C,
  250. size_t nx, size_t ny, size_t nz)
  251. @{
  252. /* Code of the CPU kernel here... */
  253. @}
  254. int
  255. main (int argc, char *argv[])
  256. @{
  257. /* The pragmas below are simply ignored when StarPU-GCC
  258. is not used. */
  259. #pragma starpu initialize
  260. float A[123][42][7], B[123][42][7], C[123][42][7];
  261. #pragma starpu register A
  262. #pragma starpu register B
  263. #pragma starpu register C
  264. /* When StarPU-GCC is used, the call below is asynchronous;
  265. otherwise, it is synchronous. */
  266. matmul (A, B, C, 123, 42, 7);
  267. #pragma starpu wait
  268. #pragma starpu shutdown
  269. return EXIT_SUCCESS;
  270. @}
  271. @end smallexample
  272. @end cartouche
  273. Note that attributes such as @code{task} are simply ignored by GCC when
  274. the StarPU plug-in is not loaded, so the @code{__task} macro could be
  275. omitted altogether. However, @command{gcc -Wall} emits a warning for
  276. unknown attributes, which can be inconvenient, and other compilers may
  277. be unable to parse the attribute syntax. Thus, using macros such as
  278. @code{__task} above is recommended.
  279. @c Local Variables:
  280. @c TeX-master: "../starpu.texi"
  281. @c ispell-local-dictionary: "american"
  282. @c End: