02basic_examples.doxy 24 KB

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