c_extensions.doxy 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  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. /*! \page cExtensions C Extensions
  9. When GCC plug-in support is available, StarPU builds a plug-in for the
  10. GNU Compiler Collection (GCC), which defines extensions to languages of
  11. the C family (C, C++, Objective-C) that make it easier to write StarPU
  12. code. This feature is only available for GCC 4.5 and later; it
  13. is known to work with GCC 4.5, 4.6, and 4.7. You
  14. may need to install a specific <c>-dev</c> package of your distro, such
  15. as <c>gcc-4.6-plugin-dev</c> on Debian and derivatives. In addition,
  16. the plug-in's test suite is only run when <a href="http://www.gnu.org/software/guile/">GNU Guile</a> is found at
  17. <c>configure</c>-time. Building the GCC plug-in
  18. can be disabled by configuring with <c>--disable-gcc-extensions</c>.
  19. Those extensions include syntactic sugar for defining
  20. tasks and their implementations, invoking a task, and manipulating data
  21. buffers. Use of these extensions can be made conditional on the
  22. availability of the plug-in, leading to valid C sequential code when the
  23. plug-in is not used (\ref Conditional_Extensions).
  24. When StarPU has been installed with its GCC plug-in, programs that use
  25. these extensions can be compiled this way:
  26. \verbatim
  27. $ gcc -c -fplugin=`pkg-config starpu-1.1 --variable=gccplugin` foo.c
  28. \endverbatim
  29. When the plug-in is not available, the above <c>pkg-config</c>
  30. command returns the empty string.
  31. In addition, the <c>-fplugin-arg-starpu-verbose</c> flag can be used to
  32. obtain feedback from the compiler as it analyzes the C extensions used
  33. in source files.
  34. This section describes the C extensions implemented by StarPU's GCC
  35. plug-in. It does not require detailed knowledge of the StarPU library.
  36. Note: this is still an area under development and subject to change.
  37. \section Defining_Tasks Defining Tasks
  38. The StarPU GCC plug-in views tasks as ``extended'' C functions:
  39. <ul>
  40. <Li>
  41. tasks may have several implementations---e.g., one for CPUs, one written
  42. in OpenCL, one written in CUDA;
  43. </li>
  44. <Li>
  45. tasks may have several implementations of the same target---e.g.,
  46. several CPU implementations;
  47. </li>
  48. <li>
  49. when a task is invoked, it may run in parallel, and StarPU is free to
  50. choose any of its implementations.
  51. </li>
  52. </ul>
  53. Tasks and their implementations must be <em>declared</em>. These
  54. declarations are annotated with attributes (@pxref{Attribute
  55. Syntax, attributes in GNU C,, gcc, Using the GNU Compiler Collection
  56. (GCC)}): the declaration of a task is a regular C function declaration
  57. with an additional <c>task</c> attribute, and task implementations are
  58. declared with a <c>task_implementation</c> attribute.
  59. The following function attributes are provided:
  60. <dl>
  61. <dt><c>task</c></dt>
  62. <dd>
  63. Declare the given function as a StarPU task. Its return type must be
  64. <c>void</c>. When a function declared as <c>task</c> has a user-defined
  65. body, that body is interpreted as the implicit definition of the
  66. task's CPU implementation (see example below). In all cases, the
  67. actual definition of a task's body is automatically generated by the
  68. compiler.
  69. Under the hood, declaring a task leads to the declaration of the
  70. corresponding <c>codelet</c> (\ref Codelet_and_Tasks). If one or
  71. more task implementations are declared in the same compilation unit,
  72. then the codelet and the function itself are also defined; they inherit
  73. the scope of the task.
  74. Scalar arguments to the task are passed by value and copied to the
  75. target device if need be---technically, they are passed as the
  76. <c>cl_arg</c> buffer (\ref Codelets_and_Tasks).
  77. Pointer arguments are assumed to be registered data buffers---the
  78. buffer argument of a task); <c>const</c>-qualified
  79. pointer arguments are viewed as read-only buffers (::STARPU_R), and
  80. non-<c>const</c>-qualified buffers are assumed to be used read-write
  81. (::STARPU_RW). In addition, the <c>output</c> type attribute can be
  82. as a type qualifier for output pointer or array parameters
  83. (::STARPU_W).
  84. </dd>
  85. <dt><c>task_implementation (target, task)</c></dt>
  86. <dd>
  87. Declare the given function as an implementation of <c>task</c> to run on
  88. <c>target</c>. <c>target</c> must be a string, currently one of
  89. <c>"cpu"</c>, <c>"opencl"</c>, or <c>"cuda"</c>.
  90. \internal
  91. FIXME: Update when OpenCL support is ready.
  92. \endinternal
  93. </dd>
  94. </dl>
  95. Here is an example:
  96. \code{.c}
  97. #define __output __attribute__ ((output))
  98. static void matmul (const float *A, const float *B,
  99. __output float *C,
  100. unsigned nx, unsigned ny, unsigned nz)
  101. __attribute__ ((task));
  102. static void matmul_cpu (const float *A, const float *B,
  103. __output float *C,
  104. unsigned nx, unsigned ny, unsigned nz)
  105. __attribute__ ((task_implementation ("cpu", matmul)));
  106. static void
  107. matmul_cpu (const float *A, const float *B, __output float *C,
  108. unsigned nx, unsigned ny, unsigned nz)
  109. {
  110. unsigned i, j, k;
  111. for (j = 0; j < ny; j++)
  112. for (i = 0; i < nx; i++)
  113. {
  114. for (k = 0; k < nz; k++)
  115. C[j * nx + i] += A[j * nz + k] * B[k * nx + i];
  116. }
  117. }
  118. \endcode
  119. A <c>matmult</c> task is defined; it has only one implementation,
  120. <c>matmult_cpu</c>, which runs on the CPU. Variables <c>A</c> and
  121. <c>B</c> are input buffers, whereas <c>C</c> is considered an input/output
  122. buffer.
  123. For convenience, when a function declared with the <c>task</c> attribute
  124. has a user-defined body, that body is assumed to be that of the CPU
  125. implementation of a task, which we call an implicit task CPU
  126. implementation. Thus, the above snippet can be simplified like this:
  127. \code{.c}
  128. #define __output __attribute__ ((output))
  129. static void matmul (const float *A, const float *B,
  130. __output float *C,
  131. unsigned nx, unsigned ny, unsigned nz)
  132. __attribute__ ((task));
  133. /* Implicit definition of the CPU implementation of the
  134. `matmul' task. */
  135. static void
  136. matmul (const float *A, const float *B, __output float *C,
  137. unsigned nx, unsigned ny, unsigned nz)
  138. {
  139. unsigned i, j, k;
  140. for (j = 0; j < ny; j++)
  141. for (i = 0; i < nx; i++)
  142. {
  143. for (k = 0; k < nz; k++)
  144. C[j * nx + i] += A[j * nz + k] * B[k * nx + i];
  145. }
  146. }
  147. \endcode
  148. Use of implicit CPU task implementations as above has the advantage that
  149. the code is valid sequential code when StarPU's GCC plug-in is not used
  150. (\ref Conditional_Extensions).
  151. CUDA and OpenCL implementations can be declared in a similar way:
  152. \code{.c}
  153. static void matmul_cuda (const float *A, const float *B, float *C,
  154. unsigned nx, unsigned ny, unsigned nz)
  155. __attribute__ ((task_implementation ("cuda", matmul)));
  156. static void matmul_opencl (const float *A, const float *B, float *C,
  157. unsigned nx, unsigned ny, unsigned nz)
  158. __attribute__ ((task_implementation ("opencl", matmul)));
  159. \endcode
  160. The CUDA and OpenCL implementations typically either invoke a kernel
  161. written in CUDA or OpenCL (for similar code, \ref CUDA_Kernel, and
  162. \ref OpenCL_Kernel), or call a library function that uses CUDA or
  163. OpenCL under the hood, such as CUBLAS functions:
  164. \code{.c}
  165. static void
  166. matmul_cuda (const float *A, const float *B, float *C,
  167. unsigned nx, unsigned ny, unsigned nz)
  168. {
  169. cublasSgemm ('n', 'n', nx, ny, nz,
  170. 1.0f, A, 0, B, 0,
  171. 0.0f, C, 0);
  172. cudaStreamSynchronize (starpu_cuda_get_local_stream ());
  173. }
  174. \endcode
  175. A task can be invoked like a regular C function:
  176. \code{.c}
  177. matmul (&A[i * zdim * bydim + k * bzdim * bydim],
  178. &B[k * xdim * bzdim + j * bxdim * bzdim],
  179. &C[i * xdim * bydim + j * bxdim * bydim],
  180. bxdim, bydim, bzdim);
  181. \endcode
  182. This leads to an asynchronous invocation, whereby <c>matmult</c>'s
  183. implementation may run in parallel with the continuation of the caller.
  184. The next section describes how memory buffers must be handled in
  185. StarPU-GCC code. For a complete example, see the
  186. <c>gcc-plugin/examples</c> directory of the source distribution, and
  187. \ref Vector_Scaling_Using_the_C_Extension.
  188. \section Synchronization_and_Other_Pragmas Initialization, Termination, and Synchronization
  189. The following pragmas allow user code to control StarPU's life time and
  190. to synchronize with tasks.
  191. <dl>
  192. <dt><c>\#pragma starpu initialize</c></dt>
  193. <dd>
  194. Initialize StarPU. This call is compulsory and is <em>never</em> added
  195. implicitly. One of the reasons this has to be done explicitly is that
  196. it provides greater control to user code over its resource usage.
  197. </dd>
  198. <dt><c>\#pragma starpu shutdown</c></dt>
  199. <dd>
  200. Shut down StarPU, giving it an opportunity to write profiling info to a
  201. file on disk, for instance (\ref Off-line_performance_feedback).
  202. </dd>
  203. <dt><c>\#pragma starpu wait</c></dt>
  204. <dd>
  205. Wait for all task invocations to complete, as with
  206. starpu_wait_for_all().
  207. </dd>
  208. </dl>
  209. \section Registered_Data_Buffers Registered Data Buffers
  210. Data buffers such as matrices and vectors that are to be passed to tasks
  211. must be registered. Registration allows StarPU to handle data
  212. transfers among devices---e.g., transferring an input buffer from the
  213. CPU's main memory to a task scheduled to run a GPU (\ref StarPU_Data_Management_Library).
  214. The following pragmas are provided:
  215. <dl>
  216. <dt><c>\#pragma starpu register ptr [size]</c></dt>
  217. <dd>
  218. Register <c>ptr</c> as a <c>size</c>-element buffer. When <c>ptr</c> has
  219. an array type whose size is known, <c>size</c> may be omitted.
  220. Alternatively, the <c>registered</c> attribute can be used (see below.)
  221. </dd>
  222. <dt><c>\#pragma starpu unregister ptr</c></dt>
  223. <dd>
  224. Unregister the previously-registered memory area pointed to by
  225. <c>ptr</c>. As a side-effect, <c>ptr</c> points to a valid copy in main
  226. memory.
  227. </dd>
  228. <dt><c>\#pragma starpu acquire ptr</c></dt>
  229. <dd>
  230. Acquire in main memory an up-to-date copy of the previously-registered
  231. memory area pointed to by <c>ptr</c>, for read-write access.
  232. </dd>
  233. <dt><c>\#pragma starpu release ptr</c></dt>
  234. <dd>
  235. Release the previously-register memory area pointed to by <c>ptr</c>,
  236. making it available to the tasks.
  237. </dd>
  238. </dl>
  239. Additionally, the following attributes offer a simple way to allocate
  240. and register storage for arrays:
  241. <dl>
  242. <dt><c>registered</c></dt>
  243. <dd>
  244. This attributes applies to local variables with an array type. Its
  245. effect is to automatically register the array's storage, as per
  246. <c>\#pragma starpu register</c>. The array is automatically unregistered
  247. when the variable's scope is left. This attribute is typically used in
  248. conjunction with the <c>heap_allocated</c> attribute, described below.
  249. </dd>
  250. <dt><c>heap_allocated</c></dt>
  251. <dd>
  252. This attributes applies to local variables with an array type. Its
  253. effect is to automatically allocate the array's storage on
  254. the heap, using starpu_malloc() under the hood. The heap-allocated array is automatically
  255. freed when the variable's scope is left, as with
  256. automatic variables.
  257. </dd>
  258. </dl>
  259. The following example illustrates use of the <c>heap_allocated</c>
  260. attribute:
  261. \code{.c}
  262. extern void cholesky(unsigned nblocks, unsigned size,
  263. float mat[nblocks][nblocks][size])
  264. __attribute__ ((task));
  265. int
  266. main (int argc, char *argv[])
  267. {
  268. #pragma starpu initialize
  269. /* ... */
  270. int nblocks, size;
  271. parse_args (&nblocks, &size);
  272. /* Allocate an array of the required size on the heap,
  273. and register it. */
  274. {
  275. float matrix[nblocks][nblocks][size]
  276. __attribute__ ((heap_allocated, registered));
  277. cholesky (nblocks, size, matrix);
  278. #pragma starpu wait
  279. } /* MATRIX is automatically unregistered & freed here. */
  280. #pragma starpu shutdown
  281. return EXIT_SUCCESS;
  282. }
  283. \endcode
  284. \section Conditional_Extensions Using C Extensions Conditionally
  285. The C extensions described in this chapter are only available when GCC
  286. and its StarPU plug-in are in use. Yet, it is possible to make use of
  287. these extensions when they are available---leading to hybrid CPU/GPU
  288. code---and discard them when they are not available---leading to valid
  289. sequential code.
  290. To that end, the GCC plug-in defines a C preprocessor macro when it is
  291. being used:
  292. @defmac STARPU_GCC_PLUGIN
  293. Defined for code being compiled with the StarPU GCC plug-in. When
  294. defined, this macro expands to an integer denoting the version of the
  295. supported C extensions.
  296. @end defmac
  297. The code below illustrates how to define a task and its implementations
  298. in a way that allows it to be compiled without the GCC plug-in:
  299. \code{.c}
  300. /* This program is valid, whether or not StarPU's GCC plug-in
  301. is being used. */
  302. #include <stdlib.h>
  303. /* The attribute below is ignored when GCC is not used. */
  304. static void matmul (const float *A, const float *B, float * C,
  305. unsigned nx, unsigned ny, unsigned nz)
  306. __attribute__ ((task));
  307. static void
  308. matmul (const float *A, const float *B, float * C,
  309. unsigned nx, unsigned ny, unsigned nz)
  310. {
  311. /* Code of the CPU kernel here... */
  312. }
  313. #ifdef STARPU_GCC_PLUGIN
  314. /* Optional OpenCL task implementation. */
  315. static void matmul_opencl (const float *A, const float *B, float * C,
  316. unsigned nx, unsigned ny, unsigned nz)
  317. __attribute__ ((task_implementation ("opencl", matmul)));
  318. static void
  319. matmul_opencl (const float *A, const float *B, float * C,
  320. unsigned nx, unsigned ny, unsigned nz)
  321. {
  322. /* Code that invokes the OpenCL kernel here... */
  323. }
  324. #endif
  325. int
  326. main (int argc, char *argv[])
  327. {
  328. /* The pragmas below are simply ignored when StarPU-GCC
  329. is not used. */
  330. #pragma starpu initialize
  331. float A[123][42][7], B[123][42][7], C[123][42][7];
  332. #pragma starpu register A
  333. #pragma starpu register B
  334. #pragma starpu register C
  335. /* When StarPU-GCC is used, the call below is asynchronous;
  336. otherwise, it is synchronous. */
  337. matmul ((float *) A, (float *) B, (float *) C, 123, 42, 7);
  338. #pragma starpu wait
  339. #pragma starpu shutdown
  340. return EXIT_SUCCESS;
  341. }
  342. \endcode
  343. The above program is a valid StarPU program when StarPU's GCC plug-in is
  344. used; it is also a valid sequential program when the plug-in is not
  345. used.
  346. Note that attributes such as <c>task</c> as well as <c>starpu</c>
  347. pragmas are simply ignored by GCC when the StarPU plug-in is not loaded.
  348. However, <c>gcc -Wall</c> emits a warning for unknown attributes and
  349. pragmas, which can be inconvenient. In addition, other compilers may be
  350. unable to parse the attribute syntax (In practice, Clang and
  351. several proprietary compilers implement attributes.), so you may want to
  352. wrap attributes in macros like this:
  353. \code{.c}
  354. /* Use the `task' attribute only when StarPU's GCC plug-in
  355. is available. */
  356. #ifdef STARPU_GCC_PLUGIN
  357. # define __task __attribute__ ((task))
  358. #else
  359. # define __task
  360. #endif
  361. static void matmul (const float *A, const float *B, float *C,
  362. unsigned nx, unsigned ny, unsigned nz) __task;
  363. \endcode
  364. */