basic-examples.texi 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961
  1. @c -*-texinfo-*-
  2. @c This file is part of the StarPU Handbook.
  3. @c Copyright (C) 2009--2011 Universit@'e de Bordeaux 1
  4. @c Copyright (C) 2010, 2011, 2012, 2013 Centre National de la Recherche Scientifique
  5. @c Copyright (C) 2011, 2012 Institut National de Recherche en Informatique et Automatique
  6. @c See the file starpu.texi for copying conditions.
  7. @menu
  8. * Hello World using the C Extension::
  9. * Hello World using StarPU's API::
  10. * Vector Scaling Using the C Extension::
  11. * Vector Scaling Using StarPU's API::
  12. * Vector Scaling on an Hybrid CPU/GPU Machine:: Handling Heterogeneous Architectures
  13. @end menu
  14. @node Hello World using the C Extension
  15. @section Hello World using the C Extension
  16. This section shows how to implement a simple program that submits a task
  17. to StarPU using the StarPU C extension (@pxref{C
  18. Extensions})@footnote{The complete example, and additional examples,
  19. is available in the @file{gcc-plugin/examples} directory of the StarPU
  20. distribution.}. A similar example showing how to directly use the StarPU's API is shown
  21. in @ref{Hello World using StarPU's API}.
  22. GCC from version 4.5 permit to use the StarPU GCC plug-in (@pxref{C
  23. Extensions}). This makes writing a task both simpler and less error-prone.
  24. In a nutshell, all it takes is to declare a task, declare and define its
  25. implementations (for CPU, OpenCL, and/or CUDA), and invoke the task like
  26. a regular C function. The example below defines @code{my_task}, which
  27. has a single implementation for CPU:
  28. @cartouche
  29. @smallexample
  30. #include <stdio.h>
  31. /* @b{Task declaration.} */
  32. static void my_task (int x) __attribute__ ((task));
  33. /* @b{Definition of the CPU implementation of `my_task'.} */
  34. static void my_task (int x)
  35. @{
  36. printf ("Hello, world! With x = %d\n", x);
  37. @}
  38. int main ()
  39. @{
  40. /* @b{Initialize StarPU.} */
  41. #pragma starpu initialize
  42. /* @b{Do an asynchronous call to `my_task'.} */
  43. my_task (42);
  44. /* @b{Wait for the call to complete.} */
  45. #pragma starpu wait
  46. /* @b{Terminate.} */
  47. #pragma starpu shutdown
  48. return 0;
  49. @}
  50. @end smallexample
  51. @end cartouche
  52. @noindent
  53. The code can then be compiled and linked with GCC and the
  54. @code{-fplugin} flag:
  55. @example
  56. $ gcc `pkg-config starpu-1.1 --cflags` hello-starpu.c \
  57. -fplugin=`pkg-config starpu-1.1 --variable=gccplugin` \
  58. `pkg-config starpu-1.1 --libs`
  59. @end example
  60. The code can also be compiled without the StarPU C extension and will
  61. behave as a normal sequential code.
  62. @example
  63. $ gcc hello-starpu.c
  64. hello-starpu.c:33:1: warning: ‘task’ attribute directive ignored [-Wattributes]
  65. $ ./a.out
  66. Hello, world! With x = 42
  67. @end example
  68. As can be seen above, the C extensions allows programmers to
  69. use StarPU tasks by essentially annotating ``regular'' C code.
  70. @node Hello World using StarPU's API
  71. @section Hello World using StarPU's API
  72. This section shows how to achieve the same result as in the previous
  73. section using StarPU's standard C API.
  74. @menu
  75. * Required Headers::
  76. * Defining a Codelet::
  77. * Submitting a Task::
  78. * Execution of Hello World::
  79. @end menu
  80. @node Required Headers
  81. @subsection Required Headers
  82. The @code{starpu.h} header should be included in any code using StarPU.
  83. @cartouche
  84. @smallexample
  85. #include <starpu.h>
  86. @end smallexample
  87. @end cartouche
  88. @node Defining a Codelet
  89. @subsection Defining a Codelet
  90. @cartouche
  91. @smallexample
  92. struct params
  93. @{
  94. int i;
  95. float f;
  96. @};
  97. void cpu_func(void *buffers[], void *cl_arg)
  98. @{
  99. struct params *params = cl_arg;
  100. printf("Hello world (params = @{%i, %f@} )\n", params->i, params->f);
  101. @}
  102. struct starpu_codelet cl =
  103. @{
  104. .where = STARPU_CPU,
  105. .cpu_funcs = @{ cpu_func, NULL @},
  106. .cpu_funcs_name = @{ "cpu_func", NULL @},
  107. .nbuffers = 0
  108. @};
  109. @end smallexample
  110. @end cartouche
  111. A codelet is a structure that represents a computational kernel. Such a codelet
  112. may contain an implementation of the same kernel on different architectures
  113. (e.g. CUDA, x86, ...). For compatibility, make sure that the whole
  114. structure is properly initialized to zero, either by using the
  115. function starpu_codelet_init (@pxref{starpu_codelet_init}), or by letting the
  116. compiler implicitly do it as examplified above.
  117. The @code{nbuffers} field specifies the number of data buffers that are
  118. manipulated by the codelet: here the codelet does not access or modify any data
  119. that is controlled by our data management library. Note that the argument
  120. passed to the codelet (the @code{cl_arg} field of the @code{starpu_task}
  121. structure) does not count as a buffer since it is not managed by our data
  122. management library, but just contain trivial parameters.
  123. @c TODO need a crossref to the proper description of "where" see bla for more ...
  124. We create a codelet which may only be executed on the CPUs. The @code{where}
  125. field is a bitmask that defines where the codelet may be executed. Here, the
  126. @code{STARPU_CPU} value means that only CPUs can execute this codelet
  127. (@pxref{Codelets and Tasks} for more details on this field). Note that
  128. the @code{where} field is optional, when unset its value is
  129. automatically set based on the availability of the different
  130. @code{XXX_funcs} fields.
  131. When a CPU core executes a codelet, it calls the @code{cpu_func} function,
  132. which @emph{must} have the following prototype:
  133. @code{void (*cpu_func)(void *buffers[], void *cl_arg);}
  134. In this example, we can ignore the first argument of this function which gives a
  135. description of the input and output buffers (e.g. the size and the location of
  136. the matrices) since there is none.
  137. The second argument is a pointer to a buffer passed as an
  138. argument to the codelet by the means of the @code{cl_arg} field of the
  139. @code{starpu_task} structure.
  140. @c TODO rewrite so that it is a little clearer ?
  141. Be aware that this may be a pointer to a
  142. @emph{copy} of the actual buffer, and not the pointer given by the programmer:
  143. if the codelet modifies this buffer, there is no guarantee that the initial
  144. buffer will be modified as well: this for instance implies that the buffer
  145. cannot be used as a synchronization medium. If synchronization is needed, data
  146. has to be registered to StarPU, see @ref{Vector Scaling Using StarPU's API}.
  147. @node Submitting a Task
  148. @subsection Submitting a Task
  149. @cartouche
  150. @smallexample
  151. void callback_func(void *callback_arg)
  152. @{
  153. printf("Callback function (arg %x)\n", callback_arg);
  154. @}
  155. int main(int argc, char **argv)
  156. @{
  157. /* @b{initialize StarPU} */
  158. starpu_init(NULL);
  159. struct starpu_task *task = starpu_task_create();
  160. task->cl = &cl; /* @b{Pointer to the codelet defined above} */
  161. struct params params = @{ 1, 2.0f @};
  162. task->cl_arg = &params;
  163. task->cl_arg_size = sizeof(params);
  164. task->callback_func = callback_func;
  165. task->callback_arg = 0x42;
  166. /* @b{starpu_task_submit will be a blocking call} */
  167. task->synchronous = 1;
  168. /* @b{submit the task to StarPU} */
  169. starpu_task_submit(task);
  170. /* @b{terminate StarPU} */
  171. starpu_shutdown();
  172. return 0;
  173. @}
  174. @end smallexample
  175. @end cartouche
  176. Before submitting any tasks to StarPU, @code{starpu_init} must be called. The
  177. @code{NULL} argument specifies that we use default configuration. Tasks cannot
  178. be submitted after the termination of StarPU by a call to
  179. @code{starpu_shutdown}.
  180. In the example above, a task structure is allocated by a call to
  181. @code{starpu_task_create}. This function only allocates and fills the
  182. corresponding structure with the default settings (@pxref{Codelets and
  183. Tasks, starpu_task_create}), but it does not submit the task to StarPU.
  184. @c not really clear ;)
  185. The @code{cl} field is a pointer to the codelet which the task will
  186. execute: in other words, the codelet structure describes which computational
  187. kernel should be offloaded on the different architectures, and the task
  188. structure is a wrapper containing a codelet and the piece of data on which the
  189. codelet should operate.
  190. The optional @code{cl_arg} field is a pointer to a buffer (of size
  191. @code{cl_arg_size}) with some parameters for the kernel
  192. described by the codelet. For instance, if a codelet implements a computational
  193. kernel that multiplies its input vector by a constant, the constant could be
  194. specified by the means of this buffer, instead of registering it as a StarPU
  195. data. It must however be noted that StarPU avoids making copy whenever possible
  196. and rather passes the pointer as such, so the buffer which is pointed at must
  197. kept allocated until the task terminates, and if several tasks are submitted
  198. with various parameters, each of them must be given a pointer to their own
  199. buffer.
  200. Once a task has been executed, an optional callback function is be called.
  201. While the computational kernel could be offloaded on various architectures, the
  202. callback function is always executed on a CPU. The @code{callback_arg}
  203. pointer is passed as an argument of the callback. The prototype of a callback
  204. function must be:
  205. @cartouche
  206. @example
  207. void (*callback_function)(void *);
  208. @end example
  209. @end cartouche
  210. If the @code{synchronous} field is non-zero, task submission will be
  211. synchronous: the @code{starpu_task_submit} function will not return until the
  212. task was executed. Note that the @code{starpu_shutdown} method does not
  213. guarantee that asynchronous tasks have been executed before it returns,
  214. @code{starpu_task_wait_for_all} can be used to that effect, or data can be
  215. unregistered (@code{starpu_data_unregister(vector_handle);}), which will
  216. implicitly wait for all the tasks scheduled to work on it, unless explicitly
  217. disabled thanks to @code{starpu_data_set_default_sequential_consistency_flag} or
  218. @code{starpu_data_set_sequential_consistency_flag}.
  219. @node Execution of Hello World
  220. @subsection Execution of Hello World
  221. @smallexample
  222. $ make hello_world
  223. cc $(pkg-config --cflags starpu-1.1) $(pkg-config --libs starpu-1.1) hello_world.c -o hello_world
  224. $ ./hello_world
  225. Hello world (params = @{1, 2.000000@} )
  226. Callback function (arg 42)
  227. @end smallexample
  228. @node Vector Scaling Using the C Extension
  229. @section Vector Scaling Using the C Extension
  230. @menu
  231. * Adding an OpenCL Task Implementation::
  232. * Adding a CUDA Task Implementation::
  233. @end menu
  234. The previous example has shown how to submit tasks. In this section,
  235. we show how StarPU tasks can manipulate data.
  236. We will first show how to use the C language extensions provided by
  237. the GCC plug-in (@pxref{C Extensions})@footnote{The complete example, and
  238. additional examples, is available in the @file{gcc-plugin/examples}
  239. directory of the StarPU distribution.}. These extensions map directly
  240. to StarPU's main concepts: tasks, task implementations for CPU,
  241. OpenCL, or CUDA, and registered data buffers. The standard C version
  242. that uses StarPU's standard C programming interface is given in the
  243. next section (@pxref{Vector Scaling Using StarPU's API, standard C
  244. version of the example}).
  245. First of all, the vector-scaling task and its simple CPU implementation
  246. has to be defined:
  247. @cartouche
  248. @smallexample
  249. /* @b{Declare the `vector_scal' task.} */
  250. static void vector_scal (unsigned size, float vector[size],
  251. float factor)
  252. __attribute__ ((task));
  253. /* @b{Define the standard CPU implementation.} */
  254. static void
  255. vector_scal (unsigned size, float vector[size], float factor)
  256. @{
  257. unsigned i;
  258. for (i = 0; i < size; i++)
  259. vector[i] *= factor;
  260. @}
  261. @end smallexample
  262. @end cartouche
  263. Next, the body of the program, which uses the task defined above, can be
  264. implemented:
  265. @cartouche
  266. @smallexample
  267. int
  268. main (void)
  269. @{
  270. #pragma starpu initialize
  271. #define NX 0x100000
  272. #define FACTOR 3.14
  273. @{
  274. float vector[NX]
  275. __attribute__ ((heap_allocated, registered));
  276. size_t i;
  277. for (i = 0; i < NX; i++)
  278. vector[i] = (float) i;
  279. vector_scal (NX, vector, FACTOR);
  280. #pragma starpu wait
  281. @} /* @b{VECTOR is automatically freed here.} */
  282. #pragma starpu shutdown
  283. return valid ? EXIT_SUCCESS : EXIT_FAILURE;
  284. @}
  285. @end smallexample
  286. @end cartouche
  287. @noindent
  288. The @code{main} function above does several things:
  289. @itemize
  290. @item
  291. It initializes StarPU.
  292. @item
  293. It allocates @var{vector} in the heap; it will automatically be freed
  294. when its scope is left. Alternatively, good old @code{malloc} and
  295. @code{free} could have been used, but they are more error-prone and
  296. require more typing.
  297. @item
  298. It @dfn{registers} the memory pointed to by @var{vector}. Eventually,
  299. when OpenCL or CUDA task implementations are added, this will allow
  300. StarPU to transfer that memory region between GPUs and the main memory.
  301. Removing this @code{pragma} is an error.
  302. @item
  303. It invokes the @code{vector_scal} task. The invocation looks the same
  304. as a standard C function call. However, it is an @dfn{asynchronous
  305. invocation}, meaning that the actual call is performed in parallel with
  306. the caller's continuation.
  307. @item
  308. It @dfn{waits} for the termination of the @code{vector_scal}
  309. asynchronous call.
  310. @item
  311. Finally, StarPU is shut down.
  312. @end itemize
  313. The program can be compiled and linked with GCC and the @code{-fplugin}
  314. flag:
  315. @example
  316. $ gcc `pkg-config starpu-1.1 --cflags` vector_scal.c \
  317. -fplugin=`pkg-config starpu-1.1 --variable=gccplugin` \
  318. `pkg-config starpu-1.1 --libs`
  319. @end example
  320. And voil@`a!
  321. @node Adding an OpenCL Task Implementation
  322. @subsection Adding an OpenCL Task Implementation
  323. Now, this is all fine and great, but you certainly want to take
  324. advantage of these newfangled GPUs that your lab just bought, don't you?
  325. So, let's add an OpenCL implementation of the @code{vector_scal} task.
  326. We assume that the OpenCL kernel is available in a file,
  327. @file{vector_scal_opencl_kernel.cl}, not shown here. The OpenCL task
  328. implementation is similar to that used with the standard C API
  329. (@pxref{Definition of the OpenCL Kernel}). It is declared and defined
  330. in our C file like this:
  331. @cartouche
  332. @smallexample
  333. /* @b{The OpenCL programs, loaded from 'main' (see below).} */
  334. static struct starpu_opencl_program cl_programs;
  335. static void vector_scal_opencl (unsigned size, float vector[size],
  336. float factor)
  337. __attribute__ ((task_implementation ("opencl", vector_scal)));
  338. static void
  339. vector_scal_opencl (unsigned size, float vector[size], float factor)
  340. @{
  341. int id, devid, err;
  342. cl_kernel kernel;
  343. cl_command_queue queue;
  344. cl_event event;
  345. /* @b{VECTOR is GPU memory pointer, not a main memory pointer.} */
  346. cl_mem val = (cl_mem) vector;
  347. id = starpu_worker_get_id ();
  348. devid = starpu_worker_get_devid (id);
  349. /* @b{Prepare to invoke the kernel. In the future, this will be largely
  350. automated.} */
  351. err = starpu_opencl_load_kernel (&kernel, &queue, &cl_programs,
  352. "vector_mult_opencl", devid);
  353. if (err != CL_SUCCESS)
  354. STARPU_OPENCL_REPORT_ERROR (err);
  355. err = clSetKernelArg (kernel, 0, sizeof (size), &size);
  356. err |= clSetKernelArg (kernel, 1, sizeof (val), &val);
  357. err |= clSetKernelArg (kernel, 2, sizeof (factor), &factor);
  358. if (err)
  359. STARPU_OPENCL_REPORT_ERROR (err);
  360. size_t global = 1, local = 1;
  361. err = clEnqueueNDRangeKernel (queue, kernel, 1, NULL, &global,
  362. &local, 0, NULL, &event);
  363. if (err != CL_SUCCESS)
  364. STARPU_OPENCL_REPORT_ERROR (err);
  365. clFinish (queue);
  366. starpu_opencl_collect_stats (event);
  367. clReleaseEvent (event);
  368. /* @b{Done with KERNEL.} */
  369. starpu_opencl_release_kernel (kernel);
  370. @}
  371. @end smallexample
  372. @end cartouche
  373. @noindent
  374. The OpenCL kernel itself must be loaded from @code{main}, sometime after
  375. the @code{initialize} pragma:
  376. @cartouche
  377. @smallexample
  378. starpu_opencl_load_opencl_from_file ("vector_scal_opencl_kernel.cl",
  379. &cl_programs, "");
  380. @end smallexample
  381. @end cartouche
  382. @noindent
  383. And that's it. The @code{vector_scal} task now has an additional
  384. implementation, for OpenCL, which StarPU's scheduler may choose to use
  385. at run-time. Unfortunately, the @code{vector_scal_opencl} above still
  386. has to go through the common OpenCL boilerplate; in the future,
  387. additional extensions will automate most of it.
  388. @node Adding a CUDA Task Implementation
  389. @subsection Adding a CUDA Task Implementation
  390. Adding a CUDA implementation of the task is very similar, except that
  391. the implementation itself is typically written in CUDA, and compiled
  392. with @code{nvcc}. Thus, the C file only needs to contain an external
  393. declaration for the task implementation:
  394. @cartouche
  395. @smallexample
  396. extern void vector_scal_cuda (unsigned size, float vector[size],
  397. float factor)
  398. __attribute__ ((task_implementation ("cuda", vector_scal)));
  399. @end smallexample
  400. @end cartouche
  401. The actual implementation of the CUDA task goes into a separate
  402. compilation unit, in a @file{.cu} file. It is very close to the
  403. implementation when using StarPU's standard C API (@pxref{Definition of
  404. the CUDA Kernel}).
  405. @cartouche
  406. @smallexample
  407. /* @b{CUDA implementation of the `vector_scal' task, to be compiled
  408. with `nvcc'.} */
  409. #include <starpu.h>
  410. #include <stdlib.h>
  411. static __global__ void
  412. vector_mult_cuda (unsigned n, float *val, float factor)
  413. @{
  414. unsigned i = blockIdx.x * blockDim.x + threadIdx.x;
  415. if (i < n)
  416. val[i] *= factor;
  417. @}
  418. /* @b{Definition of the task implementation declared in the C file.} */
  419. extern "C" void
  420. vector_scal_cuda (size_t size, float vector[], float factor)
  421. @{
  422. unsigned threads_per_block = 64;
  423. unsigned nblocks = (size + threads_per_block - 1) / threads_per_block;
  424. vector_mult_cuda <<< nblocks, threads_per_block, 0,
  425. starpu_cuda_get_local_stream () >>> (size, vector, factor);
  426. cudaStreamSynchronize (starpu_cuda_get_local_stream ());
  427. @}
  428. @end smallexample
  429. @end cartouche
  430. The complete source code, in the @file{gcc-plugin/examples/vector_scal}
  431. directory of the StarPU distribution, also shows how an SSE-specialized
  432. CPU task implementation can be added.
  433. For more details on the C extensions provided by StarPU's GCC plug-in,
  434. @xref{C Extensions}.
  435. @node Vector Scaling Using StarPU's API
  436. @section Vector Scaling Using StarPU's API
  437. This section shows how to achieve the same result as explained in the
  438. previous section using StarPU's standard C API.
  439. The full source code for
  440. this example is given in @ref{Full source code for the 'Scaling a
  441. Vector' example}.
  442. @menu
  443. * Source Code of Vector Scaling::
  444. * Execution of Vector Scaling:: Running the program
  445. @end menu
  446. @node Source Code of Vector Scaling
  447. @subsection Source Code of Vector Scaling
  448. Programmers can describe the data layout of their application so that StarPU is
  449. responsible for enforcing data coherency and availability across the machine.
  450. Instead of handling complex (and non-portable) mechanisms to perform data
  451. movements, programmers only declare which piece of data is accessed and/or
  452. modified by a task, and StarPU makes sure that when a computational kernel
  453. starts somewhere (e.g. on a GPU), its data are available locally.
  454. Before submitting those tasks, the programmer first needs to declare the
  455. different pieces of data to StarPU using the @code{starpu_*_data_register}
  456. functions. To ease the development of applications for StarPU, it is possible
  457. to describe multiple types of data layout. A type of data layout is called an
  458. @b{interface}. There are different predefined interfaces available in StarPU:
  459. here we will consider the @b{vector interface}.
  460. The following lines show how to declare an array of @code{NX} elements of type
  461. @code{float} using the vector interface:
  462. @cartouche
  463. @smallexample
  464. float vector[NX];
  465. starpu_data_handle_t vector_handle;
  466. starpu_vector_data_register(&vector_handle, 0, (uintptr_t)vector, NX,
  467. sizeof(vector[0]));
  468. @end smallexample
  469. @end cartouche
  470. The first argument, called the @b{data handle}, is an opaque pointer which
  471. designates the array in StarPU. This is also the structure which is used to
  472. describe which data is used by a task. The second argument is the node number
  473. where the data originally resides. Here it is 0 since the @code{vector} array is in
  474. the main memory. Then comes the pointer @code{vector} where the data can be found in main memory,
  475. the number of elements in the vector and the size of each element.
  476. The following shows how to construct a StarPU task that will manipulate the
  477. vector and a constant factor.
  478. @cartouche
  479. @smallexample
  480. float factor = 3.14;
  481. struct starpu_task *task = starpu_task_create();
  482. task->cl = &cl; /* @b{Pointer to the codelet defined below} */
  483. task->handles[0] = vector_handle; /* @b{First parameter of the codelet} */
  484. task->cl_arg = &factor;
  485. task->cl_arg_size = sizeof(factor);
  486. task->synchronous = 1;
  487. starpu_task_submit(task);
  488. @end smallexample
  489. @end cartouche
  490. Since the factor is a mere constant float value parameter,
  491. it does not need a preliminary registration, and
  492. can just be passed through the @code{cl_arg} pointer like in the previous
  493. example. The vector parameter is described by its handle.
  494. There are two fields in each element of the @code{buffers} array.
  495. @code{handle} is the handle of the data, and @code{mode} specifies how the
  496. kernel will access the data (@code{STARPU_R} for read-only, @code{STARPU_W} for
  497. write-only and @code{STARPU_RW} for read and write access).
  498. The definition of the codelet can be written as follows:
  499. @cartouche
  500. @smallexample
  501. void scal_cpu_func(void *buffers[], void *cl_arg)
  502. @{
  503. unsigned i;
  504. float *factor = cl_arg;
  505. /* @b{length of the vector} */
  506. unsigned n = STARPU_VECTOR_GET_NX(buffers[0]);
  507. /* @b{CPU copy of the vector pointer} */
  508. float *val = (float *)STARPU_VECTOR_GET_PTR(buffers[0]);
  509. for (i = 0; i < n; i++)
  510. val[i] *= *factor;
  511. @}
  512. struct starpu_codelet cl =
  513. @{
  514. .cpu_funcs = @{ scal_cpu_func, NULL @},
  515. .cpu_funcs_name = @{ "scal_cpu_func", NULL @},
  516. .nbuffers = 1,
  517. .modes = @{ STARPU_RW @}
  518. @};
  519. @end smallexample
  520. @end cartouche
  521. The first argument is an array that gives
  522. a description of all the buffers passed in the @code{task->handles}@ array. The
  523. size of this array is given by the @code{nbuffers} field of the codelet
  524. structure. For the sake of genericity, this array contains pointers to the
  525. different interfaces describing each buffer. In the case of the @b{vector
  526. interface}, the location of the vector (resp. its length) is accessible in the
  527. @code{ptr} (resp. @code{nx}) of this array. Since the vector is accessed in a
  528. read-write fashion, any modification will automatically affect future accesses
  529. to this vector made by other tasks.
  530. The second argument of the @code{scal_cpu_func} function contains a pointer to the
  531. parameters of the codelet (given in @code{task->cl_arg}), so that we read the
  532. constant factor from this pointer.
  533. @node Execution of Vector Scaling
  534. @subsection Execution of Vector Scaling
  535. @smallexample
  536. $ make vector_scal
  537. cc $(pkg-config --cflags starpu-1.1) $(pkg-config --libs starpu-1.1) vector_scal.c -o vector_scal
  538. $ ./vector_scal
  539. 0.000000 3.000000 6.000000 9.000000 12.000000
  540. @end smallexample
  541. @node Vector Scaling on an Hybrid CPU/GPU Machine
  542. @section Vector Scaling on an Hybrid CPU/GPU Machine
  543. Contrary to the previous examples, the task submitted in this example may not
  544. only be executed by the CPUs, but also by a CUDA device.
  545. @menu
  546. * Definition of the CUDA Kernel::
  547. * Definition of the OpenCL Kernel::
  548. * Definition of the Main Code::
  549. * Execution of Hybrid Vector Scaling::
  550. @end menu
  551. @node Definition of the CUDA Kernel
  552. @subsection Definition of the CUDA Kernel
  553. The CUDA implementation can be written as follows. It needs to be compiled with
  554. a CUDA compiler such as nvcc, the NVIDIA CUDA compiler driver. It must be noted
  555. that the vector pointer returned by STARPU_VECTOR_GET_PTR is here a pointer in GPU
  556. memory, so that it can be passed as such to the @code{vector_mult_cuda} kernel
  557. call.
  558. @cartouche
  559. @smallexample
  560. #include <starpu.h>
  561. static __global__ void vector_mult_cuda(unsigned n, float *val,
  562. float factor)
  563. @{
  564. unsigned i = blockIdx.x*blockDim.x + threadIdx.x;
  565. if (i < n)
  566. val[i] *= factor;
  567. @}
  568. extern "C" void scal_cuda_func(void *buffers[], void *_args)
  569. @{
  570. float *factor = (float *)_args;
  571. /* @b{length of the vector} */
  572. unsigned n = STARPU_VECTOR_GET_NX(buffers[0]);
  573. /* @b{CUDA copy of the vector pointer} */
  574. float *val = (float *)STARPU_VECTOR_GET_PTR(buffers[0]);
  575. unsigned threads_per_block = 64;
  576. unsigned nblocks = (n + threads_per_block-1) / threads_per_block;
  577. @i{ vector_mult_cuda<<<nblocks,threads_per_block, 0, starpu_cuda_get_local_stream()>>>}
  578. @i{ (n, val, *factor);}
  579. @i{ cudaStreamSynchronize(starpu_cuda_get_local_stream());}
  580. @}
  581. @end smallexample
  582. @end cartouche
  583. @node Definition of the OpenCL Kernel
  584. @subsection Definition of the OpenCL Kernel
  585. The OpenCL implementation can be written as follows. StarPU provides
  586. tools to compile a OpenCL kernel stored in a file.
  587. @cartouche
  588. @smallexample
  589. __kernel void vector_mult_opencl(int nx, __global float* val, float factor)
  590. @{
  591. const int i = get_global_id(0);
  592. if (i < nx) @{
  593. val[i] *= factor;
  594. @}
  595. @}
  596. @end smallexample
  597. @end cartouche
  598. Contrary to CUDA and CPU, @code{STARPU_VECTOR_GET_DEV_HANDLE} has to be used,
  599. which returns a @code{cl_mem} (which is not a device pointer, but an OpenCL
  600. handle), which can be passed as such to the OpenCL kernel. The difference is
  601. important when using partitioning, see @ref{Partitioning Data}.
  602. @cartouche
  603. @smallexample
  604. #include <starpu.h>
  605. @i{extern struct starpu_opencl_program programs;}
  606. void scal_opencl_func(void *buffers[], void *_args)
  607. @{
  608. float *factor = _args;
  609. @i{ int id, devid, err;}
  610. @i{ cl_kernel kernel;}
  611. @i{ cl_command_queue queue;}
  612. @i{ cl_event event;}
  613. /* @b{length of the vector} */
  614. unsigned n = STARPU_VECTOR_GET_NX(buffers[0]);
  615. /* @b{OpenCL copy of the vector pointer} */
  616. cl_mem val = (cl_mem) STARPU_VECTOR_GET_DEV_HANDLE(buffers[0]);
  617. @i{ id = starpu_worker_get_id();}
  618. @i{ devid = starpu_worker_get_devid(id);}
  619. @i{ err = starpu_opencl_load_kernel(&kernel, &queue, &programs,}
  620. @i{ "vector_mult_opencl", devid); /* @b{Name of the codelet defined above} */}
  621. @i{ if (err != CL_SUCCESS) STARPU_OPENCL_REPORT_ERROR(err);}
  622. @i{ err = clSetKernelArg(kernel, 0, sizeof(n), &n);}
  623. @i{ err |= clSetKernelArg(kernel, 1, sizeof(val), &val);}
  624. @i{ err |= clSetKernelArg(kernel, 2, sizeof(*factor), factor);}
  625. @i{ if (err) STARPU_OPENCL_REPORT_ERROR(err);}
  626. @i{ @{}
  627. @i{ size_t global=n;}
  628. @i{ size_t local=1;}
  629. @i{ err = clEnqueueNDRangeKernel(queue, kernel, 1, NULL,}
  630. @i{ &global, &local, 0, NULL, &event);}
  631. @i{ if (err != CL_SUCCESS) STARPU_OPENCL_REPORT_ERROR(err);}
  632. @i{ @}}
  633. @i{ clFinish(queue);}
  634. @i{ starpu_opencl_collect_stats(event);}
  635. @i{ clReleaseEvent(event);}
  636. @i{ starpu_opencl_release_kernel(kernel);}
  637. @}
  638. @end smallexample
  639. @end cartouche
  640. @node Definition of the Main Code
  641. @subsection Definition of the Main Code
  642. The CPU implementation is the same as in the previous section.
  643. Here is the source of the main application. You can notice that the fields
  644. @code{cuda_funcs} and @code{opencl_funcs} of the codelet are set to
  645. define the pointers to the CUDA and OpenCL implementations of the
  646. task.
  647. @cartouche
  648. @smallexample
  649. #include <starpu.h>
  650. #define NX 2048
  651. extern void scal_cuda_func(void *buffers[], void *_args);
  652. extern void scal_cpu_func(void *buffers[], void *_args);
  653. extern void scal_opencl_func(void *buffers[], void *_args);
  654. /* @b{Definition of the codelet} */
  655. static struct starpu_codelet cl =
  656. @{
  657. .cuda_funcs = @{ scal_cuda_func, NULL @},
  658. .cpu_funcs = @{ scal_cpu_func, NULL @},
  659. .cpu_funcs_name = @{ "scal_cpu_func", NULL @},
  660. .opencl_funcs = @{ scal_opencl_func, NULL @},
  661. .nbuffers = 1,
  662. .modes = @{ STARPU_RW @}
  663. @}
  664. #ifdef STARPU_USE_OPENCL
  665. /* @b{The compiled version of the OpenCL program} */
  666. struct starpu_opencl_program programs;
  667. #endif
  668. int main(int argc, char **argv)
  669. @{
  670. float *vector;
  671. int i, ret;
  672. float factor=3.0;
  673. struct starpu_task *task;
  674. starpu_data_handle_t vector_handle;
  675. starpu_init(NULL); /* @b{Initialising StarPU} */
  676. #ifdef STARPU_USE_OPENCL
  677. starpu_opencl_load_opencl_from_file(
  678. "examples/basic_examples/vector_scal_opencl_codelet.cl",
  679. &programs, NULL);
  680. #endif
  681. vector = malloc(NX*sizeof(vector[0]));
  682. assert(vector);
  683. for(i=0 ; i<NX ; i++) vector[i] = i;
  684. @end smallexample
  685. @end cartouche
  686. @cartouche
  687. @smallexample
  688. /* @b{Registering data within StarPU} */
  689. starpu_vector_data_register(&vector_handle, 0, (uintptr_t)vector,
  690. NX, sizeof(vector[0]));
  691. /* @b{Definition of the task} */
  692. task = starpu_task_create();
  693. task->cl = &cl;
  694. task->handles[0] = vector_handle;
  695. task->cl_arg = &factor;
  696. task->cl_arg_size = sizeof(factor);
  697. @end smallexample
  698. @end cartouche
  699. @cartouche
  700. @smallexample
  701. /* @b{Submitting the task} */
  702. ret = starpu_task_submit(task);
  703. if (ret == -ENODEV) @{
  704. fprintf(stderr, "No worker may execute this task\n");
  705. return 1;
  706. @}
  707. @c TODO: Mmm, should rather be an unregistration with an implicit dependency, no?
  708. /* @b{Waiting for its termination} */
  709. starpu_task_wait_for_all();
  710. /* @b{Update the vector in RAM} */
  711. starpu_data_acquire(vector_handle, STARPU_R);
  712. @end smallexample
  713. @end cartouche
  714. @cartouche
  715. @smallexample
  716. /* @b{Access the data} */
  717. for(i=0 ; i<NX; i++) @{
  718. fprintf(stderr, "%f ", vector[i]);
  719. @}
  720. fprintf(stderr, "\n");
  721. /* @b{Release the RAM view of the data before unregistering it and shutting down StarPU} */
  722. starpu_data_release(vector_handle);
  723. starpu_data_unregister(vector_handle);
  724. starpu_shutdown();
  725. return 0;
  726. @}
  727. @end smallexample
  728. @end cartouche
  729. @node Execution of Hybrid Vector Scaling
  730. @subsection Execution of Hybrid Vector Scaling
  731. The Makefile given at the beginning of the section must be extended to
  732. give the rules to compile the CUDA source code. Note that the source
  733. file of the OpenCL kernel does not need to be compiled now, it will
  734. be compiled at run-time when calling the function
  735. @code{starpu_opencl_load_opencl_from_file()} (@pxref{starpu_opencl_load_opencl_from_file}).
  736. @cartouche
  737. @smallexample
  738. CFLAGS += $(shell pkg-config --cflags starpu-1.1)
  739. LDFLAGS += $(shell pkg-config --libs starpu-1.1)
  740. CC = gcc
  741. vector_scal: vector_scal.o vector_scal_cpu.o vector_scal_cuda.o vector_scal_opencl.o
  742. %.o: %.cu
  743. nvcc $(CFLAGS) $< -c $@
  744. clean:
  745. rm -f vector_scal *.o
  746. @end smallexample
  747. @end cartouche
  748. @smallexample
  749. $ make
  750. @end smallexample
  751. and to execute it, with the default configuration:
  752. @smallexample
  753. $ ./vector_scal
  754. 0.000000 3.000000 6.000000 9.000000 12.000000
  755. @end smallexample
  756. or for example, by disabling CPU devices:
  757. @smallexample
  758. $ STARPU_NCPU=0 ./vector_scal
  759. 0.000000 3.000000 6.000000 9.000000 12.000000
  760. @end smallexample
  761. or by disabling CUDA devices (which may permit to enable the use of OpenCL,
  762. see @ref{Enabling OpenCL}):
  763. @smallexample
  764. $ STARPU_NCUDA=0 ./vector_scal
  765. 0.000000 3.000000 6.000000 9.000000 12.000000
  766. @end smallexample