starpu.texi 71 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163
  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. * Using StarPU:: How to run StarPU application
  29. * Configuring StarPU:: How to configure StarPU
  30. * StarPU API:: The API to use StarPU
  31. * Basic Examples:: Basic examples of the use of StarPU
  32. * Full source code for the 'Scaling a Vector' example::
  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 of
  56. efficiently mapping and executing 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. @menu
  66. * Codelet and Tasks::
  67. * StarPU Data Management Library::
  68. @end menu
  69. From a programming point of view, StarPU is not a new language but a library
  70. that executes tasks explicitly submitted by the application. The data that a
  71. task manipulates are automatically transferred onto the accelerator so that the
  72. programmer does not have to take care of complex data movements. StarPU also
  73. takes particular care of scheduling those tasks efficiently and allows
  74. scheduling experts to implement custom scheduling policies in a portable
  75. fashion.
  76. @c explain the notion of codelet and task (i.e. g(A, B)
  77. @node Codelet and Tasks
  78. @subsection Codelet and Tasks
  79. One of StarPU primary data structure is the @b{codelet}. A codelet describes a
  80. computational kernel that can possibly be implemented on multiple architectures
  81. such as a CPU, a CUDA device or a Cell's SPU.
  82. @c TODO insert illustration f : f_spu, f_cpu, ...
  83. Another important data structure is the @b{task}. Executing a StarPU task
  84. consists in applying a codelet on a data set, on one of the architectures on
  85. which the codelet is implemented. In addition to the codelet that a task
  86. implements, it also describes which data are accessed, and how they are
  87. accessed during the computation (read and/or write).
  88. StarPU tasks are asynchronous: submitting a task to StarPU is a non-blocking
  89. operation. The task structure can also specify a @b{callback} function that is
  90. called once StarPU has properly executed the task. It also contains optional
  91. fields that the application may use to give hints to the scheduler (such as
  92. priority levels).
  93. A task may be identified by a unique 64-bit number which we refer as a @b{tag}.
  94. Task dependencies can be enforced either by the means of callback functions, or
  95. by expressing dependencies between tags.
  96. @c TODO insert illustration f(Ar, Brw, Cr) + ..
  97. @c DSM
  98. @node StarPU Data Management Library
  99. @subsection StarPU Data Management Library
  100. Because StarPU schedules tasks at runtime, data transfers have to be
  101. done automatically and ``just-in-time'' between processing units,
  102. relieving the application programmer from explicit data transfers.
  103. Moreover, to avoid unnecessary transfers, StarPU keeps data
  104. where it was last needed, even if was modified there, and it
  105. allows multiple copies of the same data to reside at the same time on
  106. several processing units as long as it is not modified.
  107. @c ---------------------------------------------------------------------
  108. @c Installing StarPU
  109. @c ---------------------------------------------------------------------
  110. @node Installing StarPU
  111. @chapter Installing StarPU
  112. @menu
  113. * Configuration of StarPU::
  114. * Building and Installing StarPU::
  115. @end menu
  116. StarPU can be built and installed by the standard means of the GNU
  117. autotools. The following chapter is intended to briefly remind how these tools
  118. can be used to install StarPU.
  119. @node Configuration of StarPU
  120. @section Configuration of StarPU
  121. @menu
  122. * Generating Makefiles and configuration scripts::
  123. * Running the configuration::
  124. @end menu
  125. @node Generating Makefiles and configuration scripts
  126. @subsection Generating Makefiles and configuration scripts
  127. This step is not necessary when using the tarball releases of StarPU. If you
  128. are using the source code from the svn repository, you first need to generate
  129. the configure scripts and the Makefiles.
  130. @example
  131. % autoreconf -vfi
  132. @end example
  133. @node Running the configuration
  134. @subsection Running the configuration
  135. @example
  136. % ./configure
  137. @end example
  138. Details about options that are useful to give to @code{./configure} are given in
  139. @ref{Compilation configuration}.
  140. @node Building and Installing StarPU
  141. @section Building and Installing StarPU
  142. @menu
  143. * Building::
  144. * Sanity Checks::
  145. * Installing::
  146. @end menu
  147. @node Building
  148. @subsection Building
  149. @example
  150. % make
  151. @end example
  152. @node Sanity Checks
  153. @subsection Sanity Checks
  154. In order to make sure that StarPU is working properly on the system, it is also
  155. possible to run a test suite.
  156. @example
  157. % make check
  158. @end example
  159. @node Installing
  160. @subsection Installing
  161. In order to install StarPU at the location that was specified during
  162. configuration:
  163. @example
  164. % make install
  165. @end example
  166. @c ---------------------------------------------------------------------
  167. @c Using StarPU
  168. @c ---------------------------------------------------------------------
  169. @node Using StarPU
  170. @chapter Using StarPU
  171. @menu
  172. * Setting flags for compiling and linking applications::
  173. * Running a basic StarPU application::
  174. @end menu
  175. @node Setting flags for compiling and linking applications
  176. @section Setting flags for compiling and linking applications
  177. Compiling and linking an application against StarPU may require to use
  178. specific flags or libraries (for instance @code{CUDA} or @code{libspe2}).
  179. To this end, it is possible to use the @code{pkg-config} tool.
  180. If StarPU was not installed at some standard location, the path of StarPU's
  181. library must be specified in the @code{PKG_CONFIG_PATH} environment variable so
  182. that @code{pkg-config} can find it. For example if StarPU was installed in
  183. @code{$prefix_dir}:
  184. @example
  185. % PKG_CONFIG_PATH=$PKG_CONFIG_PATH:$prefix_dir/lib/pkgconfig
  186. @end example
  187. The flags required to compile or link against StarPU are then
  188. accessible with the following commands:
  189. @example
  190. % pkg-config --cflags libstarpu # options for the compiler
  191. % pkg-config --libs libstarpu # options for the linker
  192. @end example
  193. @node Running a basic StarPU application
  194. @section Running a basic StarPU application
  195. Basic examples using StarPU have been built in the directory
  196. @code{$prefix_dir/lib/starpu/examples/}. You can for example run the
  197. example @code{vector_scal}.
  198. @example
  199. % $prefix_dir/lib/starpu/examples/vector_scal
  200. BEFORE : First element was 1.000000
  201. AFTER First element is 3.140000
  202. %
  203. @end example
  204. @c ---------------------------------------------------------------------
  205. @c Configuration options
  206. @c ---------------------------------------------------------------------
  207. @node Configuring StarPU
  208. @chapter Configuring StarPU
  209. @menu
  210. * Compilation configuration::
  211. * Execution configuration through environment variables::
  212. @end menu
  213. @node Compilation configuration
  214. @section Compilation configuration
  215. The following arguments can be given to the @code{configure} script.
  216. @menu
  217. * Common configuration::
  218. * Configuring workers::
  219. * Advanced configuration::
  220. @end menu
  221. @node Common configuration
  222. @subsection Common configuration
  223. @menu
  224. * --enable-debug::
  225. * --enable-fast::
  226. * --enable-verbose::
  227. * --enable-coverage::
  228. @end menu
  229. @node --enable-debug
  230. @subsubsection @code{--enable-debug}
  231. @table @asis
  232. @item @emph{Description}:
  233. Enable debugging messages.
  234. @end table
  235. @node --enable-fast
  236. @subsubsection @code{--enable-fast}
  237. @table @asis
  238. @item @emph{Description}:
  239. Do not enforce assertions, saves a lot of time spent to compute them otherwise.
  240. @end table
  241. @node --enable-verbose
  242. @subsubsection @code{--enable-verbose}
  243. @table @asis
  244. @item @emph{Description}:
  245. Augment the verbosity of the debugging messages.
  246. @end table
  247. @node --enable-coverage
  248. @subsubsection @code{--enable-coverage}
  249. @table @asis
  250. @item @emph{Description}:
  251. Enable flags for the coverage tool.
  252. @end table
  253. @node Configuring workers
  254. @subsection Configuring workers
  255. @menu
  256. * --disable-cpu::
  257. * --enable-maxcudadev::
  258. * --disable-cuda::
  259. * --with-cuda-dir::
  260. * --enable-maxopencldev::
  261. * --disable-opencl::
  262. * --with-opencl-dir::
  263. * --enable-gordon::
  264. * --with-gordon-dir::
  265. @end menu
  266. @node --disable-cpu
  267. @subsubsection @code{--disable-cpu}
  268. @table @asis
  269. @item @emph{Description}:
  270. Disable the use of CPUs of the machine. Only GPUs etc. will be used.
  271. @end table
  272. @node --enable-maxcudadev
  273. @subsubsection @code{--enable-maxcudadev=<number>}
  274. @table @asis
  275. @item @emph{Description}:
  276. Defines the maximum number of CUDA devices that StarPU will support, then
  277. available as the @code{STARPU_MAXCUDADEVS} macro.
  278. @end table
  279. @node --disable-cuda
  280. @subsubsection @code{--disable-cuda}
  281. @table @asis
  282. @item @emph{Description}:
  283. Disable the use of CUDA, even if the SDK is detected.
  284. @end table
  285. @node --with-cuda-dir
  286. @subsubsection @code{--with-cuda-dir=<path>}
  287. @table @asis
  288. @item @emph{Description}:
  289. Specify the location of the CUDA SDK resides. This directory should notably contain
  290. @code{include/cuda.h}.
  291. @end table
  292. @node --enable-maxopencldev
  293. @subsubsection @code{--enable-maxopencldev=<number>}
  294. @table @asis
  295. @item @emph{Description}:
  296. Defines the maximum number of OpenCL devices that StarPU will support, then
  297. available as the @code{STARPU_MAXOPENCLDEVS} macro.
  298. @end table
  299. @node --disable-opencl
  300. @subsubsection @code{--disable-opencl}
  301. @table @asis
  302. @item @emph{Description}:
  303. Disable the use of OpenCL, even if the SDK is detected.
  304. @end table
  305. @node --with-opencl-dir
  306. @subsubsection @code{--with-opencl-dir=<path>}
  307. @table @asis
  308. @item @emph{Description}:
  309. Specify the location of the OpenCL SDK. This directory should notably contain
  310. @code{include/CL/cl.h}.
  311. @end table
  312. @node --enable-gordon
  313. @subsubsection @code{--enable-gordon}
  314. @table @asis
  315. @item @emph{Description}:
  316. Enable the use of the Gordon runtime for Cell SPUs.
  317. @c TODO: rather default to enabled when detected
  318. @end table
  319. @node --with-gordon-dir
  320. @subsubsection @code{--with-gordon-dir=<path>}
  321. @table @asis
  322. @item @emph{Description}:
  323. Specify the location of the Gordon SDK.
  324. @end table
  325. @node Advanced configuration
  326. @subsection Advanced configuration
  327. @menu
  328. * --enable-perf-debug::
  329. * --enable-model-debug::
  330. * --enable-stats::
  331. * --enable-maxbuffers::
  332. * --enable-allocation-cache::
  333. * --enable-opengl-render::
  334. * --enable-blas-lib::
  335. * --with-magma::
  336. * --with-fxt::
  337. * --with-perf-model-dir::
  338. * --with-mpicc::
  339. * --with-mpi::
  340. * --with-goto-dir::
  341. * --with-atlas-dir::
  342. @end menu
  343. @node --enable-perf-debug
  344. @subsubsection @code{--enable-perf-debug}
  345. @table @asis
  346. @item @emph{Description}:
  347. Enable performance debugging.
  348. @end table
  349. @node --enable-model-debug
  350. @subsubsection @code{--enable-model-debug}
  351. @table @asis
  352. @item @emph{Description}:
  353. Enable performance model debugging.
  354. @end table
  355. @node --enable-stats
  356. @subsubsection @code{--enable-stats}
  357. @table @asis
  358. @item @emph{Description}:
  359. Enable statistics.
  360. @end table
  361. @node --enable-maxbuffers
  362. @subsubsection @code{--enable-maxbuffers=<nbuffers>}
  363. @table @asis
  364. @item @emph{Description}:
  365. Define the maximum number of buffers that tasks will be able to take
  366. as parameters, then available as the @code{STARPU_NMAXBUFS} macro.
  367. @end table
  368. @node --enable-allocation-cache
  369. @subsubsection @code{--enable-allocation-cache}
  370. @table @asis
  371. @item @emph{Description}:
  372. Enable the use of a data allocation cache to avoid the cost of it with
  373. CUDA. Still experimental.
  374. @end table
  375. @node --enable-opengl-render
  376. @subsubsection @code{--enable-opengl-render}
  377. @table @asis
  378. @item @emph{Description}:
  379. Enable the use of OpenGL for the rendering of some examples.
  380. @c TODO: rather default to enabled when detected
  381. @end table
  382. @node --enable-blas-lib
  383. @subsubsection @code{--enable-blas-lib=<name>}
  384. @table @asis
  385. @item @emph{Description}:
  386. Specify the blas library to be used by some of the examples. The
  387. library has to be 'atlas' or 'goto'.
  388. @end table
  389. @node --with-magma
  390. @subsubsection @code{--with-magma=<path>}
  391. @table @asis
  392. @item @emph{Description}:
  393. Specify where magma is installed.
  394. @end table
  395. @node --with-fxt
  396. @subsubsection @code{--with-fxt=<path>}
  397. @table @asis
  398. @item @emph{Description}:
  399. Specify the location of FxT (for generating traces and rendering them
  400. using ViTE). This directory should notably contain
  401. @code{include/fxt/fxt.h}.
  402. @end table
  403. @node --with-perf-model-dir
  404. @subsubsection @code{--with-perf-model-dir=<dir>}
  405. @table @asis
  406. @item @emph{Description}:
  407. Specify where performance models should be stored (instead of defaulting to the
  408. current user's home).
  409. @end table
  410. @node --with-mpicc
  411. @subsubsection @code{--with-mpicc=<path to mpicc>}
  412. @table @asis
  413. @item @emph{Description}:
  414. Specify the location of the @code{mpicc} compiler to be used for starpumpi.
  415. @c TODO: also just use AC_PROG
  416. @end table
  417. @node --with-mpi
  418. @subsubsection @code{--with-mpi}
  419. @table @asis
  420. @item @emph{Description}:
  421. Enable building libstarpumpi.
  422. @c TODO: rather just use the availability of mpicc instead of a second option
  423. @end table
  424. @node --with-goto-dir
  425. @subsubsection @code{--with-goto-dir=<dir>}
  426. @table @asis
  427. @item @emph{Description}:
  428. Specify the location of GotoBLAS.
  429. @end table
  430. @node --with-atlas-dir
  431. @subsubsection @code{--with-atlas-dir=<dir>}
  432. @table @asis
  433. @item @emph{Description}:
  434. Specify the location of ATLAS. This directory should notably contain
  435. @code{include/cblas.h}.
  436. @end table
  437. @c ---------------------------------------------------------------------
  438. @c Environment variables
  439. @c ---------------------------------------------------------------------
  440. @node Execution configuration through environment variables
  441. @section Execution configuration through environment variables
  442. @menu
  443. * Workers:: Configuring workers
  444. * Scheduling:: Configuring the Scheduling engine
  445. * Misc:: Miscellaneous and debug
  446. @end menu
  447. Note: the values given in @code{starpu_conf} structure passed when
  448. calling @code{starpu_init} will override the values of the environment
  449. variables.
  450. @node Workers
  451. @subsection Configuring workers
  452. @menu
  453. * STARPU_NCPUS:: Number of CPU workers
  454. * STARPU_NCUDA:: Number of CUDA workers
  455. * STARPU_NOPENCL:: Number of OpenCL workers
  456. * STARPU_NGORDON:: Number of SPU workers (Cell)
  457. * STARPU_WORKERS_CPUID:: Bind workers to specific CPUs
  458. * STARPU_WORKERS_CUDAID:: Select specific CUDA devices
  459. * STARPU_WORKERS_OPENCLID:: Select specific OpenCL devices
  460. @end menu
  461. @node STARPU_NCPUS
  462. @subsubsection @code{STARPU_NCPUS} -- Number of CPU workers
  463. @table @asis
  464. @item @emph{Description}:
  465. Specify the maximum number of CPU workers. Note that StarPU will not allocate
  466. more CPUs than there are physical CPUs, and that some CPUs are used to control
  467. the accelerators.
  468. @end table
  469. @node STARPU_NCUDA
  470. @subsubsection @code{STARPU_NCUDA} -- Number of CUDA workers
  471. @table @asis
  472. @item @emph{Description}:
  473. Specify the maximum number of CUDA devices that StarPU can use. If
  474. @code{STARPU_NCUDA} is lower than the number of physical devices, it is
  475. possible to select which CUDA devices should be used by the means of the
  476. @code{STARPU_WORKERS_CUDAID} environment variable.
  477. @end table
  478. @node STARPU_NOPENCL
  479. @subsubsection @code{STARPU_NOPENCL} -- Number of OpenCL workers
  480. @table @asis
  481. @item @emph{Description}:
  482. OpenCL equivalent of the @code{STARPU_NCUDA} environment variable.
  483. @end table
  484. @node STARPU_NGORDON
  485. @subsubsection @code{STARPU_NGORDON} -- Number of SPU workers (Cell)
  486. @table @asis
  487. @item @emph{Description}:
  488. Specify the maximum number of SPUs that StarPU can use.
  489. @end table
  490. @node STARPU_WORKERS_CPUID
  491. @subsubsection @code{STARPU_WORKERS_CPUID} -- Bind workers to specific CPUs
  492. @table @asis
  493. @item @emph{Description}:
  494. Passing an array of integers (starting from 0) in @code{STARPU_WORKERS_CPUID}
  495. specifies on which logical CPU the different workers should be
  496. bound. For instance, if @code{STARPU_WORKERS_CPUID = "1 3 0 2"}, the first
  497. worker will be bound to logical CPU #1, the second CPU worker will be bound to
  498. logical CPU #3 and so on. Note that the logical ordering of the CPUs is either
  499. determined by the OS, or provided by the @code{hwloc} library in case it is
  500. available.
  501. Note that the first workers correspond to the CUDA workers, then come the
  502. OpenCL and the SPU, and finally the CPU workers. For example if
  503. we have @code{STARPU_NCUDA=1}, @code{STARPU_NOPENCL=1}, @code{STARPU_NCPUS=2}
  504. and @code{STARPU_WORKERS_CPUID = "0 2 1 3"}, the CUDA device will be controlled
  505. by logical CPU #0, the OpenCL device will be controlled by logical CPU #2, and
  506. the logical CPUs #1 and #3 will be used by the CPU workers.
  507. If the number of workers is larger than the array given in
  508. @code{STARPU_WORKERS_CPUID}, the workers are bound to the logical CPUs in a
  509. round-robin fashion: if @code{STARPU_WORKERS_CPUID = "0 1"}, the first and the
  510. third (resp. second and fourth) workers will be put on CPU #0 (resp. CPU #1).
  511. @end table
  512. @node STARPU_WORKERS_CUDAID
  513. @subsubsection @code{STARPU_WORKERS_CUDAID} -- Select specific CUDA devices
  514. @table @asis
  515. @item @emph{Description}:
  516. Similarly to the @code{STARPU_WORKERS_CPUID} environment variable, it is
  517. possible to select which CUDA devices should be used by StarPU. On a machine
  518. equipped with 4 GPUs, setting @code{STARPU_WORKERS_CUDAID = "1 3"} and
  519. @code{STARPU_NCUDA=2} specifies that 2 CUDA workers should be created, and that
  520. they should use CUDA devices #1 and #3 (the logical ordering of the devices is
  521. the one reported by CUDA).
  522. @end table
  523. @node STARPU_WORKERS_OPENCLID
  524. @subsubsection @code{STARPU_WORKERS_OPENCLID} -- Select specific OpenCL devices
  525. @table @asis
  526. @item @emph{Description}:
  527. OpenCL equivalent of the @code{STARPU_WORKERS_CUDAID} environment variable.
  528. @end table
  529. @node Scheduling
  530. @subsection Configuring the Scheduling engine
  531. @menu
  532. * STARPU_SCHED:: Scheduling policy
  533. * STARPU_CALIBRATE:: Calibrate performance models
  534. * STARPU_PREFETCH:: Use data prefetch
  535. * STARPU_SCHED_ALPHA:: Computation factor
  536. * STARPU_SCHED_BETA:: Communication factor
  537. @end menu
  538. @node STARPU_SCHED
  539. @subsubsection @code{STARPU_SCHED} -- Scheduling policy
  540. @table @asis
  541. @item @emph{Description}:
  542. This chooses between the different scheduling policies proposed by StarPU: work
  543. random, stealing, greedy, with performance models, etc.
  544. Use @code{STARPU_SCHED=help} to get the list of available schedulers.
  545. @end table
  546. @node STARPU_CALIBRATE
  547. @subsubsection @code{STARPU_CALIBRATE} -- Calibrate performance models
  548. @table @asis
  549. @item @emph{Description}:
  550. If this variable is set to 1, the performance models are calibrated during
  551. the execution. If it is set to 2, the previous values are dropped to restart
  552. calibration from scratch.
  553. Note: this currently only applies to dm and dmda scheduling policies.
  554. @end table
  555. @node STARPU_PREFETCH
  556. @subsubsection @code{STARPU_PREFETCH} -- Use data prefetch
  557. @table @asis
  558. @item @emph{Description}:
  559. If this variable is set, data prefetching will be enabled, that is when a task is
  560. scheduled to be executed e.g. on a GPU, StarPU will request an asynchronous
  561. transfer in advance, so that data is already present on the GPU when the task
  562. starts. As a result, computation and data transfers are overlapped.
  563. @end table
  564. @node STARPU_SCHED_ALPHA
  565. @subsubsection @code{STARPU_SCHED_ALPHA} -- Computation factor
  566. @table @asis
  567. @item @emph{Description}:
  568. To estimate the cost of a task StarPU takes into account the estimated
  569. computation time (obtained thanks to performance models). The alpha factor is
  570. the coefficient to be applied to it before adding it to the communication part.
  571. @end table
  572. @node STARPU_SCHED_BETA
  573. @subsubsection @code{STARPU_SCHED_BETA} -- Communication factor
  574. @table @asis
  575. @item @emph{Description}:
  576. To estimate the cost of a task StarPU takes into account the estimated
  577. data transfer time (obtained thanks to performance models). The beta factor is
  578. the coefficient to be applied to it before adding it to the computation part.
  579. @end table
  580. @node Misc
  581. @subsection Miscellaneous and debug
  582. @menu
  583. * STARPU_LOGFILENAME:: Select debug file name
  584. @end menu
  585. @node STARPU_LOGFILENAME
  586. @subsubsection @code{STARPU_LOGFILENAME} -- Select debug file name
  587. @table @asis
  588. @item @emph{Description}:
  589. This variable specify in which file the debugging output should be saved to.
  590. @end table
  591. @c ---------------------------------------------------------------------
  592. @c StarPU API
  593. @c ---------------------------------------------------------------------
  594. @node StarPU API
  595. @chapter StarPU API
  596. @menu
  597. * Initialization and Termination:: Initialization and Termination methods
  598. * Workers' Properties:: Methods to enumerate workers' properties
  599. * Data Library:: Methods to manipulate data
  600. * Codelets and Tasks:: Methods to construct tasks
  601. * Tags:: Task dependencies
  602. * CUDA extensions:: CUDA extensions
  603. * OpenCL extensions:: OpenCL extensions
  604. * Cell extensions:: Cell extensions
  605. * Miscellaneous helpers::
  606. @end menu
  607. @node Initialization and Termination
  608. @section Initialization and Termination
  609. @menu
  610. * starpu_init:: Initialize StarPU
  611. * struct starpu_conf:: StarPU runtime configuration
  612. * starpu_shutdown:: Terminate StarPU
  613. @end menu
  614. @node starpu_init
  615. @subsection @code{starpu_init} -- Initialize StarPU
  616. @table @asis
  617. @item @emph{Description}:
  618. This is StarPU initialization method, which must be called prior to any other
  619. StarPU call. It is possible to specify StarPU's configuration (e.g. scheduling
  620. policy, number of cores, ...) by passing a non-null argument. Default
  621. configuration is used if the passed argument is @code{NULL}.
  622. @item @emph{Return value}:
  623. Upon successful completion, this function returns 0. Otherwise, @code{-ENODEV}
  624. indicates that no worker was available (so that StarPU was not initialized).
  625. @item @emph{Prototype}:
  626. @code{int starpu_init(struct starpu_conf *conf);}
  627. @end table
  628. @node struct starpu_conf
  629. @subsection @code{struct starpu_conf} -- StarPU runtime configuration
  630. @table @asis
  631. @item @emph{Description}:
  632. This structure is passed to the @code{starpu_init} function in order
  633. to configure StarPU.
  634. When the default value is used, StarPU automatically selects the number
  635. of processing units and takes the default scheduling policy. This parameter
  636. overwrites the equivalent environment variables.
  637. @item @emph{Fields}:
  638. @table @asis
  639. @item @code{sched_policy_name} (default = NULL):
  640. This is the name of the scheduling policy. This can also be specified with the
  641. @code{STARPU_SCHED} environment variable.
  642. @item @code{sched_policy} (default = NULL):
  643. This is the definition of the scheduling policy. This field is ignored
  644. if @code{sched_policy_name} is set.
  645. @item @code{ncpus} (default = -1):
  646. This is the maximum number of CPU cores that StarPU can use. This can also be
  647. specified with the @code{STARPU_NCPUS} environment variable.
  648. @item @code{ncuda} (default = -1):
  649. This is the maximum number of CUDA devices that StarPU can use. This can also be
  650. specified with the @code{STARPU_NCUDA} environment variable.
  651. @item @code{nopencl} (default = -1):
  652. This is the maximum number of OpenCL devices that StarPU can use. This can also be
  653. specified with the @code{STARPU_NOPENCL} environment variable.
  654. @item @code{nspus} (default = -1):
  655. This is the maximum number of Cell SPUs that StarPU can use. This can also be
  656. specified with the @code{STARPU_NGORDON} environment variable.
  657. @item @code{use_explicit_workers_bindid} (default = 0)
  658. @item @code{workers_bindid[STARPU_NMAXWORKERS]}
  659. @item @code{use_explicit_workers_cuda_gpuid} (default = 0)
  660. @item @code{workers_cuda_gpuid[STARPU_NMAXWORKERS]}
  661. @item @code{use_explicit_workers_opencl_gpuid} (default = 0)
  662. @item @code{workers_opencl_gpuid[STARPU_NMAXWORKERS]}:
  663. These fields are explained in @ref{STARPU_WORKERS_CPUID}.
  664. @item @code{calibrate} (default = 0):
  665. If this flag is set, StarPU will calibrate the performance models when
  666. executing tasks. This can also be specified with the @code{STARPU_CALIBRATE}
  667. environment variable.
  668. @end table
  669. @end table
  670. @node starpu_shutdown
  671. @subsection @code{starpu_shutdown} -- Terminate StarPU
  672. @table @asis
  673. @item @emph{Description}:
  674. This is StarPU termination method. It must be called at the end of the
  675. application: statistics and other post-mortem debugging information are not
  676. guaranteed to be available until this method has been called.
  677. @item @emph{Prototype}:
  678. @code{void starpu_shutdown(void);}
  679. @end table
  680. @node Workers' Properties
  681. @section Workers' Properties
  682. @menu
  683. * starpu_worker_get_count:: Get the number of processing units
  684. * starpu_cpu_worker_get_count:: Get the number of CPU controlled by StarPU
  685. * starpu_cuda_worker_get_count:: Get the number of CUDA devices controlled by StarPU
  686. * starpu_opencl_worker_get_count:: Get the number of OpenCL devices controlled by StarPU
  687. * starpu_spu_worker_get_count:: Get the number of Cell SPUs controlled by StarPU
  688. * starpu_worker_get_id:: Get the identifier of the current worker
  689. * starpu_worker_get_type:: Get the type of processing unit associated to a worker
  690. * starpu_worker_get_name:: Get the name of a worker
  691. @end menu
  692. @node starpu_worker_get_count
  693. @subsection @code{starpu_worker_get_count} -- Get the number of processing units
  694. @table @asis
  695. @item @emph{Description}:
  696. This function returns the number of workers (i.e. processing units executing
  697. StarPU tasks). The returned value should be at most @code{STARPU_NMAXWORKERS}.
  698. @item @emph{Prototype}:
  699. @code{unsigned starpu_worker_get_count(void);}
  700. @end table
  701. @node starpu_cpu_worker_get_count
  702. @subsection @code{starpu_cpu_worker_get_count} -- Get the number of CPU controlled by StarPU
  703. @table @asis
  704. @item @emph{Description}:
  705. This function returns the number of CPUs controlled by StarPU. The returned
  706. value should be at most @code{STARPU_NMAXCPUS}.
  707. @item @emph{Prototype}:
  708. @code{unsigned starpu_cpu_worker_get_count(void);}
  709. @end table
  710. @node starpu_cuda_worker_get_count
  711. @subsection @code{starpu_cuda_worker_get_count} -- Get the number of CUDA devices controlled by StarPU
  712. @table @asis
  713. @item @emph{Description}:
  714. This function returns the number of CUDA devices controlled by StarPU. The returned
  715. value should be at most @code{STARPU_MAXCUDADEVS}.
  716. @item @emph{Prototype}:
  717. @code{unsigned starpu_cuda_worker_get_count(void);}
  718. @end table
  719. @node starpu_opencl_worker_get_count
  720. @subsection @code{starpu_opencl_worker_get_count} -- Get the number of OpenCL devices controlled by StarPU
  721. @table @asis
  722. @item @emph{Description}:
  723. This function returns the number of OpenCL devices controlled by StarPU. The returned
  724. value should be at most @code{STARPU_MAXOPENCLDEVS}.
  725. @item @emph{Prototype}:
  726. @code{unsigned starpu_opencl_worker_get_count(void);}
  727. @end table
  728. @node starpu_spu_worker_get_count
  729. @subsection @code{starpu_spu_worker_get_count} -- Get the number of Cell SPUs controlled by StarPU
  730. @table @asis
  731. @item @emph{Description}:
  732. This function returns the number of Cell SPUs controlled by StarPU.
  733. @item @emph{Prototype}:
  734. @code{unsigned starpu_opencl_worker_get_count(void);}
  735. @end table
  736. @node starpu_worker_get_id
  737. @subsection @code{starpu_worker_get_id} -- Get the identifier of the current worker
  738. @table @asis
  739. @item @emph{Description}:
  740. This function returns the identifier of the worker associated to the calling
  741. thread. The returned value is either -1 if the current context is not a StarPU
  742. worker (i.e. when called from the application outside a task or a callback), or
  743. an integer between 0 and @code{starpu_worker_get_count() - 1}.
  744. @item @emph{Prototype}:
  745. @code{int starpu_worker_get_id(void);}
  746. @end table
  747. @node starpu_worker_get_type
  748. @subsection @code{starpu_worker_get_type} -- Get the type of processing unit associated to a worker
  749. @table @asis
  750. @item @emph{Description}:
  751. This function returns the type of worker associated to an identifier (as
  752. returned by the @code{starpu_worker_get_id} function). The returned value
  753. indicates the architecture of the worker: @code{STARPU_CPU_WORKER} for a CPU
  754. core, @code{STARPU_CUDA_WORKER} for a CUDA device,
  755. @code{STARPU_OPENCL_WORKER} for a OpenCL device, and
  756. @code{STARPU_GORDON_WORKER} for a Cell SPU. The value returned for an invalid
  757. identifier is unspecified.
  758. @item @emph{Prototype}:
  759. @code{enum starpu_archtype starpu_worker_get_type(int id);}
  760. @end table
  761. @node starpu_worker_get_name
  762. @subsection @code{starpu_worker_get_name} -- Get the name of a worker
  763. @table @asis
  764. @item @emph{Description}:
  765. StarPU associates a unique human readable string to each processing unit. This
  766. function copies at most the @code{maxlen} first bytes of the unique string
  767. associated to a worker identified by its identifier @code{id} into the
  768. @code{dst} buffer. The caller is responsible for ensuring that the @code{dst}
  769. is a valid pointer to a buffer of @code{maxlen} bytes at least. Calling this
  770. function on an invalid identifier results in an unspecified behaviour.
  771. @item @emph{Prototype}:
  772. @code{void starpu_worker_get_name(int id, char *dst, size_t maxlen);}
  773. @end table
  774. @node Data Library
  775. @section Data Library
  776. This section describes the data management facilities provided by StarPU.
  777. TODO: We show how to use existing data interfaces in [ref], but developers can
  778. design their own data interfaces if required.
  779. @menu
  780. * starpu_data_handle:: StarPU opaque data handle
  781. * void *interface:: StarPU data interface
  782. * starpu_XXX_data_register::
  783. * starpu_data_unregister::
  784. @end menu
  785. @node starpu_data_handle
  786. @subsection @code{starpu_data_handle} -- StarPU opaque data handle
  787. @table @asis
  788. @item @emph{Description}:
  789. StarPU uses @code{starpu_data_handle} as an opaque handle to manage a piece of
  790. data. Once a piece of data has been registered to StarPU, it is associated to a
  791. @code{starpu_data_handle} which keeps track of the state of the piece of data
  792. over the entire machine, so that we can maintain data consistency and locate
  793. data replicates for instance.
  794. @end table
  795. @node void *interface
  796. @subsection @code{void *interface} -- StarPU data interface
  797. @table @asis
  798. @item @emph{Description}:
  799. Data management is done at a high-level in StarPU: rather than accessing a mere
  800. list of contiguous buffers, the tasks may manipulate data that are described by
  801. a high-level construct which we call data interface.
  802. TODO
  803. @end table
  804. @node starpu_XXX_data_register
  805. @subsection @code{starpu_XXX_data_register} -- Register data to StarPU
  806. @table @asis
  807. @end table
  808. @node starpu_data_unregister
  809. @subsection @code{starpu_data_unregister} -- Unregister data from StarPU
  810. @table @asis
  811. @item @emph{Description}:
  812. @item @emph{Prototype}:
  813. @code{void starpu_data_unregister(starpu_data_handle handle);}
  814. @end table
  815. @c starpu_worker_get_memory_node TODO
  816. @c
  817. @c user interaction with the DSM
  818. @c void starpu_data_sync_with_mem(struct starpu_data_state_t *state);
  819. @c void starpu_notify_data_modification(struct starpu_data_state_t *state, uint32_t modifying_node);
  820. @node Codelets and Tasks
  821. @section Codelets and Tasks
  822. @menu
  823. * struct starpu_codelet:: StarPU codelet structure
  824. * struct starpu_task:: StarPU task structure
  825. * starpu_task_init:: Initialize a Task
  826. * starpu_task_create:: Allocate and Initialize a Task
  827. * starpu_task_deinit:: Release all the resources used by a Task
  828. * starpu_task_destroy:: Destroy a dynamically allocated Task
  829. * starpu_task_wait:: Wait for the termination of a Task
  830. * starpu_task_submit:: Submit a Task
  831. * starpu_task_wait_for_all:: Wait for the termination of all Tasks
  832. @end menu
  833. @node struct starpu_codelet
  834. @subsection @code{struct starpu_codelet} -- StarPU codelet structure
  835. @table @asis
  836. @item @emph{Description}:
  837. The codelet structure describes a kernel that is possibly implemented on
  838. various targets.
  839. @item @emph{Fields}:
  840. @table @asis
  841. @item @code{where}:
  842. Indicates which types of processing units are able to execute the codelet.
  843. @code{STARPU_CPU|STARPU_CUDA} for instance indicates that the codelet is
  844. implemented for both CPU cores and CUDA devices while @code{STARPU_GORDON}
  845. indicates that it is only available on Cell SPUs.
  846. @item @code{cpu_func} (optional):
  847. Is a function pointer to the CPU implementation of the codelet. Its prototype
  848. must be: @code{void cpu_func(void *buffers[], void *cl_arg)}. The first
  849. argument being the array of data managed by the data management library, and
  850. the second argument is a pointer to the argument passed from the @code{cl_arg}
  851. field of the @code{starpu_task} structure.
  852. The @code{cpu_func} field is ignored if @code{STARPU_CPU} does not appear in
  853. the @code{where} field, it must be non-null otherwise.
  854. @item @code{cuda_func} (optional):
  855. Is a function pointer to the CUDA implementation of the codelet. @emph{This
  856. must be a host-function written in the CUDA runtime API}. Its prototype must
  857. be: @code{void cuda_func(void *buffers[], void *cl_arg);}. The @code{cuda_func}
  858. field is ignored if @code{STARPU_CUDA} does not appear in the @code{where}
  859. field, it must be non-null otherwise.
  860. @item @code{opencl_func} (optional):
  861. Is a function pointer to the OpenCL implementation of the codelet. Its
  862. prototype must be:
  863. @code{void opencl_func(starpu_data_interface_t *descr, void *arg);}.
  864. This pointer is ignored if @code{STARPU_OPENCL} does not appear in the
  865. @code{where} field, it must be non-null otherwise.
  866. @item @code{gordon_func} (optional):
  867. This is the index of the Cell SPU implementation within the Gordon library.
  868. TODO
  869. @item @code{nbuffers}:
  870. Specifies the number of arguments taken by the codelet. These arguments are
  871. managed by the DSM and are accessed from the @code{void *buffers[]}
  872. array. The constant argument passed with the @code{cl_arg} field of the
  873. @code{starpu_task} structure is not counted in this number. This value should
  874. not be above @code{STARPU_NMAXBUFS}.
  875. @item @code{model} (optional):
  876. This is a pointer to the performance model associated to this codelet. This
  877. optional field is ignored when set to @code{NULL}. TODO
  878. @end table
  879. @end table
  880. @node struct starpu_task
  881. @subsection @code{struct starpu_task} -- StarPU task structure
  882. @table @asis
  883. @item @emph{Description}:
  884. The @code{starpu_task} structure describes a task that can be offloaded on the various
  885. processing units managed by StarPU. It instantiates a codelet. It can either be
  886. allocated dynamically with the @code{starpu_task_create} method, or declared
  887. statically. In the latter case, the programmer has to zero the
  888. @code{starpu_task} structure and to fill the different fields properly. The
  889. indicated default values correspond to the configuration of a task allocated
  890. with @code{starpu_task_create}.
  891. @item @emph{Fields}:
  892. @table @asis
  893. @item @code{cl}:
  894. Is a pointer to the corresponding @code{starpu_codelet} data structure. This
  895. describes where the kernel should be executed, and supplies the appropriate
  896. implementations. When set to @code{NULL}, no code is executed during the tasks,
  897. such empty tasks can be useful for synchronization purposes.
  898. @item @code{buffers}:
  899. TODO
  900. @item @code{cl_arg} (optional) (default = NULL):
  901. This pointer is passed to the codelet through the second argument
  902. of the codelet implementation (e.g. @code{cpu_func} or @code{cuda_func}).
  903. In the specific case of the Cell processor, see the @code{cl_arg_size}
  904. argument.
  905. @item @code{cl_arg_size} (optional, Cell specific):
  906. In the case of the Cell processor, the @code{cl_arg} pointer is not directly
  907. given to the SPU function. A buffer of size @code{cl_arg_size} is allocated on
  908. the SPU. This buffer is then filled with the @code{cl_arg_size} bytes starting
  909. at address @code{cl_arg}. In this case, the argument given to the SPU codelet
  910. is therefore not the @code{cl_arg} pointer, but the address of the buffer in
  911. local store (LS) instead. This field is ignored for CPU, CUDA and OpenCL
  912. codelets.
  913. @item @code{callback_func} (optional) (default = @code{NULL}):
  914. This is a function pointer of prototype @code{void (*f)(void *)} which
  915. specifies a possible callback. If this pointer is non-null, the callback
  916. function is executed @emph{on the host} after the execution of the task. The
  917. callback is passed the value contained in the @code{callback_arg} field. No
  918. callback is executed if the field is set to @code{NULL}.
  919. @item @code{callback_arg} (optional) (default = @code{NULL}):
  920. This is the pointer passed to the callback function. This field is ignored if
  921. the @code{callback_func} is set to @code{NULL}.
  922. @item @code{use_tag} (optional) (default = 0):
  923. If set, this flag indicates that the task should be associated with the tag
  924. contained in the @code{tag_id} field. Tag allow the application to synchronize
  925. with the task and to express task dependencies easily.
  926. @item @code{tag_id}:
  927. This fields contains the tag associated to the task if the @code{use_tag} field
  928. was set, it is ignored otherwise.
  929. @item @code{synchronous}:
  930. If this flag is set, the @code{starpu_task_submit} function is blocking and
  931. returns only when the task has been executed (or if no worker is able to
  932. process the task). Otherwise, @code{starpu_task_submit} returns immediately.
  933. @item @code{priority} (optional) (default = @code{STARPU_DEFAULT_PRIO}):
  934. This field indicates a level of priority for the task. This is an integer value
  935. that must be set between @code{STARPU_MIN_PRIO} (for the least important
  936. tasks) and @code{STARPU_MAX_PRIO} (for the most important tasks) included.
  937. Default priority is @code{STARPU_DEFAULT_PRIO}. Scheduling strategies that
  938. take priorities into account can use this parameter to take better scheduling
  939. decisions, but the scheduling policy may also ignore it.
  940. @item @code{execute_on_a_specific_worker} (default = 0):
  941. If this flag is set, StarPU will bypass the scheduler and directly affect this
  942. task to the worker specified by the @code{workerid} field.
  943. @item @code{workerid} (optional):
  944. If the @code{execute_on_a_specific_worker} field is set, this field indicates
  945. which is the identifier of the worker that should process this task (as
  946. returned by @code{starpu_worker_get_id}). This field is ignored if
  947. @code{execute_on_a_specific_worker} field is set to 0.
  948. @item @code{detach} (optional) (default = 1):
  949. If this flag is set, it is not possible to synchronize with the task
  950. by the means of @code{starpu_task_wait} later on. Internal data structures
  951. are only guaranteed to be freed once @code{starpu_task_wait} is called if the
  952. flag is not set.
  953. @item @code{destroy} (optional) (default = 1):
  954. If this flag is set, the task structure will automatically be freed, either
  955. after the execution of the callback if the task is detached, or during
  956. @code{starpu_task_wait} otherwise. If this flag is not set, dynamically
  957. allocated data structures will not be freed until @code{starpu_task_destroy} is
  958. called explicitly. Setting this flag for a statically allocated task structure
  959. will result in undefined behaviour.
  960. @end table
  961. @end table
  962. @node starpu_task_init
  963. @subsection @code{starpu_task_init} -- Initialize a Task
  964. @table @asis
  965. @item @emph{Description}:
  966. Initialize a task structure with default values. This function is implicitly
  967. called by @code{starpu_task_create}. By default, tasks initialized with
  968. @code{starpu_task_init} must be deinitialized explicitly with
  969. @code{starpu_task_deinit}. Tasks can also be initialized statically, using the
  970. constant @code{STARPU_TASK_INITIALIZER}.
  971. @item @emph{Prototype}:
  972. @code{void starpu_task_init(struct starpu_task *task);}
  973. @end table
  974. @node starpu_task_create
  975. @subsection @code{starpu_task_create} -- Allocate and Initialize a Task
  976. @table @asis
  977. @item @emph{Description}:
  978. Allocate a task structure and initialize it with default values. Tasks
  979. allocated dynamically with @code{starpu_task_create} are automatically freed when the
  980. task is terminated. If the destroy flag is explicitly unset, the resources used
  981. by the task are freed by calling
  982. @code{starpu_task_destroy}.
  983. @item @emph{Prototype}:
  984. @code{struct starpu_task *starpu_task_create(void);}
  985. @end table
  986. @node starpu_task_deinit
  987. @subsection @code{starpu_task_deinit} -- Release all the resources used by a Task
  988. @table @asis
  989. @item @emph{Description}:
  990. Release all the structures automatically allocated to execute the task. This is
  991. called automatically by @code{starpu_task_destroy}, but the task structure itself is not
  992. freed. This should be used for statically allocated tasks for instance.
  993. @item @emph{Prototype}:
  994. @code{void starpu_task_deinit(struct starpu_task *task);}
  995. @end table
  996. @node starpu_task_destroy
  997. @subsection @code{starpu_task_destroy} -- Destroy a dynamically allocated Task
  998. @table @asis
  999. @item @emph{Description}:
  1000. Free the resource allocated during @code{starpu_task_create}. This function can be
  1001. called automatically after the execution of a task by setting the
  1002. @code{destroy} flag of the @code{starpu_task} structure (default behaviour).
  1003. Calling this function on a statically allocated task results in an undefined
  1004. behaviour.
  1005. @item @emph{Prototype}:
  1006. @code{void starpu_task_destroy(struct starpu_task *task);}
  1007. @end table
  1008. @node starpu_task_wait
  1009. @subsection @code{starpu_task_wait} -- Wait for the termination of a Task
  1010. @table @asis
  1011. @item @emph{Description}:
  1012. This function blocks until the task has been executed. It is not possible to
  1013. synchronize with a task more than once. It is not possible to wait for
  1014. synchronous or detached tasks.
  1015. @item @emph{Return value}:
  1016. Upon successful completion, this function returns 0. Otherwise, @code{-EINVAL}
  1017. indicates that the specified task was either synchronous or detached.
  1018. @item @emph{Prototype}:
  1019. @code{int starpu_task_wait(struct starpu_task *task);}
  1020. @end table
  1021. @node starpu_task_submit
  1022. @subsection @code{starpu_task_submit} -- Submit a Task
  1023. @table @asis
  1024. @item @emph{Description}:
  1025. This function submits a task to StarPU. Calling this function does
  1026. not mean that the task will be executed immediately as there can be data or task
  1027. (tag) dependencies that are not fulfilled yet: StarPU will take care of
  1028. scheduling this task with respect to such dependencies.
  1029. This function returns immediately if the @code{synchronous} field of the
  1030. @code{starpu_task} structure was set to 0, and block until the termination of
  1031. the task otherwise. It is also possible to synchronize the application with
  1032. asynchronous tasks by the means of tags, using the @code{starpu_tag_wait}
  1033. function for instance.
  1034. @item @emph{Return value}:
  1035. In case of success, this function returns 0, a return value of @code{-ENODEV}
  1036. means that there is no worker able to process this task (e.g. there is no GPU
  1037. available and this task is only implemented for CUDA devices).
  1038. @item @emph{Prototype}:
  1039. @code{int starpu_task_submit(struct starpu_task *task);}
  1040. @end table
  1041. @node starpu_task_wait_for_all
  1042. @subsection @code{starpu_task_wait_for_all} -- Wait for the termination of all Tasks
  1043. @table @asis
  1044. @item @emph{Description}:
  1045. This function blocks until all the tasks that were submitted are terminated.
  1046. @item @emph{Prototype}:
  1047. @code{void starpu_task_wait_for_all(void);}
  1048. @end table
  1049. @c Callbacks : what can we put in callbacks ?
  1050. @node Tags
  1051. @section Tags
  1052. @menu
  1053. * starpu_tag_t:: Task identifier
  1054. * starpu_tag_declare_deps:: Declare the Dependencies of a Tag
  1055. * starpu_tag_declare_deps_array:: Declare the Dependencies of a Tag
  1056. * starpu_tag_wait:: Block until a Tag is terminated
  1057. * starpu_tag_wait_array:: Block until a set of Tags is terminated
  1058. * starpu_tag_remove:: Destroy a Tag
  1059. * starpu_tag_notify_from_apps:: Feed a tag explicitly
  1060. @end menu
  1061. @node starpu_tag_t
  1062. @subsection @code{starpu_tag_t} -- Task identifier
  1063. @table @asis
  1064. @item @emph{Description}:
  1065. It is possible to associate a task with a unique ``tag'' and to express
  1066. dependencies between tasks by the means of those tags. To do so, fill the
  1067. @code{tag_id} field of the @code{starpu_task} structure with a tag number (can
  1068. be arbitrary) and set the @code{use_tag} field to 1.
  1069. If @code{starpu_tag_declare_deps} is called with this tag number, the task will
  1070. not be started until the tasks which holds the declared dependency tags are
  1071. completed.
  1072. @end table
  1073. @node starpu_tag_declare_deps
  1074. @subsection @code{starpu_tag_declare_deps} -- Declare the Dependencies of a Tag
  1075. @table @asis
  1076. @item @emph{Description}:
  1077. Specify the dependencies of the task identified by tag @code{id}. The first
  1078. argument specifies the tag which is configured, the second argument gives the
  1079. number of tag(s) on which @code{id} depends. The following arguments are the
  1080. tags which have to be terminated to unlock the task.
  1081. This function must be called before the associated task is submitted to StarPU
  1082. with @code{starpu_task_submit}.
  1083. @item @emph{Remark}
  1084. Because of the variable arity of @code{starpu_tag_declare_deps}, note that the
  1085. last arguments @emph{must} be of type @code{starpu_tag_t}: constant values
  1086. typically need to be explicitly casted. Using the
  1087. @code{starpu_tag_declare_deps_array} function avoids this hazard.
  1088. @item @emph{Prototype}:
  1089. @code{void starpu_tag_declare_deps(starpu_tag_t id, unsigned ndeps, ...);}
  1090. @item @emph{Example}:
  1091. @cartouche
  1092. @example
  1093. /* Tag 0x1 depends on tags 0x32 and 0x52 */
  1094. starpu_tag_declare_deps((starpu_tag_t)0x1,
  1095. 2, (starpu_tag_t)0x32, (starpu_tag_t)0x52);
  1096. @end example
  1097. @end cartouche
  1098. @end table
  1099. @node starpu_tag_declare_deps_array
  1100. @subsection @code{starpu_tag_declare_deps_array} -- Declare the Dependencies of a Tag
  1101. @table @asis
  1102. @item @emph{Description}:
  1103. This function is similar to @code{starpu_tag_declare_deps}, except that its
  1104. does not take a variable number of arguments but an array of tags of size
  1105. @code{ndeps}.
  1106. @item @emph{Prototype}:
  1107. @code{void starpu_tag_declare_deps_array(starpu_tag_t id, unsigned ndeps, starpu_tag_t *array);}
  1108. @item @emph{Example}:
  1109. @cartouche
  1110. @example
  1111. /* Tag 0x1 depends on tags 0x32 and 0x52 */
  1112. starpu_tag_t tag_array[2] = @{0x32, 0x52@};
  1113. starpu_tag_declare_deps_array((starpu_tag_t)0x1, 2, tag_array);
  1114. @end example
  1115. @end cartouche
  1116. @end table
  1117. @node starpu_tag_wait
  1118. @subsection @code{starpu_tag_wait} -- Block until a Tag is terminated
  1119. @table @asis
  1120. @item @emph{Description}:
  1121. This function blocks until the task associated to tag @code{id} has been
  1122. executed. This is a blocking call which must therefore not be called within
  1123. tasks or callbacks, but only from the application directly. It is possible to
  1124. synchronize with the same tag multiple times, as long as the
  1125. @code{starpu_tag_remove} function is not called. Note that it is still
  1126. possible to synchronize with a tag associated to a task which @code{starpu_task}
  1127. data structure was freed (e.g. if the @code{destroy} flag of the
  1128. @code{starpu_task} was enabled).
  1129. @item @emph{Prototype}:
  1130. @code{void starpu_tag_wait(starpu_tag_t id);}
  1131. @end table
  1132. @node starpu_tag_wait_array
  1133. @subsection @code{starpu_tag_wait_array} -- Block until a set of Tags is terminated
  1134. @table @asis
  1135. @item @emph{Description}:
  1136. This function is similar to @code{starpu_tag_wait} except that it blocks until
  1137. @emph{all} the @code{ntags} tags contained in the @code{id} array are
  1138. terminated.
  1139. @item @emph{Prototype}:
  1140. @code{void starpu_tag_wait_array(unsigned ntags, starpu_tag_t *id);}
  1141. @end table
  1142. @node starpu_tag_remove
  1143. @subsection @code{starpu_tag_remove} -- Destroy a Tag
  1144. @table @asis
  1145. @item @emph{Description}:
  1146. This function releases the resources associated to tag @code{id}. It can be
  1147. called once the corresponding task has been executed and when there is
  1148. no other tag that depend on this tag anymore.
  1149. @item @emph{Prototype}:
  1150. @code{void starpu_tag_remove(starpu_tag_t id);}
  1151. @end table
  1152. @node starpu_tag_notify_from_apps
  1153. @subsection @code{starpu_tag_notify_from_apps} -- Feed a Tag explicitly
  1154. @table @asis
  1155. @item @emph{Description}:
  1156. This function explicitly unlocks tag @code{id}. It may be useful in the
  1157. case of applications which execute part of their computation outside StarPU
  1158. tasks (e.g. third-party libraries). It is also provided as a
  1159. convenient tool for the programmer, for instance to entirely construct the task
  1160. DAG before actually giving StarPU the opportunity to execute the tasks.
  1161. @item @emph{Prototype}:
  1162. @code{void starpu_tag_notify_from_apps(starpu_tag_t id);}
  1163. @end table
  1164. @node CUDA extensions
  1165. @section CUDA extensions
  1166. @c void starpu_data_malloc_pinned_if_possible(float **A, size_t dim);
  1167. @c starpu_helper_cublas_init TODO
  1168. @c starpu_helper_cublas_shutdown TODO
  1169. @menu
  1170. * starpu_cuda_get_local_stream:: Get current worker's CUDA stream
  1171. * starpu_helper_cublas_init:: Initialize CUBLAS on every CUDA device
  1172. * starpu_helper_cublas_shutdown:: Deinitialize CUBLAS on every CUDA device
  1173. @end menu
  1174. @node starpu_cuda_get_local_stream
  1175. @subsection @code{starpu_cuda_get_local_stream} -- Get current worker's CUDA stream
  1176. @table @asis
  1177. @item @emph{Description}:
  1178. StarPU provides a stream for every CUDA device controlled by StarPU. This
  1179. function is only provided for convenience so that programmers can easily use
  1180. asynchronous operations within codelets without having to create a stream by
  1181. hand. Note that the application is not forced to use the stream provided by
  1182. @code{starpu_cuda_get_local_stream} and may also create its own streams.
  1183. @item @emph{Prototype}:
  1184. @code{cudaStream_t *starpu_cuda_get_local_stream(void);}
  1185. @end table
  1186. @node starpu_helper_cublas_init
  1187. @subsection @code{starpu_helper_cublas_init} -- Initialize CUBLAS on every CUDA device
  1188. @table @asis
  1189. @item @emph{Description}:
  1190. The CUBLAS library must be initialized prior to any CUBLAS call. Calling
  1191. @code{starpu_helper_cublas_init} will initialize CUBLAS on every CUDA device
  1192. controlled by StarPU. This call blocks until CUBLAS has been properly
  1193. initialized on every device.
  1194. @item @emph{Prototype}:
  1195. @code{void starpu_helper_cublas_init(void);}
  1196. @end table
  1197. @node starpu_helper_cublas_shutdown
  1198. @subsection @code{starpu_helper_cublas_shutdown} -- Deinitialize CUBLAS on every CUDA device
  1199. @table @asis
  1200. @item @emph{Description}:
  1201. This function synchronously deinitializes the CUBLAS library on every CUDA device.
  1202. @item @emph{Prototype}:
  1203. @code{void starpu_helper_cublas_shutdown(void);}
  1204. @end table
  1205. @node OpenCL extensions
  1206. @section OpenCL extensions
  1207. @menu
  1208. * Enabling OpenCL:: Enabling OpenCL
  1209. * Compiling OpenCL codelets:: Compiling OpenCL codelets
  1210. @end menu
  1211. @node Enabling OpenCL
  1212. @subsection Enabling OpenCL
  1213. On GPU devices which can run both CUDA and OpenCL, CUDA will be
  1214. enabled by default. To enable OpenCL, you need either to disable CUDA
  1215. when configuring StarPU:
  1216. @example
  1217. % ./configure --disable-cuda
  1218. @end example
  1219. or when running applications:
  1220. @example
  1221. % STARPU_NCUDA=0 ./application
  1222. @end example
  1223. OpenCL will automatically be started on any device not yet used by
  1224. CUDA. So on a machine running 4 GPUS, it is therefore possible to
  1225. enable CUDA on 2 devices, and OpenCL on the 2 other devices by doing
  1226. so:
  1227. @example
  1228. % STARPU_NCUDA=2 ./application
  1229. @end example
  1230. @node Compiling OpenCL codelets
  1231. @subsection Compiling OpenCL codelets
  1232. TODO
  1233. @node Cell extensions
  1234. @section Cell extensions
  1235. nothing yet.
  1236. @node Miscellaneous helpers
  1237. @section Miscellaneous helpers
  1238. @menu
  1239. * starpu_execute_on_each_worker:: Execute a function on a subset of workers
  1240. @end menu
  1241. @node starpu_execute_on_each_worker
  1242. @subsection @code{starpu_execute_on_each_worker} -- Execute a function on a subset of workers
  1243. @table @asis
  1244. @item @emph{Description}:
  1245. When calling this method, the offloaded function specified by the first argument is
  1246. executed by every StarPU worker that may execute the function.
  1247. The second argument is passed to the offloaded function.
  1248. The last argument specifies on which types of processing units the function
  1249. should be executed. Similarly to the @code{where} field of the
  1250. @code{starpu_codelet} structure, it is possible to specify that the function
  1251. should be executed on every CUDA device and every CPU by passing
  1252. @code{STARPU_CPU|STARPU_CUDA}.
  1253. This function blocks until the function has been executed on every appropriate
  1254. processing units, so that it may not be called from a callback function for
  1255. instance.
  1256. @item @emph{Prototype}:
  1257. @code{void starpu_execute_on_each_worker(void (*func)(void *), void *arg, uint32_t where);}
  1258. @end table
  1259. @c ---------------------------------------------------------------------
  1260. @c Basic Examples
  1261. @c ---------------------------------------------------------------------
  1262. @node Basic Examples
  1263. @chapter Basic Examples
  1264. @menu
  1265. * Compiling and linking options::
  1266. * Hello World:: Submitting Tasks
  1267. * Scaling a Vector:: Manipulating Data
  1268. * Vector Scaling on an Hybrid CPU/GPU Machine:: Handling Heterogeneous Architectures
  1269. @end menu
  1270. @node Compiling and linking options
  1271. @section Compiling and linking options
  1272. Let's suppose StarPU has been installed in the directory
  1273. @code{$STARPU_DIR}. As explained in @ref{Setting flags for compiling and linking applications},
  1274. the variable @code{PKG_CONFIG_PATH} needs to be set. It is also
  1275. necessary to set the variable @code{LD_LIBRARY_PATH} to locate dynamic
  1276. libraries at runtime.
  1277. @example
  1278. % PKG_CONFIG_PATH=$STARPU_DIR/lib/pkgconfig:$PKG_CONFIG_PATH
  1279. % LD_LIBRARY_PATH=$STARPU_DIR/lib:$LD_LIBRARY_PATH
  1280. @end example
  1281. The Makefile could for instance contain the following lines to define which
  1282. options must be given to the compiler and to the linker:
  1283. @cartouche
  1284. @example
  1285. CFLAGS += $$(pkg-config --cflags libstarpu)
  1286. LDFLAGS += $$(pkg-config --libs libstarpu)
  1287. @end example
  1288. @end cartouche
  1289. @node Hello World
  1290. @section Hello World
  1291. @menu
  1292. * Required Headers::
  1293. * Defining a Codelet::
  1294. * Submitting a Task::
  1295. * Execution of Hello World::
  1296. @end menu
  1297. In this section, we show how to implement a simple program that submits a task to StarPU.
  1298. @node Required Headers
  1299. @subsection Required Headers
  1300. The @code{starpu.h} header should be included in any code using StarPU.
  1301. @cartouche
  1302. @example
  1303. #include <starpu.h>
  1304. @end example
  1305. @end cartouche
  1306. @node Defining a Codelet
  1307. @subsection Defining a Codelet
  1308. @cartouche
  1309. @example
  1310. void cpu_func(void *buffers[], void *cl_arg)
  1311. @{
  1312. float *array = cl_arg;
  1313. printf("Hello world (array = @{%f, %f@} )\n", array[0], array[1]);
  1314. @}
  1315. starpu_codelet cl =
  1316. @{
  1317. .where = STARPU_CPU,
  1318. .cpu_func = cpu_func,
  1319. .nbuffers = 0
  1320. @};
  1321. @end example
  1322. @end cartouche
  1323. A codelet is a structure that represents a computational kernel. Such a codelet
  1324. may contain an implementation of the same kernel on different architectures
  1325. (e.g. CUDA, Cell's SPU, x86, ...).
  1326. The @code{nbuffers} field specifies the number of data buffers that are
  1327. manipulated by the codelet: here the codelet does not access or modify any data
  1328. that is controlled by our data management library. Note that the argument
  1329. passed to the codelet (the @code{cl_arg} field of the @code{starpu_task}
  1330. structure) does not count as a buffer since it is not managed by our data
  1331. management library.
  1332. @c TODO need a crossref to the proper description of "where" see bla for more ...
  1333. We create a codelet which may only be executed on the CPUs. The @code{where}
  1334. field is a bitmask that defines where the codelet may be executed. Here, the
  1335. @code{STARPU_CPU} value means that only CPUs can execute this codelet
  1336. (@pxref{Codelets and Tasks} for more details on this field).
  1337. When a CPU core executes a codelet, it calls the @code{cpu_func} function,
  1338. which @emph{must} have the following prototype:
  1339. @code{void (*cpu_func)(void *buffers[], void *cl_arg);}
  1340. In this example, we can ignore the first argument of this function which gives a
  1341. description of the input and output buffers (e.g. the size and the location of
  1342. the matrices). The second argument is a pointer to a buffer passed as an
  1343. argument to the codelet by the means of the @code{cl_arg} field of the
  1344. @code{starpu_task} structure.
  1345. @c TODO rewrite so that it is a little clearer ?
  1346. Be aware that this may be a pointer to a
  1347. @emph{copy} of the actual buffer, and not the pointer given by the programmer:
  1348. if the codelet modifies this buffer, there is no guarantee that the initial
  1349. buffer will be modified as well: this for instance implies that the buffer
  1350. cannot be used as a synchronization medium.
  1351. @node Submitting a Task
  1352. @subsection Submitting a Task
  1353. @cartouche
  1354. @example
  1355. void callback_func(void *callback_arg)
  1356. @{
  1357. printf("Callback function (arg %x)\n", callback_arg);
  1358. @}
  1359. int main(int argc, char **argv)
  1360. @{
  1361. /* @b{initialize StarPU} */
  1362. starpu_init(NULL);
  1363. struct starpu_task *task = starpu_task_create();
  1364. task->cl = &cl; /* @b{Pointer to the codelet defined above} */
  1365. float array[2] = @{1.0f, -1.0f@};
  1366. task->cl_arg = &array;
  1367. task->cl_arg_size = 2*sizeof(float);
  1368. task->callback_func = callback_func;
  1369. task->callback_arg = 0x42;
  1370. /* @b{starpu_task_submit will be a blocking call} */
  1371. task->synchronous = 1;
  1372. /* @b{submit the task to StarPU} */
  1373. starpu_task_submit(task);
  1374. /* @b{terminate StarPU} */
  1375. starpu_shutdown();
  1376. return 0;
  1377. @}
  1378. @end example
  1379. @end cartouche
  1380. Before submitting any tasks to StarPU, @code{starpu_init} must be called. The
  1381. @code{NULL} argument specifies that we use default configuration. Tasks cannot
  1382. be submitted after the termination of StarPU by a call to
  1383. @code{starpu_shutdown}.
  1384. In the example above, a task structure is allocated by a call to
  1385. @code{starpu_task_create}. This function only allocates and fills the
  1386. corresponding structure with the default settings (@pxref{starpu_task_create}),
  1387. but it does not submit the task to StarPU.
  1388. @c not really clear ;)
  1389. The @code{cl} field is a pointer to the codelet which the task will
  1390. execute: in other words, the codelet structure describes which computational
  1391. kernel should be offloaded on the different architectures, and the task
  1392. structure is a wrapper containing a codelet and the piece of data on which the
  1393. codelet should operate.
  1394. The optional @code{cl_arg} field is a pointer to a buffer (of size
  1395. @code{cl_arg_size}) with some parameters for the kernel
  1396. described by the codelet. For instance, if a codelet implements a computational
  1397. kernel that multiplies its input vector by a constant, the constant could be
  1398. specified by the means of this buffer.
  1399. Once a task has been executed, an optional callback function can be called.
  1400. While the computational kernel could be offloaded on various architectures, the
  1401. callback function is always executed on a CPU. The @code{callback_arg}
  1402. pointer is passed as an argument of the callback. The prototype of a callback
  1403. function must be:
  1404. @code{void (*callback_function)(void *);}
  1405. If the @code{synchronous} field is non-null, task submission will be
  1406. synchronous: the @code{starpu_task_submit} function will not return until the
  1407. task was executed. Note that the @code{starpu_shutdown} method does not
  1408. guarantee that asynchronous tasks have been executed before it returns.
  1409. @node Execution of Hello World
  1410. @subsection Execution of Hello World
  1411. @example
  1412. % make helloWorld
  1413. cc $(pkg-config --cflags libstarpu) $(pkg-config --libs libstarpu) helloWorld.c -o helloWorld
  1414. % ./helloWorld
  1415. Hello world (array = @{1.000000, -1.000000@} )
  1416. Callback function (arg 42)
  1417. @end example
  1418. @node Scaling a Vector
  1419. @section Manipulating Data: Scaling a Vector
  1420. The previous example has shown how to submit tasks. In this section,
  1421. we show how StarPU tasks can manipulate data. The full source code for
  1422. this example is given in @ref{Full source code for the 'Scaling a Vector' example}.
  1423. @menu
  1424. * Source code of Vector Scaling::
  1425. * Execution of Vector Scaling::
  1426. @end menu
  1427. @node Source code of Vector Scaling
  1428. @subsection Source code of Vector Scaling
  1429. Programmers can describe the data layout of their application so that StarPU is
  1430. responsible for enforcing data coherency and availability across the machine.
  1431. Instead of handling complex (and non-portable) mechanisms to perform data
  1432. movements, programmers only declare which piece of data is accessed and/or
  1433. modified by a task, and StarPU makes sure that when a computational kernel
  1434. starts somewhere (e.g. on a GPU), its data are available locally.
  1435. Before submitting those tasks, the programmer first needs to declare the
  1436. different pieces of data to StarPU using the @code{starpu_*_data_register}
  1437. functions. To ease the development of applications for StarPU, it is possible
  1438. to describe multiple types of data layout. A type of data layout is called an
  1439. @b{interface}. By default, there are different interfaces available in StarPU:
  1440. here we will consider the @b{vector interface}.
  1441. The following lines show how to declare an array of @code{NX} elements of type
  1442. @code{float} using the vector interface:
  1443. @cartouche
  1444. @example
  1445. float vector[NX];
  1446. starpu_data_handle vector_handle;
  1447. starpu_vector_data_register(&vector_handle, 0, (uintptr_t)vector, NX,
  1448. sizeof(float));
  1449. @end example
  1450. @end cartouche
  1451. The first argument, called the @b{data handle}, is an opaque pointer which
  1452. designates the array in StarPU. This is also the structure which is used to
  1453. describe which data is used by a task. The second argument is the node number
  1454. where the data currently resides. Here it is 0 since the @code{vector} array is in
  1455. the main memory. Then comes the pointer @code{vector} where the data can be found,
  1456. the number of elements in the vector and the size of each element.
  1457. It is possible to construct a StarPU task that will manipulate the
  1458. vector and a constant factor.
  1459. @cartouche
  1460. @example
  1461. float factor = 3.0;
  1462. struct starpu_task *task = starpu_task_create();
  1463. task->cl = &cl; /* @b{Pointer to the codelet defined below} */
  1464. task->callback_func = NULL;
  1465. task->buffers[0].handle = vector_handle; /* @b{First parameter of the codelet} */
  1466. task->buffers[0].mode = STARPU_RW;
  1467. task->cl_arg = &factor;
  1468. task->cl_arg_size = sizeof(float);
  1469. task->synchronous = 1;
  1470. starpu_task_submit(task);
  1471. @end example
  1472. @end cartouche
  1473. Since the factor is constant, it does not need a preliminary declaration, and
  1474. can just be passed through the @code{cl_arg} pointer like in the previous
  1475. example. The vector parameter is described by its handle.
  1476. There are two fields in each element of the @code{buffers} array.
  1477. @code{handle} is the handle of the data, and @code{mode} specifies how the
  1478. kernel will access the data (@code{STARPU_R} for read-only, @code{STARPU_W} for
  1479. write-only and @code{STARPU_RW} for read and write access).
  1480. The definition of the codelet can be written as follows:
  1481. @cartouche
  1482. @example
  1483. void scal_func(void *buffers[], void *cl_arg)
  1484. @{
  1485. unsigned i;
  1486. float *factor = cl_arg;
  1487. struct starpu_vector_interface_s *vector = buffers[0];
  1488. /* length of the vector */
  1489. unsigned n = STARPU_GET_VECTOR_NX(vector);
  1490. /* local copy of the vector pointer */
  1491. float *val = (float *)STARPU_GET_VECTOR_PTR(vector);
  1492. for (i = 0; i < n; i++)
  1493. val[i] *= *factor;
  1494. @}
  1495. starpu_codelet cl = @{
  1496. .where = STARPU_CPU,
  1497. .cpu_func = scal_func,
  1498. .nbuffers = 1
  1499. @};
  1500. @end example
  1501. @end cartouche
  1502. The second argument of the @code{scal_func} function contains a pointer to the
  1503. parameters of the codelet (given in @code{task->cl_arg}), so that we read the
  1504. constant factor from this pointer. The first argument is an array that gives
  1505. a description of all the buffers passed in the @code{task->buffers}@ array. The
  1506. size of this array is given by the @code{nbuffers} field of the codelet
  1507. structure. For the sake of generality, this array contains pointers to the
  1508. different interfaces describing each buffer. In the case of the @b{vector
  1509. interface}, the location of the vector (resp. its length) is accessible in the
  1510. @code{ptr} (resp. @code{nx}) of this array. Since the vector is accessed in a
  1511. read-write fashion, any modification will automatically affect future accesses
  1512. to this vector made by other tasks.
  1513. @node Execution of Vector Scaling
  1514. @subsection Execution of Vector Scaling
  1515. @example
  1516. % make vector
  1517. cc $(pkg-config --cflags libstarpu) $(pkg-config --libs libstarpu) vector.c -o vector
  1518. % ./vector
  1519. 0.000000 3.000000 6.000000 9.000000 12.000000
  1520. @end example
  1521. @node Vector Scaling on an Hybrid CPU/GPU Machine
  1522. @section Vector Scaling on an Hybrid CPU/GPU Machine
  1523. Contrary to the previous examples, the task submitted in this example may not
  1524. only be executed by the CPUs, but also by a CUDA device.
  1525. @menu
  1526. * Source code of Hybrid Vector Scaling::
  1527. * Compilation and execution of Hybrid Vector Scaling::
  1528. @end menu
  1529. @node Source code of Hybrid Vector Scaling
  1530. @subsection Source code of Hybrid Vector Scaling
  1531. The CUDA implementation can be written as follows. It needs to be
  1532. compiled with a CUDA compiler such as nvcc, the NVIDIA CUDA compiler
  1533. driver.
  1534. @cartouche
  1535. @example
  1536. #include <starpu.h>
  1537. static __global__ void vector_mult_cuda(float *val, unsigned n,
  1538. float factor)
  1539. @{
  1540. unsigned i;
  1541. for(i = 0 ; i < n ; i++)
  1542. val[i] *= factor;
  1543. @}
  1544. extern "C" void scal_cuda_func(void *buffers[], void *_args)
  1545. @{
  1546. float *factor = (float *)_args;
  1547. struct starpu_vector_interface_s *vector = (struct starpu_vector_interface_s *) buffers[0];
  1548. /* length of the vector */
  1549. unsigned n = STARPU_GET_VECTOR_NX(vector);
  1550. /* local copy of the vector pointer */
  1551. float *val = (float *)STARPU_GET_VECTOR_PTR(vector);
  1552. /* TODO: use more blocks and threads in blocks */
  1553. vector_mult_cuda<<<1,1>>>(val, n, *factor);
  1554. cudaThreadSynchronize();
  1555. @}
  1556. @end example
  1557. @end cartouche
  1558. The CPU implementation is the same as in the previous section.
  1559. Here is the source of the main application. You can notice the value of the
  1560. field @code{where} for the codelet. We specify
  1561. @code{STARPU_CPU|STARPU_CUDA} to indicate to StarPU that the codelet
  1562. can be executed either on a CPU or on a CUDA device.
  1563. @cartouche
  1564. @example
  1565. #include <starpu.h>
  1566. #define NX 5
  1567. extern void scal_cuda_func(void *buffers[], void *_args);
  1568. extern void scal_func(void *buffers[], void *_args);
  1569. /* @b{Definition of the codelet} */
  1570. static starpu_codelet cl = @{
  1571. .where = STARPU_CPU|STARPU_CUDA; /* @b{It can be executed on a CPU} */
  1572. /* @b{or on a CUDA device} */
  1573. .cuda_func = scal_cuda_func;
  1574. .cpu_func = scal_func;
  1575. .nbuffers = 1;
  1576. @}
  1577. int main(int argc, char **argv)
  1578. @{
  1579. float *vector;
  1580. int i, ret;
  1581. float factor=3.0;
  1582. struct starpu_task *task;
  1583. starpu_data_handle vector_handle;
  1584. starpu_init(NULL); /* @b{Initialising StarPU} */
  1585. vector = (float*)malloc(NX*sizeof(float));
  1586. assert(vector);
  1587. for(i=0 ; i<NX ; i++) vector[i] = i;
  1588. @end example
  1589. @end cartouche
  1590. @cartouche
  1591. @example
  1592. /* @b{Registering data within StarPU} */
  1593. starpu_vector_data_register(&vector_handle, 0, (uintptr_t)vector,
  1594. NX, sizeof(float));
  1595. /* @b{Definition of the task} */
  1596. task = starpu_task_create();
  1597. task->cl = &cl;
  1598. task->callback_func = NULL;
  1599. task->buffers[0].handle = vector_handle;
  1600. task->buffers[0].mode = STARPU_RW;
  1601. task->cl_arg = &factor;
  1602. task->cl_arg_size = sizeof(float);
  1603. @end example
  1604. @end cartouche
  1605. @cartouche
  1606. @example
  1607. /* @b{Submitting the task} */
  1608. ret = starpu_task_submit(task);
  1609. if (ret == -ENODEV) @{
  1610. fprintf(stderr, "No worker may execute this task\n");
  1611. return 1;
  1612. @}
  1613. /* @b{Waiting for its termination} */
  1614. starpu_task_wait_for_all();
  1615. /* @b{Update the vector in RAM} */
  1616. starpu_data_sync_with_mem(vector_handle, STARPU_R);
  1617. @end example
  1618. @end cartouche
  1619. @cartouche
  1620. @example
  1621. /* @b{Access the data} */
  1622. for(i=0 ; i<NX; i++) @{
  1623. fprintf(stderr, "%f ", vector[i]);
  1624. @}
  1625. fprintf(stderr, "\n");
  1626. /* @b{Release the data and shutdown StarPU} */
  1627. starpu_data_release_from_mem(vector_handle);
  1628. starpu_shutdown();
  1629. return 0;
  1630. @}
  1631. @end example
  1632. @end cartouche
  1633. @node Compilation and execution of Hybrid Vector Scaling
  1634. @subsection Compilation and execution of Hybrid Vector Scaling
  1635. The Makefile given at the beginning of the section must be extended to
  1636. give the rules to compile the CUDA source code.
  1637. @cartouche
  1638. @example
  1639. CFLAGS += $(shell pkg-config --cflags libstarpu)
  1640. LDFLAGS += $(shell pkg-config --libs libstarpu)
  1641. CC = gcc
  1642. vector: vector.o vector_cpu.o vector_cuda.o
  1643. %.o: %.cu
  1644. nvcc $(CFLAGS) $< -c $@
  1645. clean:
  1646. rm -f vector *.o
  1647. @end example
  1648. @end cartouche
  1649. @example
  1650. % make
  1651. @end example
  1652. and to execute it, with the default configuration:
  1653. @example
  1654. % ./vector
  1655. 0.000000 3.000000 6.000000 9.000000 12.000000
  1656. @end example
  1657. or for example, by disabling CPU devices:
  1658. @example
  1659. % STARPU_NCPUS=0 ./vector
  1660. 0.000000 3.000000 6.000000 9.000000 12.000000
  1661. @end example
  1662. or by disabling CUDA devices:
  1663. @example
  1664. % STARPU_NCUDA=0 ./vector
  1665. 0.000000 3.000000 6.000000 9.000000 12.000000
  1666. @end example
  1667. @c TODO: Add performance model example (and update basic_examples)
  1668. @c ---------------------------------------------------------------------
  1669. @c Advanced Topics
  1670. @c ---------------------------------------------------------------------
  1671. @c @node Advanced Topics
  1672. @c @chapter Advanced Topics
  1673. @c ---------------------------------------------------------------------
  1674. @c Appendices
  1675. @c ---------------------------------------------------------------------
  1676. @c ---------------------------------------------------------------------
  1677. @c Full source code for the 'Scaling a Vector' example
  1678. @c ---------------------------------------------------------------------
  1679. @node Full source code for the 'Scaling a Vector' example
  1680. @appendix Full source code for the 'Scaling a Vector' example
  1681. @example
  1682. #include <starpu.h>
  1683. void scal_func(void *buffers[], void *cl_arg)
  1684. @{
  1685. unsigned i;
  1686. float *factor = cl_arg;
  1687. struct starpu_vector_interface_s *vector = buffers[0];
  1688. /* length of the vector */
  1689. unsigned n = STARPU_GET_VECTOR_NX(vector);
  1690. /* local copy of the vector pointer */
  1691. float *val = (float *)STARPU_GET_VECTOR_PTR(vector);
  1692. for (i = 0; i < n; i++)
  1693. val[i] *= *factor;
  1694. @}
  1695. starpu_codelet cl = @{
  1696. .where = STARPU_CPU,
  1697. .cpu_func = scal_func,
  1698. .nbuffers = 1
  1699. @};
  1700. #define NX 5
  1701. int main(int argc, char **argv)
  1702. @{
  1703. float vector[NX];
  1704. starpu_data_handle vector_handle;
  1705. float factor = 3.0;
  1706. int i;
  1707. starpu_init(NULL);
  1708. for(i=0 ; i<NX; i++) vector[i] = i;
  1709. starpu_vector_data_register(&vector_handle, 0, (uintptr_t)vector, NX, sizeof(float));
  1710. struct starpu_task *task = starpu_task_create();
  1711. task->cl = &cl;
  1712. task->callback_func = NULL;
  1713. task->buffers[0].handle = vector_handle;
  1714. task->buffers[0].mode = STARPU_RW;
  1715. task->cl_arg = &factor;
  1716. task->cl_arg_size = sizeof(float);
  1717. task->synchronous = 1;
  1718. starpu_task_submit(task);
  1719. for(i=0 ; i<NX ; i++) @{
  1720. fprintf(stderr, "%f ", vector[i]);
  1721. @}
  1722. fprintf(stderr, "\n");
  1723. starpu_shutdown();
  1724. return 0;
  1725. @}
  1726. @end example
  1727. @bye