starpu.texi 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457
  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 (e.g. 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. (i.e. 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 (i.e. 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. * Miscellaneous:: Miscellaneous helpers
  320. @end menu
  321. @node Initialization and Termination
  322. @section Initialization and Termination
  323. @menu
  324. * starpu_init:: Initialize StarPU
  325. * struct starpu_conf:: StarPU runtime configuration
  326. * starpu_shutdown:: Terminate StarPU
  327. @end menu
  328. @node starpu_init
  329. @subsection @code{starpu_init} -- Initialize StarPU
  330. @table @asis
  331. @item @emph{Description}:
  332. This is StarPU initialization method, which must be called prior to any other
  333. StarPU call. It is possible to specify StarPU's configuration (e.g. scheduling
  334. policy, number of cores, ...) by passing a non-null argument. Default
  335. configuration is used if the passed argument is @code{NULL}.
  336. @item @emph{Return value}:
  337. Upon successful completion, this function returns 0. Otherwise, @code{-ENODEV}
  338. indicates that no worker was available (so that StarPU was not be initialized).
  339. @item @emph{Prototype}:
  340. @code{int starpu_init(struct starpu_conf *conf);}
  341. @end table
  342. @node struct starpu_conf
  343. @subsection @code{struct starpu_conf} -- StarPU runtime configuration
  344. @table @asis
  345. @item @emph{Description}:
  346. This structure is passed to the @code{starpu_init} function in order configure
  347. StarPU. When the default value is used, StarPU automatically select the number
  348. of processing units and takes the default scheduling policy. This parameters
  349. overwrite the equivalent environment variables.
  350. @item @emph{Fields}:
  351. @table @asis
  352. @item @code{sched_policy} (default = NULL):
  353. This is the name of the scheduling policy. This can also be specified with the
  354. @code{STARPU_SCHED} environment variable.
  355. @item @code{ncpus} (default = -1):
  356. This is the maximum number of CPU cores that StarPU can use. This can also be
  357. specified with the @code{STARPU_NCPUS} environment variable.
  358. @item @code{ncuda} (default = -1):
  359. This is the maximum number of CUDA devices that StarPU can use. This can also be
  360. specified with the @code{STARPU_NCUDA} environment variable.
  361. @item @code{nopencl} (default = -1):
  362. This is the maximum number of OpenCL devices that StarPU can use. This can also be
  363. specified with the @code{STARPU_NOPENCL} environment variable.
  364. @item @code{nspus} (default = -1):
  365. This is the maximum number of Cell SPUs that StarPU can use. This can also be
  366. specified with the @code{STARPU_NGORDON} environment variable.
  367. @item @code{calibrate} (default = 0):
  368. If this flag is set, StarPU will calibrate the performance models when
  369. executing tasks. This can also be specified with the @code{STARPU_CALIBRATE}
  370. environment variable.
  371. @end table
  372. @end table
  373. @node starpu_shutdown
  374. @subsection @code{starpu_shutdown} -- Terminate StarPU
  375. @table @asis
  376. @item @emph{Description}:
  377. This is StarPU termination method. It must be called at the end of the
  378. application: statistics and other post-mortem debugging information are not
  379. guaranteed to be available until this method has been called.
  380. @item @emph{Prototype}:
  381. @code{void starpu_shutdown(void);}
  382. @end table
  383. @node Workers' Properties
  384. @section Workers' Properties
  385. @menu
  386. * starpu_worker_get_count:: Get the number of processing units
  387. * starpu_cpu_worker_get_count:: Get the number of CPU controlled by StarPU
  388. * starpu_cuda_worker_get_count:: Get the number of CUDA devices controlled by StarPU
  389. * starpu_opencl_worker_get_count:: Get the number of OpenCL devices controlled by StarPU
  390. * starpu_spu_worker_get_count:: Get the number of Cell SPUs controlled by StarPU
  391. * starpu_worker_get_id:: Get the identifier of the current worker
  392. * starpu_worker_get_type:: Get the type of processing unit associated to a worker
  393. * starpu_worker_get_name:: Get the name of a worker
  394. @end menu
  395. @node starpu_worker_get_count
  396. @subsection @code{starpu_worker_get_count} -- Get the number of processing units
  397. @table @asis
  398. @item @emph{Description}:
  399. This function returns the number of workers (i.e. processing units executing
  400. StarPU tasks). The returned value should be at most @code{STARPU_NMAXWORKERS}.
  401. @item @emph{Prototype}:
  402. @code{unsigned starpu_worker_get_count(void);}
  403. @end table
  404. @node starpu_cpu_worker_get_count
  405. @subsection @code{starpu_cpu_worker_get_count} -- Get the number of CPU controlled by StarPU
  406. @table @asis
  407. @item @emph{Description}:
  408. This function returns the number of CPUs controlled by StarPU. The returned
  409. value should be at most @code{STARPU_NMAXCPUS}.
  410. @item @emph{Prototype}:
  411. @code{unsigned starpu_cpu_worker_get_count(void);}
  412. @end table
  413. @node starpu_cuda_worker_get_count
  414. @subsection @code{starpu_cuda_worker_get_count} -- Get the number of CUDA devices controlled by StarPU
  415. @table @asis
  416. @item @emph{Description}:
  417. This function returns the number of CUDA devices controlled by StarPU. The returned
  418. value should be at most @code{STARPU_MAXCUDADEVS}.
  419. @item @emph{Prototype}:
  420. @code{unsigned starpu_cuda_worker_get_count(void);}
  421. @end table
  422. @node starpu_opencl_worker_get_count
  423. @subsection @code{starpu_opencl_worker_get_count} -- Get the number of OpenCL devices controlled by StarPU
  424. @table @asis
  425. @item @emph{Description}:
  426. This function returns the number of OpenCL devices controlled by StarPU. The returned
  427. value should be at most @code{STARPU_MAXOPENCLDEVS}.
  428. @item @emph{Prototype}:
  429. @code{unsigned starpu_opencl_worker_get_count(void);}
  430. @end table
  431. @node starpu_spu_worker_get_count
  432. @subsection @code{starpu_spu_worker_get_count} -- Get the number of Cell SPUs controlled by StarPU
  433. @table @asis
  434. @item @emph{Description}:
  435. This function returns the number of Cell SPUs controlled by StarPU.
  436. @item @emph{Prototype}:
  437. @code{unsigned starpu_opencl_worker_get_count(void);}
  438. @end table
  439. @node starpu_worker_get_id
  440. @subsection @code{starpu_worker_get_id} -- Get the identifier of the current worker
  441. @table @asis
  442. @item @emph{Description}:
  443. This function returns the identifier of the worker associated to the calling
  444. thread. The returned value is either -1 if the current context is not a StarPU
  445. worker (i.e. when called from the application outside a task or a callback), or
  446. an integer between 0 and @code{starpu_worker_get_count() - 1}.
  447. @item @emph{Prototype}:
  448. @code{int starpu_worker_get_id(void);}
  449. @end table
  450. @node starpu_worker_get_type
  451. @subsection @code{starpu_worker_get_type} -- Get the type of processing unit associated to a worker
  452. @table @asis
  453. @item @emph{Description}:
  454. This function returns the type of worker associated to an identifier (as
  455. returned by the @code{starpu_worker_get_id} function). The returned value
  456. indicates the architecture of the worker: @code{STARPU_CPU_WORKER} for a CPU
  457. core, @code{STARPU_CUDA_WORKER} for a CUDA device,
  458. @code{STARPU_OPENCL_WORKER} for a OpenCL device, and
  459. @code{STARPU_GORDON_WORKER} for a Cell SPU. The value returned for an invalid
  460. identifier is unspecified.
  461. @item @emph{Prototype}:
  462. @code{enum starpu_archtype starpu_worker_get_type(int id);}
  463. @end table
  464. @node starpu_worker_get_name
  465. @subsection @code{starpu_worker_get_name} -- Get the name of a worker
  466. @table @asis
  467. @item @emph{Description}:
  468. StarPU associates a unique human readable string to each processing unit. This
  469. function copies at most the @code{maxlen} first bytes of the unique string
  470. associated to a worker identified by its identifier @code{id} into the
  471. @code{dst} buffer. The caller is responsible for ensuring that the @code{dst}
  472. is a valid pointer to a buffer of @code{maxlen} bytes at least. Calling this
  473. function on an invalid identifier results in an unspecified behaviour.
  474. @item @emph{Prototype}:
  475. @code{void starpu_worker_get_name(int id, char *dst, size_t maxlen);}
  476. @end table
  477. @node Data Library
  478. @section Data Library
  479. This section describes the data management facilities provided by StarPU.
  480. TODO: We show how to use existing data interfaces in [ref], but developers can
  481. design their own data interfaces if required.
  482. @menu
  483. * starpu_data_handle:: StarPU opaque data handle
  484. * void *interface:: StarPU data interface
  485. @end menu
  486. @node starpu_data_handle
  487. @subsection @code{starpu_data_handle} -- StarPU opaque data handle
  488. @table @asis
  489. @item @emph{Description}:
  490. StarPU uses @code{starpu_data_handle} as an opaque handle to manage a piece of
  491. data. Once a piece of data has been registered to StarPU, it is associated to a
  492. @code{starpu_data_handle} which keeps track of the state of the piece of data
  493. over the entire machine, so that we can maintain data consistency and locate
  494. data replicates for instance.
  495. @end table
  496. @node void *interface
  497. @subsection @code{void *interface} -- StarPU data interface
  498. @table @asis
  499. @item @emph{Description}:
  500. Data management is done at a high-level in StarPU: rather than accessing a mere
  501. list of contiguous buffers, the tasks may manipulate data that are described by
  502. a high-level construct which we call data interface.
  503. TODO
  504. @end table
  505. @c void starpu_data_unregister(struct starpu_data_state_t *state);
  506. @c starpu_worker_get_memory_node TODO
  507. @c
  508. @c user interaction with the DSM
  509. @c void starpu_data_sync_with_mem(struct starpu_data_state_t *state);
  510. @c void starpu_notify_data_modification(struct starpu_data_state_t *state, uint32_t modifying_node);
  511. @node Codelets and Tasks
  512. @section Codelets and Tasks
  513. @menu
  514. * struct starpu_codelet:: StarPU codelet structure
  515. * struct starpu_task:: StarPU task structure
  516. * starpu_task_init:: Initialize a Task
  517. * starpu_task_create:: Allocate and Initialize a Task
  518. * starpu_task_deinit:: Release all the resources used by a Task
  519. * starpu_task_destroy:: Destroy a dynamically allocated Task
  520. * starpu_task_submit:: Submit a Task
  521. * starpu_task_wait:: Wait for the termination of a Task
  522. * starpu_task_wait_for_all:: Wait for the termination of all Tasks
  523. @end menu
  524. @node struct starpu_codelet
  525. @subsection @code{struct starpu_codelet} -- StarPU codelet structure
  526. @table @asis
  527. @item @emph{Description}:
  528. The codelet structure describes a kernel that is possibly implemented on
  529. various targets.
  530. @item @emph{Fields}:
  531. @table @asis
  532. @item @code{where}:
  533. Indicates which types of processing units are able to execute that codelet.
  534. @code{STARPU_CPU|STARPU_CUDA} for instance indicates that the codelet is
  535. implemented for both CPU cores and CUDA devices while @code{STARPU_GORDON}
  536. indicates that it is only available on Cell SPUs.
  537. @item @code{cpu_func} (optional):
  538. Is a function pointer to the CPU implementation of the codelet. Its prototype
  539. must be: @code{void cpu_func(void *buffers[], void *cl_arg)}. The first
  540. argument being the array of data managed by the data management library, and
  541. the second argument is a pointer to the argument passed from the @code{.cl_arg}
  542. field of the @code{starpu_task} structure.
  543. The @code{cpu_func} field is ignored if @code{STARPU_CPU} does not appear in
  544. the @code{.where} field, it must be non-null otherwise.
  545. @item @code{cuda_func} (optional):
  546. Is a function pointer to the CUDA implementation of the codelet. @emph{This
  547. must be a host-function written in the CUDA runtime API}. Its prototype must
  548. be: @code{void cuda_func(void *buffers[], void *cl_arg);}. The @code{cuda_func}
  549. field is ignored if @code{STARPU_CUDA} does not appear in the @code{.where}
  550. field, it must be non-null otherwise.
  551. @item @code{opencl_func} (optional):
  552. Is a function pointer to the OpenCL implementation of the codelet. Its
  553. prototype must be:
  554. @code{void opencl_func(starpu_data_interface_t *descr, void *arg);}.
  555. This pointer is ignored if @code{OPENCL} does not appear in the
  556. @code{.where} field, it must be non-null otherwise.
  557. @item @code{gordon_func} (optional):
  558. This is the index of the Cell SPU implementation within the Gordon library.
  559. TODO
  560. @item @code{nbuffers}:
  561. Specifies the number of arguments taken by the codelet. These arguments are
  562. managed by the DSM and are accessed from the @code{void *buffers[]}
  563. array. The constant argument passed with the @code{.cl_arg} field of the
  564. @code{starpu_task} structure is not counted in this number. This value should
  565. not be above @code{STARPU_NMAXBUFS}.
  566. @item @code{model} (optional):
  567. This is a pointer to the performance model associated to this codelet. This
  568. optional field is ignored when null. TODO
  569. @end table
  570. @end table
  571. @node struct starpu_task
  572. @subsection @code{struct starpu_task} -- StarPU task structure
  573. @table @asis
  574. @item @emph{Description}:
  575. The starpu_task structure describes a task that can be offloaded on the various
  576. processing units managed by StarPU. It instantiates a codelet. It can either be
  577. allocated dynamically with the @code{starpu_task_create} method, or declared
  578. statically. In the latter case, the programmer has to zero the
  579. @code{starpu_task} structure and to fill the different fields properly. The
  580. indicated default values correspond to the configuration of a task allocated
  581. with @code{starpu_task_create}.
  582. @item @emph{Fields}:
  583. @table @asis
  584. @item @code{cl}:
  585. Is a pointer to the corresponding @code{starpu_codelet} data structure. This
  586. describes where the kernel should be executed, and supplies the appropriate
  587. implementations. When set to @code{NULL}, no code is executed during the tasks,
  588. such empty tasks can be useful for synchronization purposes.
  589. @item @code{buffers}:
  590. TODO
  591. @item @code{cl_arg} (optional) (default = NULL):
  592. This pointer is passed to the codelet through the second argument
  593. of the codelet implementation (e.g. @code{cpu_func} or @code{cuda_func}).
  594. In the specific case of the Cell processor, see the @code{.cl_arg_size}
  595. argument.
  596. @item @code{cl_arg_size} (optional, Cell specific):
  597. In the case of the Cell processor, the @code{.cl_arg} pointer is not directly
  598. given to the SPU function. A buffer of size @code{cl_arg_size} is allocated on
  599. the SPU. This buffer is then filled with the @code{cl_arg_size} bytes starting
  600. at address @code{cl_arg}. In that case, the argument given to the SPU codelet
  601. is therefore not the @code{.cl_arg} pointer, but the address of the buffer in
  602. local store (LS) instead. This field is ignored for CPU, CUDA and OpenCL
  603. codelets.
  604. @item @code{callback_func} (optional) (default = @code{NULL}):
  605. This is a function pointer of prototype @code{void (*f)(void *)} which
  606. specifies a possible callback. If that pointer is non-null, the callback
  607. function is executed @emph{on the host} after the execution of the task. The
  608. callback is passed the value contained in the @code{callback_arg} field. No
  609. callback is executed if that field is null.
  610. @item @code{callback_arg} (optional) (default = @code{NULL}):
  611. This is the pointer passed to the callback function. This field is ignored if
  612. the @code{callback_func} is null.
  613. @item @code{use_tag} (optional) (default = 0):
  614. If set, this flag indicates that the task should be associated with the tag
  615. contained in the @code{tag_id} field. Tag allow the application to synchronize
  616. with the task and to express task dependencies easily.
  617. @item @code{tag_id}:
  618. This fields contains the tag associated to the tag if the @code{use_tag} field
  619. was set, it is ignored otherwise.
  620. @item @code{synchronous}:
  621. If this flag is set, the @code{starpu_task_submit} function is blocking and
  622. returns only when the task has been executed (or if no worker is able to
  623. process the task). Otherwise, @code{starpu_task_submit} returns immediately.
  624. @item @code{priority} (optional) (default = @code{STARPU_DEFAULT_PRIO}):
  625. This field indicates a level of priority for the task. This is an integer value
  626. that must be selected between @code{STARPU_MIN_PRIO} (for the least important
  627. tasks) and @code{STARPU_MAX_PRIO} (for the most important tasks) included.
  628. Default priority is @code{STARPU_DEFAULT_PRIO}. Scheduling strategies that
  629. take priorities into account can use this parameter to take better scheduling
  630. decisions, but the scheduling policy may also ignore it.
  631. @item @code{execute_on_a_specific_worker} (default = 0):
  632. If this flag is set, StarPU will bypass the scheduler and directly affect this
  633. task to the worker specified by the @code{workerid} field.
  634. @item @code{workerid} (optional):
  635. If the @code{execute_on_a_specific_worker} field is set, this field indicates
  636. which is the identifier of the worker that should process this task (as
  637. returned by @code{starpu_worker_get_id}). This field is ignored if
  638. @code{execute_on_a_specific_worker} field is set to 0.
  639. @item @code{detach} (optional) (default = 1):
  640. If this flag is set, it is not possible to synchronize with the task
  641. by the means of @code{starpu_task_wait} later on. Internal data structures
  642. are only guaranteed to be liberated once @code{starpu_task_wait} is called
  643. if that flag is not set.
  644. @item @code{destroy} (optional) (default = 1):
  645. If that flag is set, the task structure will automatically be liberated, either
  646. after the execution of the callback if the task is detached, or during
  647. @code{starpu_task_wait} otherwise. If this flag is not set, dynamically allocated data
  648. structures will not be liberated until @code{starpu_task_destroy} is called
  649. explicitly. Setting this flag for a statically allocated task structure will
  650. result in undefined behaviour.
  651. @end table
  652. @end table
  653. @node starpu_task_init
  654. @subsection @code{starpu_task_init} -- Initialize a Task
  655. @table @asis
  656. @item @emph{Description}:
  657. Initialize a task structure with default values. This function is implicitly
  658. called by @code{starpu_task_create}. By default, tasks initialized with
  659. @code{starpu_task_init} must be deinitialized explicitly with
  660. @code{starpu_task_deinit}. Tasks can also be initialized statically, using the
  661. constant @code{STARPU_TASK_INITIALIZER}.
  662. @item @emph{Prototype}:
  663. @code{void starpu_task_init(struct starpu_task *task);}
  664. @end table
  665. @node starpu_task_create
  666. @subsection @code{starpu_task_create} -- Allocate and Initialize a Task
  667. @table @asis
  668. @item @emph{Description}:
  669. Allocate a task structure and initialize it with default values. Tasks
  670. allocated dynamically with starpu_task_create are automatically liberated when
  671. the task is terminated. If the destroy flag is explicitly unset, the
  672. resources used by the task are liberated by calling
  673. @code{starpu_task_destroy}.
  674. @item @emph{Prototype}:
  675. @code{struct starpu_task *starpu_task_create(void);}
  676. @end table
  677. @node starpu_task_deinit
  678. @subsection @code{starpu_task_deinit} -- Release all the resources used by a Task
  679. @table @asis
  680. @item @emph{Description}:
  681. Release all the structures automatically allocated to execute the task. This is
  682. called implicitly by starpu_task_destroy, but the task structure itself is not
  683. liberated. This should be used for statically allocated tasks for instance.
  684. Note that this function is automatically called by @code{starpu_task_destroy}.
  685. @item @emph{Prototype}:
  686. @code{void starpu_task_deinit(struct starpu_task *task);}
  687. @end table
  688. @node starpu_task_destroy
  689. @subsection @code{starpu_task_destroy} -- Destroy a dynamically allocated Task
  690. @table @asis
  691. @item @emph{Description}:
  692. Liberate the resource allocated during starpu_task_create. This function can
  693. be called automatically after the execution of a task by setting the
  694. @code{.destroy} flag of the @code{starpu_task} structure (default behaviour).
  695. Calling this function on a statically allocated task results in an undefined
  696. behaviour.
  697. @item @emph{Prototype}:
  698. @code{void starpu_task_destroy(struct starpu_task *task);}
  699. @end table
  700. @node starpu_task_wait
  701. @subsection @code{starpu_task_wait} -- Wait for the termination of a Task
  702. @table @asis
  703. @item @emph{Description}:
  704. This function blocks until the task was executed. It is not possible to
  705. synchronize with a task more than once. It is not possible to wait
  706. synchronous or detached tasks.
  707. @item @emph{Return value}:
  708. Upon successful completion, this function returns 0. Otherwise, @code{-EINVAL}
  709. indicates that the waited task was either synchronous or detached.
  710. @item @emph{Prototype}:
  711. @code{int starpu_task_wait(struct starpu_task *task);}
  712. @end table
  713. @node starpu_task_submit
  714. @subsection @code{starpu_task_submit} -- Submit a Task
  715. @table @asis
  716. @item @emph{Description}:
  717. This function submits task @code{task} to StarPU. Calling this function does
  718. not mean that the task will be executed immediately as there can be data or task
  719. (tag) dependencies that are not fulfilled yet: StarPU will take care to
  720. schedule this task with respect to such dependencies.
  721. This function returns immediately if the @code{synchronous} field of the
  722. @code{starpu_task} structure was set to 0, and block until the termination of
  723. the task otherwise. It is also possible to synchronize the application with
  724. asynchronous tasks by the means of tags, using the @code{starpu_tag_wait}
  725. function for instance.
  726. In case of success, this function returns 0, a return value of @code{-ENODEV}
  727. means that there is no worker able to process that task (e.g. there is no GPU
  728. available and this task is only implemented on top of CUDA).
  729. @item @emph{Prototype}:
  730. @code{int starpu_task_submit(struct starpu_task *task);}
  731. @end table
  732. @node starpu_task_wait_for_all
  733. @subsection @code{starpu_task_wait_for_all} -- Wait for the termination of all Tasks
  734. @table @asis
  735. @item @emph{Description}:
  736. This function blocks until all the tasks that were submitted are terminated.
  737. @item @emph{Prototype}:
  738. @code{void starpu_task_wait_for_all(void);}
  739. @end table
  740. @c Callbacks : what can we put in callbacks ?
  741. @node Tags
  742. @section Tags
  743. @menu
  744. * starpu_tag_t:: Task identifier
  745. * starpu_tag_declare_deps:: Declare the Dependencies of a Tag
  746. * starpu_tag_declare_deps_array:: Declare the Dependencies of a Tag
  747. * starpu_tag_wait:: Block until a Tag is terminated
  748. * starpu_tag_wait_array:: Block until a set of Tags is terminated
  749. * starpu_tag_remove:: Destroy a Tag
  750. * starpu_tag_notify_from_apps:: Feed a tag explicitly
  751. @end menu
  752. @node starpu_tag_t
  753. @subsection @code{starpu_tag_t} -- Task identifier
  754. @table @asis
  755. @item @emph{Description}:
  756. It is possible to associate a task with a unique "tag" and to express
  757. dependencies between tasks by the means of those tags. To do so, fill the
  758. @code{tag_id} field of the @code{starpu_task} structure with a tag number (can
  759. be arbitrary) and set the @code{use_tag} field to 1.
  760. If @code{starpu_tag_declare_deps} is called with that tag number, the task will
  761. not be started until the task which wears the declared dependency tags are
  762. complete.
  763. @end table
  764. @node starpu_tag_declare_deps
  765. @subsection @code{starpu_tag_declare_deps} -- Declare the Dependencies of a Tag
  766. @table @asis
  767. @item @emph{Description}:
  768. Specify the dependencies of the task identified by tag @code{id}. The first
  769. argument specifies the tag which is configured, the second argument gives the
  770. number of tag(s) on which @code{id} depends. The following arguments are the
  771. tags which have to terminated to unlock the task.
  772. This function must be called before the associated task is submitted to StarPU
  773. with @code{starpu_task_submit}.
  774. @item @emph{Remark}
  775. Because of the variable arity of @code{starpu_tag_declare_deps}, note that the
  776. last arguments @emph{must} be of type @code{starpu_tag_t}: constant values
  777. typically need to be explicitly casted. Using the
  778. @code{starpu_tag_declare_deps_array} function avoids this hazard.
  779. @item @emph{Prototype}:
  780. @code{void starpu_tag_declare_deps(starpu_tag_t id, unsigned ndeps, ...);}
  781. @item @emph{Example}:
  782. @example
  783. @c @cartouche
  784. /* Tag 0x1 depends on tags 0x32 and 0x52 */
  785. starpu_tag_declare_deps((starpu_tag_t)0x1,
  786. 2, (starpu_tag_t)0x32, (starpu_tag_t)0x52);
  787. @c @end cartouche
  788. @end example
  789. @end table
  790. @node starpu_tag_declare_deps_array
  791. @subsection @code{starpu_tag_declare_deps_array} -- Declare the Dependencies of a Tag
  792. @table @asis
  793. @item @emph{Description}:
  794. This function is similar to @code{starpu_tag_declare_deps}, except that its
  795. does not take a variable number of arguments but an array of tags of size
  796. @code{ndeps}.
  797. @item @emph{Prototype}:
  798. @code{void starpu_tag_declare_deps_array(starpu_tag_t id, unsigned ndeps, starpu_tag_t *array);}
  799. @item @emph{Example}:
  800. @example
  801. /* Tag 0x1 depends on tags 0x32 and 0x52 */
  802. starpu_tag_t tag_array[2] = @{0x32, 0x52@};
  803. starpu_tag_declare_deps_array((starpu_tag_t)0x1, 2, tag_array);
  804. @end example
  805. @end table
  806. @node starpu_tag_wait
  807. @subsection @code{starpu_tag_wait} -- Block until a Tag is terminated
  808. @table @asis
  809. @item @emph{Description}:
  810. This function blocks until the task associated to tag @code{id} has been
  811. executed. This is a blocking call which must therefore not be called within
  812. tasks or callbacks, but only from the application directly. It is possible to
  813. synchronize with the same tag multiple times, as long as the
  814. @code{starpu_tag_remove} function is not called. Note that it is still
  815. possible to synchronize with a tag associated to a task which @code{starpu_task}
  816. data structure was liberated (e.g. if the @code{destroy} flag of the
  817. @code{starpu_task} was enabled).
  818. @item @emph{Prototype}:
  819. @code{void starpu_tag_wait(starpu_tag_t id);}
  820. @end table
  821. @node starpu_tag_wait_array
  822. @subsection @code{starpu_tag_wait_array} -- Block until a set of Tags is terminated
  823. @table @asis
  824. @item @emph{Description}:
  825. This function is similar to @code{starpu_tag_wait} except that it blocks until
  826. @emph{all} the @code{ntags} tags contained in the @code{id} array are
  827. terminated.
  828. @item @emph{Prototype}:
  829. @code{void starpu_tag_wait_array(unsigned ntags, starpu_tag_t *id);}
  830. @end table
  831. @node starpu_tag_remove
  832. @subsection @code{starpu_tag_remove} -- Destroy a Tag
  833. @table @asis
  834. @item @emph{Description}:
  835. This function release the resources associated to tag @code{id}. It can be
  836. called once the corresponding task has been executed and when there is no tag
  837. that depend on that one anymore.
  838. @item @emph{Prototype}:
  839. @code{void starpu_tag_remove(starpu_tag_t id);}
  840. @end table
  841. @node starpu_tag_notify_from_apps
  842. @subsection @code{starpu_tag_notify_from_apps} -- Feed a Tag explicitly
  843. @table @asis
  844. @item @emph{Description}:
  845. This function explicitly unlocks tag @code{id}. It may be useful in the
  846. case of applications which execute part of their computation outside StarPU
  847. tasks (e.g. third-party libraries). It is also provided as a
  848. convenient tool for the programmer, for instance to entirely construct the task
  849. DAG before actually giving StarPU the opportunity to execute the tasks.
  850. @item @emph{Prototype}:
  851. @code{void starpu_tag_notify_from_apps(starpu_tag_t id);}
  852. @end table
  853. @node CUDA extensions
  854. @section CUDA extensions
  855. @c void starpu_data_malloc_pinned_if_possible(float **A, size_t dim);
  856. @c starpu_helper_cublas_init TODO
  857. @c starpu_helper_cublas_shutdown TODO
  858. @menu
  859. * starpu_cuda_get_local_stream:: Get current worker's CUDA stream
  860. * starpu_helper_cublas_init:: Initialize CUBLAS on every CUDA device
  861. * starpu_helper_cublas_shutdown:: Deiitialize CUBLAS on every CUDA device
  862. @end menu
  863. @node starpu_cuda_get_local_stream
  864. @subsection @code{starpu_cuda_get_local_stream} -- Get current worker's CUDA stream
  865. @table @asis
  866. @item @emph{Description}:
  867. StarPU provides a stream for every CUDA device controlled by StarPU. This
  868. function is only provided for convenience so that programmers can easily use
  869. asynchronous operations within codelets without having to create a stream by
  870. hand. Note that the application is not forced to use the stream provided by
  871. @code{starpu_cuda_get_local_stream} and may also create its own streams.
  872. @item @emph{Prototype}:
  873. @code{cudaStream_t *starpu_cuda_get_local_stream(void);}
  874. @end table
  875. @node starpu_helper_cublas_init
  876. @subsection @code{starpu_helper_cublas_init} -- Initialize CUBLAS on every CUDA device
  877. @table @asis
  878. @item @emph{Description}:
  879. The CUBLAS library must be initialized prior to any CUBLAS call. Calling
  880. @code{starpu_helper_cublas_init} will initialize CUBLAS on every CUDA device
  881. controlled by StarPU. This call blocks until CUBLAS has been properly
  882. initialized on every device.
  883. @item @emph{Prototype}:
  884. @code{void starpu_helper_cublas_init(void);}
  885. @end table
  886. @node starpu_helper_cublas_shutdown
  887. @subsection @code{starpu_helper_cublas_shutdown} -- Deinitialize CUBLAS on every CUDA device
  888. @table @asis
  889. @item @emph{Description}:
  890. This function synchronously deinitializes the CUBLAS library on every CUDA device.
  891. @item @emph{Prototype}:
  892. @code{void starpu_helper_cublas_shutdown(void);}
  893. @end table
  894. @node Cell extensions
  895. @section Cell extensions
  896. nothing yet.
  897. @node Miscellaneous
  898. @section Miscellaneous helpers
  899. @menu
  900. * starpu_execute_on_each_worker:: Execute a function on a subset of workers
  901. @end menu
  902. @node starpu_execute_on_each_worker
  903. @subsection @code{starpu_execute_on_each_worker} -- Execute a function on a subset of workers
  904. @table @asis
  905. @item @emph{Description}:
  906. When calling this method, the offloaded function specified by the first argument is
  907. executed by every StarPU worker that may execute the function.
  908. The second argument is passed to the offloaded function.
  909. The last argument specifies on which types of processing units the function
  910. should be executed. Similarly to the @code{.where} field of the
  911. @code{starpu_codelet} structure, it is possible to specify that the function
  912. should be executed on every CUDA device and every CPU by passing
  913. @code{STARPU_CPU|STARPU_CUDA}.
  914. This function blocks until the function has been executed on every appropriate
  915. processing units, so that it may not be called from a callback function for
  916. instance.
  917. @item @emph{Prototype}:
  918. @code{void starpu_execute_on_each_worker(void (*func)(void *), void *arg, uint32_t where);}
  919. @end table
  920. @c ---------------------------------------------------------------------
  921. @c Basic Examples
  922. @c ---------------------------------------------------------------------
  923. @node Basic Examples
  924. @chapter Basic Examples
  925. @menu
  926. * Compiling and linking:: Compiling and Linking Options
  927. * Hello World:: Submitting Tasks
  928. * Scaling a Vector:: Manipulating Data
  929. * Scaling a Vector (hybrid):: Handling Heterogeneous Architectures
  930. @end menu
  931. @node Compiling and linking
  932. @section Compiling and linking options
  933. The Makefile could for instance contain the following lines to define which
  934. options must be given to the compiler and to the linker:
  935. @example
  936. @c @cartouche
  937. CFLAGS+=$$(pkg-config --cflags libstarpu)
  938. LIBS+=$$(pkg-config --libs libstarpu)
  939. @c @end cartouche
  940. @end example
  941. @node Hello World
  942. @section Hello World
  943. In this section, we show how to implement a simple program that submits a task to StarPU.
  944. @subsection Required Headers
  945. The @code{starpu.h} header should be included in any code using StarPU.
  946. @example
  947. @c @cartouche
  948. #include <starpu.h>
  949. @c @end cartouche
  950. @end example
  951. @subsection Defining a Codelet
  952. @example
  953. @c @cartouche
  954. void cpu_func(void *buffers[], void *cl_arg)
  955. @{
  956. float *array = cl_arg;
  957. printf("Hello world (array = @{%f, %f@} )\n", array[0], array[1]);
  958. @}
  959. starpu_codelet cl =
  960. @{
  961. .where = STARPU_CPU,
  962. .cpu_func = cpu_func,
  963. .nbuffers = 0
  964. @};
  965. @c @end cartouche
  966. @end example
  967. A codelet is a structure that represents a computational kernel. Such a codelet
  968. may contain an implementation of the same kernel on different architectures
  969. (e.g. CUDA, Cell's SPU, x86, ...).
  970. The ''@code{.nbuffers}'' field specifies the number of data buffers that are
  971. manipulated by the codelet: here the codelet does not access or modify any data
  972. that is controlled by our data management library. Note that the argument
  973. passed to the codelet (the ''@code{.cl_arg}'' field of the @code{starpu_task}
  974. structure) does not count as a buffer since it is not managed by our data
  975. management library.
  976. @c TODO need a crossref to the proper description of "where" see bla for more ...
  977. We create a codelet which may only be executed on the CPUs. The ''@code{.where}''
  978. field is a bitmask that defines where the codelet may be executed. Here, the
  979. @code{STARPU_CPU} value means that only CPUs can execute this codelet
  980. (@pxref{Codelets and Tasks} for more details on that field).
  981. When a CPU core executes a codelet, it calls the @code{.cpu_func} function,
  982. which @emph{must} have the following prototype:
  983. @code{void (*cpu_func)(void *buffers[], void *cl_arg)}
  984. In this example, we can ignore the first argument of this function which gives a
  985. description of the input and output buffers (e.g. the size and the location of
  986. the matrices). The second argument is a pointer to a buffer passed as an
  987. argument to the codelet by the means of the ''@code{.cl_arg}'' field of the
  988. @code{starpu_task} structure.
  989. @c TODO rewrite so that it is a little clearer ?
  990. Be aware that this may be a pointer to a
  991. @emph{copy} of the actual buffer, and not the pointer given by the programmer:
  992. if the codelet modifies this buffer, there is no guarantee that the initial
  993. buffer will be modified as well: this for instance implies that the buffer
  994. cannot be used as a synchronization medium.
  995. @subsection Submitting a Task
  996. @example
  997. @c @cartouche
  998. void callback_func(void *callback_arg)
  999. @{
  1000. printf("Callback function (arg %x)\n", callback_arg);
  1001. @}
  1002. int main(int argc, char **argv)
  1003. @{
  1004. /* initialize StarPU */
  1005. starpu_init(NULL);
  1006. struct starpu_task *task = starpu_task_create();
  1007. task->cl = &cl;
  1008. float *array[2] = @{1.0f, -1.0f@};
  1009. task->cl_arg = &array;
  1010. task->cl_arg_size = 2*sizeof(float);
  1011. task->callback_func = callback_func;
  1012. task->callback_arg = 0x42;
  1013. /* starpu_task_submit will be a blocking call */
  1014. task->synchronous = 1;
  1015. /* submit the task to StarPU */
  1016. starpu_task_submit(task);
  1017. /* terminate StarPU */
  1018. starpu_shutdown();
  1019. return 0;
  1020. @}
  1021. @c @end cartouche
  1022. @end example
  1023. Before submitting any tasks to StarPU, @code{starpu_init} must be called. The
  1024. @code{NULL} argument specifies that we use default configuration. Tasks cannot
  1025. be submitted after the termination of StarPU by a call to
  1026. @code{starpu_shutdown}.
  1027. In the example above, a task structure is allocated by a call to
  1028. @code{starpu_task_create}. This function only allocates and fills the
  1029. corresponding structure with the default settings (@pxref{starpu_task_create}),
  1030. but it does not submit the task to StarPU.
  1031. @c not really clear ;)
  1032. The ''@code{.cl}'' field is a pointer to the codelet which the task will
  1033. execute: in other words, the codelet structure describes which computational
  1034. kernel should be offloaded on the different architectures, and the task
  1035. structure is a wrapper containing a codelet and the piece of data on which the
  1036. codelet should operate.
  1037. The optional ''@code{.cl_arg}'' field is a pointer to a buffer (of size
  1038. @code{.cl_arg_size}) with some parameters for the kernel
  1039. described by the codelet. For instance, if a codelet implements a computational
  1040. kernel that multiplies its input vector by a constant, the constant could be
  1041. specified by the means of this buffer.
  1042. Once a task has been executed, an optional callback function can be called.
  1043. While the computational kernel could be offloaded on various architectures, the
  1044. callback function is always executed on a CPU. The ''@code{.callback_arg}''
  1045. pointer is passed as an argument of the callback. The prototype of a callback
  1046. function must be:
  1047. @example
  1048. void (*callback_function)(void *);
  1049. @end example
  1050. If the @code{.synchronous} field is non-null, task submission will be
  1051. synchronous: the @code{starpu_task_submit} function will not return until the
  1052. task was executed. Note that the @code{starpu_shutdown} method does not
  1053. guarantee that asynchronous tasks have been executed before it returns.
  1054. @node Scaling a Vector
  1055. @section Manipulating Data: Scaling a Vector
  1056. The previous example has shown how to submit tasks. In this section we show how
  1057. StarPU tasks can manipulate data.
  1058. Programmers can describe the data layout of their application so that StarPU is
  1059. responsible for enforcing data coherency and availability across the machine.
  1060. Instead of handling complex (and non-portable) mechanisms to perform data
  1061. movements, programmers only declare which piece of data is accessed and/or
  1062. modified by a task, and StarPU makes sure that when a computational kernel
  1063. starts somewhere (e.g. on a GPU), its data are available locally.
  1064. Before submitting those tasks, the programmer first needs to declare the
  1065. different pieces of data to StarPU using the @code{starpu_register_*_data}
  1066. functions. To ease the development of applications for StarPU, it is possible
  1067. to describe multiple types of data layout. A type of data layout is called an
  1068. @b{interface}. By default, there are different interfaces available in StarPU:
  1069. here we will consider the @b{vector interface}.
  1070. The following lines show how to declare an array of @code{n} elements of type
  1071. @code{float} using the vector interface:
  1072. @example
  1073. float tab[n];
  1074. starpu_data_handle tab_handle;
  1075. starpu_vector_data_register(&tab_handle, 0, tab, n, sizeof(float));
  1076. @end example
  1077. The first argument, called the @b{data handle}, is an opaque pointer which
  1078. designates the array in StarPU. This is also the structure which is used to
  1079. describe which data is used by a task.
  1080. @c TODO: what is 0 ?
  1081. It is possible to construct a StarPU
  1082. task that multiplies this vector by a constant factor:
  1083. @example
  1084. float factor;
  1085. struct starpu_task *task = starpu_task_create();
  1086. task->cl = &cl;
  1087. task->buffers[0].handle = tab_handle;
  1088. task->buffers[0].mode = STARPU_RW;
  1089. task->cl_arg = &factor;
  1090. task->cl_arg_size = sizeof(float);
  1091. @end example
  1092. Since the factor is constant, it does not need a preliminary declaration, and
  1093. can just be passed through the @code{cl_arg} pointer like in the previous
  1094. example. The vector parameter is described by its handle.
  1095. There are two fields in each element of the @code{buffers} array.
  1096. @code{.handle} is the handle of the data, and @code{.mode} specifies how the
  1097. kernel will access the data (@code{STARPU_R} for read-only, @code{STARPU_W} for
  1098. write-only and @code{STARPU_RW} for read and write access).
  1099. The definition of the codelet can be written as follows:
  1100. @example
  1101. void scal_func(void *buffers[], void *cl_arg)
  1102. @{
  1103. unsigned i;
  1104. float *factor = cl_arg;
  1105. struct starpu_vector_interface_s *vector = buffers[0];
  1106. /* length of the vector */
  1107. unsigned n = vector->nx;
  1108. /* local copy of the vector pointer */
  1109. float *val = (float *)vector->ptr;
  1110. for (i = 0; i < n; i++)
  1111. val[i] *= *factor;
  1112. @}
  1113. starpu_codelet cl = @{
  1114. .where = STARPU_CPU,
  1115. .cpu_func = scal_func,
  1116. .nbuffers = 1
  1117. @};
  1118. @end example
  1119. The second argument of the @code{scal_func} function contains a pointer to the
  1120. parameters of the codelet (given in @code{task->cl_arg}), so that we read the
  1121. constant factor from this pointer. The first argument is an array that gives
  1122. a description of every buffers passed in the @code{task->buffers}@ array. The
  1123. size of this array is given by the @code{.nbuffers} field of the codelet
  1124. structure. For the sake of generality, this array contains pointers to the
  1125. different interfaces describing each buffer. In the case of the @b{vector
  1126. interface}, the location of the vector (resp. its length) is accessible in the
  1127. @code{ptr} (resp. @code{nx}) of this array. Since the vector is accessed in a
  1128. read-write fashion, any modification will automatically affect future accesses
  1129. to that vector made by other tasks.
  1130. @node Scaling a Vector (hybrid)
  1131. @section Vector Scaling on an Hybrid CPU/GPU Machine
  1132. Contrary to the previous examples, the task submitted in the example may not
  1133. only be executed by the CPUs, but also by a CUDA device.
  1134. TODO
  1135. @c ---------------------------------------------------------------------
  1136. @c Advanced Topics
  1137. @c ---------------------------------------------------------------------
  1138. @node Advanced Topics
  1139. @chapter Advanced Topics
  1140. @bye