data_interface.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-2016 Université de Bordeaux
  4. * Copyright (C) 2010, 2011, 2012, 2013, 2014, 2015, 2016 CNRS
  5. * Copyright (C) 2014, 2016 Inria
  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. #include <stdint.h>
  19. #include <datawizard/datawizard.h>
  20. #include <datawizard/memory_nodes.h>
  21. #include <core/dependencies/data_concurrency.h>
  22. #include <common/uthash.h>
  23. #include <common/starpu_spinlock.h>
  24. #include <core/task.h>
  25. #include <core/workers.h>
  26. #include <datawizard/memstats.h>
  27. #ifdef STARPU_OPENMP
  28. #include <util/openmp_runtime_support.h>
  29. #endif
  30. /* Entry in the `registered_handles' hash table. */
  31. struct handle_entry
  32. {
  33. UT_hash_handle hh;
  34. void *pointer;
  35. starpu_data_handle_t handle;
  36. };
  37. /* Hash table mapping host pointers to data handles. */
  38. static int nregistered, maxnregistered;
  39. static struct handle_entry *registered_handles;
  40. static struct _starpu_spinlock registered_handles_lock;
  41. static int _data_interface_number = STARPU_MAX_INTERFACE_ID;
  42. starpu_arbiter_t _starpu_global_arbiter;
  43. static void _starpu_data_unregister(starpu_data_handle_t handle, unsigned coherent, unsigned nowait);
  44. void _starpu_data_interface_init(void)
  45. {
  46. _starpu_spin_init(&registered_handles_lock);
  47. /* Just for testing purpose */
  48. if (starpu_get_env_number_default("STARPU_GLOBAL_ARBITER", 0) > 0)
  49. _starpu_global_arbiter = starpu_arbiter_create();
  50. }
  51. void _starpu_data_interface_shutdown()
  52. {
  53. struct handle_entry *entry, *tmp;
  54. if (registered_handles)
  55. {
  56. _STARPU_DISP("[warning] The application has not unregistered all data handles.\n");
  57. }
  58. _starpu_spin_destroy(&registered_handles_lock);
  59. HASH_ITER(hh, registered_handles, entry, tmp)
  60. {
  61. HASH_DEL(registered_handles, entry);
  62. nregistered--;
  63. free(entry);
  64. }
  65. if (starpu_get_env_number_default("STARPU_MAX_MEMORY_USE", 0))
  66. _STARPU_DISP("Memory used for %d data handles: %lu MiB\n", maxnregistered, (unsigned long) (maxnregistered * sizeof(struct _starpu_data_state)) >> 20);
  67. STARPU_ASSERT(nregistered == 0);
  68. registered_handles = NULL;
  69. }
  70. #ifdef STARPU_OPENMP
  71. void _starpu_omp_unregister_region_handles(struct starpu_omp_region *region)
  72. {
  73. _starpu_spin_lock(&region->registered_handles_lock);
  74. struct handle_entry *entry, *tmp;
  75. HASH_ITER(hh, (region->registered_handles), entry, tmp)
  76. {
  77. entry->handle->removed_from_context_hash = 1;
  78. HASH_DEL(region->registered_handles, entry);
  79. starpu_data_unregister(entry->handle);
  80. free(entry);
  81. }
  82. _starpu_spin_unlock(&region->registered_handles_lock);
  83. }
  84. void _starpu_omp_unregister_task_handles(struct starpu_omp_task *task)
  85. {
  86. struct handle_entry *entry, *tmp;
  87. HASH_ITER(hh, task->registered_handles, entry, tmp)
  88. {
  89. entry->handle->removed_from_context_hash = 1;
  90. HASH_DEL(task->registered_handles, entry);
  91. starpu_data_unregister(entry->handle);
  92. free(entry);
  93. }
  94. }
  95. #endif
  96. struct starpu_data_interface_ops *_starpu_data_interface_get_ops(unsigned interface_id)
  97. {
  98. switch (interface_id)
  99. {
  100. case STARPU_MATRIX_INTERFACE_ID:
  101. return &starpu_interface_matrix_ops;
  102. case STARPU_BLOCK_INTERFACE_ID:
  103. return &starpu_interface_block_ops;
  104. case STARPU_VECTOR_INTERFACE_ID:
  105. return &starpu_interface_vector_ops;
  106. case STARPU_CSR_INTERFACE_ID:
  107. return &starpu_interface_csr_ops;
  108. case STARPU_BCSR_INTERFACE_ID:
  109. return &starpu_interface_bcsr_ops;
  110. case STARPU_VARIABLE_INTERFACE_ID:
  111. return &starpu_interface_variable_ops;
  112. case STARPU_VOID_INTERFACE_ID:
  113. return &starpu_interface_void_ops;
  114. case STARPU_MULTIFORMAT_INTERFACE_ID:
  115. return &starpu_interface_multiformat_ops;
  116. default:
  117. STARPU_ABORT();
  118. return NULL;
  119. }
  120. }
  121. /* Register the mapping from PTR to HANDLE. If PTR is already mapped to
  122. * some handle, the new mapping shadows the previous one. */
  123. void _starpu_data_register_ram_pointer(starpu_data_handle_t handle, void *ptr)
  124. {
  125. struct handle_entry *entry, *old_entry;
  126. _STARPU_MALLOC(entry, sizeof(*entry));
  127. entry->pointer = ptr;
  128. entry->handle = handle;
  129. #ifdef STARPU_OPENMP
  130. struct starpu_omp_task *task = _starpu_omp_get_task();
  131. if (task)
  132. {
  133. if (task->flags & STARPU_OMP_TASK_FLAGS_IMPLICIT)
  134. {
  135. struct starpu_omp_region *parallel_region = task->owner_region;
  136. _starpu_spin_lock(&parallel_region->registered_handles_lock);
  137. HASH_ADD_PTR(parallel_region->registered_handles, pointer, entry);
  138. _starpu_spin_unlock(&parallel_region->registered_handles_lock);
  139. }
  140. else
  141. {
  142. HASH_ADD_PTR(task->registered_handles, pointer, entry);
  143. }
  144. }
  145. else
  146. #endif
  147. {
  148. _starpu_spin_lock(&registered_handles_lock);
  149. HASH_FIND_PTR(registered_handles, &ptr, old_entry);
  150. if (old_entry) {
  151. /* Already registered this pointer, avoid undefined
  152. * behavior of duplicate in hash table */
  153. _starpu_spin_unlock(&registered_handles_lock);
  154. free(entry);
  155. } else {
  156. nregistered++;
  157. if (nregistered > maxnregistered)
  158. maxnregistered = nregistered;
  159. HASH_ADD_PTR(registered_handles, pointer, entry);
  160. _starpu_spin_unlock(&registered_handles_lock);
  161. }
  162. }
  163. }
  164. starpu_data_handle_t starpu_data_lookup(const void *ptr)
  165. {
  166. starpu_data_handle_t result;
  167. #ifdef STARPU_OPENMP
  168. struct starpu_omp_task *task = _starpu_omp_get_task();
  169. if (task)
  170. {
  171. if (task->flags & STARPU_OMP_TASK_FLAGS_IMPLICIT)
  172. {
  173. struct starpu_omp_region *parallel_region = task->owner_region;
  174. _starpu_spin_lock(&parallel_region->registered_handles_lock);
  175. {
  176. struct handle_entry *entry;
  177. HASH_FIND_PTR(parallel_region->registered_handles, &ptr, entry);
  178. if(STARPU_UNLIKELY(entry == NULL))
  179. result = NULL;
  180. else
  181. result = entry->handle;
  182. }
  183. _starpu_spin_unlock(&parallel_region->registered_handles_lock);
  184. }
  185. else
  186. {
  187. struct handle_entry *entry;
  188. HASH_FIND_PTR(task->registered_handles, &ptr, entry);
  189. if(STARPU_UNLIKELY(entry == NULL))
  190. result = NULL;
  191. else
  192. result = entry->handle;
  193. }
  194. }
  195. else
  196. #endif
  197. {
  198. _starpu_spin_lock(&registered_handles_lock);
  199. {
  200. struct handle_entry *entry;
  201. HASH_FIND_PTR(registered_handles, &ptr, entry);
  202. if(STARPU_UNLIKELY(entry == NULL))
  203. result = NULL;
  204. else
  205. result = entry->handle;
  206. }
  207. _starpu_spin_unlock(&registered_handles_lock);
  208. }
  209. return result;
  210. }
  211. /*
  212. * Start monitoring a piece of data
  213. */
  214. static void _starpu_register_new_data(starpu_data_handle_t handle,
  215. int home_node, uint32_t wt_mask)
  216. {
  217. void *ptr;
  218. STARPU_ASSERT(handle);
  219. /* initialize the new lock */
  220. _starpu_data_requester_list_init(&handle->req_list);
  221. handle->refcnt = 0;
  222. handle->unlocking_reqs = 0;
  223. handle->busy_count = 0;
  224. handle->busy_waiting = 0;
  225. STARPU_PTHREAD_MUTEX_INIT(&handle->busy_mutex, NULL);
  226. STARPU_PTHREAD_COND_INIT(&handle->busy_cond, NULL);
  227. _starpu_spin_init(&handle->header_lock);
  228. /* first take care to properly lock the data */
  229. _starpu_spin_lock(&handle->header_lock);
  230. /* there is no hierarchy yet */
  231. handle->nchildren = 0;
  232. handle->nplans = 0;
  233. handle->switch_cl = NULL;
  234. handle->partitioned = 0;
  235. handle->readonly = 0;
  236. handle->root_handle = handle;
  237. handle->father_handle = NULL;
  238. handle->sibling_index = 0; /* could be anything for the root */
  239. handle->depth = 1; /* the tree is just a node yet */
  240. handle->mpi_data = NULL; /* invalid until set */
  241. handle->is_not_important = 0;
  242. handle->sequential_consistency =
  243. starpu_data_get_default_sequential_consistency_flag();
  244. handle->initialized = home_node != -1;
  245. STARPU_PTHREAD_MUTEX_INIT(&handle->sequential_consistency_mutex, NULL);
  246. handle->last_submitted_mode = STARPU_R;
  247. handle->last_sync_task = NULL;
  248. handle->last_submitted_accessors.task = NULL;
  249. handle->last_submitted_accessors.next = &handle->last_submitted_accessors;
  250. handle->last_submitted_accessors.prev = &handle->last_submitted_accessors;
  251. handle->post_sync_tasks = NULL;
  252. /* Tell helgrind that the race in _starpu_unlock_post_sync_tasks is fine */
  253. STARPU_HG_DISABLE_CHECKING(handle->post_sync_tasks_cnt);
  254. handle->post_sync_tasks_cnt = 0;
  255. /* By default, there are no methods available to perform a reduction */
  256. handle->redux_cl = NULL;
  257. handle->init_cl = NULL;
  258. handle->reduction_refcnt = 0;
  259. _starpu_data_requester_list_init(&handle->reduction_req_list);
  260. handle->reduction_tmp_handles = NULL;
  261. handle->write_invalidation_req = NULL;
  262. #ifdef STARPU_USE_FXT
  263. handle->last_submitted_ghost_sync_id_is_valid = 0;
  264. handle->last_submitted_ghost_sync_id = 0;
  265. handle->last_submitted_ghost_accessors_id = NULL;
  266. #endif
  267. handle->wt_mask = wt_mask;
  268. /* Store some values directly in the handle not to recompute them all
  269. * the time. */
  270. handle->footprint = _starpu_compute_data_footprint(handle);
  271. handle->home_node = home_node;
  272. if (_starpu_global_arbiter)
  273. /* Just for testing purpose */
  274. starpu_data_assign_arbiter(handle, _starpu_global_arbiter);
  275. else
  276. handle->arbiter = NULL;
  277. _starpu_data_requester_list_init(&handle->arbitered_req_list);
  278. handle->last_locality = -1;
  279. /* that new data is invalid from all nodes perpective except for the
  280. * home node */
  281. unsigned node;
  282. for (node = 0; node < STARPU_MAXNODES; node++)
  283. {
  284. struct _starpu_data_replicate *replicate;
  285. replicate = &handle->per_node[node];
  286. replicate->memory_node = node;
  287. replicate->relaxed_coherency = 0;
  288. replicate->refcnt = 0;
  289. if ((int) node == home_node)
  290. {
  291. /* this is the home node with the only valid copy */
  292. replicate->state = STARPU_OWNER;
  293. replicate->allocated = 1;
  294. replicate->automatically_allocated = 0;
  295. replicate->initialized = 1;
  296. }
  297. else
  298. {
  299. /* the value is not available here yet */
  300. replicate->state = STARPU_INVALID;
  301. replicate->allocated = 0;
  302. replicate->initialized = 0;
  303. }
  304. }
  305. handle->per_worker = NULL;
  306. handle->user_data = NULL;
  307. /* now the data is available ! */
  308. _starpu_spin_unlock(&handle->header_lock);
  309. ptr = starpu_data_handle_to_pointer(handle, STARPU_MAIN_RAM);
  310. if (ptr != NULL)
  311. {
  312. _starpu_data_register_ram_pointer(handle, ptr);
  313. }
  314. }
  315. void
  316. _starpu_data_initialize_per_worker(starpu_data_handle_t handle)
  317. {
  318. unsigned worker;
  319. unsigned nworkers = starpu_worker_get_count();
  320. _starpu_spin_checklocked(&handle->header_lock);
  321. _STARPU_CALLOC(handle->per_worker, nworkers, sizeof(*handle->per_worker));
  322. size_t interfacesize = handle->ops->interface_size;
  323. for (worker = 0; worker < nworkers; worker++)
  324. {
  325. struct _starpu_data_replicate *replicate;
  326. unsigned node;
  327. replicate = &handle->per_worker[worker];
  328. replicate->allocated = 0;
  329. replicate->automatically_allocated = 0;
  330. replicate->state = STARPU_INVALID;
  331. replicate->refcnt = 0;
  332. replicate->handle = handle;
  333. replicate->requested = 0;
  334. for (node = 0; node < STARPU_MAXNODES; node++)
  335. {
  336. replicate->request[node] = NULL;
  337. }
  338. /* Assuming being used for SCRATCH for now, patched when entering REDUX mode */
  339. replicate->relaxed_coherency = 1;
  340. replicate->initialized = 0;
  341. replicate->memory_node = starpu_worker_get_memory_node(worker);
  342. _STARPU_CALLOC(replicate->data_interface, 1, interfacesize);
  343. /* duplicate the content of the interface on node 0 */
  344. memcpy(replicate->data_interface, handle->per_node[STARPU_MAIN_RAM].data_interface, interfacesize);
  345. }
  346. }
  347. void starpu_data_ptr_register(starpu_data_handle_t handle, unsigned node)
  348. {
  349. struct _starpu_data_replicate *replicate = &handle->per_node[node];
  350. _starpu_spin_lock(&handle->header_lock);
  351. STARPU_ASSERT_MSG(replicate->allocated == 0, "starpu_data_ptr_register must be called right after starpu_data_register");
  352. replicate->allocated = 1;
  353. replicate->automatically_allocated = 0;
  354. _starpu_spin_unlock(&handle->header_lock);
  355. }
  356. int _starpu_data_handle_init(starpu_data_handle_t handle, struct starpu_data_interface_ops *interface_ops, unsigned int mf_node)
  357. {
  358. unsigned node;
  359. /* Tell helgrind that our access to busy_count in
  360. * starpu_data_unregister is actually safe */
  361. STARPU_HG_DISABLE_CHECKING(handle->busy_count);
  362. handle->magic = 42;
  363. handle->ops = interface_ops;
  364. handle->mf_node = mf_node;
  365. handle->mpi_data = NULL;
  366. size_t interfacesize = interface_ops->interface_size;
  367. _starpu_memory_stats_init(handle);
  368. for (node = 0; node < STARPU_MAXNODES; node++)
  369. {
  370. _starpu_memory_stats_init_per_node(handle, node);
  371. struct _starpu_data_replicate *replicate;
  372. replicate = &handle->per_node[node];
  373. /* relaxed_coherency = 0 */
  374. replicate->handle = handle;
  375. _STARPU_CALLOC(replicate->data_interface, 1, interfacesize);
  376. }
  377. return 0;
  378. }
  379. static
  380. starpu_data_handle_t _starpu_data_handle_allocate(struct starpu_data_interface_ops *interface_ops, unsigned int mf_node)
  381. {
  382. starpu_data_handle_t handle;
  383. _STARPU_CALLOC(handle, 1, sizeof(struct _starpu_data_state));
  384. _starpu_data_handle_init(handle, interface_ops, mf_node);
  385. return handle;
  386. }
  387. void starpu_data_register(starpu_data_handle_t *handleptr, int home_node,
  388. void *data_interface,
  389. struct starpu_data_interface_ops *ops)
  390. {
  391. starpu_data_handle_t handle = _starpu_data_handle_allocate(ops, home_node);
  392. _STARPU_TRACE_HANDLE_DATA_REGISTER(handle);
  393. STARPU_ASSERT(handleptr);
  394. *handleptr = handle;
  395. /* fill the interface fields with the appropriate method */
  396. STARPU_ASSERT(ops->register_data_handle);
  397. ops->register_data_handle(handle, home_node, data_interface);
  398. _starpu_register_new_data(handle, home_node, 0);
  399. }
  400. void starpu_data_register_same(starpu_data_handle_t *handledst, starpu_data_handle_t handlesrc)
  401. {
  402. void *local_interface = starpu_data_get_interface_on_node(handlesrc, STARPU_MAIN_RAM);
  403. starpu_data_register(handledst, -1, local_interface, handlesrc->ops);
  404. }
  405. void *starpu_data_handle_to_pointer(starpu_data_handle_t handle, unsigned node)
  406. {
  407. /* Check whether the operation is supported and the node has actually
  408. * been allocated. */
  409. if (handle->ops->handle_to_pointer
  410. && starpu_data_test_if_allocated_on_node(handle, node))
  411. {
  412. return handle->ops->handle_to_pointer(handle, node);
  413. }
  414. return NULL;
  415. }
  416. void *starpu_data_get_local_ptr(starpu_data_handle_t handle)
  417. {
  418. return starpu_data_handle_to_pointer(handle,
  419. _starpu_memory_node_get_local_key());
  420. }
  421. struct starpu_data_interface_ops* starpu_data_get_interface_ops(starpu_data_handle_t handle)
  422. {
  423. return handle->ops;
  424. }
  425. /*
  426. * Stop monitoring a piece of data
  427. */
  428. void _starpu_data_unregister_ram_pointer(starpu_data_handle_t handle)
  429. {
  430. const void *ram_ptr = starpu_data_handle_to_pointer(handle, STARPU_MAIN_RAM);
  431. #ifdef STARPU_OPENMP
  432. if (handle->removed_from_context_hash)
  433. return;
  434. #endif
  435. if (ram_ptr != NULL)
  436. {
  437. /* Remove the PTR -> HANDLE mapping. If a mapping from PTR
  438. * to another handle existed before (e.g., when using
  439. * filters), it becomes visible again. */
  440. struct handle_entry *entry;
  441. #ifdef STARPU_OPENMP
  442. struct starpu_omp_task *task = _starpu_omp_get_task();
  443. if (task)
  444. {
  445. if (task->flags & STARPU_OMP_TASK_FLAGS_IMPLICIT)
  446. {
  447. struct starpu_omp_region *parallel_region = task->owner_region;
  448. _starpu_spin_lock(&parallel_region->registered_handles_lock);
  449. HASH_FIND_PTR(parallel_region->registered_handles, &ram_ptr, entry);
  450. STARPU_ASSERT(entry != NULL);
  451. HASH_DEL(registered_handles, entry);
  452. _starpu_spin_unlock(&parallel_region->registered_handles_lock);
  453. }
  454. else
  455. {
  456. HASH_FIND_PTR(task->registered_handles, &ram_ptr, entry);
  457. STARPU_ASSERT(entry != NULL);
  458. HASH_DEL(task->registered_handles, entry);
  459. }
  460. }
  461. else
  462. #endif
  463. {
  464. _starpu_spin_lock(&registered_handles_lock);
  465. HASH_FIND_PTR(registered_handles, &ram_ptr, entry);
  466. if (entry)
  467. {
  468. if (entry->handle == handle)
  469. {
  470. nregistered--;
  471. HASH_DEL(registered_handles, entry);
  472. }
  473. else
  474. /* don't free it, it's not ours */
  475. entry = NULL;
  476. }
  477. _starpu_spin_unlock(&registered_handles_lock);
  478. }
  479. free(entry);
  480. }
  481. }
  482. void _starpu_data_free_interfaces(starpu_data_handle_t handle)
  483. {
  484. unsigned node;
  485. unsigned nworkers = starpu_worker_get_count();
  486. for (node = 0; node < STARPU_MAXNODES; node++)
  487. free(handle->per_node[node].data_interface);
  488. if (handle->per_worker)
  489. {
  490. unsigned worker;
  491. for (worker = 0; worker < nworkers; worker++)
  492. free(handle->per_worker[worker].data_interface);
  493. free(handle->per_worker);
  494. }
  495. }
  496. struct _starpu_unregister_callback_arg
  497. {
  498. unsigned memory_node;
  499. starpu_data_handle_t handle;
  500. unsigned terminated;
  501. starpu_pthread_mutex_t mutex;
  502. starpu_pthread_cond_t cond;
  503. };
  504. /* Check whether we should tell starpu_data_unregister that the data handle is
  505. * not busy any more.
  506. * The header is supposed to be locked.
  507. * This may free the handle, if it was lazily unregistered (1 is returned in
  508. * that case). The handle pointer thus becomes invalid for the caller.
  509. *
  510. * Note: we inline some of the tests in the _starpu_data_check_not_busy macro.
  511. */
  512. int __starpu_data_check_not_busy(starpu_data_handle_t handle)
  513. {
  514. if (STARPU_LIKELY(handle->busy_count))
  515. return 0;
  516. /* Not busy any more, perhaps have to unregister etc. */
  517. if (STARPU_UNLIKELY(handle->busy_waiting))
  518. {
  519. STARPU_PTHREAD_MUTEX_LOCK(&handle->busy_mutex);
  520. STARPU_PTHREAD_COND_BROADCAST(&handle->busy_cond);
  521. STARPU_PTHREAD_MUTEX_UNLOCK(&handle->busy_mutex);
  522. }
  523. /* The handle has been destroyed in between (eg. this was a temporary
  524. * handle created for a reduction.) */
  525. if (STARPU_UNLIKELY(handle->lazy_unregister))
  526. {
  527. handle->lazy_unregister = 0;
  528. _starpu_spin_unlock(&handle->header_lock);
  529. _starpu_data_unregister(handle, 0, 1);
  530. /* Warning: in case we unregister the handle, we must be sure
  531. * that the caller will not try to unlock the header after
  532. * !*/
  533. return 1;
  534. }
  535. return 0;
  536. }
  537. static
  538. void _starpu_check_if_valid_and_fetch_data_on_node(starpu_data_handle_t handle, struct _starpu_data_replicate *replicate, const char *origin)
  539. {
  540. unsigned node;
  541. unsigned nnodes = starpu_memory_nodes_get_count();
  542. int valid = 0;
  543. for (node = 0; node < nnodes; node++)
  544. {
  545. if (handle->per_node[node].state != STARPU_INVALID)
  546. {
  547. /* we found a copy ! */
  548. valid = 1;
  549. }
  550. }
  551. if (valid)
  552. {
  553. int ret = _starpu_fetch_data_on_node(handle, handle->home_node, replicate, STARPU_R, 0, 0, 0, NULL, NULL, 0, origin);
  554. STARPU_ASSERT(!ret);
  555. _starpu_release_data_on_node(handle, handle->home_node, replicate);
  556. }
  557. else
  558. {
  559. _starpu_spin_lock(&handle->header_lock);
  560. if (!_starpu_notify_data_dependencies(handle))
  561. _starpu_spin_unlock(&handle->header_lock);
  562. }
  563. }
  564. static void _starpu_data_unregister_fetch_data_callback(void *_arg)
  565. {
  566. struct _starpu_unregister_callback_arg *arg = (struct _starpu_unregister_callback_arg *) _arg;
  567. starpu_data_handle_t handle = arg->handle;
  568. STARPU_ASSERT(handle);
  569. struct _starpu_data_replicate *replicate = &handle->per_node[arg->memory_node];
  570. _starpu_check_if_valid_and_fetch_data_on_node(handle, replicate, "_starpu_data_unregister_fetch_data_callback");
  571. /* unlock the caller */
  572. STARPU_PTHREAD_MUTEX_LOCK(&arg->mutex);
  573. arg->terminated = 1;
  574. STARPU_PTHREAD_COND_SIGNAL(&arg->cond);
  575. STARPU_PTHREAD_MUTEX_UNLOCK(&arg->mutex);
  576. }
  577. /* Unregister the data handle, perhaps we don't need to update the home_node
  578. * (in that case coherent is set to 0)
  579. * nowait is for internal use when we already know for sure that we won't have to wait.
  580. */
  581. static void _starpu_data_unregister(starpu_data_handle_t handle, unsigned coherent, unsigned nowait)
  582. {
  583. STARPU_ASSERT(handle);
  584. /* Prevent any further unregistration */
  585. handle->magic = 0;
  586. STARPU_ASSERT_MSG(handle->nchildren == 0, "data %p needs to be unpartitioned before unregistration", handle);
  587. STARPU_ASSERT_MSG(handle->nplans == 0, "data %p needs its partition plans to be cleaned before unregistration", handle);
  588. STARPU_ASSERT_MSG(handle->partitioned == 0, "data %p needs its partitioned plans to be unpartitioned before unregistration", handle);
  589. /* TODO: also check that it has the latest coherency */
  590. STARPU_ASSERT(!(nowait && handle->busy_count != 0));
  591. int sequential_consistency = handle->sequential_consistency;
  592. if (sequential_consistency && !nowait)
  593. {
  594. STARPU_ASSERT_MSG(_starpu_worker_may_perform_blocking_calls(), "starpu_data_unregister must not be called from a task or callback, perhaps you can use starpu_data_unregister_submit instead");
  595. /* If sequential consistency is enabled, wait until data is available */
  596. _starpu_data_wait_until_available(handle, STARPU_RW, "starpu_data_unregister");
  597. }
  598. if (coherent && !nowait)
  599. {
  600. STARPU_ASSERT_MSG(_starpu_worker_may_perform_blocking_calls(), "starpu_data_unregister must not be called from a task or callback, perhaps you can use starpu_data_unregister_submit instead");
  601. /* Fetch data in the home of the data to ensure we have a valid copy
  602. * where we registered it */
  603. int home_node = handle->home_node;
  604. if (home_node >= 0)
  605. {
  606. struct _starpu_unregister_callback_arg arg;
  607. arg.handle = handle;
  608. arg.memory_node = (unsigned)home_node;
  609. arg.terminated = 0;
  610. STARPU_PTHREAD_MUTEX_INIT(&arg.mutex, NULL);
  611. STARPU_PTHREAD_COND_INIT(&arg.cond, NULL);
  612. if (!_starpu_attempt_to_submit_data_request_from_apps(handle, STARPU_R,
  613. _starpu_data_unregister_fetch_data_callback, &arg))
  614. {
  615. /* no one has locked this data yet, so we proceed immediately */
  616. struct _starpu_data_replicate *home_replicate = &handle->per_node[home_node];
  617. _starpu_check_if_valid_and_fetch_data_on_node(handle, home_replicate, "_starpu_data_unregister");
  618. }
  619. else
  620. {
  621. STARPU_PTHREAD_MUTEX_LOCK(&arg.mutex);
  622. while (!arg.terminated)
  623. STARPU_PTHREAD_COND_WAIT(&arg.cond, &arg.mutex);
  624. STARPU_PTHREAD_MUTEX_UNLOCK(&arg.mutex);
  625. }
  626. STARPU_PTHREAD_MUTEX_DESTROY(&arg.mutex);
  627. STARPU_PTHREAD_COND_DESTROY(&arg.cond);
  628. }
  629. /* If this handle uses a multiformat interface, we may have to convert
  630. * this piece of data back into the CPU format.
  631. * XXX : This is quite hacky, could we submit a task instead ?
  632. */
  633. if (_starpu_data_is_multiformat_handle(handle) &&
  634. ( starpu_node_get_kind(handle->mf_node) != STARPU_CPU_RAM
  635. && starpu_node_get_kind(handle->mf_node) != STARPU_SCC_RAM
  636. && starpu_node_get_kind(handle->mf_node) != STARPU_SCC_SHM
  637. ))
  638. {
  639. _STARPU_DEBUG("Conversion needed\n");
  640. void *buffers[1];
  641. struct starpu_multiformat_interface *format_interface;
  642. format_interface = (struct starpu_multiformat_interface *) starpu_data_get_interface_on_node(handle, STARPU_MAIN_RAM);
  643. struct starpu_codelet *cl = NULL;
  644. enum starpu_node_kind node_kind = starpu_node_get_kind(handle->mf_node);
  645. switch (node_kind)
  646. {
  647. #ifdef STARPU_USE_CUDA
  648. case STARPU_CUDA_RAM:
  649. {
  650. struct starpu_multiformat_data_interface_ops *mf_ops;
  651. mf_ops = (struct starpu_multiformat_data_interface_ops *) handle->ops->get_mf_ops(format_interface);
  652. cl = mf_ops->cuda_to_cpu_cl;
  653. break;
  654. }
  655. #endif
  656. #ifdef STARPU_USE_OPENCL
  657. case STARPU_OPENCL_RAM:
  658. {
  659. struct starpu_multiformat_data_interface_ops *mf_ops;
  660. mf_ops = (struct starpu_multiformat_data_interface_ops *) handle->ops->get_mf_ops(format_interface);
  661. cl = mf_ops->opencl_to_cpu_cl;
  662. break;
  663. }
  664. #endif
  665. #ifdef STARPU_USE_MIC
  666. case STARPU_MIC_RAM:
  667. {
  668. struct starpu_multiformat_data_interface_ops *mf_ops;
  669. mf_ops = (struct starpu_multiformat_data_interface_ops *) handle->ops->get_mf_ops(format_interface);
  670. cl = mf_ops->mic_to_cpu_cl;
  671. break;
  672. }
  673. #endif
  674. case STARPU_CPU_RAM: /* Impossible ! */
  675. case STARPU_SCC_RAM: /* Impossible ! */
  676. case STARPU_SCC_SHM: /* Impossible ! */
  677. default:
  678. STARPU_ABORT();
  679. }
  680. buffers[0] = format_interface;
  681. _starpu_cl_func_t func = _starpu_task_get_cpu_nth_implementation(cl, 0);
  682. STARPU_ASSERT(func);
  683. func(buffers, NULL);
  684. }
  685. }
  686. _starpu_spin_lock(&handle->header_lock);
  687. if (!coherent)
  688. {
  689. /* Should we postpone the unregister operation ? */
  690. if ((handle->busy_count > 0) && handle->lazy_unregister)
  691. {
  692. _starpu_spin_unlock(&handle->header_lock);
  693. return;
  694. }
  695. }
  696. /* Tell holders of references that we're starting waiting */
  697. handle->busy_waiting = 1;
  698. _starpu_spin_unlock(&handle->header_lock);
  699. retry_busy:
  700. /* Wait for all requests to finish (notably WT requests) */
  701. STARPU_PTHREAD_MUTEX_LOCK(&handle->busy_mutex);
  702. while (1)
  703. {
  704. /* Here helgrind would shout that this an unprotected access,
  705. * but this is actually fine: all threads who do busy_count--
  706. * are supposed to call _starpu_data_check_not_busy, which will
  707. * wake us up through the busy_mutex/busy_cond. */
  708. if (!handle->busy_count)
  709. break;
  710. /* This is woken by _starpu_data_check_not_busy, always called
  711. * after decrementing busy_count */
  712. STARPU_PTHREAD_COND_WAIT(&handle->busy_cond, &handle->busy_mutex);
  713. }
  714. STARPU_PTHREAD_MUTEX_UNLOCK(&handle->busy_mutex);
  715. /* Wait for finished requests to release the handle */
  716. _starpu_spin_lock(&handle->header_lock);
  717. if (handle->busy_count)
  718. {
  719. /* Bad luck: some request went in in between, wait again... */
  720. _starpu_spin_unlock(&handle->header_lock);
  721. goto retry_busy;
  722. }
  723. size_t size = _starpu_data_get_size(handle);
  724. _starpu_data_unregister_ram_pointer(handle);
  725. /* Destroy the data now */
  726. unsigned node;
  727. for (node = 0; node < STARPU_MAXNODES; node++)
  728. {
  729. struct _starpu_data_replicate *local = &handle->per_node[node];
  730. /* free the data copy in a lazy fashion */
  731. if (local->allocated && local->automatically_allocated)
  732. _starpu_request_mem_chunk_removal(handle, local, node, size);
  733. }
  734. if (handle->per_worker)
  735. {
  736. unsigned worker;
  737. unsigned nworkers = starpu_worker_get_count();
  738. for (worker = 0; worker < nworkers; worker++)
  739. {
  740. struct _starpu_data_replicate *local = &handle->per_worker[worker];
  741. /* free the data copy in a lazy fashion */
  742. if (local->allocated && local->automatically_allocated)
  743. _starpu_request_mem_chunk_removal(handle, local, starpu_worker_get_memory_node(worker), size);
  744. }
  745. }
  746. _starpu_data_free_interfaces(handle);
  747. _starpu_memory_stats_free(handle);
  748. _starpu_spin_unlock(&handle->header_lock);
  749. _starpu_spin_destroy(&handle->header_lock);
  750. _starpu_data_clear_implicit(handle);
  751. STARPU_PTHREAD_MUTEX_DESTROY(&handle->busy_mutex);
  752. STARPU_PTHREAD_COND_DESTROY(&handle->busy_cond);
  753. STARPU_PTHREAD_MUTEX_DESTROY(&handle->sequential_consistency_mutex);
  754. STARPU_HG_ENABLE_CHECKING(handle->post_sync_tasks_cnt);
  755. STARPU_HG_ENABLE_CHECKING(handle->busy_count);
  756. if (handle->switch_cl)
  757. {
  758. free(handle->switch_cl->dyn_nodes);
  759. free(handle->switch_cl);
  760. }
  761. free(handle);
  762. }
  763. void starpu_data_unregister(starpu_data_handle_t handle)
  764. {
  765. STARPU_ASSERT_MSG(handle->magic == 42, "data %p is invalid (was it already registered?)", handle);
  766. STARPU_ASSERT_MSG(!handle->lazy_unregister, "data %p can not be unregistered twice", handle);
  767. if (handle->unregister_hook)
  768. {
  769. handle->unregister_hook(handle);
  770. }
  771. _starpu_data_unregister(handle, 1, 0);
  772. }
  773. void starpu_data_unregister_no_coherency(starpu_data_handle_t handle)
  774. {
  775. STARPU_ASSERT_MSG(handle->magic == 42, "data %p is invalid (was it already registered?)", handle);
  776. if (handle->unregister_hook)
  777. {
  778. handle->unregister_hook(handle);
  779. }
  780. _starpu_data_unregister(handle, 0, 0);
  781. }
  782. static void _starpu_data_unregister_submit_cb(void *arg)
  783. {
  784. starpu_data_handle_t handle = arg;
  785. _starpu_spin_lock(&handle->header_lock);
  786. handle->lazy_unregister = 1;
  787. /* The handle should be busy since we are working on it.
  788. * when we releases the handle below, it will be destroyed by
  789. * _starpu_data_check_not_busy */
  790. STARPU_ASSERT(handle->busy_count);
  791. _starpu_spin_unlock(&handle->header_lock);
  792. starpu_data_release_on_node(handle, STARPU_ACQUIRE_NO_NODE_LOCK_ALL);
  793. }
  794. void starpu_data_unregister_submit(starpu_data_handle_t handle)
  795. {
  796. STARPU_ASSERT_MSG(handle->magic == 42, "data %p is invalid (was it already registered?)", handle);
  797. STARPU_ASSERT_MSG(!handle->lazy_unregister, "data %p can not be unregistered twice", handle);
  798. if (handle->unregister_hook)
  799. {
  800. handle->unregister_hook(handle);
  801. }
  802. /* Wait for all task dependencies on this handle before putting it for free */
  803. starpu_data_acquire_on_node_cb(handle, STARPU_ACQUIRE_NO_NODE_LOCK_ALL, STARPU_RW, _starpu_data_unregister_submit_cb, handle);
  804. }
  805. static void _starpu_data_invalidate(void *data)
  806. {
  807. starpu_data_handle_t handle = data;
  808. size_t size = _starpu_data_get_size(handle);
  809. _starpu_spin_lock(&handle->header_lock);
  810. //_STARPU_DEBUG("Really invalidating data %p\n", data);
  811. #ifdef STARPU_DEBUG
  812. {
  813. /* There shouldn't be any pending request since we acquired the data in W mode */
  814. unsigned i, j, nnodes = starpu_memory_nodes_get_count();
  815. for (i = 0; i < nnodes; i++)
  816. for (j = 0; j < nnodes; j++)
  817. STARPU_ASSERT_MSG(!handle->per_node[i].request[j], "request for handle %p pending from %d to %d while invalidating data!", handle, j, i);
  818. }
  819. #endif
  820. unsigned node;
  821. for (node = 0; node < STARPU_MAXNODES; node++)
  822. {
  823. struct _starpu_data_replicate *local = &handle->per_node[node];
  824. if (local->mc && local->allocated && local->automatically_allocated)
  825. {
  826. if (node == STARPU_MAIN_RAM)
  827. _starpu_data_unregister_ram_pointer(handle);
  828. /* free the data copy in a lazy fashion */
  829. _starpu_request_mem_chunk_removal(handle, local, node, size);
  830. }
  831. if (local->state != STARPU_INVALID)
  832. _STARPU_TRACE_DATA_INVALIDATE(handle, node);
  833. local->state = STARPU_INVALID;
  834. }
  835. if (handle->per_worker)
  836. {
  837. unsigned worker;
  838. unsigned nworkers = starpu_worker_get_count();
  839. for (worker = 0; worker < nworkers; worker++)
  840. {
  841. struct _starpu_data_replicate *local = &handle->per_worker[worker];
  842. if (local->mc && local->allocated && local->automatically_allocated)
  843. /* free the data copy in a lazy fashion */
  844. _starpu_request_mem_chunk_removal(handle, local, starpu_worker_get_memory_node(worker), size);
  845. local->state = STARPU_INVALID;
  846. }
  847. }
  848. _starpu_spin_unlock(&handle->header_lock);
  849. starpu_data_release_on_node(handle, STARPU_ACQUIRE_NO_NODE_LOCK_ALL);
  850. }
  851. void starpu_data_invalidate(starpu_data_handle_t handle)
  852. {
  853. STARPU_ASSERT(handle);
  854. starpu_data_acquire_on_node(handle, STARPU_ACQUIRE_NO_NODE_LOCK_ALL, STARPU_W);
  855. _starpu_data_invalidate(handle);
  856. handle->initialized = 0;
  857. }
  858. void starpu_data_invalidate_submit(starpu_data_handle_t handle)
  859. {
  860. STARPU_ASSERT(handle);
  861. starpu_data_acquire_on_node_cb(handle, STARPU_ACQUIRE_NO_NODE_LOCK_ALL, STARPU_W, _starpu_data_invalidate, handle);
  862. handle->initialized = 0;
  863. }
  864. enum starpu_data_interface_id starpu_data_get_interface_id(starpu_data_handle_t handle)
  865. {
  866. return handle->ops->interfaceid;
  867. }
  868. void *starpu_data_get_interface_on_node(starpu_data_handle_t handle, unsigned memory_node)
  869. {
  870. return handle->per_node[memory_node].data_interface;
  871. }
  872. int starpu_data_interface_get_next_id(void)
  873. {
  874. _data_interface_number += 1;
  875. return _data_interface_number-1;
  876. }
  877. int starpu_data_pack(starpu_data_handle_t handle, void **ptr, starpu_ssize_t *count)
  878. {
  879. STARPU_ASSERT(handle->ops->pack_data);
  880. return handle->ops->pack_data(handle, _starpu_memory_node_get_local_key(), ptr, count);
  881. }
  882. int starpu_data_unpack(starpu_data_handle_t handle, void *ptr, size_t count)
  883. {
  884. STARPU_ASSERT(handle->ops->unpack_data);
  885. int ret;
  886. ret = handle->ops->unpack_data(handle, _starpu_memory_node_get_local_key(), ptr, count);
  887. starpu_free_flags(ptr, count, 0);
  888. return ret;
  889. }
  890. size_t starpu_data_get_size(starpu_data_handle_t handle)
  891. {
  892. return handle->ops->get_size(handle);
  893. }