data_interface.c 34 KB

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