starpu.texi 51 KB

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