c-extensions.texi 5.1 KB

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