c-extensions.texi 12 KB

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