c-extensions.texi 5.5 KB

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