c-extensions.texi 5.8 KB

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