c-extensions.texi 5.2 KB

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