c-extensions.texi 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. when a task is invoked, it may run in parallel, and StarPU is free to
  33. choose any of its implementations.
  34. @end enumerate
  35. Tasks and their implementations must be @emph{declared}. These
  36. declarations are annotated with @dfn{attributes} (@pxref{Attribute
  37. Syntax, attributes in GNU C,, gcc, Using the GNU Compiler Collection
  38. (GCC)}): the declaration of a task is a regular C function declaration
  39. with an additional @code{task} attribute, and task implementations are
  40. declared with a @code{task_implementation} attribute.
  41. The following function attributes are provided:
  42. @table @code
  43. @item task
  44. @cindex @code{task} attribute
  45. Declare the given function as a StarPU task. Its return type must be
  46. @code{void}, and it must not be defined---instead, a definition will
  47. automatically be provided by the compiler.
  48. Under the hood, declaring a task leads to the declaration of the
  49. corresponding @code{codelet} (@pxref{Codelet and Tasks}). If one or
  50. more task implementations are declared in the same compilation unit,
  51. then the codelet and the function itself are also defined; they inherit
  52. the scope of the task.
  53. Scalar arguments to the task are passed by value and copied to the
  54. target device if need be---technically, they are passed as the
  55. @code{cl_arg} buffer (@pxref{Codelets and Tasks, @code{cl_arg}}).
  56. Pointer arguments are assumed to be registered data buffers---the
  57. @code{buffers} argument of a task (@pxref{Codelets and Tasks,
  58. @code{buffers}}); @code{const}-qualified pointer arguments are viewed as
  59. read-only buffers (@code{STARPU_R}), and non-@code{const}-qualified
  60. buffers are assumed to be used read-write (@code{STARPU_RW}).
  61. @item task_implementation (@var{target}, @var{task})
  62. @cindex @code{task_implementation} attribute
  63. Declare the given function as an implementation of @var{task} to run on
  64. @var{target}. @var{target} must be a string, currently one of
  65. @code{"cpu"} or @code{"cuda"}.
  66. @c FIXME: Update when OpenCL support is ready.
  67. @end table
  68. Here is an example:
  69. @example
  70. static void matmul (const float *A, const float *B, float *C,
  71. size_t nx, size_t ny, size_t nz)
  72. __attribute__ ((task));
  73. static void matmul_cpu (const float *A, const float *B, float *C,
  74. size_t nx, size_t ny, size_t nz)
  75. __attribute__ ((task_implementation ("cpu", matmul)));
  76. static void
  77. matmul_cpu (const float *A, const float *B, float *C,
  78. size_t nx, size_t ny, size_t nz)
  79. @{
  80. size_t i, j, k;
  81. for (j = 0; j < ny; j++)
  82. for (i = 0; i < nx; i++)
  83. @{
  84. for (k = 0; k < nz; k++)
  85. C[j * nx + i] += A[j * nz + k] * B[k * nx + i];
  86. @}
  87. @}
  88. @end example
  89. @noindent
  90. A @code{matmult} task is defined; it has only one implementation,
  91. @code{matmult_cpu}, which runs on the CPU. Variables @var{A} and
  92. @var{B} are input buffers, whereas @var{C} is considered an input/output
  93. buffer. The task can be invoked like a regular C function:
  94. @example
  95. matmul (&A[i * zdim * bydim + k * bzdim * bydim],
  96. &B[k * xdim * bzdim + j * bxdim * bzdim],
  97. &C[i * xdim * bydim + j * bxdim * bydim],
  98. bxdim, bydim, bzdim);
  99. @end example
  100. @noindent
  101. This leads to an @dfn{asynchronous invocation}, whereby @code{matmult}'s
  102. implementation may run in parallel with the continuation of the caller.
  103. The next section describes how memory buffers must be handled in
  104. StarPU-GCC code.
  105. @node Registered Data Buffers
  106. @section Registered Data Buffers
  107. Data buffers such as matrices and vectors that are to be passed to tasks
  108. must be @dfn{registered}. Registration allows StarPU to handle data
  109. transfers among devices---e.g., transferring an input buffer from the
  110. CPU's main memory to a task scheduled to run a GPU (@pxref{StarPU Data
  111. Management Library}).
  112. The following pragmas are provided:
  113. @table @code
  114. @item #pragma starpu register @var{ptr} [@var{size}]
  115. Register @var{ptr} as a @var{size}-element buffer.
  116. @item #pragma starpu unregister @var{ptr}
  117. @item #pragma starpu acquire @var{ptr}
  118. @end table
  119. FIXME: finish
  120. @c Local Variables:
  121. @c TeX-master: "guile.texi"
  122. @c ispell-local-dictionary: "american"
  123. @c End: