110_basic_examples.doxy 16 KB

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