starpu.texi 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326
  1. \input texinfo @c -*-texinfo-*-
  2. @c %**start of header
  3. @setfilename starpu.info
  4. @settitle StarPU
  5. @c %**end of header
  6. @setchapternewpage odd
  7. @titlepage
  8. @title StarPU
  9. @page
  10. @vskip 0pt plus 1filll
  11. @comment For the @value{version-GCC} Version*
  12. @end titlepage
  13. @summarycontents
  14. @contents
  15. @page
  16. @node Top
  17. @top Preface
  18. @cindex Preface
  19. This manual documents the usage of StarPU
  20. @comment
  21. @comment When you add a new menu item, please keep the right hand
  22. @comment aligned to the same column. Do not use tabs. This provides
  23. @comment better formatting.
  24. @comment
  25. @menu
  26. * Introduction:: A basic introduction to using StarPU.
  27. * Installing StarPU:: How to configure, build and install StarPU.
  28. * Configuration options:: Configurations options
  29. * Environment variables:: Environment variables used by StarPU.
  30. * StarPU API:: The API to use StarPU.
  31. * Basic Examples:: Basic examples of the use of StarPU.
  32. * Advanced Topics:: Advanced use of StarPU.
  33. @end menu
  34. @c ---------------------------------------------------------------------
  35. @c Introduction to StarPU
  36. @c ---------------------------------------------------------------------
  37. @node Introduction
  38. @chapter Introduction to StarPU
  39. @menu
  40. * Motivation:: Why StarPU ?
  41. * StarPU in a Nutshell:: The Fundamentals of StarPU
  42. @end menu
  43. @node Motivation
  44. @section Motivation
  45. @c complex machines with heterogeneous cores/devices
  46. The use of specialized hardware such as accelerators or coprocessors offers an
  47. interesting approach to overcome the physical limits encountered by processor
  48. architects. As a result, many machines are now equipped with one or several
  49. accelerators (eg. a GPU), in addition to the usual processor(s). While a lot of
  50. efforts have been devoted to offload computation onto such accelerators, very
  51. little attention as been paid to portability concerns on the one hand, and to the
  52. possibility of having heterogeneous accelerators and processors to interact on the other hand.
  53. StarPU is a runtime system that offers support for heterogeneous multicore
  54. architectures, it not only offers a unified view of the computational resources
  55. (ie. CPUs and accelerators at the same time), but it also takes care to
  56. efficiently map and execute tasks onto an heterogeneous machine while
  57. transparently handling low-level issues in a portable fashion.
  58. @c this leads to a complicated distributed memory design
  59. @c which is not (easily) manageable by hand
  60. @c added value/benefits of StarPU
  61. @c - portability
  62. @c - scheduling, perf. portability
  63. @node StarPU in a Nutshell
  64. @section StarPU in a Nutshell
  65. From a programming point of view, StarPU is not a new language but a library
  66. that executes tasks explicitly submitted by the application. The data that a
  67. task manipulate are automatically transferred onto the accelerator so that the
  68. programmer does not have to take care of complex data movements. StarPU also
  69. takes particular care of scheduling those tasks efficiently and allows
  70. scheduling experts to implement custom scheduling policies in a portable
  71. fashion.
  72. @c explain the notion of codelet and task (ie. g(A, B)
  73. @subsection Codelet and Tasks
  74. One of StarPU primary data structure is the @b{codelet}. A codelet describes a
  75. computational kernel that can possibly be implemented on multiple architectures
  76. such as a CPU, a CUDA device or a Cell's SPU.
  77. @c TODO insert illustration f : f_spu, f_cpu, ...
  78. Another important data structure is the @b{task}. Executing a StarPU task
  79. consists in applying a codelet on a data set, on one of the architecture on
  80. which the codelet is implemented. In addition to the codelet that a task
  81. implements, it also describes which data are accessed, and how they are
  82. accessed during the computation (read and/or write).
  83. StarPU tasks are asynchronous: submitting a task to StarPU is a non-blocking
  84. operation. The task structure can also specify a @b{callback} function that is
  85. called once StarPU has properly executed the task. It also contains optional
  86. fields that the application may use to give hints to the scheduler (such as
  87. priority levels).
  88. A task may be identified by a unique 64-bit number which we refer as a @b{tag}.
  89. Task dependencies can be enforced either by the means of callback functions, or
  90. by expressing dependencies between tags.
  91. @c TODO insert illustration f(Ar, Brw, Cr) + ..
  92. @c DSM
  93. @subsection StarPU Data Management Library
  94. Because StarPU schedules tasks at runtime, data transfers have to be
  95. done automatically and ``just-in-time'' between processing units,
  96. relieving the application programmer from explicit data transfers.
  97. Moreover, to avoid unnecessary transfers, StarPU keeps data
  98. where it was last needed, even if was modified there, and it
  99. allows multiple copies of the same data to reside at the same time on
  100. several processing units as long as it is not modified.
  101. @c ---------------------------------------------------------------------
  102. @c Installing StarPU
  103. @c ---------------------------------------------------------------------
  104. @node Installing StarPU
  105. @chapter Installing StarPU
  106. StarPU can be built and installed by the standard means of the GNU
  107. autotools. The following chapter is intended to briefly remind how these tools
  108. can be used to install StarPU.
  109. @section Configuring StarPU
  110. @subsection Generating Makefiles and configuration scripts
  111. This step is not necessary when using the tarball releases of StarPU. If you
  112. are using the source code from the svn repository, you first need to generate
  113. the configure scripts and the Makefiles.
  114. @example
  115. $ autoreconf -vfi
  116. @end example
  117. @subsection Configuring StarPU
  118. @example
  119. $ ./configure
  120. @end example
  121. @c TODO enumerate the list of interesting options: refer to a specific section
  122. @section Building and Installing StarPU
  123. @subsection Building
  124. @example
  125. $ make
  126. @end example
  127. @subsection Sanity Checks
  128. In order to make sure that StarPU is working properly on the system, it is also
  129. possible to run a test suite.
  130. @example
  131. $ make check
  132. @end example
  133. @subsection Installing
  134. In order to install StarPU at the location that was specified during
  135. configuration:
  136. @example
  137. $ make install
  138. @end example
  139. @subsection pkg-config configuration
  140. It is possible that compiling and linking an application against StarPU
  141. requires to use specific flags or libraries (for instance @code{CUDA} or
  142. @code{libspe2}). Therefore, it is possible to use the @code{pkg-config} tool.
  143. If StarPU was not installed at some standard location, the path of StarPU's
  144. library must be specified in the @code{PKG_CONFIG_PATH} environment variable so
  145. that @code{pkg-config} can find it. So if StarPU was installed in
  146. @code{$(prefix_dir)}:
  147. @example
  148. @c TODO: heu, c'est vraiment du shell ça ? :)
  149. $ PKG_CONFIG_PATH = @{PKG_CONFIG_PATH@}:$(prefix_dir)/lib/
  150. @end example
  151. The flags required to compiled or linked against StarPU are then
  152. accessible with the following commands:
  153. @example
  154. $ pkg-config --cflags libstarpu # options for the compiler
  155. $ pkg-config --libs libstarpu # options for the linker
  156. @end example
  157. @c ---------------------------------------------------------------------
  158. @c Configuration options
  159. @c ---------------------------------------------------------------------
  160. @node Configuration options
  161. @chapter Configuration options
  162. TODO
  163. @c ---------------------------------------------------------------------
  164. @c Environment variables
  165. @c ---------------------------------------------------------------------
  166. @node Environment variables
  167. @chapter Environment variables
  168. @menu
  169. * Workers:: Configuring workers
  170. * Scheduling:: Configuring the Scheduling engine
  171. * Misc:: Miscellaneous and debug
  172. @end menu
  173. TODO, explicit configuration (passed to starpu_init) overrides env variables.
  174. @node Workers
  175. @section Configuring workers
  176. @menu
  177. * STARPU_NCPUS :: Number of CPU workers
  178. * STARPU_NCUDA :: Number of CUDA workers
  179. * STARPU_NOPENCL :: Number of OpenCL workers
  180. * STARPU_NGORDON :: Number of SPU workers (Cell)
  181. * STARPU_WORKERS_CPUID :: Bind workers to specific CPUs
  182. * STARPU_WORKERS_CUDAID :: Select specific CUDA devices
  183. * STARPU_WORKERS_OPENCLID :: Select specific OpenCL devices
  184. @end menu
  185. @node STARPU_NCPUS
  186. @subsection @code{STARPU_NCPUS} -- Number of CPU workers
  187. @table @asis
  188. @item @emph{Description}:
  189. Specify the maximum number of CPU workers. Note that StarPU will not allocate
  190. more CPUs than there are physical CPUs, and that some CPUs are used to control
  191. the accelerators.
  192. @end table
  193. @node STARPU_NCUDA
  194. @subsection @code{STARPU_NCUDA} -- Number of CUDA workers
  195. @table @asis
  196. @item @emph{Description}:
  197. Specify the maximum number of CUDA devices that StarPU can use. In case there
  198. @code{STARPU_NCUDA} is lower than the number of physical devices, it is
  199. possible to select which CUDA devices should be used by the means of the
  200. @code{STARPU_WORKERS_CUDAID} environment variable.
  201. @end table
  202. @node STARPU_NOPENCL
  203. @subsection @code{STARPU_NOPENCL} -- Number of OpenCL workers
  204. @table @asis
  205. @item @emph{Description}:
  206. OpenCL equivalent of the @code{STARPU_NCUDA} environment variable.
  207. @end table
  208. @node STARPU_NGORDON
  209. @subsection @code{STARPU_NGORDON} -- Number of SPU workers (Cell)
  210. @table @asis
  211. @item @emph{Description}:
  212. Specify the maximum number of SPUs that StarPU can use.
  213. @end table
  214. @node STARPU_WORKERS_CPUID
  215. @subsection @code{STARPU_WORKERS_CPUID} -- Bind workers to specific CPUs
  216. @table @asis
  217. @item @emph{Description}:
  218. Passing an array of integers (starting from 0) in @code{STARPU_WORKERS_CPUID}
  219. specifies on which logical CPU the different workers should be
  220. bound. For instance, if @code{STARPU_WORKERS_CPUID = "1 3 0 2"}, the first
  221. worker will be bound to logical CPU #1, the second CPU worker will be bound to
  222. logical CPU #3 and so on. Note that the logical ordering of the CPUs is either
  223. determined by the OS, or provided by the @code{hwloc}  library in case it is
  224. available.
  225. Note that the first workers correspond to the CUDA workers, then come the
  226. OpenCL and the SPU, and finally the CPU workers. For example if
  227. we have @code{STARPU_NCUDA=1}, @code{STARPU_NOPENCL=1}, @code{STARPU_NCPUS=2}
  228. and @code{STARPU_WORKERS_CPUID = "0 2 1 3"}, the CUDA device will be controlled
  229. by logical CPU #0, the OpenCL device will be controlled by logical CPU #2, and
  230. the logical CPUs #1 and #3 will be used by the CPU workers.
  231. If the number of workers is larger than the array given in
  232. @code{STARPU_WORKERS_CPUID}, the workers are bound to the logical CPUs in a
  233. round-robin fashion: if @code{STARPU_WORKERS_CPUID = "0 1"}, the first and the
  234. third (resp. second and fourth) workers will be put on CPU #0 (resp. CPU #1).
  235. @end table
  236. @node STARPU_WORKERS_CUDAID
  237. @subsection @code{STARPU_WORKERS_CUDAID} -- Select specific CUDA devices
  238. @table @asis
  239. @item @emph{Description}:
  240. Similarly to the @code{STARPU_WORKERS_CPUID} environment variable, it is
  241. possible to select which CUDA devices should be used by StarPU. On a machine
  242. equipped with 4 GPUs, setting @code{STARPU_WORKERS_CUDAID = "1 3"} and
  243. @code{STARPU_NCUDA=2} specifies that 2 CUDA workers should be created, and that
  244. they should use CUDA devices #1 and #3 (the logical ordering of the devices is
  245. the one reported by CUDA).
  246. @end table
  247. @node STARPU_WORKERS_OPENCLID
  248. @subsection @code{STARPU_WORKERS_OPENCLID} -- Select specific OpenCL devices
  249. @table @asis
  250. @item @emph{Description}:
  251. OpenCL equivalent of the @code{STARPU_WORKERS_CUDAID} environment variable.
  252. @end table
  253. @node Scheduling
  254. @section Configuring the Scheduling engine
  255. @menu
  256. * STARPU_SCHED :: Scheduling policy
  257. * STARPU_CALIBRATE :: Calibrate performance models
  258. * STARPU_PREFETCH :: Use data prefetch
  259. * STARPU_SCHED_ALPHA :: Computation factor
  260. * STARPU_SCHED_BETA :: Communication factor
  261. @end menu
  262. @node STARPU_SCHED
  263. @subsection @code{STARPU_SCHED} -- Scheduling policy
  264. @table @asis
  265. @item @emph{Description}:
  266. TODO
  267. Use @code{STARPU_SCHED=help} to get the list of available schedulers
  268. @end table
  269. @node STARPU_CALIBRATE
  270. @subsection @code{STARPU_CALIBRATE} -- Calibrate performance models
  271. @table @asis
  272. @item @emph{Description}:
  273. If this variable is set, the performance models are calibrated during the execution.
  274. TODO
  275. Note: this currently only applies to dm and dmda scheduling policies.
  276. @end table
  277. @node STARPU_PREFETCH
  278. @subsection @code{STARPU_PREFETCH} -- Use data prefetch
  279. @table @asis
  280. @item @emph{Description}:
  281. TODO
  282. @end table
  283. @node STARPU_SCHED_ALPHA
  284. @subsection @code{STARPU_SCHED_ALPHA} -- Computation factor
  285. @table @asis
  286. @item @emph{Description}:
  287. TODO
  288. @end table
  289. @node STARPU_SCHED_BETA
  290. @subsection @code{STARPU_SCHED_BETA} -- Communication factor
  291. @table @asis
  292. @item @emph{Description}:
  293. TODO
  294. @end table
  295. @node Misc
  296. @section Miscellaneous and debug
  297. @menu
  298. * STARPU_LOGFILENAME :: Select debug file name
  299. @end menu
  300. @node STARPU_LOGFILENAME
  301. @subsection @code{STARPU_LOGFILENAME} -- Select debug file name
  302. @table @asis
  303. @item @emph{Description}:
  304. TODO
  305. @end table
  306. @c ---------------------------------------------------------------------
  307. @c StarPU API
  308. @c ---------------------------------------------------------------------
  309. @node StarPU API
  310. @chapter StarPU API
  311. @menu
  312. * Initialization and Termination:: Initialization and Termination methods
  313. * Workers' Properties:: Methods to enumerate workers' properties
  314. * Data Library:: Methods to manipulate data
  315. * Codelets and Tasks:: Methods to construct tasks
  316. * Tags:: Task dependencies
  317. * CUDA extensions:: CUDA extensions
  318. * Cell extensions:: Cell extensions
  319. @end menu
  320. @node Initialization and Termination
  321. @section Initialization and Termination
  322. @menu
  323. * starpu_init:: Initialize StarPU
  324. * struct starpu_conf:: StarPU runtime configuration
  325. * starpu_shutdown:: Terminate StarPU
  326. @end menu
  327. @node starpu_init
  328. @subsection @code{starpu_init} -- Initialize StarPU
  329. @table @asis
  330. @item @emph{Description}:
  331. This is StarPU initialization method, which must be called prior to any other
  332. StarPU call. It is possible to specify StarPU's configuration (eg. scheduling
  333. policy, number of cores, ...) by passing a non-null argument. Default
  334. configuration is used if the passed argument is @code{NULL}.
  335. @item @emph{Return value}:
  336. Upon successful completion, this function returns 0. Otherwise, @code{-ENODEV}
  337. indicates that no worker was available (so that StarPU was not be initialized).
  338. @item @emph{Prototype}:
  339. @code{int starpu_init(struct starpu_conf *conf);}
  340. @end table
  341. @node struct starpu_conf
  342. @subsection @code{struct starpu_conf} -- StarPU runtime configuration
  343. @table @asis
  344. @item @emph{Description}:
  345. This structure is passed to the @code{starpu_init} function in order configure
  346. StarPU. When the default value is used, StarPU automatically select the number
  347. of processing units and takes the default scheduling policy. This parameters
  348. overwrite the equivalent environnement variables.
  349. @item @emph{Fields}:
  350. @table @asis
  351. @item @code{sched_policy} (default = NULL):
  352. This is the name of the scheduling policy. This can also be specified with the
  353. @code{STARPU_SCHED} environment variable.
  354. @item @code{ncpus} (default = -1):
  355. This is the maximum number of CPU cores that StarPU can use. This can also be
  356. specified with the @code{STARPU_NCPUS} environment variable.
  357. @item @code{ncuda} (default = -1):
  358. This is the maximum number of CUDA devices that StarPU can use. This can also be
  359. specified with the @code{STARPU_NCUDA} environment variable.
  360. @item @code{nspus} (default = -1):
  361. This is the maximum number of Cell SPUs that StarPU can use. This can also be
  362. specified with the @code{STARPU_NGORDON} environment variable.
  363. @item @code{calibrate} (default = 0):
  364. If this flag is set, StarPU will calibrate the performance models when
  365. executing tasks. This can also be specified with the @code{STARPU_CALIBRATE}
  366. environment variable.
  367. @end table
  368. @end table
  369. @node starpu_shutdown
  370. @subsection @code{starpu_shutdown} -- Terminate StarPU
  371. @table @asis
  372. @item @emph{Description}:
  373. This is StarPU termination method. It must be called at the end of the
  374. application: statistics and other post-mortem debugging information are not
  375. garanteed to be available until this method has been called.
  376. @item @emph{Prototype}:
  377. @code{void starpu_shutdown(void);}
  378. @end table
  379. @node Workers' Properties
  380. @section Workers' Properties
  381. @menu
  382. * starpu_get_worker_count:: Get the number of processing units
  383. * starpu_get_worker_id:: Get the identifier of the current worker
  384. * starpu_get_worker_type:: Get the type of processing unit associated to a worker
  385. * starpu_get_worker_name:: Get the name of a worker
  386. @end menu
  387. @node starpu_get_worker_count
  388. @subsection @code{starpu_get_worker_count} -- Get the number of processing units
  389. @table @asis
  390. @item @emph{Description}:
  391. This function returns the number of workers (ie. processing units executing
  392. StarPU tasks). The returned value should be at most @code{STARPU_NMAXWORKERS}.
  393. @item @emph{Prototype}:
  394. @code{unsigned starpu_get_worker_count(void);}
  395. @end table
  396. @node starpu_get_worker_id
  397. @subsection @code{starpu_get_worker_id} -- Get the identifier of the current worker
  398. @table @asis
  399. @item @emph{Description}:
  400. This function returns the identifier of the worker associated to the calling
  401. thread. The returned value is either -1 if the current context is not a StarPU
  402. worker (ie. when called from the application outside a task or a callback), or
  403. an integer between 0 and @code{starpu_get_worker_count() - 1}.
  404. @item @emph{Prototype}:
  405. @code{int starpu_get_worker_count(void);}
  406. @end table
  407. @node starpu_get_worker_type
  408. @subsection @code{starpu_get_worker_type} -- Get the type of processing unit associated to a worker
  409. @table @asis
  410. @item @emph{Description}:
  411. This function returns the type of worker associated to an identifier (as
  412. returned by the @code{starpu_get_worker_id} function). The returned value
  413. indicates the architecture of the worker: @code{STARPU_CPU_WORKER} for a CPU
  414. core, @code{STARPU_CUDA_WORKER} for a CUDA device, and
  415. @code{STARPU_GORDON_WORKER} for a Cell SPU. The value returned for an invalid
  416. identifier is unspecified.
  417. @item @emph{Prototype}:
  418. @code{enum starpu_archtype starpu_get_worker_type(int id);}
  419. @end table
  420. @node starpu_get_worker_name
  421. @subsection @code{starpu_get_worker_name} -- Get the name of a worker
  422. @table @asis
  423. @item @emph{Description}:
  424. StarPU associates a unique human readable string to each processing unit. This
  425. function copies at most the @code{maxlen} first bytes of the unique string
  426. associated to a worker identified by its identifier @code{id} into the
  427. @code{dst} buffer. The caller is responsible for ensuring that the @code{dst}
  428. is a valid pointer to a buffer of @code{maxlen} bytes at least. Calling this
  429. function on an invalid identifier results in an unspecified behaviour.
  430. @item @emph{Prototype}:
  431. @code{void starpu_get_worker_name(int id, char *dst, size_t maxlen);}
  432. @end table
  433. @node Data Library
  434. @section Data Library
  435. @c data_handle_t
  436. @c void starpu_delete_data(struct starpu_data_state_t *state);
  437. @c user interaction with the DSM
  438. @c void starpu_sync_data_with_mem(struct starpu_data_state_t *state);
  439. @c void starpu_notify_data_modification(struct starpu_data_state_t *state, uint32_t modifying_node);
  440. @node Codelets and Tasks
  441. @section Codelets and Tasks
  442. @menu
  443. * struct starpu_codelet:: StarPU codelet structure
  444. * struct starpu_task:: StarPU task structure
  445. * starpu_task_init:: Initialize a Task
  446. * starpu_task_create:: Allocate and Initialize a Task
  447. * starpu_task_deinit:: Release all the resources used by a Task
  448. * starpu_task_destroy:: Destroy a dynamically allocated Task
  449. * starpu_submit_task:: Submit a Task
  450. * starpu_wait_task:: Wait for the termination of a Task
  451. * starpu_wait_all_tasks:: Wait for the termination of all Tasks
  452. @end menu
  453. @node struct starpu_codelet
  454. @subsection @code{struct starpu_codelet} -- StarPU codelet structure
  455. @table @asis
  456. @item @emph{Description}:
  457. The codelet structure describes a kernel that is possibly implemented on
  458. various targets.
  459. @item @emph{Fields}:
  460. @table @asis
  461. @item @code{where}:
  462. Indicates which types of processing units are able to execute that codelet.
  463. @code{STARPU_CPU|STARPU_CUDA} for instance indicates that the codelet is
  464. implemented for both CPU cores and CUDA devices while @code{STARPU_GORDON}
  465. indicates that it is only available on Cell SPUs.
  466. @item @code{cpu_func} (optionnal):
  467. Is a function pointer to the CPU implementation of the codelet. Its prototype
  468. must be: @code{void cpu_func(void *buffers[], void *cl_arg)}. The
  469. first argument being the array of data managed by the data management library,
  470. and the second argument is a pointer to the argument (possibly a copy of it)
  471. passed from the @code{.cl_arg} field of the @code{starpu_task} structure. This
  472. pointer is ignored if @code{STARPU_CPU} does not appear in the @code{.where} field,
  473. it must be non-null otherwise.
  474. @item @code{cuda_func} (optionnal):
  475. Is a function pointer to the CUDA implementation of the codelet. @emph{This
  476. must be a host-function written in the CUDA runtime API}. Its prototype must
  477. be: @code{void cuda_func(void *buffers[], void *cl_arg);}. This
  478. pointer is ignored if @code{STARPU_CUDA} does not appear in the @code{.where}
  479. field, it must be non-null otherwise.
  480. @item @code{gordon_func} (optionnal):
  481. This is the index of the Cell SPU implementation within the Gordon library.
  482. TODO
  483. @item @code{nbuffers}:
  484. Specifies the number of arguments taken by the codelet. These arguments are
  485. managed by the DSM and are accessed from the @code{void *buffers[]}
  486. array. The constant argument passed with the @code{.cl_arg} field of the
  487. @code{starpu_task} structure is not counted in this number. This value should
  488. not be above @code{STARPU_NMAXBUFS}.
  489. @item @code{model} (optionnal):
  490. This is a pointer to the performance model associated to this codelet. This
  491. optionnal field is ignored when null. TODO
  492. @end table
  493. @end table
  494. @node struct starpu_task
  495. @subsection @code{struct starpu_task} -- StarPU task structure
  496. @table @asis
  497. @item @emph{Description}:
  498. The starpu_task structure describes a task that can be offloaded on the various
  499. processing units managed by StarPU. It instanciates a codelet. It can either be
  500. allocated dynamically with the @code{starpu_task_create} method, or declared
  501. statically. In the latter case, the programmer has to zero the
  502. @code{starpu_task} structure and to fill the different fields properly. The
  503. indicated default values correspond to the configuration of a task allocated
  504. with @code{starpu_task_create}.
  505. @item @emph{Fields}:
  506. @table @asis
  507. @item @code{cl}:
  508. Is a pointer to the corresponding @code{starpu_codelet} data structure. This
  509. describes where the kernel should be executed, and supplies the appropriate
  510. implementations. When set to @code{NULL}, no code is executed during the tasks,
  511. such empty tasks can be useful for synchronization purposes.
  512. @item @code{buffers}:
  513. TODO
  514. @item @code{cl_arg} (optional) (default = NULL):
  515. TODO
  516. @item @code{cl_arg_size} (optional):
  517. TODO
  518. @c ignored if only executable on CPUs or CUDA ...
  519. @item @code{callback_func} (optional) (default = @code{NULL}):
  520. This is a function pointer of prototype @code{void (*f)(void *)} which
  521. specifies a possible callback. If that pointer is non-null, the callback
  522. function is executed @emph{on the host} after the execution of the task. The
  523. callback is passed the value contained in the @code{callback_arg} field. No
  524. callback is executed if that field is null.
  525. @item @code{callback_arg} (optional) (default = @code{NULL}):
  526. This is the pointer passed to the callback function. This field is ignored if
  527. the @code{callback_func} is null.
  528. @item @code{use_tag} (optional) (default = 0):
  529. If set, this flag indicates that the task should be associated with the tag
  530. conained in the @code{tag_id} field. Tag allow the application to synchronize
  531. with the task and to express task dependencies easily.
  532. @item @code{tag_id}:
  533. This fields contains the tag associated to the tag if the @code{use_tag} field
  534. was set, it is ignored otherwise.
  535. @item @code{synchronous}:
  536. If this flag is set, the @code{starpu_submit_task} function is blocking and
  537. returns only when the task has been executed (or if no worker is able to
  538. process the task). Otherwise, @code{starpu_submit_task} returns immediately.
  539. @item @code{priority} (optionnal) (default = @code{DEFAULT_PRIO}):
  540. This field indicates a level of priority for the task. This is an integer value
  541. that must be selected between @code{MIN_PRIO} (for the least important tasks)
  542. and @code{MAX_PRIO} (for the most important tasks) included. Default priority
  543. is @code{DEFAULT_PRIO}. Scheduling strategies that take priorities into
  544. account can use this parameter to take better scheduling decisions, but the
  545. scheduling policy may also ignore it.
  546. @item @code{execute_on_a_specific_worker} (default = 0):
  547. If this flag is set, StarPU will bypass the scheduler and directly affect this
  548. task to the worker specified by the @code{workerid} field.
  549. @item @code{workerid} (optional):
  550. If the @code{execute_on_a_specific_worker} field is set, this field indicates
  551. which is the identifier of the worker that should process this task (as
  552. returned by @code{starpu_get_worker_id}). This field is ignored if
  553. @code{execute_on_a_specific_worker} field is set to 0.
  554. @item @code{detach} (optional) (default = 1):
  555. If this flag is set, it is not possible to synchronize with the task
  556. by the means of @code{starpu_wait_task} later on. Internal data structures
  557. are only garanteed to be liberated once @code{starpu_wait_task} is called
  558. if that flag is not set.
  559. @item @code{destroy} (optional) (default = 1):
  560. If that flag is set, the task structure will automatically be liberated, either
  561. after the execution of the callback if the task is detached, or during
  562. @code{starpu_task_wait} otherwise. If this flag is not set, dynamically allocated data
  563. structures will not be liberated until @code{starpu_task_destroy} is called
  564. explicitely. Setting this flag for a statically allocated task structure will
  565. result in undefined behaviour.
  566. @end table
  567. @end table
  568. @node starpu_task_init
  569. @subsection @code{starpu_task_init} -- Initialize a Task
  570. @table @asis
  571. @item @emph{Description}:
  572. Initialize a task structure with default values. This function is implicitely
  573. called by @code{starpu_task_create}. By default, tasks initialized with
  574. @code{starpu_task_init} must be deinitialized explicitely with
  575. @code{starpu_task_deinit}.
  576. @item @emph{Prototype}:
  577. @code{void starpu_task_init(struct starpu_task *task);}
  578. @end table
  579. @node starpu_task_create
  580. @subsection @code{starpu_task_create} -- Allocate and Initialize a Task
  581. @table @asis
  582. @item @emph{Description}:
  583. Allocate a task structure and initialize it with default values. Tasks
  584. allocated dynamically with starpu_task_create are automatically liberated when
  585. the task is terminated. If the destroy flag is explicitely unset, the
  586. ressources used by the task are liberated by calling
  587. @code{starpu_task_destroy}.
  588. @item @emph{Prototype}:
  589. @code{struct starpu_task *starpu_task_create(void);}
  590. @end table
  591. @node starpu_task_deinit
  592. @subsection @code{starpu_task_deinit} -- Release all the resources used by a Task
  593. @table @asis
  594. @item @emph{Description}:
  595. Release all the structures automatically allocated to execute the task. This is
  596. called implicitely by starpu_task_destroy, but the task structure itself is not
  597. liberated. This should be used for statically allocated tasks for instance.
  598. Note that this function is automatically called by @code{starpu_task_destroy}.
  599. @item @emph{Prototype}:
  600. @code{void starpu_task_deinit(struct starpu_task *task);}
  601. @end table
  602. @node starpu_task_destroy
  603. @subsection @code{starpu_task_destroy} -- Destroy a dynamically allocated Task
  604. @table @asis
  605. @item @emph{Description}:
  606. Liberate the ressource allocated during starpu_task_create. This function can
  607. be called automatically after the execution of a task by setting the
  608. @code{.destroy} flag of the @code{starpu_task} structure (default behaviour).
  609. Calling this function on a statically allocated task results in an undefined
  610. behaviour.
  611. @item @emph{Prototype}:
  612. @code{void starpu_task_destroy(struct starpu_task *task);}
  613. @end table
  614. @node starpu_wait_task
  615. @subsection @code{starpu_wait_task} -- Wait for the termination of a Task
  616. @table @asis
  617. @item @emph{Description}:
  618. This function blocks until the task was executed. It is not possible to
  619. synchronize with a task more than once. It is not possible to wait
  620. synchronous or detached tasks.
  621. @item @emph{Return value}:
  622. Upon successful completion, this function returns 0. Otherwise, @code{-EINVAL}
  623. indicates that the waited task was either synchronous or detached.
  624. @item @emph{Prototype}:
  625. @code{int starpu_wait_task(struct starpu_task *task);}
  626. @end table
  627. @node starpu_submit_task
  628. @subsection @code{starpu_submit_task} -- Submit a Task
  629. @table @asis
  630. @item @emph{Description}:
  631. This function submits task @code{task} to StarPU. Calling this function does
  632. not mean that the task will be executed immediatly as there can be data or task
  633. (tag) dependencies that are not fulfilled yet: StarPU will take care to
  634. schedule this task with respect to such dependencies.
  635. This function returns immediately if the @code{synchronous} field of the
  636. @code{starpu_task} structure was set to 0, and block until the termination of
  637. the task otherwise. It is also possible to synchronize the application with
  638. asynchronous tasks by the means of tags, using the @code{starpu_tag_wait}
  639. function for instance.
  640. In case of success, this function returns 0, a return value of @code{-ENODEV}
  641. means that there is no worker able to process that task (eg. there is no GPU
  642. available and this task is only implemented on top of CUDA).
  643. @item @emph{Prototype}:
  644. @code{int starpu_submit_task(struct starpu_task *task);}
  645. @end table
  646. @node starpu_wait_all_tasks
  647. @subsection @code{starpu_wait_all_tasks} -- Wait for the termination of all Tasks
  648. @table @asis
  649. @item @emph{Description}:
  650. This function blocks until all the tasks that were submitted are terminated.
  651. @item @emph{Prototype}:
  652. @code{void starpu_wait_all_tasks(void);}
  653. @end table
  654. @c Callbacks : what can we put in callbacks ?
  655. @node Tags
  656. @section Tags
  657. @menu
  658. * starpu_tag_t:: Task identifier
  659. * starpu_tag_declare_deps:: Declare the Dependencies of a Tag
  660. * starpu_tag_declare_deps_array:: Declare the Dependencies of a Tag
  661. * starpu_tag_wait:: Block until a Tag is terminated
  662. * starpu_tag_wait_array:: Block until a set of Tags is terminated
  663. * starpu_tag_remove:: Destroy a Tag
  664. * starpu_tag_notify_from_apps:: Feed a tag explicitely
  665. @end menu
  666. @node starpu_tag_t
  667. @subsection @code{starpu_tag_t} -- Task identifier
  668. @table @asis
  669. @item @emph{Description}:
  670. It is possible to associate a task with a unique "tag" and to express
  671. dependencies between tasks by the means of those tags. To do so, fill the
  672. @code{tag_id} field of the @code{starpu_task} structure with a tag number (can
  673. be arbitrary) and set the @code{use_tag} field to 1.
  674. If @code{starpu_tag_declare_deps} is called with that tag number, the task will
  675. not be started until the task which wears the declared dependency tags are
  676. complete.
  677. @end table
  678. @node starpu_tag_declare_deps
  679. @subsection @code{starpu_tag_declare_deps} -- Declare the Dependencies of a Tag
  680. @table @asis
  681. @item @emph{Description}:
  682. Specify the dependencies of the task identified by tag @code{id}. The first
  683. argument specifies the tag which is configured, the second argument gives the
  684. number of tag(s) on which @code{id} depends. The following arguments are the
  685. tags which have to terminated to unlock the task.
  686. This function must be called before the associated task is submitted to StarPU
  687. with @code{starpu_submit_task}.
  688. @item @emph{Remark}
  689. Because of the variable arity of @code{starpu_tag_declare_deps}, note that the
  690. last arguments @emph{must} be of type @code{starpu_tag_t}: constant values
  691. typically need to be explicitely casted. Using the
  692. @code{starpu_tag_declare_deps_array} function avoids this hazard.
  693. @item @emph{Prototype}:
  694. @code{void starpu_tag_declare_deps(starpu_tag_t id, unsigned ndeps, ...);}
  695. @item @emph{Example}:
  696. @example
  697. @c @cartouche
  698. /* Tag 0x1 depends on tags 0x32 and 0x52 */
  699. starpu_tag_declare_deps((starpu_tag_t)0x1,
  700. 2, (starpu_tag_t)0x32, (starpu_tag_t)0x52);
  701. @c @end cartouche
  702. @end example
  703. @end table
  704. @node starpu_tag_declare_deps_array
  705. @subsection @code{starpu_tag_declare_deps_array} -- Declare the Dependencies of a Tag
  706. @table @asis
  707. @item @emph{Description}:
  708. This function is similar to @code{starpu_tag_declare_deps}, except that its
  709. does not take a variable number of arguments but an array of tags of size
  710. @code{ndeps}.
  711. @item @emph{Prototype}:
  712. @code{void starpu_tag_declare_deps_array(starpu_tag_t id, unsigned ndeps, starpu_tag_t *array);}
  713. @item @emph{Example}:
  714. @example
  715. @c @cartouche
  716. /* Tag 0x1 depends on tags 0x32 and 0x52 */
  717. starpu_tag_t tag_array[2] = @{0x32, 0x52@};
  718. starpu_tag_declare_deps((starpu_tag_t)0x1, 2, tag_array);
  719. @c @end cartouche
  720. @end example
  721. @end table
  722. @node starpu_tag_wait
  723. @subsection @code{starpu_tag_wait} -- Block until a Tag is terminated
  724. @table @asis
  725. @item @emph{Description}:
  726. This function blocks until the task associated to tag @code{id} has been
  727. executed. This is a blocking call which must therefore not be called within
  728. tasks or callbacks, but only from the application directly. It is possible to
  729. synchronize with the same tag multiple times, as long as the
  730. @code{starpu_tag_remove} function is not called. Note that it is still
  731. possible to synchronize wih a tag associated to a task which @code{starpu_task}
  732. data structure was liberated (eg. if the @code{destroy} flag of the
  733. @code{starpu_task} was enabled).
  734. @item @emph{Prototype}:
  735. @code{void starpu_tag_wait(starpu_tag_t id);}
  736. @end table
  737. @node starpu_tag_wait_array
  738. @subsection @code{starpu_tag_wait_array} -- Block until a set of Tags is terminated
  739. @table @asis
  740. @item @emph{Description}:
  741. This function is similar to @code{starpu_tag_wait} except that it blocks until
  742. @emph{all} the @code{ntags} tags contained in the @code{id} array are
  743. terminated.
  744. @item @emph{Prototype}:
  745. @code{void starpu_tag_wait_array(unsigned ntags, starpu_tag_t *id);}
  746. @end table
  747. @node starpu_tag_remove
  748. @subsection @code{starpu_tag_remove} -- Destroy a Tag
  749. @table @asis
  750. @item @emph{Description}:
  751. This function release the resources associated to tag @code{id}. It can be
  752. called once the corresponding task has been executed and when there is no tag
  753. that depend on that one anymore.
  754. @item @emph{Prototype}:
  755. @code{void starpu_tag_remove(starpu_tag_t id);}
  756. @end table
  757. @node starpu_tag_notify_from_apps
  758. @subsection @code{starpu_tag_notify_from_apps} -- Feed a Tag explicitely
  759. @table @asis
  760. @item @emph{Description}:
  761. This function explicitely unlocks tag @code{id}. It may be useful in the
  762. case of applications which execute part of their computation outside StarPU
  763. tasks (eg. third-party libraries). It is also provided as a
  764. convenient tool for the programmer, for instance to entirely construct the task
  765. DAG before actually giving StarPU the opportunity to execute the tasks.
  766. @item @emph{Prototype}:
  767. @code{void starpu_tag_notify_from_apps(starpu_tag_t id);}
  768. @end table
  769. @node CUDA extensions
  770. @section CUDA extensions
  771. @c void starpu_malloc_pinned_if_possible(float **A, size_t dim);
  772. @c starpu_helper_init_cublas TODO
  773. @c starpu_helper_shutdown_cublas TODO
  774. @menu
  775. * starpu_get_local_cuda_stream:: Get current worker's CUDA stream
  776. * starpu_helper_init_cublas:: Initialize CUBLAS on every CUDA device
  777. * starpu_helper_shutdown_cublas:: Deiitialize CUBLAS on every CUDA device
  778. @end menu
  779. @node starpu_get_local_cuda_stream
  780. @subsection @code{starpu_get_local_cuda_stream} -- Get current worker's CUDA stream
  781. @table @asis
  782. @item @emph{Description}:
  783. StarPU provides a stream for every CUDA device controlled by StarPU. This
  784. function is only provided for convenience so that programmers can easily use
  785. asynchronous operations within codelets without having to create a stream by
  786. hand. Note that the application is not forced to use the stream provided by
  787. @code{starpu_get_local_cuda_stream} and may also create its own streams.
  788. @item @emph{Prototype}:
  789. @code{cudaStream_t *starpu_get_local_cuda_stream(void);}
  790. @end table
  791. @node starpu_helper_init_cublas
  792. @subsection @code{starpu_helper_init_cublas} -- Initialize CUBLAS on every CUDA device
  793. @table @asis
  794. @item @emph{Description}:
  795. The CUBLAS library must be initialized prior to any CUBLAS call. Calling
  796. @code{starpu_helper_init_cublas} will initialize CUBLAS on every CUDA device
  797. controlled by StarPU. This call blocks until CUBLAS has been properly
  798. initialized on every device.
  799. @item @emph{Prototype}:
  800. @code{void starpu_helper_init_cublas(void);}
  801. @end table
  802. @node starpu_helper_shutdown_cublas
  803. @subsection @code{starpu_helper_shutdown_cublas} -- Deinitialize CUBLAS on every CUDA device
  804. @table @asis
  805. @item @emph{Description}:
  806. This function synchronously deinitializes the CUBLAS library on every CUDA device.
  807. @item @emph{Prototype}:
  808. @code{void starpu_helper_shutdown_cublas(void);}
  809. @end table
  810. @node Cell extensions
  811. @section Cell extensions
  812. @c ---------------------------------------------------------------------
  813. @c Basic Examples
  814. @c ---------------------------------------------------------------------
  815. @node Basic Examples
  816. @chapter Basic Examples
  817. @menu
  818. * Compiling and linking:: Compiling and Linking Options
  819. * Hello World:: Submitting Tasks
  820. * Scaling a Vector:: Manipulating Data
  821. * Scaling a Vector (hybrid):: Handling Heterogeneous Architectures
  822. @end menu
  823. @node Compiling and linking
  824. @section Compiling and linking options
  825. The Makefile could for instance contain the following lines to define which
  826. options must be given to the compiler and to the linker:
  827. @example
  828. @c @cartouche
  829. CFLAGS+=$$(pkg-config --cflags libstarpu)
  830. LIBS+=$$(pkg-config --libs libstarpu)
  831. @c @end cartouche
  832. @end example
  833. @node Hello World
  834. @section Hello World
  835. In this section, we show how to implement a simple program that submits a task to StarPU.
  836. @subsection Required Headers
  837. The @code{starpu.h} header should be included in any code using StarPU.
  838. @example
  839. @c @cartouche
  840. #include <starpu.h>
  841. @c @end cartouche
  842. @end example
  843. @subsection Defining a Codelet
  844. @example
  845. @c @cartouche
  846. void cpu_func(void *buffers[], void *cl_arg)
  847. @{
  848. float *array = cl_arg;
  849. printf("Hello world (array = @{%f, %f@} )\n", array[0], array[1]);
  850. @}
  851. starpu_codelet cl =
  852. @{
  853. .where = STARPU_CPU,
  854. .cpu_func = cpu_func,
  855. .nbuffers = 0
  856. @};
  857. @c @end cartouche
  858. @end example
  859. A codelet is a structure that represents a computational kernel. Such a codelet
  860. may contain an implementation of the same kernel on different architectures
  861. (eg. CUDA, Cell's SPU, x86, ...).
  862. The ''@code{.nbuffers}'' field specifies the number of data buffers that are
  863. manipulated by the codelet: here the codelet does not access or modify any data
  864. that is controlled by our data management library. Note that the argument
  865. passed to the codelet (the ''@code{.cl_arg}'' field of the @code{starpu_task}
  866. structure) does not count as a buffer since it is not managed by our data
  867. management library.
  868. @c TODO need a crossref to the proper description of "where" see bla for more ...
  869. We create a codelet which may only be executed on the CPUs. The ''@code{.where}''
  870. field is a bitmask that defines where the codelet may be executed. Here, the
  871. @code{STARPU_CPU} value means that only CPUs can execute this codelet
  872. (@pxref{Codelets and Tasks} for more details on that field).
  873. When a CPU core executes a codelet, it calls the @code{.cpu_func} function,
  874. which @emph{must} have the following prototype:
  875. @code{void (*cpu_func)(void *buffers[], void *cl_arg)}
  876. In this example, we can ignore the first argument of this function which gives a
  877. description of the input and output buffers (eg. the size and the location of
  878. the matrices). The second argument is a pointer to a buffer passed as an
  879. argument to the codelet by the means of the ''@code{.cl_arg}'' field of the
  880. @code{starpu_task} structure.
  881. @c TODO rewrite so that it is a little clearer ?
  882. Be aware that this may be a pointer to a
  883. @emph{copy} of the actual buffer, and not the pointer given by the programmer:
  884. if the codelet modifies this buffer, there is no garantee that the initial
  885. buffer will be modified as well: this for instance implies that the buffer
  886. cannot be used as a synchronization medium.
  887. @subsection Submitting a Task
  888. @example
  889. @c @cartouche
  890. void callback_func(void *callback_arg)
  891. @{
  892. printf("Callback function (arg %x)\n", callback_arg);
  893. @}
  894. int main(int argc, char **argv)
  895. @{
  896. /* initialize StarPU */
  897. starpu_init(NULL);
  898. struct starpu_task *task = starpu_task_create();
  899. task->cl = &cl;
  900. float *array[2] = @{1.0f, -1.0f@};
  901. task->cl_arg = &array;
  902. task->cl_arg_size = 2*sizeof(float);
  903. task->callback_func = callback_func;
  904. task->callback_arg = 0x42;
  905. /* starpu_submit_task will be a blocking call */
  906. task->synchronous = 1;
  907. /* submit the task to StarPU */
  908. starpu_submit_task(task);
  909. /* terminate StarPU */
  910. starpu_shutdown();
  911. return 0;
  912. @}
  913. @c @end cartouche
  914. @end example
  915. Before submitting any tasks to StarPU, @code{starpu_init} must be called. The
  916. @code{NULL} argument specifies that we use default configuration. Tasks cannot
  917. be submitted after the termination of StarPU by a call to
  918. @code{starpu_shutdown}.
  919. In the example above, a task structure is allocated by a call to
  920. @code{starpu_task_create}. This function only allocates and fills the
  921. corresponding structure with the default settings (@pxref{starpu_task_create}),
  922. but it does not submit the task to StarPU.
  923. @c not really clear ;)
  924. The ''@code{.cl}'' field is a pointer to the codelet which the task will
  925. execute: in other words, the codelet structure describes which computational
  926. kernel should be offloaded on the different architectures, and the task
  927. structure is a wrapper containing a codelet and the piece of data on which the
  928. codelet should operate.
  929. The optional ''@code{.cl_arg}'' field is a pointer to a buffer (of size
  930. @code{.cl_arg_size}) with some parameters for the kernel
  931. described by the codelet. For instance, if a codelet implements a computational
  932. kernel that multiplies its input vector by a constant, the constant could be
  933. specified by the means of this buffer.
  934. Once a task has been executed, an optional callback function can be called.
  935. While the computational kernel could be offloaded on various architectures, the
  936. callback function is always executed on a CPU. The ''@code{.callback_arg}''
  937. pointer is passed as an argument of the callback. The prototype of a callback
  938. function must be:
  939. @example
  940. void (*callback_function)(void *);
  941. @end example
  942. If the @code{.synchronous} field is non-null, task submission will be
  943. synchronous: the @code{starpu_submit_task} function will not return until the
  944. task was executed. Note that the @code{starpu_shutdown} method does not
  945. guarantee that asynchronous tasks have been executed before it returns.
  946. @node Scaling a Vector
  947. @section Manipulating Data: Scaling a Vector
  948. The previous example has shown how to submit tasks. In this section we show how
  949. StarPU tasks can manipulate data.
  950. Programmers can describe the data layout of their application so that StarPU is
  951. responsible for enforcing data coherency and availability accross the machine.
  952. Instead of handling complex (and non-portable) mechanisms to perform data
  953. movements, programmers only declare which piece of data is accessed and/or
  954. modified by a task, and StarPU makes sure that when a computational kernel
  955. starts somewhere (eg. on a GPU), its data are available locally.
  956. Before submitting those tasks, the programmer first needs to declare the
  957. different pieces of data to StarPU using the @code{starpu_register_*_data}
  958. functions. To ease the development of applications for StarPU, it is possible
  959. to describe multiple types of data layout. A type of data layout is called an
  960. @b{interface}. By default, there are different interfaces available in StarPU:
  961. here we will consider the @b{vector interface}.
  962. The following lines show how to declare an array of @code{n} elements of type
  963. @code{float} using the vector interface:
  964. @example
  965. float tab[n];
  966. starpu_data_handle tab_handle;
  967. starpu_register_vector_data(&tab_handle, 0, tab, n, sizeof(float));
  968. @end example
  969. The first argument, called the @b{data handle}, is an opaque pointer which
  970. designates the array in StarPU. This is also the structure which is used to
  971. describe which data is used by a task.
  972. @c TODO: what is 0 ?
  973. It is possible to construct a StarPU
  974. task that multiplies this vector by a constant factor:
  975. @example
  976. float factor;
  977. struct starpu_task *task = starpu_task_create();
  978. task->cl = &cl;
  979. task->buffers[0].handle = tab_handle;
  980. task->buffers[0].mode = STARPU_RW;
  981. task->cl_arg = &factor;
  982. task->cl_arg_size = sizeof(float);
  983. @end example
  984. Since the factor is constant, it does not need a preliminary declaration, and
  985. can just be passed through the @code{cl_arg} pointer like in the previous
  986. example. The vector parameter is described by its handle.
  987. There are two fields in each element of the @code{buffers} array.
  988. @code{.handle} is the handle of the data, and @code{.mode} specifies how the
  989. kernel will access the data (@code{STARPU_R} for read-only, @code{STARPU_W} for
  990. write-only and @code{STARPU_RW} for read and write access).
  991. The definition of the codelet can be written as follows:
  992. @example
  993. void scal_func(void *buffers[], void *cl_arg)
  994. @{
  995. unsigned i;
  996. float *factor = cl_arg;
  997. struct starpu_vector_interface_s *vector = buffers[0];
  998. /* length of the vector */
  999. unsigned n = vector->nx;
  1000. /* local copy of the vector pointer */
  1001. float *val = (float *)vector->ptr;
  1002. for (i = 0; i < n; i++)
  1003. val[i] *= *factor;
  1004. @}
  1005. starpu_codelet cl = @{
  1006. .where = STARPU_CPU,
  1007. .cpu_func = scal_func,
  1008. .nbuffers = 1
  1009. @};
  1010. @end example
  1011. The second argument of the @code{scal_func} function contains a pointer to the
  1012. parameters of the codelet (given in @code{task->cl_arg}), so that we read the
  1013. constant factor from this pointer. The first argument is an array that gives
  1014. a description of every buffers passed in the @code{task->buffers}@ array. The
  1015. size of this array is given by the @code{.nbuffers} field of the codelet
  1016. structure. For the sake of generality, this array contains pointers to the
  1017. different interfaces describing each buffer. In the case of the @b{vector
  1018. interface}, the location of the vector (resp. its length) is accessible in the
  1019. @code{ptr} (resp. @code{nx}) of this array. Since the vector is accessed in a
  1020. read-write fashion, any modification will automatically affect future accesses
  1021. to that vector made by other tasks.
  1022. @node Scaling a Vector (hybrid)
  1023. @section Vector Scaling on an Hybrid CPU/GPU Machine
  1024. Contrary to the previous examples, the task submitted in the example may not
  1025. only be executed by the CPUs, but also by a CUDA device.
  1026. TODO
  1027. @c ---------------------------------------------------------------------
  1028. @c Advanced Topics
  1029. @c ---------------------------------------------------------------------
  1030. @node Advanced Topics
  1031. @chapter Advanced Topics
  1032. @bye