starpu_data.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-2019 Université de Bordeaux
  4. * Copyright (C) 2011-2013,2016,2017 Inria
  5. * Copyright (C) 2010-2015,2017,2019 CNRS
  6. *
  7. * StarPU is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation; either version 2.1 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * StarPU is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. *
  16. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  17. */
  18. #ifndef __STARPU_DATA_H__
  19. #define __STARPU_DATA_H__
  20. #include <starpu.h>
  21. #ifdef __cplusplus
  22. extern "C"
  23. {
  24. #endif
  25. /**
  26. @defgroup API_Data_Management Data Management
  27. @brief Data management facilities provided by StarPU. We show how
  28. to use existing data interfaces in \ref API_Data_Interfaces, but
  29. developers can design their own data interfaces if required.
  30. @{
  31. */
  32. struct _starpu_data_state;
  33. /**
  34. StarPU uses ::starpu_data_handle_t as an opaque handle to manage a
  35. piece of data. Once a piece of data has been registered to StarPU,
  36. it is associated to a ::starpu_data_handle_t which keeps track of
  37. the state of the piece of data over the entire machine, so that we
  38. can maintain data consistency and locate data replicates for
  39. instance.
  40. */
  41. typedef struct _starpu_data_state* starpu_data_handle_t;
  42. /**
  43. Describe a StarPU data access mode
  44. Note: when adding a flag here, update
  45. _starpu_detect_implicit_data_deps_with_handle
  46. Note: other STARPU_* values in include/starpu_task_util.h
  47. */
  48. enum starpu_data_access_mode
  49. {
  50. STARPU_NONE=0, /**< todo */
  51. STARPU_R=(1<<0), /**< read-only mode */
  52. STARPU_W=(1<<1), /**< write-only mode */
  53. STARPU_RW=(STARPU_R|STARPU_W), /**< read-write mode. Equivalent to ::STARPU_R|::STARPU_W */
  54. STARPU_SCRATCH=(1<<2), /**< A temporary buffer is allocated
  55. for the task, but StarPU does not
  56. enforce data consistency---i.e. each
  57. device has its own buffer,
  58. independently from each other (even
  59. for CPUs), and no data transfer is
  60. ever performed. This is useful for
  61. temporary variables to avoid
  62. allocating/freeing buffers inside
  63. each task. Currently, no behavior is
  64. defined concerning the relation with
  65. the ::STARPU_R and ::STARPU_W modes
  66. and the value provided at
  67. registration --- i.e., the value of
  68. the scratch buffer is undefined at
  69. entry of the codelet function. It
  70. is being considered for future
  71. extensions at least to define the
  72. initial value. For now, data to be
  73. used in ::STARPU_SCRATCH mode should
  74. be registered with node -1 and a
  75. <c>NULL</c> pointer, since the value
  76. of the provided buffer is simply
  77. ignored for now.
  78. */
  79. STARPU_REDUX=(1<<3), /**< todo */
  80. STARPU_COMMUTE=(1<<4), /**< ::STARPU_COMMUTE can be passed
  81. along ::STARPU_W or ::STARPU_RW to
  82. express that StarPU can let tasks
  83. commute, which is useful e.g. when
  84. bringing a contribution into some
  85. data, which can be done in any order
  86. (but still require sequential
  87. consistency against reads or
  88. non-commutative writes).
  89. */
  90. STARPU_SSEND=(1<<5), /**< used in starpu_mpi_insert_task() to
  91. specify the data has to be sent using
  92. a synchronous and non-blocking mode
  93. (see starpu_mpi_issend())
  94. */
  95. STARPU_LOCALITY=(1<<6), /**< used to tell the scheduler which
  96. data is the most important for the
  97. task, and should thus be used to
  98. try to group tasks on the same core
  99. or cache, etc. For now only the ws
  100. and lws schedulers take this flag
  101. into account, and only when rebuild
  102. with \c USE_LOCALITY flag defined in
  103. the
  104. src/sched_policies/work_stealing_policy.c
  105. source code.
  106. */
  107. STARPU_ACCESS_MODE_MAX=(1<<7) /**< todo */
  108. };
  109. struct starpu_data_interface_ops;
  110. /** Set the name of the data, to be shown in various profiling tools. */
  111. void starpu_data_set_name(starpu_data_handle_t handle, const char *name);
  112. /**
  113. Set the coordinates of the data, to be shown in various profiling
  114. tools. \p dimensions is the size of the \p dims array. This can be
  115. for instance the tile coordinates within a big matrix.
  116. */
  117. void starpu_data_set_coordinates_array(starpu_data_handle_t handle, int dimensions, int dims[]);
  118. /**
  119. Set the coordinates of the data, to be shown in various profiling
  120. tools. \p dimensions is the number of subsequent \c int parameters.
  121. This can be for instance the tile coordinates within a big matrix.
  122. */
  123. void starpu_data_set_coordinates(starpu_data_handle_t handle, unsigned dimensions, ...);
  124. /**
  125. Unregister a data \p handle from StarPU. If the data was
  126. automatically allocated by StarPU because the home node was -1, all
  127. automatically allocated buffers are freed. Otherwise, a valid copy
  128. of the data is put back into the home node in the buffer that was
  129. initially registered. Using a data handle that has been
  130. unregistered from StarPU results in an undefined behaviour. In case
  131. we do not need to update the value of the data in the home node, we
  132. can use the function starpu_data_unregister_no_coherency() instead.
  133. */
  134. void starpu_data_unregister(starpu_data_handle_t handle);
  135. /**
  136. Similar to starpu_data_unregister(), except that StarPU does not
  137. put back a valid copy into the home node, in the buffer that was
  138. initially registered.
  139. */
  140. void starpu_data_unregister_no_coherency(starpu_data_handle_t handle);
  141. /**
  142. Destroy the data \p handle once it is no longer needed by any
  143. submitted task. No coherency is assumed.
  144. */
  145. void starpu_data_unregister_submit(starpu_data_handle_t handle);
  146. /**
  147. Destroy all replicates of the data \p handle immediately. After
  148. data invalidation, the first access to \p handle must be performed
  149. in ::STARPU_W mode. Accessing an invalidated data in ::STARPU_R
  150. mode results in undefined behaviour.
  151. */
  152. void starpu_data_invalidate(starpu_data_handle_t handle);
  153. /**
  154. Submit invalidation of the data \p handle after completion of
  155. previously submitted tasks.
  156. */
  157. void starpu_data_invalidate_submit(starpu_data_handle_t handle);
  158. /**
  159. Specify that the data \p handle can be discarded without impacting
  160. the application.
  161. */
  162. void starpu_data_advise_as_important(starpu_data_handle_t handle, unsigned is_important);
  163. /**
  164. @name Access registered data from the application
  165. @{
  166. */
  167. /**
  168. This macro can be used to acquire data, but not require it to be
  169. available on a given node, only enforce R/W dependencies. This can
  170. for instance be used to wait for tasks which produce the data, but
  171. without requesting a fetch to the main memory.
  172. */
  173. #define STARPU_ACQUIRE_NO_NODE -1
  174. /**
  175. Similar to ::STARPU_ACQUIRE_NO_NODE, but will lock the data on all
  176. nodes, preventing them from being evicted for instance. This is
  177. mostly useful inside StarPU only.
  178. */
  179. #define STARPU_ACQUIRE_NO_NODE_LOCK_ALL -2
  180. /**
  181. The application must call this function prior to accessing
  182. registered data from main memory outside tasks. StarPU ensures that
  183. the application will get an up-to-date copy of \p handle in main
  184. memory located where the data was originally registered, and that
  185. all concurrent accesses (e.g. from tasks) will be consistent with
  186. the access mode specified with \p mode. starpu_data_release() must
  187. be called once the application no longer needs to access the piece
  188. of data. Note that implicit data dependencies are also enforced by
  189. starpu_data_acquire(), i.e. starpu_data_acquire() will wait for all
  190. tasks scheduled to work on the data, unless they have been disabled
  191. explictly by calling
  192. starpu_data_set_default_sequential_consistency_flag() or
  193. starpu_data_set_sequential_consistency_flag().
  194. starpu_data_acquire() is a blocking call, so that it cannot be
  195. called from tasks or from their callbacks (in that case,
  196. starpu_data_acquire() returns <c>-EDEADLK</c>). Upon successful
  197. completion, this function returns 0.
  198. */
  199. int starpu_data_acquire(starpu_data_handle_t handle, enum starpu_data_access_mode mode);
  200. /**
  201. Similar to starpu_data_acquire(), except that the data will be
  202. available on the given memory node instead of main memory.
  203. ::STARPU_ACQUIRE_NO_NODE and ::STARPU_ACQUIRE_NO_NODE_LOCK_ALL can
  204. be used instead of an explicit node number.
  205. */
  206. int starpu_data_acquire_on_node(starpu_data_handle_t handle, int node, enum starpu_data_access_mode mode);
  207. /**
  208. Asynchronous equivalent of starpu_data_acquire(). When the data
  209. specified in \p handle is available in the access \p mode, the \p
  210. callback function is executed. The application may access
  211. the requested data during the execution of \p callback. The \p callback
  212. function must call starpu_data_release() once the application no longer
  213. needs to access the piece of data. Note that implicit data
  214. dependencies are also enforced by starpu_data_acquire_cb() in case they
  215. are not disabled. Contrary to starpu_data_acquire(), this function is
  216. non-blocking and may be called from task callbacks. Upon successful
  217. completion, this function returns 0.
  218. */
  219. int starpu_data_acquire_cb(starpu_data_handle_t handle, enum starpu_data_access_mode mode, void (*callback)(void *), void *arg);
  220. /**
  221. Similar to starpu_data_acquire_cb(), except that the
  222. data will be available on the given memory node instead of main
  223. memory.
  224. ::STARPU_ACQUIRE_NO_NODE and ::STARPU_ACQUIRE_NO_NODE_LOCK_ALL can be
  225. used instead of an explicit node number.
  226. */
  227. int starpu_data_acquire_on_node_cb(starpu_data_handle_t handle, int node, enum starpu_data_access_mode mode, void (*callback)(void *), void *arg);
  228. /**
  229. Similar to starpu_data_acquire_cb() with the possibility of
  230. enabling or disabling data dependencies.
  231. When the data specified in \p handle is available in the access
  232. \p mode, the \p callback function is executed. The application may access
  233. the requested data during the execution of this \p callback. The \p callback
  234. function must call starpu_data_release() once the application no longer
  235. needs to access the piece of data. Note that implicit data
  236. dependencies are also enforced by starpu_data_acquire_cb_sequential_consistency() in case they
  237. are not disabled specifically for the given \p handle or by the parameter \p sequential_consistency.
  238. Similarly to starpu_data_acquire_cb(), this function is
  239. non-blocking and may be called from task callbacks. Upon successful
  240. completion, this function returns 0.
  241. */
  242. int starpu_data_acquire_cb_sequential_consistency(starpu_data_handle_t handle, enum starpu_data_access_mode mode, void (*callback)(void *), void *arg, int sequential_consistency);
  243. /**
  244. Similar to starpu_data_acquire_cb_sequential_consistency(), except that the
  245. data will be available on the given memory node instead of main
  246. memory.
  247. ::STARPU_ACQUIRE_NO_NODE and ::STARPU_ACQUIRE_NO_NODE_LOCK_ALL can be used instead of an
  248. explicit node number.
  249. */
  250. int starpu_data_acquire_on_node_cb_sequential_consistency(starpu_data_handle_t handle, int node, enum starpu_data_access_mode mode, void (*callback)(void *), void *arg, int sequential_consistency);
  251. int starpu_data_acquire_on_node_cb_sequential_consistency_quick(starpu_data_handle_t handle, int node, enum starpu_data_access_mode mode, void (*callback)(void *), void *arg, int sequential_consistency, int quick);
  252. /**
  253. Similar to starpu_data_acquire_on_node_cb_sequential_consistency(),
  254. except that the \e pre_sync_jobid and \e post_sync_jobid parameters can be used
  255. to retrieve the jobid of the synchronization tasks. \e pre_sync_jobid happens
  256. just before the acquisition, and \e post_sync_jobid happens just after the
  257. release.
  258. */
  259. int starpu_data_acquire_on_node_cb_sequential_consistency_sync_jobids(starpu_data_handle_t handle, int node, enum starpu_data_access_mode mode, void (*callback)(void *), void *arg, int sequential_consistency, int quick, long *pre_sync_jobid, long *post_sync_jobid);
  260. /**
  261. The application can call this function instead of starpu_data_acquire() so as to
  262. acquire the data like starpu_data_acquire(), but only if all
  263. previously-submitted tasks have completed, in which case starpu_data_acquire_try()
  264. returns 0. StarPU will have ensured that the application will get an up-to-date
  265. copy of \p handle in main memory located where the data was originally
  266. registered. starpu_data_release() must be called once the application no longer
  267. needs to access the piece of data.
  268. */
  269. int starpu_data_acquire_try(starpu_data_handle_t handle, enum starpu_data_access_mode mode);
  270. /**
  271. Similar to starpu_data_acquire_try(), except that the
  272. data will be available on the given memory node instead of main
  273. memory.
  274. ::STARPU_ACQUIRE_NO_NODE and ::STARPU_ACQUIRE_NO_NODE_LOCK_ALL can be used instead of an
  275. explicit node number.
  276. */
  277. int starpu_data_acquire_on_node_try(starpu_data_handle_t handle, int node, enum starpu_data_access_mode mode);
  278. #ifdef __GCC__
  279. /**
  280. STARPU_DATA_ACQUIRE_CB() is the same as starpu_data_acquire_cb(),
  281. except that the code to be executed in a callback is directly provided
  282. as a macro parameter, and the data \p handle is automatically released
  283. after it. This permits to easily execute code which depends on the
  284. value of some registered data. This is non-blocking too and may be
  285. called from task callbacks.
  286. */
  287. # define STARPU_DATA_ACQUIRE_CB(handle, mode, code) do \
  288. { \ \
  289. void callback(void *arg) \
  290. { \
  291. code; \
  292. starpu_data_release(handle); \
  293. } \
  294. starpu_data_acquire_cb(handle, mode, callback, NULL); \
  295. } \
  296. while(0)
  297. #endif
  298. /**
  299. Release the piece of data acquired by the
  300. application either by starpu_data_acquire() or by
  301. starpu_data_acquire_cb().
  302. */
  303. void starpu_data_release(starpu_data_handle_t handle);
  304. /**
  305. Similar to starpu_data_release(), except that the data
  306. will be available on the given memory \p node instead of main memory.
  307. The \p node parameter must be exactly the same as the corresponding \c
  308. starpu_data_acquire_on_node* call.
  309. */
  310. void starpu_data_release_on_node(starpu_data_handle_t handle, int node);
  311. /** @} */
  312. /**
  313. This is an arbiter, which implements an advanced but centralized
  314. management of concurrent data accesses, see \ref
  315. ConcurrentDataAccess for the details.
  316. */
  317. typedef struct starpu_arbiter *starpu_arbiter_t;
  318. /**
  319. Create a data access arbiter, see \ref ConcurrentDataAccess for the
  320. details
  321. */
  322. starpu_arbiter_t starpu_arbiter_create(void) STARPU_ATTRIBUTE_MALLOC;
  323. /**
  324. Make access to \p handle managed by \p arbiter
  325. */
  326. void starpu_data_assign_arbiter(starpu_data_handle_t handle, starpu_arbiter_t arbiter);
  327. /**
  328. Destroy the \p arbiter . This must only be called after all data
  329. assigned to it have been unregistered.
  330. */
  331. void starpu_arbiter_destroy(starpu_arbiter_t arbiter);
  332. /**
  333. Explicitly ask StarPU to allocate room for a piece of data on
  334. the specified memory \p node.
  335. */
  336. int starpu_data_request_allocation(starpu_data_handle_t handle, unsigned node);
  337. /**
  338. Issue a fetch request for the data \p handle to \p node, i.e.
  339. requests that the data be replicated to the given node as soon as possible, so that it is
  340. available there for tasks. If \p async is 0, the call will
  341. block until the transfer is achieved, else the call will return immediately,
  342. after having just queued the request. In the latter case, the request will
  343. asynchronously wait for the completion of any task writing on the
  344. data.
  345. */
  346. int starpu_data_fetch_on_node(starpu_data_handle_t handle, unsigned node, unsigned async);
  347. /**
  348. Issue a prefetch request for the data \p handle to \p node, i.e.
  349. requests that the data be replicated to \p node when there is room for it, so that it is
  350. available there for tasks. If \p async is 0, the call will
  351. block until the transfer is achieved, else the call will return immediately,
  352. after having just queued the request. In the latter case, the request will
  353. asynchronously wait for the completion of any task writing on the
  354. data.
  355. */
  356. int starpu_data_prefetch_on_node(starpu_data_handle_t handle, unsigned node, unsigned async);
  357. int starpu_data_prefetch_on_node_prio(starpu_data_handle_t handle, unsigned node, unsigned async, int prio);
  358. /**
  359. Issue an idle prefetch request for the data \p handle to \p node, i.e.
  360. requests that the data be replicated to \p node, so that it is
  361. available there for tasks, but only when the bus is really idle. If \p async is 0, the call will
  362. block until the transfer is achieved, else the call will return immediately,
  363. after having just queued the request. In the latter case, the request will
  364. asynchronously wait for the completion of any task writing on the data.
  365. */
  366. int starpu_data_idle_prefetch_on_node(starpu_data_handle_t handle, unsigned node, unsigned async);
  367. int starpu_data_idle_prefetch_on_node_prio(starpu_data_handle_t handle, unsigned node, unsigned async, int prio);
  368. /**
  369. Check whether a valid copy of \p handle is currently available on
  370. memory node \p node.
  371. */
  372. unsigned starpu_data_is_on_node(starpu_data_handle_t handle, unsigned node);
  373. /**
  374. Advise StarPU that \p handle will not be used in the close future, and is
  375. thus a good candidate for eviction from GPUs. StarPU will thus write its value
  376. back to its home node when the bus is idle, and select this data in priority
  377. for eviction when memory gets low.
  378. */
  379. void starpu_data_wont_use(starpu_data_handle_t handle);
  380. /**
  381. Set the write-through mask of the data \p handle (and
  382. its children), i.e. a bitmask of nodes where the data should be always
  383. replicated after modification. It also prevents the data from being
  384. evicted from these nodes when memory gets scarse. When the data is
  385. modified, it is automatically transfered into those memory nodes. For
  386. instance a <c>1<<0</c> write-through mask means that the CUDA workers
  387. will commit their changes in main memory (node 0).
  388. */
  389. void starpu_data_set_wt_mask(starpu_data_handle_t handle, uint32_t wt_mask);
  390. /**
  391. @name Implicit Data Dependencies
  392. In this section, we describe how StarPU makes it possible to
  393. insert implicit task dependencies in order to enforce sequential data
  394. consistency. When this data consistency is enabled on a specific data
  395. handle, any data access will appear as sequentially consistent from
  396. the application. For instance, if the application submits two tasks
  397. that access the same piece of data in read-only mode, and then a third
  398. task that access it in write mode, dependencies will be added between
  399. the two first tasks and the third one. Implicit data dependencies are
  400. also inserted in the case of data accesses from the application.
  401. @{
  402. */
  403. /**
  404. Set the data consistency mode associated to a data handle. The
  405. consistency mode set using this function has the priority over the
  406. default mode which can be set with
  407. starpu_data_set_default_sequential_consistency_flag().
  408. */
  409. void starpu_data_set_sequential_consistency_flag(starpu_data_handle_t handle, unsigned flag);
  410. /**
  411. Get the data consistency mode associated to the data handle \p handle
  412. */
  413. unsigned starpu_data_get_sequential_consistency_flag(starpu_data_handle_t handle);
  414. /**
  415. Return the default sequential consistency flag
  416. */
  417. unsigned starpu_data_get_default_sequential_consistency_flag(void);
  418. /**
  419. Set the default sequential consistency flag. If a non-zero
  420. value is passed, a sequential data consistency will be enforced for
  421. all handles registered after this function call, otherwise it is
  422. disabled. By default, StarPU enables sequential data consistency. It
  423. is also possible to select the data consistency mode of a specific
  424. data handle with the function
  425. starpu_data_set_sequential_consistency_flag().
  426. */
  427. void starpu_data_set_default_sequential_consistency_flag(unsigned flag);
  428. /** @} */
  429. /**
  430. Set whether this data should be elligible to be evicted to disk
  431. storage (1) or not (0). The default is 1.
  432. */
  433. void starpu_data_set_ooc_flag(starpu_data_handle_t handle, unsigned flag);
  434. /**
  435. Get whether this data was set to be elligible to be evicted to disk
  436. storage (1) or not (0).
  437. */
  438. unsigned starpu_data_get_ooc_flag(starpu_data_handle_t handle);
  439. /**
  440. Query the status of \p handle on the specified \p memory_node.
  441. */
  442. void starpu_data_query_status(starpu_data_handle_t handle, int memory_node, int *is_allocated, int *is_valid, int *is_requested);
  443. struct starpu_codelet;
  444. /**
  445. Set the codelets to be used for \p handle when it is accessed in the
  446. mode ::STARPU_REDUX. Per-worker buffers will be initialized with
  447. the codelet \p init_cl, and reduction between per-worker buffers will be
  448. done with the codelet \p redux_cl.
  449. */
  450. void starpu_data_set_reduction_methods(starpu_data_handle_t handle, struct starpu_codelet *redux_cl, struct starpu_codelet *init_cl);
  451. struct starpu_data_interface_ops* starpu_data_get_interface_ops(starpu_data_handle_t handle);
  452. unsigned starpu_data_test_if_allocated_on_node(starpu_data_handle_t handle, unsigned memory_node);
  453. void starpu_memchunk_tidy(unsigned memory_node);
  454. /**
  455. Set the field \c user_data for the \p handle to \p user_data . It can
  456. then be retrieved with starpu_data_get_user_data(). \p user_data can be any
  457. application-defined value, for instance a pointer to an object-oriented
  458. container for the data.
  459. */
  460. void starpu_data_set_user_data(starpu_data_handle_t handle, void* user_data);
  461. /**
  462. Retrieve the field \c user_data previously set for the \p handle.
  463. */
  464. void *starpu_data_get_user_data(starpu_data_handle_t handle);
  465. /** @} */
  466. #ifdef __cplusplus
  467. }
  468. #endif
  469. #endif /* __STARPU_DATA_H__ */