basic-examples.texi 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  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 Centre National de la Recherche Scientifique
  5. @c Copyright (C) 2011 Institut National de Recherche en Informatique et Automatique
  6. @c See the file starpu.texi for copying conditions.
  7. @node Basic Examples
  8. @chapter Basic Examples
  9. @menu
  10. * Compiling and linking options::
  11. * Hello World:: Submitting Tasks
  12. * Scaling a Vector:: Manipulating Data
  13. * Vector Scaling on an Hybrid CPU/GPU Machine:: Handling Heterogeneous Architectures
  14. @end menu
  15. @node Compiling and linking options
  16. @section Compiling and linking options
  17. Let's suppose StarPU has been installed in the directory
  18. @code{$STARPU_DIR}. As explained in @ref{Setting flags for compiling and linking applications},
  19. the variable @code{PKG_CONFIG_PATH} needs to be set. It is also
  20. necessary to set the variable @code{LD_LIBRARY_PATH} to locate dynamic
  21. libraries at runtime.
  22. @example
  23. % PKG_CONFIG_PATH=$STARPU_DIR/lib/pkgconfig:$PKG_CONFIG_PATH
  24. % LD_LIBRARY_PATH=$STARPU_DIR/lib:$LD_LIBRARY_PATH
  25. @end example
  26. The Makefile could for instance contain the following lines to define which
  27. options must be given to the compiler and to the linker:
  28. @cartouche
  29. @example
  30. CFLAGS += $$(pkg-config --cflags libstarpu)
  31. LDFLAGS += $$(pkg-config --libs libstarpu)
  32. @end example
  33. @end cartouche
  34. Also pass the @code{--static} option if the application is to be linked statically.
  35. @node Hello World
  36. @section Hello World
  37. @menu
  38. * Required Headers::
  39. * Defining a Codelet::
  40. * Submitting a Task::
  41. * Execution of Hello World::
  42. @end menu
  43. In this section, we show how to implement a simple program that submits a task to StarPU.
  44. @node Required Headers
  45. @subsection Required Headers
  46. The @code{starpu.h} header should be included in any code using StarPU.
  47. @cartouche
  48. @smallexample
  49. #include <starpu.h>
  50. @end smallexample
  51. @end cartouche
  52. @node Defining a Codelet
  53. @subsection Defining a Codelet
  54. @cartouche
  55. @smallexample
  56. struct params @{
  57. int i;
  58. float f;
  59. @};
  60. void cpu_func(void *buffers[], void *cl_arg)
  61. @{
  62. struct params *params = cl_arg;
  63. printf("Hello world (params = @{%i, %f@} )\n", params->i, params->f);
  64. @}
  65. struct starpu_codelet cl =
  66. @{
  67. .where = STARPU_CPU,
  68. .cpu_funcs = @{ cpu_func, NULL @},
  69. .nbuffers = 0
  70. @};
  71. @end smallexample
  72. @end cartouche
  73. A codelet is a structure that represents a computational kernel. Such a codelet
  74. may contain an implementation of the same kernel on different architectures
  75. (e.g. CUDA, Cell's SPU, x86, ...).
  76. The @code{nbuffers} field specifies the number of data buffers that are
  77. manipulated by the codelet: here the codelet does not access or modify any data
  78. that is controlled by our data management library. Note that the argument
  79. passed to the codelet (the @code{cl_arg} field of the @code{starpu_task}
  80. structure) does not count as a buffer since it is not managed by our data
  81. management library, but just contain trivial parameters.
  82. @c TODO need a crossref to the proper description of "where" see bla for more ...
  83. We create a codelet which may only be executed on the CPUs. The @code{where}
  84. field is a bitmask that defines where the codelet may be executed. Here, the
  85. @code{STARPU_CPU} value means that only CPUs can execute this codelet
  86. (@pxref{Codelets and Tasks} for more details on this field).
  87. When a CPU core executes a codelet, it calls the @code{cpu_func} function,
  88. which @emph{must} have the following prototype:
  89. @code{void (*cpu_func)(void *buffers[], void *cl_arg);}
  90. In this example, we can ignore the first argument of this function which gives a
  91. description of the input and output buffers (e.g. the size and the location of
  92. the matrices) since there is none.
  93. The second argument is a pointer to a buffer passed as an
  94. argument to the codelet by the means of the @code{cl_arg} field of the
  95. @code{starpu_task} structure.
  96. @c TODO rewrite so that it is a little clearer ?
  97. Be aware that this may be a pointer to a
  98. @emph{copy} of the actual buffer, and not the pointer given by the programmer:
  99. if the codelet modifies this buffer, there is no guarantee that the initial
  100. buffer will be modified as well: this for instance implies that the buffer
  101. cannot be used as a synchronization medium. If synchronization is needed, data
  102. has to be registered to StarPU, see @ref{Scaling a Vector}.
  103. @node Submitting a Task
  104. @subsection Submitting a Task
  105. @cartouche
  106. @smallexample
  107. void callback_func(void *callback_arg)
  108. @{
  109. printf("Callback function (arg %x)\n", callback_arg);
  110. @}
  111. int main(int argc, char **argv)
  112. @{
  113. /* @b{initialize StarPU} */
  114. starpu_init(NULL);
  115. struct starpu_task *task = starpu_task_create();
  116. task->cl = &cl; /* @b{Pointer to the codelet defined above} */
  117. struct params params = @{ 1, 2.0f @};
  118. task->cl_arg = &params;
  119. task->cl_arg_size = sizeof(params);
  120. task->callback_func = callback_func;
  121. task->callback_arg = 0x42;
  122. /* @b{starpu_task_submit will be a blocking call} */
  123. task->synchronous = 1;
  124. /* @b{submit the task to StarPU} */
  125. starpu_task_submit(task);
  126. /* @b{terminate StarPU} */
  127. starpu_shutdown();
  128. return 0;
  129. @}
  130. @end smallexample
  131. @end cartouche
  132. Before submitting any tasks to StarPU, @code{starpu_init} must be called. The
  133. @code{NULL} argument specifies that we use default configuration. Tasks cannot
  134. be submitted after the termination of StarPU by a call to
  135. @code{starpu_shutdown}.
  136. In the example above, a task structure is allocated by a call to
  137. @code{starpu_task_create}. This function only allocates and fills the
  138. corresponding structure with the default settings (@pxref{Codelets and
  139. Tasks, starpu_task_create}), but it does not submit the task to StarPU.
  140. @c not really clear ;)
  141. The @code{cl} field is a pointer to the codelet which the task will
  142. execute: in other words, the codelet structure describes which computational
  143. kernel should be offloaded on the different architectures, and the task
  144. structure is a wrapper containing a codelet and the piece of data on which the
  145. codelet should operate.
  146. The optional @code{cl_arg} field is a pointer to a buffer (of size
  147. @code{cl_arg_size}) with some parameters for the kernel
  148. described by the codelet. For instance, if a codelet implements a computational
  149. kernel that multiplies its input vector by a constant, the constant could be
  150. specified by the means of this buffer, instead of registering it as a StarPU
  151. data. It must however be noted that StarPU avoids making copy whenever possible
  152. and rather passes the pointer as such, so the buffer which is pointed at must
  153. kept allocated until the task terminates, and if several tasks are submitted
  154. with various parameters, each of them must be given a pointer to their own
  155. buffer.
  156. Once a task has been executed, an optional callback function is be called.
  157. While the computational kernel could be offloaded on various architectures, the
  158. callback function is always executed on a CPU. The @code{callback_arg}
  159. pointer is passed as an argument of the callback. The prototype of a callback
  160. function must be:
  161. @code{void (*callback_function)(void *);}
  162. If the @code{synchronous} field is non-zero, task submission will be
  163. synchronous: the @code{starpu_task_submit} function will not return until the
  164. task was executed. Note that the @code{starpu_shutdown} method does not
  165. guarantee that asynchronous tasks have been executed before it returns,
  166. @code{starpu_task_wait_for_all} can be used to that effect, or data can be
  167. unregistered (@code{starpu_data_unregister(vector_handle);}), which will
  168. implicitly wait for all the tasks scheduled to work on it, unless explicitly
  169. disabled thanks to @code{starpu_data_set_default_sequential_consistency_flag} or
  170. @code{starpu_data_set_sequential_consistency_flag}.
  171. @node Execution of Hello World
  172. @subsection Execution of Hello World
  173. @smallexample
  174. % make hello_world
  175. cc $(pkg-config --cflags libstarpu) $(pkg-config --libs libstarpu) hello_world.c -o hello_world
  176. % ./hello_world
  177. Hello world (params = @{1, 2.000000@} )
  178. Callback function (arg 42)
  179. @end smallexample
  180. @node Scaling a Vector
  181. @section Manipulating Data: Scaling a Vector
  182. The previous example has shown how to submit tasks. In this section,
  183. we show how StarPU tasks can manipulate data. The full source code for
  184. this example is given in @ref{Full source code for the 'Scaling a Vector' example}.
  185. @menu
  186. * Source code of Vector Scaling::
  187. * Execution of Vector Scaling::
  188. @end menu
  189. @node Source code of Vector Scaling
  190. @subsection Source code of Vector Scaling
  191. Programmers can describe the data layout of their application so that StarPU is
  192. responsible for enforcing data coherency and availability across the machine.
  193. Instead of handling complex (and non-portable) mechanisms to perform data
  194. movements, programmers only declare which piece of data is accessed and/or
  195. modified by a task, and StarPU makes sure that when a computational kernel
  196. starts somewhere (e.g. on a GPU), its data are available locally.
  197. Before submitting those tasks, the programmer first needs to declare the
  198. different pieces of data to StarPU using the @code{starpu_*_data_register}
  199. functions. To ease the development of applications for StarPU, it is possible
  200. to describe multiple types of data layout. A type of data layout is called an
  201. @b{interface}. There are different predefined interfaces available in StarPU:
  202. here we will consider the @b{vector interface}.
  203. The following lines show how to declare an array of @code{NX} elements of type
  204. @code{float} using the vector interface:
  205. @cartouche
  206. @smallexample
  207. float vector[NX];
  208. starpu_data_handle_t vector_handle;
  209. starpu_vector_data_register(&vector_handle, 0, (uintptr_t)vector, NX,
  210. sizeof(vector[0]));
  211. @end smallexample
  212. @end cartouche
  213. The first argument, called the @b{data handle}, is an opaque pointer which
  214. designates the array in StarPU. This is also the structure which is used to
  215. describe which data is used by a task. The second argument is the node number
  216. where the data originally resides. Here it is 0 since the @code{vector} array is in
  217. the main memory. Then comes the pointer @code{vector} where the data can be found in main memory,
  218. the number of elements in the vector and the size of each element.
  219. The following shows how to construct a StarPU task that will manipulate the
  220. vector and a constant factor.
  221. @cartouche
  222. @smallexample
  223. float factor = 3.14;
  224. struct starpu_task *task = starpu_task_create();
  225. task->cl = &cl; /* @b{Pointer to the codelet defined below} */
  226. task->buffers[0].handle = vector_handle; /* @b{First parameter of the codelet} */
  227. task->buffers[0].mode = STARPU_RW;
  228. task->cl_arg = &factor;
  229. task->cl_arg_size = sizeof(factor);
  230. task->synchronous = 1;
  231. starpu_task_submit(task);
  232. @end smallexample
  233. @end cartouche
  234. Since the factor is a mere constant float value parameter,
  235. it does not need a preliminary registration, and
  236. can just be passed through the @code{cl_arg} pointer like in the previous
  237. example. The vector parameter is described by its handle.
  238. There are two fields in each element of the @code{buffers} array.
  239. @code{handle} is the handle of the data, and @code{mode} specifies how the
  240. kernel will access the data (@code{STARPU_R} for read-only, @code{STARPU_W} for
  241. write-only and @code{STARPU_RW} for read and write access).
  242. The definition of the codelet can be written as follows:
  243. @cartouche
  244. @smallexample
  245. void scal_cpu_func(void *buffers[], void *cl_arg)
  246. @{
  247. unsigned i;
  248. float *factor = cl_arg;
  249. /* length of the vector */
  250. unsigned n = STARPU_VECTOR_GET_NX(buffers[0]);
  251. /* CPU copy of the vector pointer */
  252. float *val = (float *)STARPU_VECTOR_GET_PTR(buffers[0]);
  253. for (i = 0; i < n; i++)
  254. val[i] *= *factor;
  255. @}
  256. struct starpu_codelet cl = @{
  257. .where = STARPU_CPU,
  258. .cpu_funcs = @{ scal_cpu_func, NULL @},
  259. .nbuffers = 1
  260. @};
  261. @end smallexample
  262. @end cartouche
  263. The first argument is an array that gives
  264. a description of all the buffers passed in the @code{task->buffers}@ array. The
  265. size of this array is given by the @code{nbuffers} field of the codelet
  266. structure. For the sake of genericity, this array contains pointers to the
  267. different interfaces describing each buffer. In the case of the @b{vector
  268. interface}, the location of the vector (resp. its length) is accessible in the
  269. @code{ptr} (resp. @code{nx}) of this array. Since the vector is accessed in a
  270. read-write fashion, any modification will automatically affect future accesses
  271. to this vector made by other tasks.
  272. The second argument of the @code{scal_cpu_func} function contains a pointer to the
  273. parameters of the codelet (given in @code{task->cl_arg}), so that we read the
  274. constant factor from this pointer.
  275. @node Execution of Vector Scaling
  276. @subsection Execution of Vector Scaling
  277. @smallexample
  278. % make vector_scal
  279. cc $(pkg-config --cflags libstarpu) $(pkg-config --libs libstarpu) vector_scal.c -o vector_scal
  280. % ./vector_scal
  281. 0.000000 3.000000 6.000000 9.000000 12.000000
  282. @end smallexample
  283. @node Vector Scaling on an Hybrid CPU/GPU Machine
  284. @section Vector Scaling on an Hybrid CPU/GPU Machine
  285. Contrary to the previous examples, the task submitted in this example may not
  286. only be executed by the CPUs, but also by a CUDA device.
  287. @menu
  288. * Definition of the CUDA Kernel::
  289. * Definition of the OpenCL Kernel::
  290. * Definition of the Main Code::
  291. * Execution of Hybrid Vector Scaling::
  292. @end menu
  293. @node Definition of the CUDA Kernel
  294. @subsection Definition of the CUDA Kernel
  295. The CUDA implementation can be written as follows. It needs to be compiled with
  296. a CUDA compiler such as nvcc, the NVIDIA CUDA compiler driver. It must be noted
  297. that the vector pointer returned by STARPU_VECTOR_GET_PTR is here a pointer in GPU
  298. memory, so that it can be passed as such to the @code{vector_mult_cuda} kernel
  299. call.
  300. @cartouche
  301. @smallexample
  302. #include <starpu.h>
  303. #include <starpu_cuda.h>
  304. static __global__ void vector_mult_cuda(float *val, unsigned n,
  305. float factor)
  306. @{
  307. unsigned i = blockIdx.x*blockDim.x + threadIdx.x;
  308. if (i < n)
  309. val[i] *= factor;
  310. @}
  311. extern "C" void scal_cuda_func(void *buffers[], void *_args)
  312. @{
  313. float *factor = (float *)_args;
  314. /* length of the vector */
  315. unsigned n = STARPU_VECTOR_GET_NX(buffers[0]);
  316. /* CUDA copy of the vector pointer */
  317. float *val = (float *)STARPU_VECTOR_GET_PTR(buffers[0]);
  318. unsigned threads_per_block = 64;
  319. unsigned nblocks = (n + threads_per_block-1) / threads_per_block;
  320. @i{ vector_mult_cuda<<<nblocks,threads_per_block, 0, starpu_cuda_get_local_stream()>>>(val, n, *factor);}
  321. @i{ cudaStreamSynchronize(starpu_cuda_get_local_stream());}
  322. @}
  323. @end smallexample
  324. @end cartouche
  325. @node Definition of the OpenCL Kernel
  326. @subsection Definition of the OpenCL Kernel
  327. The OpenCL implementation can be written as follows. StarPU provides
  328. tools to compile a OpenCL kernel stored in a file.
  329. @cartouche
  330. @smallexample
  331. __kernel void vector_mult_opencl(__global float* val, int nx, float factor)
  332. @{
  333. const int i = get_global_id(0);
  334. if (i < nx) @{
  335. val[i] *= factor;
  336. @}
  337. @}
  338. @end smallexample
  339. @end cartouche
  340. Contrary to CUDA and CPU, @code{STARPU_VECTOR_GET_DEV_HANDLE} has to be used,
  341. which returns a @code{cl_mem} (which is not a device pointer, but an OpenCL
  342. handle), which can be passed as such to the OpenCL kernel. The difference is
  343. important when using partitioning, see @ref{Partitioning Data}.
  344. @cartouche
  345. @smallexample
  346. #include <starpu.h>
  347. @i{#include <starpu_opencl.h>}
  348. @i{extern struct starpu_opencl_program programs;}
  349. void scal_opencl_func(void *buffers[], void *_args)
  350. @{
  351. float *factor = _args;
  352. @i{ int id, devid, err;}
  353. @i{ cl_kernel kernel;}
  354. @i{ cl_command_queue queue;}
  355. @i{ cl_event event;}
  356. /* length of the vector */
  357. unsigned n = STARPU_VECTOR_GET_NX(buffers[0]);
  358. /* OpenCL copy of the vector pointer */
  359. cl_mem val = (cl_mem) STARPU_VECTOR_GET_DEV_HANDLE(buffers[0]);
  360. @i{ id = starpu_worker_get_id();}
  361. @i{ devid = starpu_worker_get_devid(id);}
  362. @i{ err = starpu_opencl_load_kernel(&kernel, &queue, &programs,}
  363. @i{ "vector_mult_opencl", devid); /* @b{Name of the codelet defined above} */}
  364. @i{ if (err != CL_SUCCESS) STARPU_OPENCL_REPORT_ERROR(err);}
  365. @i{ err = clSetKernelArg(kernel, 0, sizeof(val), &val);}
  366. @i{ err |= clSetKernelArg(kernel, 1, sizeof(n), &n);}
  367. @i{ err |= clSetKernelArg(kernel, 2, sizeof(*factor), factor);}
  368. @i{ if (err) STARPU_OPENCL_REPORT_ERROR(err);}
  369. @i{ @{}
  370. @i{ size_t global=1;}
  371. @i{ size_t local=1;}
  372. @i{ err = clEnqueueNDRangeKernel(queue, kernel, 1, NULL, &global, &local, 0, NULL, &event);}
  373. @i{ if (err != CL_SUCCESS) STARPU_OPENCL_REPORT_ERROR(err);}
  374. @i{ @}}
  375. @i{ clFinish(queue);}
  376. @i{ starpu_opencl_collect_stats(event);}
  377. @i{ clReleaseEvent(event);}
  378. @i{ starpu_opencl_release_kernel(kernel);}
  379. @}
  380. @end smallexample
  381. @end cartouche
  382. @node Definition of the Main Code
  383. @subsection Definition of the Main Code
  384. The CPU implementation is the same as in the previous section.
  385. Here is the source of the main application. You can notice the value of the
  386. field @code{where} for the codelet. We specify
  387. @code{STARPU_CPU|STARPU_CUDA|STARPU_OPENCL} to indicate to StarPU that the codelet
  388. can be executed either on a CPU or on a CUDA or an OpenCL device.
  389. @cartouche
  390. @smallexample
  391. #include <starpu.h>
  392. #define NX 2048
  393. extern void scal_cuda_func(void *buffers[], void *_args);
  394. extern void scal_cpu_func(void *buffers[], void *_args);
  395. extern void scal_opencl_func(void *buffers[], void *_args);
  396. /* @b{Definition of the codelet} */
  397. static struct starpu_codelet cl = @{
  398. .where = STARPU_CPU|STARPU_CUDA|STARPU_OPENCL; /* @b{It can be executed on a CPU,} */
  399. /* @b{on a CUDA device, or on an OpenCL device} */
  400. .cuda_funcs = @{ scal_cuda_func, NULL @},
  401. .cpu_funcs = @{ scal_cpu_func, NULL @},
  402. .opencl_funcs = @{ scal_opencl_func, NULL @},
  403. .nbuffers = 1
  404. @}
  405. #ifdef STARPU_USE_OPENCL
  406. /* @b{The compiled version of the OpenCL program} */
  407. struct starpu_opencl_program programs;
  408. #endif
  409. int main(int argc, char **argv)
  410. @{
  411. float *vector;
  412. int i, ret;
  413. float factor=3.0;
  414. struct starpu_task *task;
  415. starpu_data_handle_t vector_handle;
  416. starpu_init(NULL); /* @b{Initialising StarPU} */
  417. #ifdef STARPU_USE_OPENCL
  418. starpu_opencl_load_opencl_from_file(
  419. "examples/basic_examples/vector_scal_opencl_codelet.cl",
  420. &programs, NULL);
  421. #endif
  422. vector = malloc(NX*sizeof(vector[0]));
  423. assert(vector);
  424. for(i=0 ; i<NX ; i++) vector[i] = i;
  425. @end smallexample
  426. @end cartouche
  427. @cartouche
  428. @smallexample
  429. /* @b{Registering data within StarPU} */
  430. starpu_vector_data_register(&vector_handle, 0, (uintptr_t)vector,
  431. NX, sizeof(vector[0]));
  432. /* @b{Definition of the task} */
  433. task = starpu_task_create();
  434. task->cl = &cl;
  435. task->buffers[0].handle = vector_handle;
  436. task->buffers[0].mode = STARPU_RW;
  437. task->cl_arg = &factor;
  438. task->cl_arg_size = sizeof(factor);
  439. @end smallexample
  440. @end cartouche
  441. @cartouche
  442. @smallexample
  443. /* @b{Submitting the task} */
  444. ret = starpu_task_submit(task);
  445. if (ret == -ENODEV) @{
  446. fprintf(stderr, "No worker may execute this task\n");
  447. return 1;
  448. @}
  449. @c TODO: Mmm, should rather be an unregistration with an implicit dependency, no?
  450. /* @b{Waiting for its termination} */
  451. starpu_task_wait_for_all();
  452. /* @b{Update the vector in RAM} */
  453. starpu_data_acquire(vector_handle, STARPU_R);
  454. @end smallexample
  455. @end cartouche
  456. @cartouche
  457. @smallexample
  458. /* @b{Access the data} */
  459. for(i=0 ; i<NX; i++) @{
  460. fprintf(stderr, "%f ", vector[i]);
  461. @}
  462. fprintf(stderr, "\n");
  463. /* @b{Release the RAM view of the data before unregistering it and shutting down StarPU} */
  464. starpu_data_release(vector_handle);
  465. starpu_data_unregister(vector_handle);
  466. starpu_shutdown();
  467. return 0;
  468. @}
  469. @end smallexample
  470. @end cartouche
  471. @node Execution of Hybrid Vector Scaling
  472. @subsection Execution of Hybrid Vector Scaling
  473. The Makefile given at the beginning of the section must be extended to
  474. give the rules to compile the CUDA source code. Note that the source
  475. file of the OpenCL kernel does not need to be compiled now, it will
  476. be compiled at run-time when calling the function
  477. @code{starpu_opencl_load_opencl_from_file()} (@pxref{starpu_opencl_load_opencl_from_file}).
  478. @cartouche
  479. @smallexample
  480. CFLAGS += $(shell pkg-config --cflags libstarpu)
  481. LDFLAGS += $(shell pkg-config --libs libstarpu)
  482. CC = gcc
  483. vector_scal: vector_scal.o vector_scal_cpu.o vector_scal_cuda.o vector_scal_opencl.o
  484. %.o: %.cu
  485. nvcc $(CFLAGS) $< -c $@
  486. clean:
  487. rm -f vector_scal *.o
  488. @end smallexample
  489. @end cartouche
  490. @smallexample
  491. % make
  492. @end smallexample
  493. and to execute it, with the default configuration:
  494. @smallexample
  495. % ./vector_scal
  496. 0.000000 3.000000 6.000000 9.000000 12.000000
  497. @end smallexample
  498. or for example, by disabling CPU devices:
  499. @smallexample
  500. % STARPU_NCPUS=0 ./vector_scal
  501. 0.000000 3.000000 6.000000 9.000000 12.000000
  502. @end smallexample
  503. or by disabling CUDA devices (which may permit to enable the use of OpenCL,
  504. see @ref{Enabling OpenCL}):
  505. @smallexample
  506. % STARPU_NCUDA=0 ./vector_scal
  507. 0.000000 3.000000 6.000000 9.000000 12.000000
  508. @end smallexample