110_basic_examples.doxy 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2013,2015-2019 CNRS
  4. * Copyright (C) 2011-2013 Inria
  5. * Copyright (C) 2009-2011,2014,2015 Université de Bordeaux
  6. *
  7. * StarPU is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation; either version 2.1 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * StarPU is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. *
  16. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  17. */
  18. /*! \page BasicExamples Basic Examples
  19. \section HelloWorldUsingTheCExtension Hello World Using The C Extension
  20. This section shows how to implement a simple program that submits a task
  21. to StarPU using the StarPU C extension (\ref cExtensions). The complete example, and additional examples,
  22. is available in the directory <c>gcc-plugin/examples</c> of the StarPU
  23. distribution. A similar example showing how to directly use the StarPU's API is shown
  24. in \ref HelloWorldUsingStarPUAPI.
  25. GCC from version 4.5 permit to use the StarPU GCC plug-in (\ref cExtensions). This makes writing a task both simpler and less error-prone.
  26. In a nutshell, all it takes is to declare a task, declare and define its
  27. implementations (for CPU, OpenCL, and/or CUDA), and invoke the task like
  28. a regular C function. The example below defines <c>my_task</c> which
  29. has a single implementation for CPU:
  30. \snippet hello_pragma.c To be included. You should update doxygen if you see this text.
  31. The code can then be compiled and linked with GCC and the flag <c>-fplugin</c>:
  32. \verbatim
  33. $ gcc `pkg-config starpu-1.3 --cflags` hello-starpu.c \
  34. -fplugin=`pkg-config starpu-1.3 --variable=gccplugin` \
  35. `pkg-config starpu-1.3 --libs`
  36. \endverbatim
  37. The code can also be compiled without the StarPU C extension and will
  38. behave as a normal sequential code.
  39. \verbatim
  40. $ gcc hello-starpu.c
  41. hello-starpu.c:33:1: warning: ‘task’ attribute directive ignored [-Wattributes]
  42. $ ./a.out
  43. Hello, world! With x = 42
  44. \endverbatim
  45. As can be seen above, the C extensions allows programmers to
  46. use StarPU tasks by essentially annotating ``regular'' C code.
  47. \section HelloWorldUsingStarPUAPI Hello World Using StarPU's API
  48. This section shows how to achieve the same result as in the previous
  49. section using StarPU's standard C API.
  50. \subsection RequiredHeaders Required Headers
  51. The header starpu.h should be included in any code using StarPU.
  52. \code{.c}
  53. #include <starpu.h>
  54. \endcode
  55. \subsection DefiningACodelet Defining A Codelet
  56. A codelet is a structure that represents a computational kernel. Such a codelet
  57. may contain an implementation of the same kernel on different architectures
  58. (e.g. CUDA, x86, ...). For compatibility, make sure that the whole
  59. structure is properly initialized to zero, either by using the
  60. function starpu_codelet_init(), or by letting the
  61. compiler implicitly do it as examplified below.
  62. The field starpu_codelet::nbuffers specifies the number of data buffers that are
  63. manipulated by the codelet: here the codelet does not access or modify any data
  64. that is controlled by our data management library.
  65. We create a codelet which may only be executed on CPUs. When a CPU
  66. core will execute a codelet, it will call the function
  67. <c>cpu_func</c>, which \em must have the following prototype:
  68. \code{.c}
  69. void (*cpu_func)(void *buffers[], void *cl_arg);
  70. \endcode
  71. In this example, we can ignore the first argument of this function which gives a
  72. description of the input and output buffers (e.g. the size and the location of
  73. the matrices) since there is none. We also ignore the second argument
  74. which is a pointer to optional arguments for the codelet.
  75. \code{.c}
  76. void cpu_func(void *buffers[], void *cl_arg)
  77. {
  78. printf("Hello world\n");
  79. }
  80. struct starpu_codelet cl =
  81. {
  82. .cpu_funcs = { cpu_func },
  83. .nbuffers = 0
  84. };
  85. \endcode
  86. \subsection SubmittingATask Submitting A Task
  87. Before submitting any tasks to StarPU, starpu_init() must be called. The
  88. <c>NULL</c> argument specifies that we use the default configuration.
  89. Tasks can then be submitted until the termination of StarPU -- done by a
  90. call to starpu_shutdown().
  91. In the example below, a task structure is allocated by a call to
  92. starpu_task_create(). This function allocates and fills the
  93. task structure with its default settings, it does not
  94. submit the task to StarPU.
  95. The field starpu_task::cl is a pointer to the codelet which the task will
  96. execute: in other words, the codelet structure describes which computational
  97. kernel should be offloaded on the different architectures, and the task
  98. structure is a wrapper containing a codelet and the piece of data on which the
  99. codelet should operate.
  100. If the field starpu_task::synchronous is non-zero, task submission
  101. will be synchronous: the function starpu_task_submit() will not return
  102. until the task has been executed. Note that the function starpu_shutdown()
  103. does not guarantee that asynchronous tasks have been executed before
  104. it returns, starpu_task_wait_for_all() can be used to this effect, or
  105. data can be unregistered (starpu_data_unregister()), which will
  106. implicitly wait for all the tasks scheduled to work on it, unless
  107. explicitly disabled thanks to
  108. starpu_data_set_default_sequential_consistency_flag() or
  109. starpu_data_set_sequential_consistency_flag().
  110. \code{.c}
  111. int main(int argc, char **argv)
  112. {
  113. /* initialize StarPU */
  114. starpu_init(NULL);
  115. struct starpu_task *task = starpu_task_create();
  116. task->cl = &cl; /* Pointer to the codelet defined above */
  117. /* starpu_task_submit will be a blocking call. If unset,
  118. starpu_task_wait() needs to be called after submitting the task. */
  119. task->synchronous = 1;
  120. /* submit the task to StarPU */
  121. starpu_task_submit(task);
  122. /* terminate StarPU */
  123. starpu_shutdown();
  124. return 0;
  125. }
  126. \endcode
  127. \subsection ExecutionOfHelloWorld Execution Of Hello World
  128. \verbatim
  129. $ make hello_world
  130. cc $(pkg-config --cflags starpu-1.3) hello_world.c -o hello_world $(pkg-config --libs starpu-1.3)
  131. $ ./hello_world
  132. Hello world
  133. \endverbatim
  134. \subsection PassingArgumentsToTheCodelet Passing Arguments To The Codelet
  135. The optional field starpu_task::cl_arg field is a pointer to a buffer
  136. (of size starpu_task::cl_arg_size) with some parameters for the kernel
  137. described by the codelet. For instance, if a codelet implements a
  138. computational kernel that multiplies its input vector by a constant,
  139. the constant could be specified by the means of this buffer, instead
  140. of registering it as a StarPU data. It must however be noted that
  141. StarPU avoids making copy whenever possible and rather passes the
  142. pointer as such, so the buffer which is pointed at must be kept allocated
  143. until the task terminates, and if several tasks are submitted with
  144. various parameters, each of them must be given a pointer to their
  145. own buffer.
  146. \code{.c}
  147. struct params
  148. {
  149. int i;
  150. float f;
  151. };
  152. void cpu_func(void *buffers[], void *cl_arg)
  153. {
  154. struct params *params = cl_arg;
  155. printf("Hello world (params = {%i, %f} )\n", params->i, params->f);
  156. }
  157. \endcode
  158. As said before, the field starpu_codelet::nbuffers specifies the
  159. number of data buffers which are manipulated by the codelet. It does
  160. not count the argument --- the parameter <c>cl_arg</c> of the function
  161. <c>cpu_func</c> --- since it is not managed by our data management
  162. library, but just contains trivial parameters.
  163. // TODO rewrite so that it is a little clearer ?
  164. Be aware that this may be a pointer to a
  165. \em copy of the actual buffer, and not the pointer given by the programmer:
  166. if the codelet modifies this buffer, there is no guarantee that the initial
  167. buffer will be modified as well: this for instance implies that the buffer
  168. cannot be used as a synchronization medium. If synchronization is needed, data
  169. has to be registered to StarPU, see \ref VectorScalingUsingStarPUAPI.
  170. \code{.c}
  171. int main(int argc, char **argv)
  172. {
  173. /* initialize StarPU */
  174. starpu_init(NULL);
  175. struct starpu_task *task = starpu_task_create();
  176. task->cl = &cl; /* Pointer to the codelet defined above */
  177. struct params params = { 1, 2.0f };
  178. task->cl_arg = &params;
  179. task->cl_arg_size = sizeof(params);
  180. /* starpu_task_submit will be a blocking call */
  181. task->synchronous = 1;
  182. /* submit the task to StarPU */
  183. starpu_task_submit(task);
  184. /* terminate StarPU */
  185. starpu_shutdown();
  186. return 0;
  187. }
  188. \endcode
  189. \verbatim
  190. $ make hello_world
  191. cc $(pkg-config --cflags starpu-1.3) hello_world.c -o hello_world $(pkg-config --libs starpu-1.3)
  192. $ ./hello_world
  193. Hello world (params = {1, 2.000000} )
  194. \endverbatim
  195. \subsection DefiningACallback Defining A Callback
  196. Once a task has been executed, an optional callback function
  197. starpu_task::callback_func is called when defined.
  198. While the computational kernel could be offloaded on various architectures, the
  199. callback function is always executed on a CPU. The pointer
  200. starpu_task::callback_arg is passed as an argument of the callback
  201. function. The prototype of a callback function must be:
  202. \code{.c}
  203. void (*callback_function)(void *);
  204. \endcode
  205. \code{.c}
  206. void callback_func(void *callback_arg)
  207. {
  208. printf("Callback function (arg %x)\n", callback_arg);
  209. }
  210. int main(int argc, char **argv)
  211. {
  212. /* initialize StarPU */
  213. starpu_init(NULL);
  214. struct starpu_task *task = starpu_task_create();
  215. task->cl = &cl; /* Pointer to the codelet defined above */
  216. task->callback_func = callback_func;
  217. task->callback_arg = 0x42;
  218. /* starpu_task_submit will be a blocking call */
  219. task->synchronous = 1;
  220. /* submit the task to StarPU */
  221. starpu_task_submit(task);
  222. /* terminate StarPU */
  223. starpu_shutdown();
  224. return 0;
  225. }
  226. \endcode
  227. \verbatim
  228. $ make hello_world
  229. cc $(pkg-config --cflags starpu-1.3) hello_world.c -o hello_world $(pkg-config --libs starpu-1.3)
  230. $ ./hello_world
  231. Hello world
  232. Callback function (arg 42)
  233. \endverbatim
  234. \subsection WhereToExecuteACodelet Where To Execute A Codelet
  235. \code{.c}
  236. struct starpu_codelet cl =
  237. {
  238. .where = STARPU_CPU,
  239. .cpu_funcs = { cpu_func },
  240. .cpu_funcs_name = { "cpu_func" },
  241. .nbuffers = 0
  242. };
  243. \endcode
  244. We create a codelet which may only be executed on the CPUs. The
  245. optional field starpu_codelet::where is a bitmask which defines where
  246. the codelet may be executed. Here, the value ::STARPU_CPU means that
  247. only CPUs can execute this codelet. When the optional field
  248. starpu_codelet::where is unset, its value is automatically set based
  249. on the availability of the different fields <c>XXX_funcs</c>.
  250. TODO: explain starpu_codelet::cpu_funcs_name
  251. \section VectorScalingUsingTheCExtension Vector Scaling Using the C Extension
  252. The previous example has shown how to submit tasks. In this section,
  253. we show how StarPU tasks can manipulate data.
  254. We will first show how to use the C language extensions provided by
  255. the GCC plug-in (\ref cExtensions). The complete example, and
  256. additional examples, is available in the directory <c>gcc-plugin/examples</c>
  257. of the StarPU distribution. These extensions map directly
  258. to StarPU's main concepts: tasks, task implementations for CPU,
  259. OpenCL, or CUDA, and registered data buffers. The standard C version
  260. that uses StarPU's standard C programming interface is given in \ref VectorScalingUsingStarPUAPI.
  261. First of all, the vector-scaling task and its simple CPU implementation
  262. has to be defined:
  263. \code{.c}
  264. /* Declare the `vector_scal' task. */
  265. static void vector_scal (unsigned size, float vector[size], float factor) __attribute__ ((task));
  266. /* Define the standard CPU implementation. */
  267. static void vector_scal (unsigned size, float vector[size], float factor)
  268. {
  269. unsigned i;
  270. for (i = 0; i < size; i++)
  271. vector[i] *= factor;
  272. }
  273. \endcode
  274. Next, the body of the program, which uses the task defined above, can be
  275. implemented:
  276. \snippet hello_pragma2.c To be included. You should update doxygen if you see this text.
  277. The function <c>main</c> above does several things:
  278. <ul>
  279. <li>
  280. It initializes StarPU.
  281. </li>
  282. <li>
  283. It allocates <c>vector</c> in the heap; it will automatically be freed
  284. when its scope is left. Alternatively, good old <c>malloc</c> and
  285. <c>free</c> could have been used, but they are more error-prone and
  286. require more typing.
  287. </li>
  288. <li>
  289. It registers the memory pointed to by <c>vector</c>. Eventually,
  290. when OpenCL or CUDA task implementations are added, this will allow
  291. StarPU to transfer the memory region between GPUs and the main memory.
  292. Removing this <c>pragma</c> is an error.
  293. </li>
  294. <li>
  295. It invokes the task <c>vector_scal</c>. The invocation looks the same
  296. as a standard C function call. However, it is an asynchronous
  297. invocation, meaning that the actual call is performed in parallel with
  298. the caller's continuation.
  299. </li>
  300. <li>
  301. It waits for the termination of the asynchronous call <c>vector_scal</c>.
  302. </li>
  303. <li>
  304. Finally, StarPU is shut down.
  305. </li>
  306. </ul>
  307. The program can be compiled and linked with GCC and the flag <c>-fplugin</c>:
  308. \verbatim
  309. $ gcc `pkg-config starpu-1.3 --cflags` vector_scal.c \
  310. -fplugin=`pkg-config starpu-1.3 --variable=gccplugin` \
  311. `pkg-config starpu-1.3 --libs`
  312. \endverbatim
  313. And voilà!
  314. \subsection AddingAnOpenCLTaskImplementation Adding an OpenCL Task Implementation
  315. Now, this is all fine and great, but you certainly want to take
  316. advantage of these newfangled GPUs that your lab just bought, don't you?
  317. So, let's add an OpenCL implementation of the task <c>vector_scal</c>.
  318. We assume that the OpenCL kernel is available in a file,
  319. <c>vector_scal_opencl_kernel.cl</c>, not shown here. The OpenCL task
  320. implementation is similar to the one used with the standard C API
  321. (\ref DefinitionOfTheOpenCLKernel). It is declared and defined
  322. in our C file like this:
  323. \code{.c}
  324. /* The OpenCL programs, loaded from 'main' (see below). */
  325. static struct starpu_opencl_program cl_programs;
  326. static void vector_scal_opencl (unsigned size, float vector[size], float factor)
  327. __attribute__ ((task_implementation ("opencl", vector_scal)));
  328. static void vector_scal_opencl (unsigned size, float vector[size], float factor)
  329. {
  330. int id, devid, err;
  331. cl_kernel kernel;
  332. cl_command_queue queue;
  333. cl_event event;
  334. /* VECTOR is GPU memory pointer, not a main memory pointer. */
  335. cl_mem val = (cl_mem) vector;
  336. id = starpu_worker_get_id ();
  337. devid = starpu_worker_get_devid (id);
  338. /* Prepare to invoke the kernel. In the future, this will be largely automated. */
  339. err = starpu_opencl_load_kernel (&kernel, &queue, &cl_programs, "vector_mult_opencl", devid);
  340. if (err != CL_SUCCESS) STARPU_OPENCL_REPORT_ERROR (err);
  341. err = clSetKernelArg (kernel, 0, sizeof (size), &size);
  342. err |= clSetKernelArg (kernel, 1, sizeof (val), &val);
  343. err |= clSetKernelArg (kernel, 2, sizeof (factor), &factor);
  344. if (err) STARPU_OPENCL_REPORT_ERROR (err);
  345. size_t global = 1, local = 1;
  346. err = clEnqueueNDRangeKernel (queue, kernel, 1, NULL, &global, &local, 0, NULL, &event);
  347. if (err != CL_SUCCESS) STARPU_OPENCL_REPORT_ERROR (err);
  348. clFinish (queue);
  349. starpu_opencl_collect_stats (event);
  350. clReleaseEvent (event);
  351. /* Done with KERNEL. */
  352. starpu_opencl_release_kernel (kernel);
  353. }
  354. \endcode
  355. The OpenCL kernel itself must be loaded from <c>main</c>, sometime after
  356. the pragma <c>initialize</c>:
  357. \code{.c}
  358. starpu_opencl_load_opencl_from_file ("vector_scal_opencl_kernel.cl", &cl_programs, "");
  359. \endcode
  360. And that's it. The task <c>vector_scal</c> now has an additional
  361. implementation, for OpenCL, which StarPU's scheduler may choose to use
  362. at runtime. Unfortunately, the <c>vector_scal_opencl</c> above still
  363. has to go through the common OpenCL boilerplate; in the future,
  364. additional extensions will automate most of it.
  365. \subsection AddingACUDATaskImplementation Adding a CUDA Task Implementation
  366. Adding a CUDA implementation of the task is very similar, except that
  367. the implementation itself is typically written in CUDA, and compiled
  368. with <c>nvcc</c>. Thus, the C file only needs to contain an external
  369. declaration for the task implementation:
  370. \code{.c}
  371. extern void vector_scal_cuda (unsigned size, float vector[size], float factor)
  372. __attribute__ ((task_implementation ("cuda", vector_scal)));
  373. \endcode
  374. The actual implementation of the CUDA task goes into a separate
  375. compilation unit, in a <c>.cu</c> file. It is very close to the
  376. implementation when using StarPU's standard C API (\ref DefinitionOfTheCUDAKernel).
  377. \snippet scal_pragma.cu To be included. You should update doxygen if you see this text.
  378. The complete source code, in the directory <c>gcc-plugin/examples/vector_scal</c>
  379. of the StarPU distribution, also shows how an SSE-specialized
  380. CPU task implementation can be added.
  381. For more details on the C extensions provided by StarPU's GCC plug-in, see
  382. \ref cExtensions.
  383. \section VectorScalingUsingStarPUAPI Vector Scaling Using StarPU's API
  384. This section shows how to achieve the same result as explained in the
  385. previous section using StarPU's standard C API.
  386. The full source code for
  387. this example is given in \ref FullSourceCodeVectorScal.
  388. \subsection SourceCodeOfVectorScaling Source Code of Vector Scaling
  389. Programmers can describe the data layout of their application so that StarPU is
  390. responsible for enforcing data coherency and availability across the machine.
  391. Instead of handling complex (and non-portable) mechanisms to perform data
  392. movements, programmers only declare which piece of data is accessed and/or
  393. modified by a task, and StarPU makes sure that when a computational kernel
  394. starts somewhere (e.g. on a GPU), its data are available locally.
  395. Before submitting those tasks, the programmer first needs to declare the
  396. different pieces of data to StarPU using the functions
  397. <c>starpu_*_data_register</c>. To ease the development of applications
  398. for StarPU, it is possible to describe multiple types of data layout.
  399. A type of data layout is called an <b>interface</b>. There are
  400. different predefined interfaces available in StarPU: here we will
  401. consider the <b>vector interface</b>.
  402. The following lines show how to declare an array of <c>NX</c> elements of type
  403. <c>float</c> using the vector interface:
  404. \code{.c}
  405. float vector[NX];
  406. starpu_data_handle_t vector_handle;
  407. starpu_vector_data_register(&vector_handle, STARPU_MAIN_RAM, (uintptr_t)vector, NX, sizeof(vector[0]));
  408. \endcode
  409. The first argument, called the <b>data handle</b>, is an opaque pointer which
  410. designates the array within StarPU. This is also the structure which is used to
  411. describe which data is used by a task. The second argument is the node number
  412. where the data originally resides. Here it is ::STARPU_MAIN_RAM since the array <c>vector</c> is in
  413. the main memory. Then comes the pointer <c>vector</c> where the data can be found in main memory,
  414. the number of elements in the vector and the size of each element.
  415. The following shows how to construct a StarPU task that will manipulate the
  416. vector and a constant factor.
  417. \code{.c}
  418. float factor = 3.14;
  419. struct starpu_task *task = starpu_task_create();
  420. task->cl = &cl; /* Pointer to the codelet defined below */
  421. task->handles[0] = vector_handle; /* First parameter of the codelet */
  422. task->cl_arg = &factor;
  423. task->cl_arg_size = sizeof(factor);
  424. task->synchronous = 1;
  425. starpu_task_submit(task);
  426. \endcode
  427. Since the factor is a mere constant float value parameter,
  428. it does not need a preliminary registration, and
  429. can just be passed through the pointer starpu_task::cl_arg like in the previous
  430. example. The vector parameter is described by its handle.
  431. starpu_task::handles should be set with the handles of the data, the
  432. access modes for the data are defined in the field
  433. starpu_codelet::modes (::STARPU_R for read-only, ::STARPU_W for
  434. write-only and ::STARPU_RW for read and write access).
  435. The definition of the codelet can be written as follows:
  436. \code{.c}
  437. void scal_cpu_func(void *buffers[], void *cl_arg)
  438. {
  439. unsigned i;
  440. float *factor = cl_arg;
  441. /* length of the vector */
  442. unsigned n = STARPU_VECTOR_GET_NX(buffers[0]);
  443. /* CPU copy of the vector pointer */
  444. float *val = (float *)STARPU_VECTOR_GET_PTR(buffers[0]);
  445. for (i = 0; i < n; i++)
  446. val[i] *= *factor;
  447. }
  448. struct starpu_codelet cl =
  449. {
  450. .cpu_funcs = { scal_cpu_func },
  451. .cpu_funcs_name = { "scal_cpu_func" },
  452. .nbuffers = 1,
  453. .modes = { STARPU_RW }
  454. };
  455. \endcode
  456. The first argument is an array that gives
  457. a description of all the buffers passed in the array starpu_task::handles. The
  458. size of this array is given by the field starpu_codelet::nbuffers. For
  459. the sake of genericity, this array contains pointers to the different
  460. interfaces describing each buffer. In the case of the <b>vector
  461. interface</b>, the location of the vector (resp. its length) is
  462. accessible in the starpu_vector_interface::ptr (resp.
  463. starpu_vector_interface::nx) of this interface. Since the vector is
  464. accessed in a read-write fashion, any modification will automatically
  465. affect future accesses to this vector made by other tasks.
  466. The second argument of the function <c>scal_cpu_func</c> contains a
  467. pointer to the parameters of the codelet (given in
  468. starpu_task::cl_arg), so that we read the constant factor from this
  469. pointer.
  470. \subsection ExecutionOfVectorScaling Execution of Vector Scaling
  471. \verbatim
  472. $ make vector_scal
  473. cc $(pkg-config --cflags starpu-1.3) vector_scal.c -o vector_scal $(pkg-config --libs starpu-1.3)
  474. $ ./vector_scal
  475. 0.000000 3.000000 6.000000 9.000000 12.000000
  476. \endverbatim
  477. \section VectorScalingOnAnHybridCPUGPUMachine Vector Scaling on an Hybrid CPU/GPU Machine
  478. Contrary to the previous examples, the task submitted in this example may not
  479. only be executed by the CPUs, but also by a CUDA device.
  480. \subsection DefinitionOfTheCUDAKernel Definition of the CUDA Kernel
  481. The CUDA implementation can be written as follows. It needs to be compiled with
  482. a CUDA compiler such as nvcc, the NVIDIA CUDA compiler driver. It must be noted
  483. that the vector pointer returned by ::STARPU_VECTOR_GET_PTR is here a
  484. pointer in GPU memory, so that it can be passed as such to the
  485. kernel call <c>vector_mult_cuda</c>.
  486. \snippet vector_scal_cuda.c To be included. You should update doxygen if you see this text.
  487. \subsection DefinitionOfTheOpenCLKernel Definition of the OpenCL Kernel
  488. The OpenCL implementation can be written as follows. StarPU provides
  489. tools to compile a OpenCL kernel stored in a file.
  490. \code{.c}
  491. __kernel void vector_mult_opencl(int nx, __global float* val, float factor)
  492. {
  493. const int i = get_global_id(0);
  494. if (i < nx)
  495. {
  496. val[i] *= factor;
  497. }
  498. }
  499. \endcode
  500. Contrary to CUDA and CPU, ::STARPU_VECTOR_GET_DEV_HANDLE has to be used,
  501. which returns a <c>cl_mem</c> (which is not a device pointer, but an OpenCL
  502. handle), which can be passed as such to the OpenCL kernel. The difference is
  503. important when using partitioning, see \ref PartitioningData.
  504. \snippet vector_scal_opencl.c To be included. You should update doxygen if you see this text.
  505. \subsection DefinitionOfTheMainCode Definition of the Main Code
  506. The CPU implementation is the same as in the previous section.
  507. Here is the source of the main application. You can notice that the fields
  508. starpu_codelet::cuda_funcs and starpu_codelet::opencl_funcs are set to
  509. define the pointers to the CUDA and OpenCL implementations of the
  510. task.
  511. \snippet vector_scal_c.c To be included. You should update doxygen if you see this text.
  512. \subsection ExecutionOfHybridVectorScaling Execution of Hybrid Vector Scaling
  513. The Makefile given at the beginning of the section must be extended to
  514. give the rules to compile the CUDA source code. Note that the source
  515. file of the OpenCL kernel does not need to be compiled now, it will
  516. be compiled at run-time when calling the function
  517. starpu_opencl_load_opencl_from_file().
  518. \verbatim
  519. CFLAGS += $(shell pkg-config --cflags starpu-1.3)
  520. LDFLAGS += $(shell pkg-config --libs starpu-1.3)
  521. CC = gcc
  522. vector_scal: vector_scal.o vector_scal_cpu.o vector_scal_cuda.o vector_scal_opencl.o
  523. %.o: %.cu
  524. nvcc $(CFLAGS) $< -c $@
  525. clean:
  526. rm -f vector_scal *.o
  527. \endverbatim
  528. \verbatim
  529. $ make
  530. \endverbatim
  531. and to execute it, with the default configuration:
  532. \verbatim
  533. $ ./vector_scal
  534. 0.000000 3.000000 6.000000 9.000000 12.000000
  535. \endverbatim
  536. or for example, by disabling CPU devices:
  537. \verbatim
  538. $ STARPU_NCPU=0 ./vector_scal
  539. 0.000000 3.000000 6.000000 9.000000 12.000000
  540. \endverbatim
  541. or by disabling CUDA devices (which may permit to enable the use of OpenCL,
  542. see \ref EnablingOpenCL) :
  543. \verbatim
  544. $ STARPU_NCUDA=0 ./vector_scal
  545. 0.000000 3.000000 6.000000 9.000000 12.000000
  546. \endverbatim
  547. */