data_interface.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-2017 Université de Bordeaux
  4. * Copyright (C) 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017 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 <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, *tmp;
  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, *tmp;
  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, *tmp;
  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_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->root_handle = handle;
  243. handle->father_handle = NULL;
  244. handle->sibling_index = 0; /* could be anything for the root */
  245. handle->depth = 1; /* the tree is just a node yet */
  246. handle->mpi_data = NULL; /* invalid until set */
  247. handle->is_not_important = 0;
  248. handle->sequential_consistency =
  249. starpu_data_get_default_sequential_consistency_flag();
  250. handle->initialized = home_node != -1;
  251. STARPU_PTHREAD_MUTEX_INIT(&handle->sequential_consistency_mutex, NULL);
  252. handle->last_submitted_mode = STARPU_R;
  253. handle->last_sync_task = NULL;
  254. handle->last_submitted_accessors.task = NULL;
  255. handle->last_submitted_accessors.next = &handle->last_submitted_accessors;
  256. handle->last_submitted_accessors.prev = &handle->last_submitted_accessors;
  257. handle->post_sync_tasks = NULL;
  258. /* Tell helgrind that the race in _starpu_unlock_post_sync_tasks is fine */
  259. STARPU_HG_DISABLE_CHECKING(handle->post_sync_tasks_cnt);
  260. handle->post_sync_tasks_cnt = 0;
  261. /* By default, there are no methods available to perform a reduction */
  262. handle->redux_cl = NULL;
  263. handle->init_cl = NULL;
  264. handle->reduction_refcnt = 0;
  265. _starpu_data_requester_list_init(&handle->reduction_req_list);
  266. handle->reduction_tmp_handles = NULL;
  267. handle->write_invalidation_req = NULL;
  268. #ifdef STARPU_USE_FXT
  269. handle->last_submitted_ghost_sync_id_is_valid = 0;
  270. handle->last_submitted_ghost_sync_id = 0;
  271. handle->last_submitted_ghost_accessors_id = NULL;
  272. #endif
  273. handle->wt_mask = wt_mask;
  274. /* Store some values directly in the handle not to recompute them all
  275. * the time. */
  276. handle->footprint = _starpu_compute_data_footprint(handle);
  277. handle->home_node = home_node;
  278. if (_starpu_global_arbiter)
  279. /* Just for testing purpose */
  280. starpu_data_assign_arbiter(handle, _starpu_global_arbiter);
  281. else
  282. handle->arbiter = NULL;
  283. _starpu_data_requester_list_init(&handle->arbitered_req_list);
  284. handle->last_locality = -1;
  285. /* that new data is invalid from all nodes perpective except for the
  286. * home node */
  287. unsigned node;
  288. for (node = 0; node < STARPU_MAXNODES; node++)
  289. {
  290. struct _starpu_data_replicate *replicate;
  291. replicate = &handle->per_node[node];
  292. replicate->memory_node = node;
  293. replicate->relaxed_coherency = 0;
  294. replicate->refcnt = 0;
  295. if ((int) node == home_node)
  296. {
  297. /* this is the home node with the only valid copy */
  298. replicate->state = STARPU_OWNER;
  299. replicate->allocated = 1;
  300. replicate->automatically_allocated = 0;
  301. replicate->initialized = 1;
  302. }
  303. else
  304. {
  305. /* the value is not available here yet */
  306. replicate->state = STARPU_INVALID;
  307. replicate->allocated = 0;
  308. replicate->initialized = 0;
  309. }
  310. }
  311. handle->per_worker = NULL;
  312. handle->user_data = NULL;
  313. /* now the data is available ! */
  314. _starpu_spin_unlock(&handle->header_lock);
  315. for (node = 0; node < STARPU_MAXNODES; node++)
  316. {
  317. if (starpu_node_get_kind(node) != STARPU_CPU_RAM)
  318. continue;
  319. ptr = starpu_data_handle_to_pointer(handle, node);
  320. if (ptr != NULL)
  321. _starpu_data_register_ram_pointer(handle, ptr);
  322. }
  323. }
  324. void
  325. _starpu_data_initialize_per_worker(starpu_data_handle_t handle)
  326. {
  327. unsigned worker;
  328. unsigned nworkers = starpu_worker_get_count();
  329. _starpu_spin_checklocked(&handle->header_lock);
  330. _STARPU_CALLOC(handle->per_worker, nworkers, sizeof(*handle->per_worker));
  331. size_t interfacesize = handle->ops->interface_size;
  332. for (worker = 0; worker < nworkers; worker++)
  333. {
  334. struct _starpu_data_replicate *replicate;
  335. unsigned node;
  336. replicate = &handle->per_worker[worker];
  337. replicate->allocated = 0;
  338. replicate->automatically_allocated = 0;
  339. replicate->state = STARPU_INVALID;
  340. replicate->refcnt = 0;
  341. replicate->handle = handle;
  342. replicate->requested = 0;
  343. for (node = 0; node < STARPU_MAXNODES; node++)
  344. {
  345. replicate->request[node] = NULL;
  346. }
  347. /* Assuming being used for SCRATCH for now, patched when entering REDUX mode */
  348. replicate->relaxed_coherency = 1;
  349. replicate->initialized = 0;
  350. replicate->memory_node = starpu_worker_get_memory_node(worker);
  351. _STARPU_CALLOC(replicate->data_interface, 1, interfacesize);
  352. /* duplicate the content of the interface on node 0 */
  353. memcpy(replicate->data_interface, handle->per_node[STARPU_MAIN_RAM].data_interface, interfacesize);
  354. }
  355. }
  356. void starpu_data_ptr_register(starpu_data_handle_t handle, unsigned node)
  357. {
  358. struct _starpu_data_replicate *replicate = &handle->per_node[node];
  359. _starpu_spin_lock(&handle->header_lock);
  360. STARPU_ASSERT_MSG(replicate->allocated == 0, "starpu_data_ptr_register must be called right after starpu_data_register");
  361. replicate->allocated = 1;
  362. replicate->automatically_allocated = 0;
  363. _starpu_spin_unlock(&handle->header_lock);
  364. }
  365. int _starpu_data_handle_init(starpu_data_handle_t handle, struct starpu_data_interface_ops *interface_ops, unsigned int mf_node)
  366. {
  367. unsigned node;
  368. /* Tell helgrind that our access to busy_count in
  369. * starpu_data_unregister is actually safe */
  370. STARPU_HG_DISABLE_CHECKING(handle->busy_count);
  371. handle->magic = 42;
  372. handle->ops = interface_ops;
  373. handle->mf_node = mf_node;
  374. handle->mpi_data = NULL;
  375. size_t interfacesize = interface_ops->interface_size;
  376. _starpu_memory_stats_init(handle);
  377. for (node = 0; node < STARPU_MAXNODES; node++)
  378. {
  379. _starpu_memory_stats_init_per_node(handle, node);
  380. struct _starpu_data_replicate *replicate;
  381. replicate = &handle->per_node[node];
  382. /* relaxed_coherency = 0 */
  383. replicate->handle = handle;
  384. _STARPU_CALLOC(replicate->data_interface, 1, interfacesize);
  385. }
  386. return 0;
  387. }
  388. static
  389. starpu_data_handle_t _starpu_data_handle_allocate(struct starpu_data_interface_ops *interface_ops, unsigned int mf_node)
  390. {
  391. starpu_data_handle_t handle;
  392. _STARPU_CALLOC(handle, 1, sizeof(struct _starpu_data_state));
  393. _starpu_data_handle_init(handle, interface_ops, mf_node);
  394. return handle;
  395. }
  396. void starpu_data_register(starpu_data_handle_t *handleptr, int home_node,
  397. void *data_interface,
  398. struct starpu_data_interface_ops *ops)
  399. {
  400. starpu_data_handle_t handle = _starpu_data_handle_allocate(ops, home_node);
  401. STARPU_ASSERT(handleptr);
  402. *handleptr = handle;
  403. /* fill the interface fields with the appropriate method */
  404. STARPU_ASSERT(ops->register_data_handle);
  405. ops->register_data_handle(handle, home_node, data_interface);
  406. _starpu_register_new_data(handle, home_node, 0);
  407. _STARPU_TRACE_HANDLE_DATA_REGISTER(handle);
  408. }
  409. void starpu_data_register_same(starpu_data_handle_t *handledst, starpu_data_handle_t handlesrc)
  410. {
  411. void *local_interface = starpu_data_get_interface_on_node(handlesrc, STARPU_MAIN_RAM);
  412. starpu_data_register(handledst, -1, local_interface, handlesrc->ops);
  413. }
  414. void *starpu_data_handle_to_pointer(starpu_data_handle_t handle, unsigned node)
  415. {
  416. /* Check whether the operation is supported and the node has actually
  417. * been allocated. */
  418. if (handle->ops->handle_to_pointer
  419. && starpu_data_test_if_allocated_on_node(handle, node))
  420. {
  421. return handle->ops->handle_to_pointer(handle, node);
  422. }
  423. return NULL;
  424. }
  425. void *starpu_data_get_local_ptr(starpu_data_handle_t handle)
  426. {
  427. return starpu_data_handle_to_pointer(handle,
  428. _starpu_memory_node_get_local_key());
  429. }
  430. struct starpu_data_interface_ops* starpu_data_get_interface_ops(starpu_data_handle_t handle)
  431. {
  432. return handle->ops;
  433. }
  434. /*
  435. * Stop monitoring a piece of data
  436. */
  437. void _starpu_data_unregister_ram_pointer(starpu_data_handle_t handle, unsigned node)
  438. {
  439. if (starpu_node_get_kind(node) != STARPU_CPU_RAM)
  440. return;
  441. #ifdef STARPU_OPENMP
  442. if (handle->removed_from_context_hash)
  443. return;
  444. #endif
  445. const void *ram_ptr = starpu_data_handle_to_pointer(handle, node);
  446. if (ram_ptr != NULL)
  447. {
  448. /* Remove the PTR -> HANDLE mapping. If a mapping from PTR
  449. * to another handle existed before (e.g., when using
  450. * filters), it becomes visible again. */
  451. struct handle_entry *entry;
  452. #ifdef STARPU_OPENMP
  453. struct starpu_omp_task *task = _starpu_omp_get_task();
  454. if (task)
  455. {
  456. if (task->flags & STARPU_OMP_TASK_FLAGS_IMPLICIT)
  457. {
  458. struct starpu_omp_region *parallel_region = task->owner_region;
  459. _starpu_spin_lock(&parallel_region->registered_handles_lock);
  460. HASH_FIND_PTR(parallel_region->registered_handles, &ram_ptr, entry);
  461. STARPU_ASSERT(entry != NULL);
  462. HASH_DEL(registered_handles, entry);
  463. _starpu_spin_unlock(&parallel_region->registered_handles_lock);
  464. }
  465. else
  466. {
  467. HASH_FIND_PTR(task->registered_handles, &ram_ptr, entry);
  468. STARPU_ASSERT(entry != NULL);
  469. HASH_DEL(task->registered_handles, entry);
  470. }
  471. }
  472. else
  473. #endif
  474. {
  475. _starpu_spin_lock(&registered_handles_lock);
  476. HASH_FIND_PTR(registered_handles, &ram_ptr, entry);
  477. if (entry)
  478. {
  479. if (entry->handle == handle)
  480. {
  481. nregistered--;
  482. HASH_DEL(registered_handles, entry);
  483. }
  484. else
  485. /* don't free it, it's not ours */
  486. entry = NULL;
  487. }
  488. _starpu_spin_unlock(&registered_handles_lock);
  489. }
  490. free(entry);
  491. }
  492. }
  493. void _starpu_data_free_interfaces(starpu_data_handle_t handle)
  494. {
  495. unsigned node;
  496. unsigned nworkers = starpu_worker_get_count();
  497. for (node = 0; node < STARPU_MAXNODES; node++)
  498. free(handle->per_node[node].data_interface);
  499. if (handle->per_worker)
  500. {
  501. unsigned worker;
  502. for (worker = 0; worker < nworkers; worker++)
  503. free(handle->per_worker[worker].data_interface);
  504. free(handle->per_worker);
  505. }
  506. }
  507. struct _starpu_unregister_callback_arg
  508. {
  509. unsigned memory_node;
  510. starpu_data_handle_t handle;
  511. unsigned terminated;
  512. starpu_pthread_mutex_t mutex;
  513. starpu_pthread_cond_t cond;
  514. };
  515. /* Check whether we should tell starpu_data_unregister that the data handle is
  516. * not busy any more.
  517. * The header is supposed to be locked.
  518. * This may free the handle, if it was lazily unregistered (1 is returned in
  519. * that case). The handle pointer thus becomes invalid for the caller.
  520. *
  521. * Note: we inline some of the tests in the _starpu_data_check_not_busy macro.
  522. */
  523. int __starpu_data_check_not_busy(starpu_data_handle_t handle)
  524. {
  525. if (STARPU_LIKELY(handle->busy_count))
  526. return 0;
  527. /* Not busy any more, perhaps have to unregister etc. */
  528. if (STARPU_UNLIKELY(handle->busy_waiting))
  529. {
  530. STARPU_PTHREAD_MUTEX_LOCK(&handle->busy_mutex);
  531. STARPU_PTHREAD_COND_BROADCAST(&handle->busy_cond);
  532. STARPU_PTHREAD_MUTEX_UNLOCK(&handle->busy_mutex);
  533. }
  534. /* The handle has been destroyed in between (eg. this was a temporary
  535. * handle created for a reduction.) */
  536. if (STARPU_UNLIKELY(handle->lazy_unregister))
  537. {
  538. handle->lazy_unregister = 0;
  539. _starpu_spin_unlock(&handle->header_lock);
  540. _starpu_data_unregister(handle, 0, 1);
  541. /* Warning: in case we unregister the handle, we must be sure
  542. * that the caller will not try to unlock the header after
  543. * !*/
  544. return 1;
  545. }
  546. return 0;
  547. }
  548. static
  549. void _starpu_check_if_valid_and_fetch_data_on_node(starpu_data_handle_t handle, struct _starpu_data_replicate *replicate, const char *origin)
  550. {
  551. unsigned node;
  552. unsigned nnodes = starpu_memory_nodes_get_count();
  553. int valid = 0;
  554. for (node = 0; node < nnodes; node++)
  555. {
  556. if (handle->per_node[node].state != STARPU_INVALID)
  557. {
  558. /* we found a copy ! */
  559. valid = 1;
  560. }
  561. }
  562. if (valid)
  563. {
  564. int ret = _starpu_fetch_data_on_node(handle, handle->home_node, replicate, STARPU_R, 0, 0, 0, NULL, NULL, 0, origin);
  565. STARPU_ASSERT(!ret);
  566. _starpu_release_data_on_node(handle, handle->home_node, replicate);
  567. }
  568. else
  569. {
  570. _starpu_spin_lock(&handle->header_lock);
  571. if (!_starpu_notify_data_dependencies(handle))
  572. _starpu_spin_unlock(&handle->header_lock);
  573. }
  574. }
  575. static void _starpu_data_unregister_fetch_data_callback(void *_arg)
  576. {
  577. struct _starpu_unregister_callback_arg *arg = (struct _starpu_unregister_callback_arg *) _arg;
  578. starpu_data_handle_t handle = arg->handle;
  579. STARPU_ASSERT(handle);
  580. struct _starpu_data_replicate *replicate = &handle->per_node[arg->memory_node];
  581. _starpu_check_if_valid_and_fetch_data_on_node(handle, replicate, "_starpu_data_unregister_fetch_data_callback");
  582. /* unlock the caller */
  583. STARPU_PTHREAD_MUTEX_LOCK(&arg->mutex);
  584. arg->terminated = 1;
  585. STARPU_PTHREAD_COND_SIGNAL(&arg->cond);
  586. STARPU_PTHREAD_MUTEX_UNLOCK(&arg->mutex);
  587. }
  588. /* Unregister the data handle, perhaps we don't need to update the home_node
  589. * (in that case coherent is set to 0)
  590. * nowait is for internal use when we already know for sure that we won't have to wait.
  591. */
  592. static void _starpu_data_unregister(starpu_data_handle_t handle, unsigned coherent, unsigned nowait)
  593. {
  594. STARPU_ASSERT(handle);
  595. STARPU_ASSERT_MSG(handle->nchildren == 0, "data %p needs to be unpartitioned before unregistration", handle);
  596. STARPU_ASSERT_MSG(handle->nplans == 0, "data %p needs its partition plans to be cleaned before unregistration", handle);
  597. STARPU_ASSERT_MSG(handle->partitioned == 0, "data %p needs its partitioned plans to be unpartitioned before unregistration", handle);
  598. /* TODO: also check that it has the latest coherency */
  599. STARPU_ASSERT(!(nowait && handle->busy_count != 0));
  600. int sequential_consistency = handle->sequential_consistency;
  601. if (sequential_consistency && !nowait)
  602. {
  603. 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");
  604. /* If sequential consistency is enabled, wait until data is available */
  605. _starpu_data_wait_until_available(handle, STARPU_RW, "starpu_data_unregister");
  606. }
  607. if (coherent && !nowait)
  608. {
  609. 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");
  610. /* Fetch data in the home of the data to ensure we have a valid copy
  611. * where we registered it */
  612. int home_node = handle->home_node;
  613. if (home_node >= 0)
  614. {
  615. struct _starpu_unregister_callback_arg arg;
  616. arg.handle = handle;
  617. arg.memory_node = (unsigned)home_node;
  618. arg.terminated = 0;
  619. STARPU_PTHREAD_MUTEX_INIT(&arg.mutex, NULL);
  620. STARPU_PTHREAD_COND_INIT(&arg.cond, NULL);
  621. if (!_starpu_attempt_to_submit_data_request_from_apps(handle, STARPU_R,
  622. _starpu_data_unregister_fetch_data_callback, &arg))
  623. {
  624. /* no one has locked this data yet, so we proceed immediately */
  625. struct _starpu_data_replicate *home_replicate = &handle->per_node[home_node];
  626. _starpu_check_if_valid_and_fetch_data_on_node(handle, home_replicate, "_starpu_data_unregister");
  627. }
  628. else
  629. {
  630. STARPU_PTHREAD_MUTEX_LOCK(&arg.mutex);
  631. while (!arg.terminated)
  632. STARPU_PTHREAD_COND_WAIT(&arg.cond, &arg.mutex);
  633. STARPU_PTHREAD_MUTEX_UNLOCK(&arg.mutex);
  634. }
  635. STARPU_PTHREAD_MUTEX_DESTROY(&arg.mutex);
  636. STARPU_PTHREAD_COND_DESTROY(&arg.cond);
  637. }
  638. /* If this handle uses a multiformat interface, we may have to convert
  639. * this piece of data back into the CPU format.
  640. * XXX : This is quite hacky, could we submit a task instead ?
  641. */
  642. if (_starpu_data_is_multiformat_handle(handle) &&
  643. ( starpu_node_get_kind(handle->mf_node) != STARPU_CPU_RAM
  644. && starpu_node_get_kind(handle->mf_node) != STARPU_SCC_RAM
  645. && starpu_node_get_kind(handle->mf_node) != STARPU_SCC_SHM
  646. ))
  647. {
  648. _STARPU_DEBUG("Conversion needed\n");
  649. void *buffers[1];
  650. struct starpu_multiformat_interface *format_interface;
  651. home_node = handle->home_node;
  652. if (home_node < 0 || (starpu_node_get_kind(home_node) != STARPU_CPU_RAM))
  653. home_node = STARPU_MAIN_RAM;
  654. format_interface = (struct starpu_multiformat_interface *) starpu_data_get_interface_on_node(handle, home_node);
  655. struct starpu_codelet *cl = NULL;
  656. enum starpu_node_kind node_kind = starpu_node_get_kind(handle->mf_node);
  657. switch (node_kind)
  658. {
  659. #ifdef STARPU_USE_CUDA
  660. case STARPU_CUDA_RAM:
  661. {
  662. struct starpu_multiformat_data_interface_ops *mf_ops;
  663. mf_ops = (struct starpu_multiformat_data_interface_ops *) handle->ops->get_mf_ops(format_interface);
  664. cl = mf_ops->cuda_to_cpu_cl;
  665. break;
  666. }
  667. #endif
  668. #ifdef STARPU_USE_OPENCL
  669. case STARPU_OPENCL_RAM:
  670. {
  671. struct starpu_multiformat_data_interface_ops *mf_ops;
  672. mf_ops = (struct starpu_multiformat_data_interface_ops *) handle->ops->get_mf_ops(format_interface);
  673. cl = mf_ops->opencl_to_cpu_cl;
  674. break;
  675. }
  676. #endif
  677. #ifdef STARPU_USE_MIC
  678. case STARPU_MIC_RAM:
  679. {
  680. struct starpu_multiformat_data_interface_ops *mf_ops;
  681. mf_ops = (struct starpu_multiformat_data_interface_ops *) handle->ops->get_mf_ops(format_interface);
  682. cl = mf_ops->mic_to_cpu_cl;
  683. break;
  684. }
  685. #endif
  686. case STARPU_CPU_RAM: /* Impossible ! */
  687. case STARPU_SCC_RAM: /* Impossible ! */
  688. case STARPU_SCC_SHM: /* Impossible ! */
  689. default:
  690. STARPU_ABORT();
  691. }
  692. buffers[0] = format_interface;
  693. _starpu_cl_func_t func = _starpu_task_get_cpu_nth_implementation(cl, 0);
  694. STARPU_ASSERT(func);
  695. func(buffers, NULL);
  696. }
  697. }
  698. /* Prevent any further unregistration */
  699. handle->magic = 0;
  700. _starpu_spin_lock(&handle->header_lock);
  701. if (!coherent)
  702. {
  703. /* Should we postpone the unregister operation ? */
  704. if ((handle->busy_count > 0) && handle->lazy_unregister)
  705. {
  706. _starpu_spin_unlock(&handle->header_lock);
  707. return;
  708. }
  709. }
  710. /* Tell holders of references that we're starting waiting */
  711. handle->busy_waiting = 1;
  712. _starpu_spin_unlock(&handle->header_lock);
  713. retry_busy:
  714. /* Wait for all requests to finish (notably WT requests) */
  715. STARPU_PTHREAD_MUTEX_LOCK(&handle->busy_mutex);
  716. while (1)
  717. {
  718. /* Here helgrind would shout that this an unprotected access,
  719. * but this is actually fine: all threads who do busy_count--
  720. * are supposed to call _starpu_data_check_not_busy, which will
  721. * wake us up through the busy_mutex/busy_cond. */
  722. if (!handle->busy_count)
  723. break;
  724. /* This is woken by _starpu_data_check_not_busy, always called
  725. * after decrementing busy_count */
  726. STARPU_PTHREAD_COND_WAIT(&handle->busy_cond, &handle->busy_mutex);
  727. }
  728. STARPU_PTHREAD_MUTEX_UNLOCK(&handle->busy_mutex);
  729. /* Wait for finished requests to release the handle */
  730. _starpu_spin_lock(&handle->header_lock);
  731. if (handle->busy_count)
  732. {
  733. /* Bad luck: some request went in in between, wait again... */
  734. _starpu_spin_unlock(&handle->header_lock);
  735. goto retry_busy;
  736. }
  737. size_t size = _starpu_data_get_size(handle);
  738. /* Destroy the data now */
  739. unsigned node;
  740. for (node = 0; node < STARPU_MAXNODES; node++)
  741. {
  742. struct _starpu_data_replicate *local = &handle->per_node[node];
  743. if (local->allocated)
  744. {
  745. _starpu_data_unregister_ram_pointer(handle, node);
  746. /* free the data copy in a lazy fashion */
  747. if (local->automatically_allocated)
  748. _starpu_request_mem_chunk_removal(handle, local, node, size);
  749. }
  750. }
  751. if (handle->per_worker)
  752. {
  753. unsigned worker;
  754. unsigned nworkers = starpu_worker_get_count();
  755. for (worker = 0; worker < nworkers; worker++)
  756. {
  757. struct _starpu_data_replicate *local = &handle->per_worker[worker];
  758. /* free the data copy in a lazy fashion */
  759. if (local->allocated && local->automatically_allocated)
  760. _starpu_request_mem_chunk_removal(handle, local, starpu_worker_get_memory_node(worker), size);
  761. }
  762. }
  763. _starpu_data_free_interfaces(handle);
  764. _starpu_memory_stats_free(handle);
  765. _starpu_spin_unlock(&handle->header_lock);
  766. _starpu_spin_destroy(&handle->header_lock);
  767. _starpu_data_clear_implicit(handle);
  768. STARPU_PTHREAD_MUTEX_DESTROY(&handle->busy_mutex);
  769. STARPU_PTHREAD_COND_DESTROY(&handle->busy_cond);
  770. STARPU_PTHREAD_MUTEX_DESTROY(&handle->sequential_consistency_mutex);
  771. STARPU_HG_ENABLE_CHECKING(handle->post_sync_tasks_cnt);
  772. STARPU_HG_ENABLE_CHECKING(handle->busy_count);
  773. if (handle->switch_cl)
  774. {
  775. free(handle->switch_cl->dyn_nodes);
  776. free(handle->switch_cl);
  777. }
  778. _STARPU_TRACE_HANDLE_DATA_UNREGISTER(handle);
  779. free(handle);
  780. }
  781. void starpu_data_unregister(starpu_data_handle_t handle)
  782. {
  783. STARPU_ASSERT_MSG(handle->magic == 42, "data %p is invalid (was it already registered?)", handle);
  784. STARPU_ASSERT_MSG(!handle->lazy_unregister, "data %p can not be unregistered twice", handle);
  785. if (handle->unregister_hook)
  786. {
  787. handle->unregister_hook(handle);
  788. }
  789. _starpu_data_unregister(handle, 1, 0);
  790. }
  791. void starpu_data_unregister_no_coherency(starpu_data_handle_t handle)
  792. {
  793. STARPU_ASSERT_MSG(handle->magic == 42, "data %p is invalid (was it already registered?)", handle);
  794. if (handle->unregister_hook)
  795. {
  796. handle->unregister_hook(handle);
  797. }
  798. _starpu_data_unregister(handle, 0, 0);
  799. }
  800. static void _starpu_data_unregister_submit_cb(void *arg)
  801. {
  802. starpu_data_handle_t handle = arg;
  803. _starpu_spin_lock(&handle->header_lock);
  804. handle->lazy_unregister = 1;
  805. /* The handle should be busy since we are working on it.
  806. * when we releases the handle below, it will be destroyed by
  807. * _starpu_data_check_not_busy */
  808. STARPU_ASSERT(handle->busy_count);
  809. _starpu_spin_unlock(&handle->header_lock);
  810. starpu_data_release_on_node(handle, STARPU_ACQUIRE_NO_NODE_LOCK_ALL);
  811. }
  812. void starpu_data_unregister_submit(starpu_data_handle_t handle)
  813. {
  814. STARPU_ASSERT_MSG(handle->magic == 42, "data %p is invalid (was it already registered?)", handle);
  815. STARPU_ASSERT_MSG(!handle->lazy_unregister, "data %p can not be unregistered twice", handle);
  816. if (handle->unregister_hook)
  817. {
  818. handle->unregister_hook(handle);
  819. }
  820. /* Wait for all task dependencies on this handle before putting it for free */
  821. 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);
  822. }
  823. static void _starpu_data_invalidate(void *data)
  824. {
  825. starpu_data_handle_t handle = data;
  826. size_t size = _starpu_data_get_size(handle);
  827. _starpu_spin_lock(&handle->header_lock);
  828. //_STARPU_DEBUG("Really invalidating data %p\n", data);
  829. #ifdef STARPU_DEBUG
  830. {
  831. /* There shouldn't be any pending request since we acquired the data in W mode */
  832. unsigned i, j, nnodes = starpu_memory_nodes_get_count();
  833. for (i = 0; i < nnodes; i++)
  834. for (j = 0; j < nnodes; j++)
  835. STARPU_ASSERT_MSG(!handle->per_node[i].request[j], "request for handle %p pending from %u to %u while invalidating data!", handle, j, i);
  836. }
  837. #endif
  838. unsigned node;
  839. for (node = 0; node < STARPU_MAXNODES; node++)
  840. {
  841. struct _starpu_data_replicate *local = &handle->per_node[node];
  842. if (local->mc && local->allocated && local->automatically_allocated)
  843. {
  844. _starpu_data_unregister_ram_pointer(handle, node);
  845. /* free the data copy in a lazy fashion */
  846. _starpu_request_mem_chunk_removal(handle, local, node, size);
  847. }
  848. if (local->state != STARPU_INVALID)
  849. _STARPU_TRACE_DATA_INVALIDATE(handle, node);
  850. local->state = STARPU_INVALID;
  851. }
  852. if (handle->per_worker)
  853. {
  854. unsigned worker;
  855. unsigned nworkers = starpu_worker_get_count();
  856. for (worker = 0; worker < nworkers; worker++)
  857. {
  858. struct _starpu_data_replicate *local = &handle->per_worker[worker];
  859. if (local->mc && local->allocated && local->automatically_allocated)
  860. /* free the data copy in a lazy fashion */
  861. _starpu_request_mem_chunk_removal(handle, local, starpu_worker_get_memory_node(worker), size);
  862. local->state = STARPU_INVALID;
  863. }
  864. }
  865. _starpu_spin_unlock(&handle->header_lock);
  866. starpu_data_release_on_node(handle, STARPU_ACQUIRE_NO_NODE_LOCK_ALL);
  867. }
  868. void starpu_data_invalidate(starpu_data_handle_t handle)
  869. {
  870. STARPU_ASSERT(handle);
  871. starpu_data_acquire_on_node(handle, STARPU_ACQUIRE_NO_NODE_LOCK_ALL, STARPU_W);
  872. _starpu_data_invalidate(handle);
  873. handle->initialized = 0;
  874. }
  875. void starpu_data_invalidate_submit(starpu_data_handle_t handle)
  876. {
  877. STARPU_ASSERT(handle);
  878. starpu_data_acquire_on_node_cb(handle, STARPU_ACQUIRE_NO_NODE_LOCK_ALL, STARPU_W, _starpu_data_invalidate, handle);
  879. handle->initialized = 0;
  880. }
  881. enum starpu_data_interface_id starpu_data_get_interface_id(starpu_data_handle_t handle)
  882. {
  883. return handle->ops->interfaceid;
  884. }
  885. void *starpu_data_get_interface_on_node(starpu_data_handle_t handle, unsigned memory_node)
  886. {
  887. return handle->per_node[memory_node].data_interface;
  888. }
  889. int starpu_data_interface_get_next_id(void)
  890. {
  891. _data_interface_number += 1;
  892. return _data_interface_number-1;
  893. }
  894. int starpu_data_pack(starpu_data_handle_t handle, void **ptr, starpu_ssize_t *count)
  895. {
  896. STARPU_ASSERT_MSG(handle->ops->pack_data, "The datatype interface %s (%d) does not have a pack operation", handle->ops->name, handle->ops->interfaceid);
  897. return handle->ops->pack_data(handle, _starpu_memory_node_get_local_key(), ptr, count);
  898. }
  899. int starpu_data_unpack(starpu_data_handle_t handle, void *ptr, size_t count)
  900. {
  901. STARPU_ASSERT_MSG(handle->ops->unpack_data, "The datatype interface %s (%d) does not have an unpack operation", handle->ops->name, handle->ops->interfaceid);
  902. int ret;
  903. ret = handle->ops->unpack_data(handle, _starpu_memory_node_get_local_key(), ptr, count);
  904. _starpu_free_flags_on_node(_starpu_memory_node_get_local_key(), ptr, count, 0);
  905. return ret;
  906. }
  907. size_t starpu_data_get_size(starpu_data_handle_t handle)
  908. {
  909. return handle->ops->get_size(handle);
  910. }
  911. void starpu_data_set_name(starpu_data_handle_t handle, const char *name)
  912. {
  913. _STARPU_TRACE_DATA_NAME(handle, name);
  914. }
  915. int starpu_data_get_home_node(starpu_data_handle_t handle)
  916. {
  917. return handle->home_node;
  918. }
  919. void starpu_data_set_coordinates_array(starpu_data_handle_t handle, int dimensions, int dims[])
  920. {
  921. _STARPU_TRACE_DATA_COORDINATES(handle, dimensions, dims);
  922. }
  923. void starpu_data_set_coordinates(starpu_data_handle_t handle, unsigned dimensions, ...)
  924. {
  925. int dims[dimensions];
  926. unsigned i;
  927. va_list varg_list;
  928. va_start(varg_list, dimensions);
  929. for (i = 0; i < dimensions; i++)
  930. dims[i] = va_arg(varg_list, int);
  931. va_end(varg_list);
  932. starpu_data_set_coordinates_array(handle, dimensions, dims);
  933. }