c-extensions.texi 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. @c -*-texinfo-*-
  2. @c This file is part of the StarPU Handbook.
  3. @c Copyright (C) 2011 Institut National de Recherche en Informatique et Automatique
  4. @c See the file starpu.texi for copying conditions.
  5. @node C Extensions
  6. @chapter C Extensions
  7. @cindex C extensions
  8. @cindex GCC plug-in
  9. When configured with @code{--enable-gcc-extensions}, StarPU builds a
  10. plug-in for the GNU Compiler Collection (GCC), which defines extensions
  11. to the C language that make it easier to write StarPU code@footnote{This
  12. feature is only available for GCC 4.5 and later.}. Those extensions
  13. include syntactic sugar for defining tasks and their implementations,
  14. invoking a task, and manipulating data buffers.
  15. This section does not require detailed knowledge of the StarPU library.
  16. Note: as of StarPU @value{VERSION}, this is still an area under
  17. development and subject to change.
  18. @menu
  19. * Defining Tasks:: Defining StarPU tasks
  20. * Registered Data Buffers:: Manipulating data buffers
  21. @end menu
  22. @node Defining Tasks
  23. @section Defining Tasks
  24. @cindex task
  25. @cindex task implementation
  26. The StarPU GCC plug-in views @dfn{tasks} as ``extended'' C functions:
  27. @enumerate
  28. @item
  29. tasks may have several implementations---e.g., one for CPUs, one written
  30. in OpenCL, one written in CUDA;
  31. @item
  32. tasks may have several implementations of the same target---e.g.,
  33. several CPU implementations;
  34. @item
  35. when a task is invoked, it may run in parallel, and StarPU is free to
  36. choose any of its implementations.
  37. @end enumerate
  38. Tasks and their implementations must be @emph{declared}. These
  39. declarations are annotated with @dfn{attributes} (@pxref{Attribute
  40. Syntax, attributes in GNU C,, gcc, Using the GNU Compiler Collection
  41. (GCC)}): the declaration of a task is a regular C function declaration
  42. with an additional @code{task} attribute, and task implementations are
  43. declared with a @code{task_implementation} attribute.
  44. The following function attributes are provided:
  45. @table @code
  46. @item task
  47. @cindex @code{task} attribute
  48. Declare the given function as a StarPU task. Its return type must be
  49. @code{void}, and it must not be defined---instead, a definition will
  50. automatically be provided by the compiler.
  51. Under the hood, declaring a task leads to the declaration of the
  52. corresponding @code{codelet} (@pxref{Codelet and Tasks}). If one or
  53. more task implementations are declared in the same compilation unit,
  54. then the codelet and the function itself are also defined; they inherit
  55. the scope of the task.
  56. Scalar arguments to the task are passed by value and copied to the
  57. target device if need be---technically, they are passed as the
  58. @code{cl_arg} buffer (@pxref{Codelets and Tasks, @code{cl_arg}}).
  59. Pointer arguments are assumed to be registered data buffers---the
  60. @code{buffers} argument of a task (@pxref{Codelets and Tasks,
  61. @code{buffers}}); @code{const}-qualified pointer arguments are viewed as
  62. read-only buffers (@code{STARPU_R}), and non-@code{const}-qualified
  63. buffers are assumed to be used read-write (@code{STARPU_RW}).
  64. @item task_implementation (@var{target}, @var{task})
  65. @cindex @code{task_implementation} attribute
  66. Declare the given function as an implementation of @var{task} to run on
  67. @var{target}. @var{target} must be a string, currently one of
  68. @code{"cpu"} or @code{"cuda"}.
  69. @c FIXME: Update when OpenCL support is ready.
  70. @end table
  71. Here is an example:
  72. @example
  73. static void matmul (const float *A, const float *B, float *C,
  74. size_t nx, size_t ny, size_t nz)
  75. __attribute__ ((task));
  76. static void matmul_cpu (const float *A, const float *B, float *C,
  77. size_t nx, size_t ny, size_t nz)
  78. __attribute__ ((task_implementation ("cpu", matmul)));
  79. static void
  80. matmul_cpu (const float *A, const float *B, float *C,
  81. size_t nx, size_t ny, size_t nz)
  82. @{
  83. size_t i, j, k;
  84. for (j = 0; j < ny; j++)
  85. for (i = 0; i < nx; i++)
  86. @{
  87. for (k = 0; k < nz; k++)
  88. C[j * nx + i] += A[j * nz + k] * B[k * nx + i];
  89. @}
  90. @}
  91. @end example
  92. @noindent
  93. A @code{matmult} task is defined; it has only one implementation,
  94. @code{matmult_cpu}, which runs on the CPU. Variables @var{A} and
  95. @var{B} are input buffers, whereas @var{C} is considered an input/output
  96. buffer. The task can be invoked like a regular C function:
  97. @example
  98. matmul (&A[i * zdim * bydim + k * bzdim * bydim],
  99. &B[k * xdim * bzdim + j * bxdim * bzdim],
  100. &C[i * xdim * bydim + j * bxdim * bydim],
  101. bxdim, bydim, bzdim);
  102. @end example
  103. @noindent
  104. This leads to an @dfn{asynchronous invocation}, whereby @code{matmult}'s
  105. implementation may run in parallel with the continuation of the caller.
  106. The next section describes how memory buffers must be handled in
  107. StarPU-GCC code.
  108. @node Registered Data Buffers
  109. @section Registered Data Buffers
  110. Data buffers such as matrices and vectors that are to be passed to tasks
  111. must be @dfn{registered}. Registration allows StarPU to handle data
  112. transfers among devices---e.g., transferring an input buffer from the
  113. CPU's main memory to a task scheduled to run a GPU (@pxref{StarPU Data
  114. Management Library}).
  115. The following pragmas are provided:
  116. @table @code
  117. @item #pragma starpu register @var{ptr} [@var{size}]
  118. Register @var{ptr} as a @var{size}-element buffer.
  119. @item #pragma starpu unregister @var{ptr}
  120. @item #pragma starpu acquire @var{ptr}
  121. @end table
  122. FIXME: finish
  123. @c Local Variables:
  124. @c TeX-master: "guile.texi"
  125. @c ispell-local-dictionary: "american"
  126. @c End: