data_interface.c 30 KB

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