110_basic_examples.doxy 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  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,2019 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 HelloWorldUsingStarPUAPI Hello World
  20. This section shows how to implement a simple program that submits a task
  21. to StarPU.
  22. \subsection RequiredHeaders Required Headers
  23. The header starpu.h should be included in any code using StarPU.
  24. \code{.c}
  25. #include <starpu.h>
  26. \endcode
  27. \subsection DefiningACodelet Defining A Codelet
  28. A codelet is a structure that represents a computational kernel. Such a codelet
  29. may contain an implementation of the same kernel on different architectures
  30. (e.g. CUDA, x86, ...). For compatibility, make sure that the whole
  31. structure is properly initialized to zero, either by using the
  32. function starpu_codelet_init(), or by letting the
  33. compiler implicitly do it as examplified below.
  34. The field starpu_codelet::nbuffers specifies the number of data buffers that are
  35. manipulated by the codelet: here the codelet does not access or modify any data
  36. that is controlled by our data management library.
  37. We create a codelet which may only be executed on CPUs. When a CPU
  38. core will execute a codelet, it will call the function
  39. <c>cpu_func</c>, which \em must have the following prototype:
  40. \code{.c}
  41. void (*cpu_func)(void *buffers[], void *cl_arg);
  42. \endcode
  43. In this example, we can ignore the first argument of this function which gives a
  44. description of the input and output buffers (e.g. the size and the location of
  45. the matrices) since there is none. We also ignore the second argument
  46. which is a pointer to optional arguments for the codelet.
  47. \code{.c}
  48. void cpu_func(void *buffers[], void *cl_arg)
  49. {
  50. printf("Hello world\n");
  51. }
  52. struct starpu_codelet cl =
  53. {
  54. .cpu_funcs = { cpu_func },
  55. .nbuffers = 0
  56. };
  57. \endcode
  58. \subsection SubmittingATask Submitting A Task
  59. Before submitting any tasks to StarPU, starpu_init() must be called. The
  60. <c>NULL</c> argument specifies that we use the default configuration.
  61. Tasks can then be submitted until the termination of StarPU -- done by a
  62. call to starpu_shutdown().
  63. In the example below, a task structure is allocated by a call to
  64. starpu_task_create(). This function allocates and fills the
  65. task structure with its default settings, it does not
  66. submit the task to StarPU.
  67. The field starpu_task::cl is a pointer to the codelet which the task will
  68. execute: in other words, the codelet structure describes which computational
  69. kernel should be offloaded on the different architectures, and the task
  70. structure is a wrapper containing a codelet and the piece of data on which the
  71. codelet should operate.
  72. If the field starpu_task::synchronous is non-zero, task submission
  73. will be synchronous: the function starpu_task_submit() will not return
  74. until the task has been executed. Note that the function starpu_shutdown()
  75. does not guarantee that asynchronous tasks have been executed before
  76. it returns, starpu_task_wait_for_all() can be used to this effect, or
  77. data can be unregistered (starpu_data_unregister()), which will
  78. implicitly wait for all the tasks scheduled to work on it, unless
  79. explicitly disabled thanks to
  80. starpu_data_set_default_sequential_consistency_flag() or
  81. starpu_data_set_sequential_consistency_flag().
  82. \code{.c}
  83. int main(int argc, char **argv)
  84. {
  85. /* initialize StarPU */
  86. starpu_init(NULL);
  87. struct starpu_task *task = starpu_task_create();
  88. task->cl = &cl; /* Pointer to the codelet defined above */
  89. /* starpu_task_submit will be a blocking call. If unset,
  90. starpu_task_wait() needs to be called after submitting the task. */
  91. task->synchronous = 1;
  92. /* submit the task to StarPU */
  93. starpu_task_submit(task);
  94. /* terminate StarPU */
  95. starpu_shutdown();
  96. return 0;
  97. }
  98. \endcode
  99. \subsection ExecutionOfHelloWorld Execution Of Hello World
  100. \verbatim
  101. $ make hello_world
  102. cc $(pkg-config --cflags starpu-1.3) hello_world.c -o hello_world $(pkg-config --libs starpu-1.3)
  103. $ ./hello_world
  104. Hello world
  105. \endverbatim
  106. \subsection PassingArgumentsToTheCodelet Passing Arguments To The Codelet
  107. The optional field starpu_task::cl_arg field is a pointer to a buffer
  108. (of size starpu_task::cl_arg_size) with some parameters for the kernel
  109. described by the codelet. For instance, if a codelet implements a
  110. computational kernel that multiplies its input vector by a constant,
  111. the constant could be specified by the means of this buffer, instead
  112. of registering it as a StarPU data. It must however be noted that
  113. StarPU avoids making copy whenever possible and rather passes the
  114. pointer as such, so the buffer which is pointed at must be kept allocated
  115. until the task terminates, and if several tasks are submitted with
  116. various parameters, each of them must be given a pointer to their
  117. own buffer.
  118. \code{.c}
  119. struct params
  120. {
  121. int i;
  122. float f;
  123. };
  124. void cpu_func(void *buffers[], void *cl_arg)
  125. {
  126. struct params *params = cl_arg;
  127. printf("Hello world (params = {%i, %f} )\n", params->i, params->f);
  128. }
  129. \endcode
  130. As said before, the field starpu_codelet::nbuffers specifies the
  131. number of data buffers which are manipulated by the codelet. It does
  132. not count the argument --- the parameter <c>cl_arg</c> of the function
  133. <c>cpu_func</c> --- since it is not managed by our data management
  134. library, but just contains trivial parameters.
  135. // TODO rewrite so that it is a little clearer ?
  136. Be aware that this may be a pointer to a
  137. \em copy of the actual buffer, and not the pointer given by the programmer:
  138. if the codelet modifies this buffer, there is no guarantee that the initial
  139. buffer will be modified as well: this for instance implies that the buffer
  140. cannot be used as a synchronization medium. If synchronization is needed, data
  141. has to be registered to StarPU, see \ref VectorScalingUsingStarPUAPI.
  142. \code{.c}
  143. int main(int argc, char **argv)
  144. {
  145. /* initialize StarPU */
  146. starpu_init(NULL);
  147. struct starpu_task *task = starpu_task_create();
  148. task->cl = &cl; /* Pointer to the codelet defined above */
  149. struct params params = { 1, 2.0f };
  150. task->cl_arg = &params;
  151. task->cl_arg_size = sizeof(params);
  152. /* starpu_task_submit will be a blocking call */
  153. task->synchronous = 1;
  154. /* submit the task to StarPU */
  155. starpu_task_submit(task);
  156. /* terminate StarPU */
  157. starpu_shutdown();
  158. return 0;
  159. }
  160. \endcode
  161. \verbatim
  162. $ make hello_world
  163. cc $(pkg-config --cflags starpu-1.3) hello_world.c -o hello_world $(pkg-config --libs starpu-1.3)
  164. $ ./hello_world
  165. Hello world (params = {1, 2.000000} )
  166. \endverbatim
  167. \subsection DefiningACallback Defining A Callback
  168. Once a task has been executed, an optional callback function
  169. starpu_task::callback_func is called when defined.
  170. While the computational kernel could be offloaded on various architectures, the
  171. callback function is always executed on a CPU. The pointer
  172. starpu_task::callback_arg is passed as an argument of the callback
  173. function. The prototype of a callback function must be:
  174. \code{.c}
  175. void (*callback_function)(void *);
  176. \endcode
  177. \code{.c}
  178. void callback_func(void *callback_arg)
  179. {
  180. printf("Callback function (arg %x)\n", callback_arg);
  181. }
  182. int main(int argc, char **argv)
  183. {
  184. /* initialize StarPU */
  185. starpu_init(NULL);
  186. struct starpu_task *task = starpu_task_create();
  187. task->cl = &cl; /* Pointer to the codelet defined above */
  188. task->callback_func = callback_func;
  189. task->callback_arg = 0x42;
  190. /* starpu_task_submit will be a blocking call */
  191. task->synchronous = 1;
  192. /* submit the task to StarPU */
  193. starpu_task_submit(task);
  194. /* terminate StarPU */
  195. starpu_shutdown();
  196. return 0;
  197. }
  198. \endcode
  199. \verbatim
  200. $ make hello_world
  201. cc $(pkg-config --cflags starpu-1.3) hello_world.c -o hello_world $(pkg-config --libs starpu-1.3)
  202. $ ./hello_world
  203. Hello world
  204. Callback function (arg 42)
  205. \endverbatim
  206. \subsection WhereToExecuteACodelet Where To Execute A Codelet
  207. \code{.c}
  208. struct starpu_codelet cl =
  209. {
  210. .where = STARPU_CPU,
  211. .cpu_funcs = { cpu_func },
  212. .cpu_funcs_name = { "cpu_func" },
  213. .nbuffers = 0
  214. };
  215. \endcode
  216. We create a codelet which may only be executed on the CPUs. The
  217. optional field starpu_codelet::where is a bitmask which defines where
  218. the codelet may be executed. Here, the value ::STARPU_CPU means that
  219. only CPUs can execute this codelet. When the optional field
  220. starpu_codelet::where is unset, its value is automatically set based
  221. on the availability of the different fields <c>XXX_funcs</c>.
  222. TODO: explain starpu_codelet::cpu_funcs_name
  223. \section VectorScalingUsingStarPUAPI Vector Scaling
  224. The previous example has shown how to submit tasks. In this section,
  225. we show how StarPU tasks can manipulate data.
  226. The full source code for
  227. this example is given in \ref FullSourceCodeVectorScal.
  228. \subsection SourceCodeOfVectorScaling Source Code of Vector Scaling
  229. Programmers can describe the data layout of their application so that StarPU is
  230. responsible for enforcing data coherency and availability across the machine.
  231. Instead of handling complex (and non-portable) mechanisms to perform data
  232. movements, programmers only declare which piece of data is accessed and/or
  233. modified by a task, and StarPU makes sure that when a computational kernel
  234. starts somewhere (e.g. on a GPU), its data are available locally.
  235. Before submitting those tasks, the programmer first needs to declare the
  236. different pieces of data to StarPU using the functions
  237. <c>starpu_*_data_register</c>. To ease the development of applications
  238. for StarPU, it is possible to describe multiple types of data layout.
  239. A type of data layout is called an <b>interface</b>. There are
  240. different predefined interfaces available in StarPU: here we will
  241. consider the <b>vector interface</b>.
  242. The following lines show how to declare an array of <c>NX</c> elements of type
  243. <c>float</c> using the vector interface:
  244. \code{.c}
  245. float vector[NX];
  246. starpu_data_handle_t vector_handle;
  247. starpu_vector_data_register(&vector_handle, STARPU_MAIN_RAM, (uintptr_t)vector, NX, sizeof(vector[0]));
  248. \endcode
  249. The first argument, called the <b>data handle</b>, is an opaque pointer which
  250. designates the array within StarPU. This is also the structure which is used to
  251. describe which data is used by a task. The second argument is the node number
  252. where the data originally resides. Here it is ::STARPU_MAIN_RAM since the array <c>vector</c> is in
  253. the main memory. Then comes the pointer <c>vector</c> where the data can be found in main memory,
  254. the number of elements in the vector and the size of each element.
  255. The following shows how to construct a StarPU task that will manipulate the
  256. vector and a constant factor.
  257. \code{.c}
  258. float factor = 3.14;
  259. struct starpu_task *task = starpu_task_create();
  260. task->cl = &cl; /* Pointer to the codelet defined below */
  261. task->handles[0] = vector_handle; /* First parameter of the codelet */
  262. task->cl_arg = &factor;
  263. task->cl_arg_size = sizeof(factor);
  264. task->synchronous = 1;
  265. starpu_task_submit(task);
  266. \endcode
  267. Since the factor is a mere constant float value parameter,
  268. it does not need a preliminary registration, and
  269. can just be passed through the pointer starpu_task::cl_arg like in the previous
  270. example. The vector parameter is described by its handle.
  271. starpu_task::handles should be set with the handles of the data, the
  272. access modes for the data are defined in the field
  273. starpu_codelet::modes (::STARPU_R for read-only, ::STARPU_W for
  274. write-only and ::STARPU_RW for read and write access).
  275. The definition of the codelet can be written as follows:
  276. \code{.c}
  277. void scal_cpu_func(void *buffers[], void *cl_arg)
  278. {
  279. unsigned i;
  280. float *factor = cl_arg;
  281. /* length of the vector */
  282. unsigned n = STARPU_VECTOR_GET_NX(buffers[0]);
  283. /* CPU copy of the vector pointer */
  284. float *val = (float *)STARPU_VECTOR_GET_PTR(buffers[0]);
  285. for (i = 0; i < n; i++)
  286. val[i] *= *factor;
  287. }
  288. struct starpu_codelet cl =
  289. {
  290. .cpu_funcs = { scal_cpu_func },
  291. .cpu_funcs_name = { "scal_cpu_func" },
  292. .nbuffers = 1,
  293. .modes = { STARPU_RW }
  294. };
  295. \endcode
  296. The first argument is an array that gives
  297. a description of all the buffers passed in the array starpu_task::handles. The
  298. size of this array is given by the field starpu_codelet::nbuffers. For
  299. the sake of genericity, this array contains pointers to the different
  300. interfaces describing each buffer. In the case of the <b>vector
  301. interface</b>, the location of the vector (resp. its length) is
  302. accessible in the starpu_vector_interface::ptr (resp.
  303. starpu_vector_interface::nx) of this interface. Since the vector is
  304. accessed in a read-write fashion, any modification will automatically
  305. affect future accesses to this vector made by other tasks.
  306. The second argument of the function <c>scal_cpu_func</c> contains a
  307. pointer to the parameters of the codelet (given in
  308. starpu_task::cl_arg), so that we read the constant factor from this
  309. pointer.
  310. \subsection ExecutionOfVectorScaling Execution of Vector Scaling
  311. \verbatim
  312. $ make vector_scal
  313. cc $(pkg-config --cflags starpu-1.3) vector_scal.c -o vector_scal $(pkg-config --libs starpu-1.3)
  314. $ ./vector_scal
  315. 0.000000 3.000000 6.000000 9.000000 12.000000
  316. \endverbatim
  317. \section VectorScalingOnAnHybridCPUGPUMachine Vector Scaling on an Hybrid CPU/GPU Machine
  318. Contrary to the previous examples, the task submitted in this example may not
  319. only be executed by the CPUs, but also by a CUDA device.
  320. \subsection DefinitionOfTheCUDAKernel Definition of the CUDA Kernel
  321. The CUDA implementation can be written as follows. It needs to be compiled with
  322. a CUDA compiler such as nvcc, the NVIDIA CUDA compiler driver. It must be noted
  323. that the vector pointer returned by ::STARPU_VECTOR_GET_PTR is here a
  324. pointer in GPU memory, so that it can be passed as such to the
  325. kernel call <c>vector_mult_cuda</c>.
  326. \snippet vector_scal_cuda.c To be included. You should update doxygen if you see this text.
  327. \subsection DefinitionOfTheOpenCLKernel Definition of the OpenCL Kernel
  328. The OpenCL implementation can be written as follows. StarPU provides
  329. tools to compile a OpenCL kernel stored in a file.
  330. \code{.c}
  331. __kernel void vector_mult_opencl(int nx, __global float* val, float factor)
  332. {
  333. const int i = get_global_id(0);
  334. if (i < nx)
  335. {
  336. val[i] *= factor;
  337. }
  338. }
  339. \endcode
  340. Contrary to CUDA and CPU, ::STARPU_VECTOR_GET_DEV_HANDLE has to be used,
  341. which returns a <c>cl_mem</c> (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 PartitioningData.
  344. \snippet vector_scal_opencl.c To be included. You should update doxygen if you see this text.
  345. \subsection DefinitionOfTheMainCode Definition of the Main Code
  346. The CPU implementation is the same as in the previous section.
  347. Here is the source of the main application. You can notice that the fields
  348. starpu_codelet::cuda_funcs and starpu_codelet::opencl_funcs are set to
  349. define the pointers to the CUDA and OpenCL implementations of the
  350. task.
  351. \snippet vector_scal_c.c To be included. You should update doxygen if you see this text.
  352. \subsection ExecutionOfHybridVectorScaling Execution of Hybrid Vector Scaling
  353. The Makefile given at the beginning of the section must be extended to
  354. give the rules to compile the CUDA source code. Note that the source
  355. file of the OpenCL kernel does not need to be compiled now, it will
  356. be compiled at run-time when calling the function
  357. starpu_opencl_load_opencl_from_file().
  358. \verbatim
  359. CFLAGS += $(shell pkg-config --cflags starpu-1.3)
  360. LDLIBS += $(shell pkg-config --libs starpu-1.3)
  361. CC = gcc
  362. vector_scal: vector_scal.o vector_scal_cpu.o vector_scal_cuda.o vector_scal_opencl.o
  363. %.o: %.cu
  364. nvcc $(CFLAGS) $< -c $@
  365. clean:
  366. rm -f vector_scal *.o
  367. \endverbatim
  368. \verbatim
  369. $ make
  370. \endverbatim
  371. and to execute it, with the default configuration:
  372. \verbatim
  373. $ ./vector_scal
  374. 0.000000 3.000000 6.000000 9.000000 12.000000
  375. \endverbatim
  376. or for example, by disabling CPU devices:
  377. \verbatim
  378. $ STARPU_NCPU=0 ./vector_scal
  379. 0.000000 3.000000 6.000000 9.000000 12.000000
  380. \endverbatim
  381. or by disabling CUDA devices (which may permit to enable the use of OpenCL,
  382. see \ref EnablingOpenCL) :
  383. \verbatim
  384. $ STARPU_NCUDA=0 ./vector_scal
  385. 0.000000 3.000000 6.000000 9.000000 12.000000
  386. \endverbatim
  387. */