19c_extensions.doxy 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. /*
  2. * This file is part of the StarPU Handbook.
  3. * Copyright (C) 2009--2011 Universit@'e de Bordeaux
  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 \ref disable-gcc-extensions "--disable-gcc-extensions".
  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 UsingCExtensionsConditionally).
  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.2 --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 DefiningTasks 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
  55. (http://gcc.gnu.org/onlinedocs/gcc/Attribute-Syntax.html#Attribute-Syntax):
  56. the declaration of a task is a regular C function declaration with an
  57. 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 CodeletAndTasks). 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 buffer
  76. starpu_task::cl_arg (\ref CodeletAndTasks).
  77. Pointer arguments are assumed to be registered data buffers---the
  78. handles argument of a task (starpu_task::handles) ; <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. // FIXME: Update when OpenCL support is ready.
  91. </dd>
  92. </dl>
  93. Here is an example:
  94. \code{.c}
  95. #define __output __attribute__ ((output))
  96. static void matmul (const float *A, const float *B,
  97. __output float *C,
  98. unsigned nx, unsigned ny, unsigned nz)
  99. __attribute__ ((task));
  100. static void matmul_cpu (const float *A, const float *B,
  101. __output float *C,
  102. unsigned nx, unsigned ny, unsigned nz)
  103. __attribute__ ((task_implementation ("cpu", matmul)));
  104. static void
  105. matmul_cpu (const float *A, const float *B, __output float *C,
  106. unsigned nx, unsigned ny, unsigned nz)
  107. {
  108. unsigned i, j, k;
  109. for (j = 0; j < ny; j++)
  110. for (i = 0; i < nx; i++)
  111. {
  112. for (k = 0; k < nz; k++)
  113. C[j * nx + i] += A[j * nz + k] * B[k * nx + i];
  114. }
  115. }
  116. \endcode
  117. A <c>matmult</c> task is defined; it has only one implementation,
  118. <c>matmult_cpu</c>, which runs on the CPU. Variables <c>A</c> and
  119. <c>B</c> are input buffers, whereas <c>C</c> is considered an input/output
  120. buffer.
  121. For convenience, when a function declared with the <c>task</c> attribute
  122. has a user-defined body, that body is assumed to be that of the CPU
  123. implementation of a task, which we call an implicit task CPU
  124. implementation. Thus, the above snippet can be simplified like this:
  125. \code{.c}
  126. #define __output __attribute__ ((output))
  127. static void matmul (const float *A, const float *B,
  128. __output float *C,
  129. unsigned nx, unsigned ny, unsigned nz)
  130. __attribute__ ((task));
  131. /* Implicit definition of the CPU implementation of the
  132. `matmul' task. */
  133. static void
  134. matmul (const float *A, const float *B, __output float *C,
  135. unsigned nx, unsigned ny, unsigned nz)
  136. {
  137. unsigned i, j, k;
  138. for (j = 0; j < ny; j++)
  139. for (i = 0; i < nx; i++)
  140. {
  141. for (k = 0; k < nz; k++)
  142. C[j * nx + i] += A[j * nz + k] * B[k * nx + i];
  143. }
  144. }
  145. \endcode
  146. Use of implicit CPU task implementations as above has the advantage that
  147. the code is valid sequential code when StarPU's GCC plug-in is not used
  148. (\ref UsingCExtensionsConditionally).
  149. CUDA and OpenCL implementations can be declared in a similar way:
  150. \code{.c}
  151. static void matmul_cuda (const float *A, const float *B, float *C,
  152. unsigned nx, unsigned ny, unsigned nz)
  153. __attribute__ ((task_implementation ("cuda", matmul)));
  154. static void matmul_opencl (const float *A, const float *B, float *C,
  155. unsigned nx, unsigned ny, unsigned nz)
  156. __attribute__ ((task_implementation ("opencl", matmul)));
  157. \endcode
  158. The CUDA and OpenCL implementations typically either invoke a kernel
  159. written in CUDA or OpenCL (for similar code, \ref CUDAKernel, and
  160. \ref OpenCLKernel), or call a library function that uses CUDA or
  161. OpenCL under the hood, such as CUBLAS functions:
  162. \code{.c}
  163. static void
  164. matmul_cuda (const float *A, const float *B, float *C,
  165. unsigned nx, unsigned ny, unsigned nz)
  166. {
  167. cublasSgemm ('n', 'n', nx, ny, nz,
  168. 1.0f, A, 0, B, 0,
  169. 0.0f, C, 0);
  170. cudaStreamSynchronize (starpu_cuda_get_local_stream ());
  171. }
  172. \endcode
  173. A task can be invoked like a regular C function:
  174. \code{.c}
  175. matmul (&A[i * zdim * bydim + k * bzdim * bydim],
  176. &B[k * xdim * bzdim + j * bxdim * bzdim],
  177. &C[i * xdim * bydim + j * bxdim * bydim],
  178. bxdim, bydim, bzdim);
  179. \endcode
  180. This leads to an asynchronous invocation, whereby <c>matmult</c>'s
  181. implementation may run in parallel with the continuation of the caller.
  182. The next section describes how memory buffers must be handled in
  183. StarPU-GCC code. For a complete example, see the
  184. <c>gcc-plugin/examples</c> directory of the source distribution, and
  185. \ref VectorScalingUsingTheCExtension.
  186. \section InitializationTerminationAndSynchronization Initialization, Termination, and Synchronization
  187. The following pragmas allow user code to control StarPU's life time and
  188. to synchronize with tasks.
  189. <dl>
  190. <dt><c>\#pragma starpu initialize</c></dt>
  191. <dd>
  192. Initialize StarPU. This call is compulsory and is <em>never</em> added
  193. implicitly. One of the reasons this has to be done explicitly is that
  194. it provides greater control to user code over its resource usage.
  195. </dd>
  196. <dt><c>\#pragma starpu shutdown</c></dt>
  197. <dd>
  198. Shut down StarPU, giving it an opportunity to write profiling info to a
  199. file on disk, for instance (\ref Off-linePerformanceFeedback).
  200. </dd>
  201. <dt><c>\#pragma starpu wait</c></dt>
  202. <dd>
  203. Wait for all task invocations to complete, as with
  204. starpu_task_wait_for_all().
  205. </dd>
  206. </dl>
  207. \section RegisteredDataBuffers Registered Data Buffers
  208. Data buffers such as matrices and vectors that are to be passed to tasks
  209. must be registered. Registration allows StarPU to handle data
  210. transfers among devices---e.g., transferring an input buffer from the
  211. CPU's main memory to a task scheduled to run a GPU (\ref StarPUDataManagementLibrary).
  212. The following pragmas are provided:
  213. <dl>
  214. <dt><c>\#pragma starpu register ptr [size]</c></dt>
  215. <dd>
  216. Register <c>ptr</c> as a <c>size</c>-element buffer. When <c>ptr</c> has
  217. an array type whose size is known, <c>size</c> may be omitted.
  218. Alternatively, the <c>registered</c> attribute can be used (see below.)
  219. </dd>
  220. <dt><c>\#pragma starpu unregister ptr</c></dt>
  221. <dd>
  222. Unregister the previously-registered memory area pointed to by
  223. <c>ptr</c>. As a side-effect, <c>ptr</c> points to a valid copy in main
  224. memory.
  225. </dd>
  226. <dt><c>\#pragma starpu acquire ptr</c></dt>
  227. <dd>
  228. Acquire in main memory an up-to-date copy of the previously-registered
  229. memory area pointed to by <c>ptr</c>, for read-write access.
  230. </dd>
  231. <dt><c>\#pragma starpu release ptr</c></dt>
  232. <dd>
  233. Release the previously-register memory area pointed to by <c>ptr</c>,
  234. making it available to the tasks.
  235. </dd>
  236. </dl>
  237. Additionally, the following attributes offer a simple way to allocate
  238. and register storage for arrays:
  239. <dl>
  240. <dt><c>registered</c></dt>
  241. <dd>
  242. This attributes applies to local variables with an array type. Its
  243. effect is to automatically register the array's storage, as per
  244. <c>\#pragma starpu register</c>. The array is automatically unregistered
  245. when the variable's scope is left. This attribute is typically used in
  246. conjunction with the <c>heap_allocated</c> attribute, described below.
  247. </dd>
  248. <dt><c>heap_allocated</c></dt>
  249. <dd>
  250. This attributes applies to local variables with an array type. Its
  251. effect is to automatically allocate the array's storage on
  252. the heap, using starpu_malloc() under the hood. The heap-allocated array is automatically
  253. freed when the variable's scope is left, as with
  254. automatic variables.
  255. </dd>
  256. </dl>
  257. The following example illustrates use of the <c>heap_allocated</c>
  258. attribute:
  259. \snippet cholesky_pragma.c To be included. You should update doxygen if you see this text.
  260. \section UsingCExtensionsConditionally Using C Extensions Conditionally
  261. The C extensions described in this chapter are only available when GCC
  262. and its StarPU plug-in are in use. Yet, it is possible to make use of
  263. these extensions when they are available---leading to hybrid CPU/GPU
  264. code---and discard them when they are not available---leading to valid
  265. sequential code.
  266. To that end, the GCC plug-in defines the C preprocessor macro ---
  267. <c>STARPU_GCC_PLUGIN</c> --- when it is being used. When defined, this
  268. macro expands to an integer denoting the version of the supported C
  269. extensions.
  270. The code below illustrates how to define a task and its implementations
  271. in a way that allows it to be compiled without the GCC plug-in:
  272. \snippet matmul_pragma.c To be included. You should update doxygen if you see this text.
  273. The above program is a valid StarPU program when StarPU's GCC plug-in is
  274. used; it is also a valid sequential program when the plug-in is not
  275. used.
  276. Note that attributes such as <c>task</c> as well as <c>starpu</c>
  277. pragmas are simply ignored by GCC when the StarPU plug-in is not loaded.
  278. However, <c>gcc -Wall</c> emits a warning for unknown attributes and
  279. pragmas, which can be inconvenient. In addition, other compilers may be
  280. unable to parse the attribute syntax (In practice, Clang and
  281. several proprietary compilers implement attributes.), so you may want to
  282. wrap attributes in macros like this:
  283. \snippet matmul_pragma2.c To be included. You should update doxygen if you see this text.
  284. */