data_interface.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-2020 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
  4. *
  5. * StarPU is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation; either version 2.1 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * StarPU is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. *
  14. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  15. */
  16. #include <stdint.h>
  17. #include <stdarg.h>
  18. #include <datawizard/datawizard.h>
  19. #include <datawizard/memory_nodes.h>
  20. #include <datawizard/memstats.h>
  21. #include <datawizard/malloc.h>
  22. #include <core/dependencies/data_concurrency.h>
  23. #include <common/uthash.h>
  24. #include <common/starpu_spinlock.h>
  25. #include <core/task.h>
  26. #include <core/workers.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=NULL, *tmp=NULL;
  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=NULL, *tmp=NULL;
  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=NULL, *tmp=NULL;
  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;
  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. struct handle_entry *old_entry;
  149. _starpu_spin_lock(&registered_handles_lock);
  150. HASH_FIND_PTR(registered_handles, &ptr, old_entry);
  151. if (old_entry)
  152. {
  153. /* Already registered this pointer, avoid undefined
  154. * behavior of duplicate in hash table */
  155. _starpu_spin_unlock(&registered_handles_lock);
  156. free(entry);
  157. }
  158. else
  159. {
  160. nregistered++;
  161. if (nregistered > maxnregistered)
  162. maxnregistered = nregistered;
  163. HASH_ADD_PTR(registered_handles, pointer, entry);
  164. _starpu_spin_unlock(&registered_handles_lock);
  165. }
  166. }
  167. }
  168. starpu_data_handle_t starpu_data_lookup(const void *ptr)
  169. {
  170. starpu_data_handle_t result;
  171. #ifdef STARPU_OPENMP
  172. struct starpu_omp_task *task = _starpu_omp_get_task();
  173. if (task)
  174. {
  175. if (task->flags & STARPU_OMP_TASK_FLAGS_IMPLICIT)
  176. {
  177. struct starpu_omp_region *parallel_region = task->owner_region;
  178. _starpu_spin_lock(&parallel_region->registered_handles_lock);
  179. {
  180. struct handle_entry *entry;
  181. HASH_FIND_PTR(parallel_region->registered_handles, &ptr, entry);
  182. if(STARPU_UNLIKELY(entry == NULL))
  183. result = NULL;
  184. else
  185. result = entry->handle;
  186. }
  187. _starpu_spin_unlock(&parallel_region->registered_handles_lock);
  188. }
  189. else
  190. {
  191. struct handle_entry *entry;
  192. HASH_FIND_PTR(task->registered_handles, &ptr, entry);
  193. if(STARPU_UNLIKELY(entry == NULL))
  194. result = NULL;
  195. else
  196. result = entry->handle;
  197. }
  198. }
  199. else
  200. #endif
  201. {
  202. _starpu_spin_lock(&registered_handles_lock);
  203. {
  204. struct handle_entry *entry;
  205. HASH_FIND_PTR(registered_handles, &ptr, entry);
  206. if(STARPU_UNLIKELY(entry == NULL))
  207. result = NULL;
  208. else
  209. result = entry->handle;
  210. }
  211. _starpu_spin_unlock(&registered_handles_lock);
  212. }
  213. return result;
  214. }
  215. /*
  216. * Start monitoring a piece of data
  217. */
  218. static void _starpu_register_new_data(starpu_data_handle_t handle,
  219. int home_node, uint32_t wt_mask)
  220. {
  221. void *ptr;
  222. STARPU_ASSERT(handle);
  223. /* initialize the new lock */
  224. _starpu_data_requester_prio_list_init0(&handle->req_list);
  225. //handle->refcnt = 0;
  226. //handle->unlocking_reqs = 0;
  227. //handle->busy_count = 0;
  228. //handle->busy_waiting = 0;
  229. STARPU_PTHREAD_MUTEX_INIT0(&handle->busy_mutex, NULL);
  230. STARPU_PTHREAD_COND_INIT0(&handle->busy_cond, NULL);
  231. _starpu_spin_init(&handle->header_lock);
  232. /* first take care to properly lock the data */
  233. _starpu_spin_lock(&handle->header_lock);
  234. /* there is no hierarchy yet */
  235. //handle->nchildren = 0;
  236. //handle->nplans = 0;
  237. //handle->switch_cl = NULL;
  238. //handle->partitioned = 0;
  239. //handle->readonly = 0;
  240. handle->active = 1;
  241. //handle->active_ro = 0;
  242. handle->root_handle = handle;
  243. //handle->father_handle = NULL;
  244. //handle->active_children = NULL;
  245. //handle->active_readonly_children = NULL;
  246. //handle->nactive_readonly_children = 0;
  247. //handle->nsiblings = 0;
  248. //handle->siblings = NULL;
  249. //handle->sibling_index = 0; /* could be anything for the root */
  250. handle->depth = 1; /* the tree is just a node yet */
  251. //handle->mpi_data = NULL; /* invalid until set */
  252. //handle->is_not_important = 0;
  253. handle->sequential_consistency =
  254. starpu_data_get_default_sequential_consistency_flag();
  255. handle->initialized = home_node != -1;
  256. handle->ooc = 1;
  257. STARPU_PTHREAD_MUTEX_INIT0(&handle->sequential_consistency_mutex, NULL);
  258. handle->last_submitted_mode = STARPU_R;
  259. //handle->last_sync_task = NULL;
  260. //handle->last_submitted_accessors.task = NULL;
  261. handle->last_submitted_accessors.next = &handle->last_submitted_accessors;
  262. handle->last_submitted_accessors.prev = &handle->last_submitted_accessors;
  263. //handle->post_sync_tasks = NULL;
  264. /* Tell helgrind that the race in _starpu_unlock_post_sync_tasks is fine */
  265. STARPU_HG_DISABLE_CHECKING(handle->post_sync_tasks_cnt);
  266. //handle->post_sync_tasks_cnt = 0;
  267. /* By default, there are no methods available to perform a reduction */
  268. //handle->redux_cl = NULL;
  269. //handle->init_cl = NULL;
  270. //handle->reduction_refcnt = 0;
  271. _starpu_data_requester_prio_list_init0(&handle->reduction_req_list);
  272. //handle->reduction_tmp_handles = NULL;
  273. //handle->write_invalidation_req = NULL;
  274. #ifdef STARPU_USE_FXT
  275. //handle->last_submitted_ghost_sync_id_is_valid = 0;
  276. //handle->last_submitted_ghost_sync_id = 0;
  277. //handle->last_submitted_ghost_accessors_id = NULL;
  278. #endif
  279. handle->wt_mask = wt_mask;
  280. /* Store some values directly in the handle not to recompute them all
  281. * the time. */
  282. handle->footprint = _starpu_compute_data_footprint(handle);
  283. handle->home_node = home_node;
  284. if (_starpu_global_arbiter)
  285. /* Just for testing purpose */
  286. starpu_data_assign_arbiter(handle, _starpu_global_arbiter);
  287. else
  288. {
  289. //handle->arbiter = NULL;
  290. }
  291. _starpu_data_requester_prio_list_init0(&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, starpu_worker_get_local_memory_node());
  457. }
  458. struct starpu_data_interface_ops* starpu_data_get_interface_ops(starpu_data_handle_t handle)
  459. {
  460. return handle->ops;
  461. }
  462. /*
  463. * Stop monitoring a piece of data
  464. */
  465. void _starpu_data_unregister_ram_pointer(starpu_data_handle_t handle, unsigned node)
  466. {
  467. if (starpu_node_get_kind(node) != STARPU_CPU_RAM)
  468. return;
  469. #ifdef STARPU_OPENMP
  470. if (handle->removed_from_context_hash)
  471. return;
  472. #endif
  473. const void *ram_ptr = starpu_data_handle_to_pointer(handle, node);
  474. if (ram_ptr != NULL)
  475. {
  476. /* Remove the PTR -> HANDLE mapping. If a mapping from PTR
  477. * to another handle existed before (e.g., when using
  478. * filters), it becomes visible again. */
  479. struct handle_entry *entry;
  480. #ifdef STARPU_OPENMP
  481. struct starpu_omp_task *task = _starpu_omp_get_task();
  482. if (task)
  483. {
  484. if (task->flags & STARPU_OMP_TASK_FLAGS_IMPLICIT)
  485. {
  486. struct starpu_omp_region *parallel_region = task->owner_region;
  487. _starpu_spin_lock(&parallel_region->registered_handles_lock);
  488. HASH_FIND_PTR(parallel_region->registered_handles, &ram_ptr, entry);
  489. STARPU_ASSERT(entry != NULL);
  490. HASH_DEL(registered_handles, entry);
  491. _starpu_spin_unlock(&parallel_region->registered_handles_lock);
  492. }
  493. else
  494. {
  495. HASH_FIND_PTR(task->registered_handles, &ram_ptr, entry);
  496. STARPU_ASSERT(entry != NULL);
  497. HASH_DEL(task->registered_handles, entry);
  498. }
  499. }
  500. else
  501. #endif
  502. {
  503. _starpu_spin_lock(&registered_handles_lock);
  504. HASH_FIND_PTR(registered_handles, &ram_ptr, entry);
  505. if (entry)
  506. {
  507. if (entry->handle == handle)
  508. {
  509. nregistered--;
  510. HASH_DEL(registered_handles, entry);
  511. }
  512. else
  513. /* don't free it, it's not ours */
  514. entry = NULL;
  515. }
  516. _starpu_spin_unlock(&registered_handles_lock);
  517. }
  518. free(entry);
  519. }
  520. }
  521. void _starpu_data_free_interfaces(starpu_data_handle_t handle)
  522. {
  523. unsigned node;
  524. unsigned nworkers = starpu_worker_get_count();
  525. for (node = 0; node < STARPU_MAXNODES; node++)
  526. free(handle->per_node[node].data_interface);
  527. if (handle->per_worker)
  528. {
  529. unsigned worker;
  530. for (worker = 0; worker < nworkers; worker++)
  531. free(handle->per_worker[worker].data_interface);
  532. free(handle->per_worker);
  533. }
  534. }
  535. struct _starpu_unregister_callback_arg
  536. {
  537. unsigned memory_node;
  538. starpu_data_handle_t handle;
  539. unsigned terminated;
  540. starpu_pthread_mutex_t mutex;
  541. starpu_pthread_cond_t cond;
  542. };
  543. /* Check whether we should tell starpu_data_unregister that the data handle is
  544. * not busy any more.
  545. * The header is supposed to be locked.
  546. * This may free the handle, if it was lazily unregistered (1 is returned in
  547. * that case). The handle pointer thus becomes invalid for the caller.
  548. *
  549. * Note: we inline some of the tests in the _starpu_data_check_not_busy macro.
  550. */
  551. int __starpu_data_check_not_busy(starpu_data_handle_t handle)
  552. {
  553. if (STARPU_LIKELY(handle->busy_count))
  554. return 0;
  555. /* Not busy any more, perhaps have to unregister etc. */
  556. if (STARPU_UNLIKELY(handle->busy_waiting))
  557. {
  558. STARPU_PTHREAD_MUTEX_LOCK(&handle->busy_mutex);
  559. STARPU_PTHREAD_COND_BROADCAST(&handle->busy_cond);
  560. STARPU_PTHREAD_MUTEX_UNLOCK(&handle->busy_mutex);
  561. }
  562. /* The handle has been destroyed in between (eg. this was a temporary
  563. * handle created for a reduction.) */
  564. if (STARPU_UNLIKELY(handle->lazy_unregister))
  565. {
  566. handle->lazy_unregister = 0;
  567. _starpu_spin_unlock(&handle->header_lock);
  568. _starpu_data_unregister(handle, 0, 1);
  569. /* Warning: in case we unregister the handle, we must be sure
  570. * that the caller will not try to unlock the header after
  571. * !*/
  572. return 1;
  573. }
  574. return 0;
  575. }
  576. static
  577. void _starpu_check_if_valid_and_fetch_data_on_node(starpu_data_handle_t handle, struct _starpu_data_replicate *replicate, const char *origin)
  578. {
  579. unsigned node;
  580. unsigned nnodes = starpu_memory_nodes_get_count();
  581. int valid = 0;
  582. for (node = 0; node < nnodes; node++)
  583. {
  584. if (handle->per_node[node].state != STARPU_INVALID)
  585. {
  586. /* we found a copy ! */
  587. valid = 1;
  588. }
  589. }
  590. if (valid)
  591. {
  592. int ret = _starpu_fetch_data_on_node(handle, handle->home_node, replicate, STARPU_R, 0, STARPU_FETCH, 0, NULL, NULL, 0, origin);
  593. STARPU_ASSERT(!ret);
  594. _starpu_release_data_on_node(handle, handle->home_node, replicate);
  595. }
  596. else
  597. {
  598. _starpu_spin_lock(&handle->header_lock);
  599. if (!_starpu_notify_data_dependencies(handle))
  600. _starpu_spin_unlock(&handle->header_lock);
  601. }
  602. }
  603. static void _starpu_data_unregister_fetch_data_callback(void *_arg)
  604. {
  605. struct _starpu_unregister_callback_arg *arg = (struct _starpu_unregister_callback_arg *) _arg;
  606. starpu_data_handle_t handle = arg->handle;
  607. STARPU_ASSERT(handle);
  608. struct _starpu_data_replicate *replicate = &handle->per_node[arg->memory_node];
  609. _starpu_check_if_valid_and_fetch_data_on_node(handle, replicate, "_starpu_data_unregister_fetch_data_callback");
  610. /* unlock the caller */
  611. STARPU_PTHREAD_MUTEX_LOCK(&arg->mutex);
  612. arg->terminated = 1;
  613. STARPU_PTHREAD_COND_SIGNAL(&arg->cond);
  614. STARPU_PTHREAD_MUTEX_UNLOCK(&arg->mutex);
  615. }
  616. void _starpu_data_set_unregister_hook(starpu_data_handle_t handle, _starpu_data_handle_unregister_hook func)
  617. {
  618. STARPU_ASSERT(handle->unregister_hook == NULL);
  619. handle->unregister_hook = func;
  620. }
  621. /* Unregister the data handle, perhaps we don't need to update the home_node
  622. * (in that case coherent is set to 0)
  623. * nowait is for internal use when we already know for sure that we won't have to wait.
  624. */
  625. static void _starpu_data_unregister(starpu_data_handle_t handle, unsigned coherent, unsigned nowait)
  626. {
  627. STARPU_ASSERT(handle);
  628. STARPU_ASSERT_MSG(handle->nchildren == 0, "data %p needs to be unpartitioned before unregistration", handle);
  629. STARPU_ASSERT_MSG(handle->nplans == 0, "data %p needs its partition plans to be cleaned before unregistration", handle);
  630. STARPU_ASSERT_MSG(handle->partitioned == 0, "data %p needs its partitioned plans to be unpartitioned before unregistration", handle);
  631. /* TODO: also check that it has the latest coherency */
  632. STARPU_ASSERT(!(nowait && handle->busy_count != 0));
  633. int sequential_consistency = handle->sequential_consistency;
  634. if (sequential_consistency && !nowait)
  635. {
  636. 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");
  637. /* If sequential consistency is enabled, wait until data is available */
  638. _starpu_data_wait_until_available(handle, STARPU_RW, "starpu_data_unregister");
  639. }
  640. if (coherent && !nowait)
  641. {
  642. 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");
  643. /* Fetch data in the home of the data to ensure we have a valid copy
  644. * where we registered it */
  645. int home_node = handle->home_node;
  646. if (home_node >= 0)
  647. {
  648. struct _starpu_unregister_callback_arg arg = { 0 };
  649. arg.handle = handle;
  650. arg.memory_node = (unsigned)home_node;
  651. arg.terminated = 0;
  652. STARPU_PTHREAD_MUTEX_INIT0(&arg.mutex, NULL);
  653. STARPU_PTHREAD_COND_INIT0(&arg.cond, NULL);
  654. if (!_starpu_attempt_to_submit_data_request_from_apps(handle, STARPU_R,
  655. _starpu_data_unregister_fetch_data_callback, &arg))
  656. {
  657. /* no one has locked this data yet, so we proceed immediately */
  658. struct _starpu_data_replicate *home_replicate = &handle->per_node[home_node];
  659. _starpu_check_if_valid_and_fetch_data_on_node(handle, home_replicate, "_starpu_data_unregister");
  660. }
  661. else
  662. {
  663. STARPU_PTHREAD_MUTEX_LOCK(&arg.mutex);
  664. while (!arg.terminated)
  665. STARPU_PTHREAD_COND_WAIT(&arg.cond, &arg.mutex);
  666. STARPU_PTHREAD_MUTEX_UNLOCK(&arg.mutex);
  667. }
  668. STARPU_PTHREAD_MUTEX_DESTROY(&arg.mutex);
  669. STARPU_PTHREAD_COND_DESTROY(&arg.cond);
  670. }
  671. /* If this handle uses a multiformat interface, we may have to convert
  672. * this piece of data back into the CPU format.
  673. * XXX : This is quite hacky, could we submit a task instead ?
  674. */
  675. if (_starpu_data_is_multiformat_handle(handle) && (starpu_node_get_kind(handle->mf_node) != STARPU_CPU_RAM))
  676. {
  677. _STARPU_DEBUG("Conversion needed\n");
  678. void *buffers[1];
  679. struct starpu_multiformat_interface *format_interface;
  680. home_node = handle->home_node;
  681. if (home_node < 0 || (starpu_node_get_kind(home_node) != STARPU_CPU_RAM))
  682. home_node = STARPU_MAIN_RAM;
  683. format_interface = (struct starpu_multiformat_interface *) starpu_data_get_interface_on_node(handle, home_node);
  684. struct starpu_codelet *cl = NULL;
  685. enum starpu_node_kind node_kind = starpu_node_get_kind(handle->mf_node);
  686. switch (node_kind)
  687. {
  688. #ifdef STARPU_USE_CUDA
  689. case STARPU_CUDA_RAM:
  690. {
  691. struct starpu_multiformat_data_interface_ops *mf_ops;
  692. mf_ops = (struct starpu_multiformat_data_interface_ops *) handle->ops->get_mf_ops(format_interface);
  693. cl = mf_ops->cuda_to_cpu_cl;
  694. break;
  695. }
  696. #endif
  697. #ifdef STARPU_USE_OPENCL
  698. case STARPU_OPENCL_RAM:
  699. {
  700. struct starpu_multiformat_data_interface_ops *mf_ops;
  701. mf_ops = (struct starpu_multiformat_data_interface_ops *) handle->ops->get_mf_ops(format_interface);
  702. cl = mf_ops->opencl_to_cpu_cl;
  703. break;
  704. }
  705. #endif
  706. #ifdef STARPU_USE_MIC
  707. case STARPU_MIC_RAM:
  708. {
  709. struct starpu_multiformat_data_interface_ops *mf_ops;
  710. mf_ops = (struct starpu_multiformat_data_interface_ops *) handle->ops->get_mf_ops(format_interface);
  711. cl = mf_ops->mic_to_cpu_cl;
  712. break;
  713. }
  714. #endif
  715. case STARPU_CPU_RAM: /* Impossible ! */
  716. default:
  717. STARPU_ABORT();
  718. }
  719. buffers[0] = format_interface;
  720. _starpu_cl_func_t func = _starpu_task_get_cpu_nth_implementation(cl, 0);
  721. STARPU_ASSERT(func);
  722. func(buffers, NULL);
  723. }
  724. }
  725. /* Prevent any further unregistration */
  726. handle->magic = 0;
  727. _starpu_spin_lock(&handle->header_lock);
  728. if (!coherent)
  729. {
  730. /* Should we postpone the unregister operation ? */
  731. if ((handle->busy_count > 0) && handle->lazy_unregister)
  732. {
  733. _starpu_spin_unlock(&handle->header_lock);
  734. return;
  735. }
  736. }
  737. /* Tell holders of references that we're starting waiting */
  738. handle->busy_waiting = 1;
  739. _starpu_spin_unlock(&handle->header_lock);
  740. retry_busy:
  741. /* Wait for all requests to finish (notably WT requests) */
  742. STARPU_PTHREAD_MUTEX_LOCK(&handle->busy_mutex);
  743. while (1)
  744. {
  745. /* Here helgrind would shout that this an unprotected access,
  746. * but this is actually fine: all threads who do busy_count--
  747. * are supposed to call _starpu_data_check_not_busy, which will
  748. * wake us up through the busy_mutex/busy_cond. */
  749. if (!handle->busy_count)
  750. break;
  751. /* This is woken by _starpu_data_check_not_busy, always called
  752. * after decrementing busy_count */
  753. STARPU_PTHREAD_COND_WAIT(&handle->busy_cond, &handle->busy_mutex);
  754. }
  755. STARPU_PTHREAD_MUTEX_UNLOCK(&handle->busy_mutex);
  756. /* Unregister MPI things after having waiting for MPI reqs etc. to settle down */
  757. if (handle->unregister_hook)
  758. {
  759. handle->unregister_hook(handle);
  760. handle->unregister_hook = NULL;
  761. }
  762. /* Wait for finished requests to release the handle */
  763. _starpu_spin_lock(&handle->header_lock);
  764. if (handle->busy_count)
  765. {
  766. /* Bad luck: some request went in in between, wait again... */
  767. _starpu_spin_unlock(&handle->header_lock);
  768. goto retry_busy;
  769. }
  770. size_t size = _starpu_data_get_alloc_size(handle);
  771. /* Destroy the data now */
  772. unsigned node;
  773. for (node = 0; node < STARPU_MAXNODES; node++)
  774. {
  775. struct _starpu_data_replicate *local = &handle->per_node[node];
  776. if (local->allocated)
  777. {
  778. _starpu_data_unregister_ram_pointer(handle, node);
  779. /* free the data copy in a lazy fashion */
  780. if (local->automatically_allocated)
  781. _starpu_request_mem_chunk_removal(handle, local, node, size);
  782. }
  783. }
  784. if (handle->per_worker)
  785. {
  786. unsigned worker;
  787. unsigned nworkers = starpu_worker_get_count();
  788. for (worker = 0; worker < nworkers; worker++)
  789. {
  790. struct _starpu_data_replicate *local = &handle->per_worker[worker];
  791. /* free the data copy in a lazy fashion */
  792. if (local->allocated && local->automatically_allocated)
  793. _starpu_request_mem_chunk_removal(handle, local, starpu_worker_get_memory_node(worker), size);
  794. }
  795. }
  796. _starpu_data_free_interfaces(handle);
  797. _starpu_memory_stats_free(handle);
  798. _starpu_spin_unlock(&handle->header_lock);
  799. _starpu_spin_destroy(&handle->header_lock);
  800. _starpu_data_clear_implicit(handle);
  801. free(handle->active_readonly_children);
  802. STARPU_PTHREAD_MUTEX_DESTROY(&handle->busy_mutex);
  803. STARPU_PTHREAD_COND_DESTROY(&handle->busy_cond);
  804. STARPU_PTHREAD_MUTEX_DESTROY(&handle->sequential_consistency_mutex);
  805. STARPU_HG_ENABLE_CHECKING(handle->post_sync_tasks_cnt);
  806. STARPU_HG_ENABLE_CHECKING(handle->busy_count);
  807. if (handle->switch_cl)
  808. {
  809. free(handle->switch_cl->dyn_nodes);
  810. free(handle->switch_cl);
  811. }
  812. _STARPU_TRACE_HANDLE_DATA_UNREGISTER(handle);
  813. free(handle);
  814. }
  815. void starpu_data_unregister(starpu_data_handle_t handle)
  816. {
  817. STARPU_ASSERT_MSG(handle->magic == 42, "data %p is invalid (was it already registered?)", handle);
  818. STARPU_ASSERT_MSG(!handle->lazy_unregister, "data %p can not be unregistered twice", handle);
  819. _starpu_data_unregister(handle, 1, 0);
  820. }
  821. void starpu_data_unregister_no_coherency(starpu_data_handle_t handle)
  822. {
  823. STARPU_ASSERT_MSG(handle->magic == 42, "data %p is invalid (was it already registered?)", handle);
  824. _starpu_data_unregister(handle, 0, 0);
  825. }
  826. static void _starpu_data_unregister_submit_cb(void *arg)
  827. {
  828. starpu_data_handle_t handle = arg;
  829. _starpu_spin_lock(&handle->header_lock);
  830. handle->lazy_unregister = 1;
  831. /* The handle should be busy since we are working on it.
  832. * when we releases the handle below, it will be destroyed by
  833. * _starpu_data_check_not_busy */
  834. STARPU_ASSERT(handle->busy_count);
  835. _starpu_spin_unlock(&handle->header_lock);
  836. starpu_data_release_on_node(handle, STARPU_ACQUIRE_NO_NODE_LOCK_ALL);
  837. }
  838. void starpu_data_unregister_submit(starpu_data_handle_t handle)
  839. {
  840. STARPU_ASSERT_MSG(handle->magic == 42, "data %p is invalid (was it already registered?)", handle);
  841. STARPU_ASSERT_MSG(!handle->lazy_unregister, "data %p can not be unregistered twice", handle);
  842. /* Wait for all task dependencies on this handle before putting it for free */
  843. 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);
  844. }
  845. static void _starpu_data_invalidate(void *data)
  846. {
  847. starpu_data_handle_t handle = data;
  848. size_t size = _starpu_data_get_alloc_size(handle);
  849. _starpu_spin_lock(&handle->header_lock);
  850. //_STARPU_DEBUG("Really invalidating data %p\n", data);
  851. #ifdef STARPU_DEBUG
  852. {
  853. /* There shouldn't be any pending request since we acquired the data in W mode */
  854. unsigned i, j, nnodes = starpu_memory_nodes_get_count();
  855. for (i = 0; i < nnodes; i++)
  856. for (j = 0; j < nnodes; j++)
  857. STARPU_ASSERT_MSG(!handle->per_node[i].request[j], "request for handle %p pending from %u to %u while invalidating data!", handle, j, i);
  858. }
  859. #endif
  860. unsigned node;
  861. for (node = 0; node < STARPU_MAXNODES; node++)
  862. {
  863. struct _starpu_data_replicate *local = &handle->per_node[node];
  864. if (local->mc && local->allocated && local->automatically_allocated)
  865. {
  866. _starpu_data_unregister_ram_pointer(handle, node);
  867. /* free the data copy in a lazy fashion */
  868. _starpu_request_mem_chunk_removal(handle, local, node, size);
  869. }
  870. if (local->state != STARPU_INVALID)
  871. _STARPU_TRACE_DATA_STATE_INVALID(handle, node);
  872. local->state = STARPU_INVALID;
  873. }
  874. if (handle->per_worker)
  875. {
  876. unsigned worker;
  877. unsigned nworkers = starpu_worker_get_count();
  878. for (worker = 0; worker < nworkers; worker++)
  879. {
  880. struct _starpu_data_replicate *local = &handle->per_worker[worker];
  881. if (local->mc && local->allocated && local->automatically_allocated)
  882. /* free the data copy in a lazy fashion */
  883. _starpu_request_mem_chunk_removal(handle, local, starpu_worker_get_memory_node(worker), size);
  884. local->state = STARPU_INVALID;
  885. }
  886. }
  887. _starpu_spin_unlock(&handle->header_lock);
  888. starpu_data_release_on_node(handle, STARPU_ACQUIRE_NO_NODE_LOCK_ALL);
  889. }
  890. void starpu_data_invalidate(starpu_data_handle_t handle)
  891. {
  892. STARPU_ASSERT(handle);
  893. starpu_data_acquire_on_node(handle, STARPU_ACQUIRE_NO_NODE_LOCK_ALL, STARPU_W);
  894. _starpu_data_invalidate(handle);
  895. handle->initialized = 0;
  896. }
  897. void starpu_data_invalidate_submit(starpu_data_handle_t handle)
  898. {
  899. STARPU_ASSERT(handle);
  900. starpu_data_acquire_on_node_cb(handle, STARPU_ACQUIRE_NO_NODE_LOCK_ALL, STARPU_W, _starpu_data_invalidate, handle);
  901. handle->initialized = 0;
  902. }
  903. enum starpu_data_interface_id starpu_data_get_interface_id(starpu_data_handle_t handle)
  904. {
  905. return handle->ops->interfaceid;
  906. }
  907. void *starpu_data_get_interface_on_node(starpu_data_handle_t handle, unsigned memory_node)
  908. {
  909. return handle->per_node[memory_node].data_interface;
  910. }
  911. int starpu_data_interface_get_next_id(void)
  912. {
  913. _data_interface_number += 1;
  914. return _data_interface_number-1;
  915. }
  916. int starpu_data_pack(starpu_data_handle_t handle, void **ptr, starpu_ssize_t *count)
  917. {
  918. STARPU_ASSERT_MSG(handle->ops->pack_data, "The datatype interface %s (%d) does not have a pack operation", handle->ops->name, handle->ops->interfaceid);
  919. return handle->ops->pack_data(handle, starpu_worker_get_local_memory_node(), ptr, count);
  920. }
  921. int starpu_data_unpack(starpu_data_handle_t handle, void *ptr, size_t count)
  922. {
  923. STARPU_ASSERT_MSG(handle->ops->unpack_data, "The datatype interface %s (%d) does not have an unpack operation", handle->ops->name, handle->ops->interfaceid);
  924. int ret;
  925. ret = handle->ops->unpack_data(handle, starpu_worker_get_local_memory_node(), ptr, count);
  926. return ret;
  927. }
  928. size_t starpu_data_get_size(starpu_data_handle_t handle)
  929. {
  930. return handle->ops->get_size(handle);
  931. }
  932. size_t starpu_data_get_alloc_size(starpu_data_handle_t handle)
  933. {
  934. if (handle->ops->get_alloc_size)
  935. return handle->ops->get_alloc_size(handle);
  936. else
  937. return handle->ops->get_size(handle);
  938. }
  939. void starpu_data_set_name(starpu_data_handle_t handle STARPU_ATTRIBUTE_UNUSED, const char *name STARPU_ATTRIBUTE_UNUSED)
  940. {
  941. _STARPU_TRACE_DATA_NAME(handle, name);
  942. }
  943. int starpu_data_get_home_node(starpu_data_handle_t handle)
  944. {
  945. return handle->home_node;
  946. }
  947. void starpu_data_set_coordinates_array(starpu_data_handle_t handle, unsigned dimensions, int dims[])
  948. {
  949. unsigned i;
  950. unsigned max_dimensions = sizeof(handle->coordinates)/sizeof(handle->coordinates[0]);
  951. if (dimensions > max_dimensions)
  952. dimensions = max_dimensions;
  953. handle->dimensions = dimensions;
  954. for (i = 0; i < dimensions; i++)
  955. handle->coordinates[i] = dims[i];
  956. _STARPU_TRACE_DATA_COORDINATES(handle, dimensions, dims);
  957. }
  958. void starpu_data_set_coordinates(starpu_data_handle_t handle, unsigned dimensions, ...)
  959. {
  960. int dims[dimensions];
  961. unsigned i;
  962. va_list varg_list;
  963. va_start(varg_list, dimensions);
  964. for (i = 0; i < dimensions; i++)
  965. dims[i] = va_arg(varg_list, int);
  966. va_end(varg_list);
  967. starpu_data_set_coordinates_array(handle, dimensions, dims);
  968. }
  969. unsigned starpu_data_get_coordinates_array(starpu_data_handle_t handle, unsigned dimensions, int dims[])
  970. {
  971. unsigned i;
  972. if (dimensions > handle->dimensions)
  973. dimensions = handle->dimensions;
  974. for (i = 0; i < dimensions; i++)
  975. dims[i] = handle->coordinates[i];
  976. return dimensions;
  977. }