data_interface.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-2013 Université de Bordeaux 1
  4. * Copyright (C) 2010, 2011, 2012, 2013 Centre National de la Recherche Scientifique
  5. *
  6. * StarPU is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation; either version 2.1 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * StarPU is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. *
  15. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  16. */
  17. #include <stdint.h>
  18. #include <datawizard/datawizard.h>
  19. #include <core/dependencies/data_concurrency.h>
  20. #include <common/uthash.h>
  21. #include <common/starpu_spinlock.h>
  22. #include <core/task.h>
  23. #include <core/workers.h>
  24. #include <datawizard/memstats.h>
  25. /* Entry in the `registered_handles' hash table. */
  26. struct handle_entry
  27. {
  28. UT_hash_handle hh;
  29. void *pointer;
  30. starpu_data_handle_t handle;
  31. };
  32. /* Hash table mapping host pointers to data handles. */
  33. static struct handle_entry *registered_handles;
  34. static struct _starpu_spinlock registered_handles_lock;
  35. static int _data_interface_number = STARPU_MAX_INTERFACE_ID;
  36. /* Entry in the `registered_tag_handles' hash table. */
  37. struct handle_tag_entry
  38. {
  39. UT_hash_handle hh;
  40. int tag;
  41. starpu_data_handle_t handle;
  42. };
  43. /* Hash table mapping host tags to data handles. */
  44. static struct handle_tag_entry *registered_tag_handles;
  45. static struct _starpu_spinlock registered_tag_handles_lock;
  46. void _starpu_data_interface_init(void)
  47. {
  48. _starpu_spin_init(&registered_handles_lock);
  49. _starpu_spin_init(&registered_tag_handles_lock);
  50. }
  51. void _starpu_data_interface_shutdown()
  52. {
  53. struct handle_entry *entry, *tmp;
  54. _starpu_spin_destroy(&registered_handles_lock);
  55. HASH_ITER(hh, registered_handles, entry, tmp)
  56. {
  57. HASH_DEL(registered_handles, entry);
  58. free(entry);
  59. }
  60. registered_handles = NULL;
  61. struct handle_tag_entry *tag_entry, *tag_tmp;
  62. _starpu_spin_destroy(&registered_tag_handles_lock);
  63. HASH_ITER(hh, registered_tag_handles, tag_entry, tag_tmp)
  64. {
  65. HASH_DEL(registered_tag_handles, tag_entry);
  66. free(tag_entry);
  67. }
  68. registered_tag_handles = NULL;
  69. }
  70. /* Register the mapping from PTR to HANDLE. If PTR is already mapped to
  71. * some handle, the new mapping shadows the previous one. */
  72. void _starpu_data_register_ram_pointer(starpu_data_handle_t handle, void *ptr)
  73. {
  74. struct handle_entry *entry;
  75. entry = (struct handle_entry *) malloc(sizeof(*entry));
  76. STARPU_ASSERT(entry != NULL);
  77. entry->pointer = ptr;
  78. entry->handle = handle;
  79. _starpu_spin_lock(&registered_handles_lock);
  80. HASH_ADD_PTR(registered_handles, pointer, entry);
  81. _starpu_spin_unlock(&registered_handles_lock);
  82. }
  83. starpu_data_handle_t starpu_data_lookup(const void *ptr)
  84. {
  85. starpu_data_handle_t result;
  86. _starpu_spin_lock(&registered_handles_lock);
  87. {
  88. struct handle_entry *entry;
  89. HASH_FIND_PTR(registered_handles, &ptr, entry);
  90. if(STARPU_UNLIKELY(entry == NULL))
  91. result = NULL;
  92. else
  93. result = entry->handle;
  94. }
  95. _starpu_spin_unlock(&registered_handles_lock);
  96. return result;
  97. }
  98. int
  99. _starpu_data_is_multiformat_handle(starpu_data_handle_t handle)
  100. {
  101. return handle->ops->is_multiformat;
  102. }
  103. /*
  104. * Start monitoring a piece of data
  105. */
  106. static void _starpu_register_new_data(starpu_data_handle_t handle,
  107. unsigned home_node, uint32_t wt_mask)
  108. {
  109. void *ptr;
  110. STARPU_ASSERT(handle);
  111. /* initialize the new lock */
  112. handle->req_list = _starpu_data_requester_list_new();
  113. handle->refcnt = 0;
  114. handle->busy_count = 0;
  115. handle->busy_waiting = 0;
  116. STARPU_PTHREAD_MUTEX_INIT(&handle->busy_mutex, NULL);
  117. STARPU_PTHREAD_COND_INIT(&handle->busy_cond, NULL);
  118. _starpu_spin_init(&handle->header_lock);
  119. /* first take care to properly lock the data */
  120. _starpu_spin_lock(&handle->header_lock);
  121. /* there is no hierarchy yet */
  122. handle->nchildren = 0;
  123. handle->root_handle = handle;
  124. handle->father_handle = NULL;
  125. handle->sibling_index = 0; /* could be anything for the root */
  126. handle->depth = 1; /* the tree is just a node yet */
  127. handle->rank = -1; /* invalid until set */
  128. handle->tag = -1; /* invalid until set */
  129. handle->is_not_important = 0;
  130. handle->sequential_consistency =
  131. starpu_data_get_default_sequential_consistency_flag();
  132. STARPU_PTHREAD_MUTEX_INIT(&handle->sequential_consistency_mutex, NULL);
  133. handle->last_submitted_mode = STARPU_R;
  134. handle->last_submitted_writer = NULL;
  135. handle->last_submitted_readers = NULL;
  136. handle->post_sync_tasks = NULL;
  137. handle->post_sync_tasks_cnt = 0;
  138. /* By default, there are no methods available to perform a reduction */
  139. handle->redux_cl = NULL;
  140. handle->init_cl = NULL;
  141. handle->reduction_refcnt = 0;
  142. handle->reduction_req_list = _starpu_data_requester_list_new();
  143. #ifdef STARPU_USE_FXT
  144. handle->last_submitted_ghost_writer_id_is_valid = 0;
  145. handle->last_submitted_ghost_writer_id = 0;
  146. handle->last_submitted_ghost_readers_id = NULL;
  147. #endif
  148. handle->wt_mask = wt_mask;
  149. /* Store some values directly in the handle not to recompute them all
  150. * the time. */
  151. handle->footprint = _starpu_compute_data_footprint(handle);
  152. handle->home_node = home_node;
  153. /* that new data is invalid from all nodes perpective except for the
  154. * home node */
  155. unsigned node;
  156. for (node = 0; node < STARPU_MAXNODES; node++)
  157. {
  158. struct _starpu_data_replicate *replicate;
  159. replicate = &handle->per_node[node];
  160. replicate->memory_node = node;
  161. replicate->relaxed_coherency = 0;
  162. replicate->refcnt = 0;
  163. if (node == home_node)
  164. {
  165. /* this is the home node with the only valid copy */
  166. replicate->state = STARPU_OWNER;
  167. replicate->allocated = 1;
  168. replicate->automatically_allocated = 0;
  169. }
  170. else
  171. {
  172. /* the value is not available here yet */
  173. replicate->state = STARPU_INVALID;
  174. replicate->allocated = 0;
  175. }
  176. }
  177. unsigned worker;
  178. unsigned nworkers = starpu_worker_get_count();
  179. for (worker = 0; worker < nworkers; worker++)
  180. {
  181. struct _starpu_data_replicate *replicate;
  182. replicate = &handle->per_worker[worker];
  183. replicate->allocated = 0;
  184. replicate->automatically_allocated = 0;
  185. replicate->state = STARPU_INVALID;
  186. replicate->refcnt = 0;
  187. replicate->handle = handle;
  188. for (node = 0; node < STARPU_MAXNODES; node++)
  189. {
  190. replicate->requested[node] = 0;
  191. replicate->request[node] = NULL;
  192. }
  193. /* Assuming being used for SCRATCH for now, patched when entering REDUX mode */
  194. replicate->relaxed_coherency = 1;
  195. replicate->initialized = 0;
  196. replicate->memory_node = starpu_worker_get_memory_node(worker);
  197. /* duplicate the content of the interface on node 0 */
  198. memcpy(replicate->data_interface, handle->per_node[0].data_interface, handle->ops->interface_size);
  199. }
  200. /* now the data is available ! */
  201. _starpu_spin_unlock(&handle->header_lock);
  202. ptr = starpu_data_handle_to_pointer(handle, 0);
  203. if (ptr != NULL)
  204. {
  205. _starpu_data_register_ram_pointer(handle, ptr);
  206. }
  207. }
  208. int _starpu_data_handle_init(starpu_data_handle_t handle, struct starpu_data_interface_ops *interface_ops, unsigned int mf_node)
  209. {
  210. unsigned node;
  211. unsigned worker;
  212. handle->ops = interface_ops;
  213. handle->mf_node = mf_node;
  214. size_t interfacesize = interface_ops->interface_size;
  215. _starpu_memory_stats_init(handle);
  216. for (node = 0; node < STARPU_MAXNODES; node++)
  217. {
  218. _starpu_memory_stats_init_per_node(handle, node);
  219. struct _starpu_data_replicate *replicate;
  220. replicate = &handle->per_node[node];
  221. /* relaxed_coherency = 0 */
  222. replicate->handle = handle;
  223. replicate->data_interface = calloc(1, interfacesize);
  224. STARPU_ASSERT(replicate->data_interface);
  225. }
  226. unsigned nworkers = starpu_worker_get_count();
  227. for (worker = 0; worker < nworkers; worker++)
  228. {
  229. struct _starpu_data_replicate *replicate;
  230. replicate = &handle->per_worker[worker];
  231. replicate->handle = handle;
  232. replicate->data_interface = calloc(1, interfacesize);
  233. STARPU_ASSERT(replicate->data_interface);
  234. }
  235. handle->tag = -1;
  236. handle->rank = -1;
  237. return 0;
  238. }
  239. static
  240. starpu_data_handle_t _starpu_data_handle_allocate(struct starpu_data_interface_ops *interface_ops, unsigned int mf_node)
  241. {
  242. starpu_data_handle_t handle = (starpu_data_handle_t) calloc(1, sizeof(struct _starpu_data_state));
  243. STARPU_ASSERT(handle);
  244. _starpu_data_handle_init(handle, interface_ops, mf_node);
  245. return handle;
  246. }
  247. void starpu_data_register(starpu_data_handle_t *handleptr, unsigned home_node,
  248. void *data_interface,
  249. struct starpu_data_interface_ops *ops)
  250. {
  251. starpu_data_handle_t handle = _starpu_data_handle_allocate(ops, home_node);
  252. STARPU_ASSERT(handleptr);
  253. *handleptr = handle;
  254. /* fill the interface fields with the appropriate method */
  255. STARPU_ASSERT(ops->register_data_handle);
  256. ops->register_data_handle(handle, home_node, data_interface);
  257. _starpu_register_new_data(handle, home_node, 0);
  258. }
  259. void starpu_data_register_same(starpu_data_handle_t *handledst, starpu_data_handle_t handlesrc)
  260. {
  261. void *local_interface = starpu_data_get_interface_on_node(handlesrc, 0);
  262. starpu_data_register(handledst, -1, local_interface, handlesrc->ops);
  263. }
  264. void *starpu_data_handle_to_pointer(starpu_data_handle_t handle, unsigned node)
  265. {
  266. /* Check whether the operation is supported and the node has actually
  267. * been allocated. */
  268. if (handle->ops->handle_to_pointer
  269. && starpu_data_test_if_allocated_on_node(handle, node))
  270. {
  271. return handle->ops->handle_to_pointer(handle, node);
  272. }
  273. return NULL;
  274. }
  275. void *starpu_data_get_local_ptr(starpu_data_handle_t handle)
  276. {
  277. return starpu_data_handle_to_pointer(handle,
  278. _starpu_memory_node_get_local_key());
  279. }
  280. int starpu_data_get_rank(starpu_data_handle_t handle)
  281. {
  282. return handle->rank;
  283. }
  284. int starpu_data_set_rank(starpu_data_handle_t handle, int rank)
  285. {
  286. handle->rank = rank;
  287. return 0;
  288. }
  289. int starpu_data_get_tag(starpu_data_handle_t handle)
  290. {
  291. return handle->tag;
  292. }
  293. starpu_data_handle_t starpu_data_get_data_handle_from_tag(int tag)
  294. {
  295. struct handle_tag_entry *ret;
  296. _starpu_spin_lock(&registered_tag_handles_lock);
  297. HASH_FIND_INT(registered_tag_handles, &tag, ret);
  298. _starpu_spin_unlock(&registered_tag_handles_lock);
  299. if (ret)
  300. {
  301. return ret->handle;
  302. }
  303. else
  304. {
  305. return NULL;
  306. }
  307. }
  308. int starpu_data_set_tag(starpu_data_handle_t handle, int tag)
  309. {
  310. struct handle_tag_entry *entry;
  311. entry = (struct handle_tag_entry *) malloc(sizeof(*entry));
  312. STARPU_ASSERT(entry != NULL);
  313. STARPU_ASSERT_MSG(!(starpu_data_get_data_handle_from_tag(tag)),"A data handle with tag %d had already been registered.\n",tag);
  314. entry->tag = tag;
  315. entry->handle = handle;
  316. _starpu_spin_lock(&registered_tag_handles_lock);
  317. HASH_ADD_INT(registered_tag_handles, tag, entry);
  318. _starpu_spin_unlock(&registered_tag_handles_lock);
  319. handle->tag = tag;
  320. return 0;
  321. }
  322. int starpu_data_release_tag(starpu_data_handle_t handle)
  323. {
  324. struct handle_tag_entry *tag_entry;
  325. if (handle->tag != -1)
  326. {
  327. _starpu_spin_lock(&registered_tag_handles_lock);
  328. HASH_FIND_INT(registered_tag_handles, &handle->tag, tag_entry);
  329. STARPU_ASSERT_MSG((tag_entry != NULL),"Handle %p with tag %d isn't in the hashmap !",handle,handle->tag);
  330. HASH_DEL(registered_tag_handles, tag_entry);
  331. free(tag_entry);
  332. _starpu_spin_unlock(&registered_tag_handles_lock);
  333. }
  334. return 0;
  335. }
  336. struct starpu_data_interface_ops* starpu_data_get_interface_ops(starpu_data_handle_t handle)
  337. {
  338. return handle->ops;
  339. }
  340. /*
  341. * Stop monitoring a piece of data
  342. */
  343. void _starpu_data_free_interfaces(starpu_data_handle_t handle)
  344. {
  345. const void *ram_ptr;
  346. unsigned node;
  347. unsigned worker;
  348. unsigned nworkers = starpu_worker_get_count();
  349. ram_ptr = starpu_data_handle_to_pointer(handle, 0);
  350. for (node = 0; node < STARPU_MAXNODES; node++)
  351. free(handle->per_node[node].data_interface);
  352. for (worker = 0; worker < nworkers; worker++)
  353. free(handle->per_worker[worker].data_interface);
  354. if (ram_ptr != NULL)
  355. {
  356. /* Remove the PTR -> HANDLE mapping. If a mapping from PTR
  357. * to another handle existed before (e.g., when using
  358. * filters), it becomes visible again. */
  359. struct handle_entry *entry;
  360. _starpu_spin_lock(&registered_handles_lock);
  361. HASH_FIND_PTR(registered_handles, &ram_ptr, entry);
  362. STARPU_ASSERT(entry != NULL);
  363. HASH_DEL(registered_handles, entry);
  364. free(entry);
  365. _starpu_spin_unlock(&registered_handles_lock);
  366. }
  367. }
  368. struct _starpu_unregister_callback_arg
  369. {
  370. unsigned memory_node;
  371. starpu_data_handle_t handle;
  372. unsigned terminated;
  373. starpu_pthread_mutex_t mutex;
  374. starpu_pthread_cond_t cond;
  375. };
  376. /* Check whether we should tell starpu_data_unregister that the data handle is
  377. * not busy any more.
  378. * The header is supposed to be locked.
  379. * This may free the handle, if it was lazily unregistered (1 is returned in
  380. * that case). The handle pointer thus becomes invalid for the caller.
  381. */
  382. int _starpu_data_check_not_busy(starpu_data_handle_t handle)
  383. {
  384. if (!handle->busy_count && handle->busy_waiting)
  385. {
  386. STARPU_PTHREAD_MUTEX_LOCK(&handle->busy_mutex);
  387. STARPU_PTHREAD_COND_BROADCAST(&handle->busy_cond);
  388. STARPU_PTHREAD_MUTEX_UNLOCK(&handle->busy_mutex);
  389. }
  390. /* The handle has been destroyed in between (eg. this was a temporary
  391. * handle created for a reduction.) */
  392. if (handle->lazy_unregister && handle->busy_count == 0)
  393. {
  394. _starpu_spin_unlock(&handle->header_lock);
  395. starpu_data_unregister_no_coherency(handle);
  396. /* Warning: in case we unregister the handle, we must be sure
  397. * that the caller will not try to unlock the header after
  398. * !*/
  399. return 1;
  400. }
  401. return 0;
  402. }
  403. static void _starpu_data_unregister_fetch_data_callback(void *_arg)
  404. {
  405. int ret;
  406. struct _starpu_unregister_callback_arg *arg = (struct _starpu_unregister_callback_arg *) _arg;
  407. starpu_data_handle_t handle = arg->handle;
  408. STARPU_ASSERT(handle);
  409. struct _starpu_data_replicate *replicate = &handle->per_node[arg->memory_node];
  410. ret = _starpu_fetch_data_on_node(handle, replicate, STARPU_R, 0, 0, NULL, NULL);
  411. STARPU_ASSERT(!ret);
  412. /* unlock the caller */
  413. STARPU_PTHREAD_MUTEX_LOCK(&arg->mutex);
  414. arg->terminated = 1;
  415. STARPU_PTHREAD_COND_SIGNAL(&arg->cond);
  416. STARPU_PTHREAD_MUTEX_UNLOCK(&arg->mutex);
  417. }
  418. /* Unregister the data handle, perhaps we don't need to update the home_node
  419. * (in that case coherent is set to 0) */
  420. static void _starpu_data_unregister(starpu_data_handle_t handle, unsigned coherent)
  421. {
  422. STARPU_ASSERT(handle);
  423. STARPU_ASSERT_MSG(handle->nchildren == 0, "data needs to be unpartitioned before unregistration");
  424. if (coherent)
  425. {
  426. /* If sequential consistency is enabled, wait until data is available */
  427. _starpu_data_wait_until_available(handle, STARPU_RW);
  428. /* Fetch data in the home of the data to ensure we have a valid copy
  429. * where we registered it */
  430. int home_node = handle->home_node;
  431. if (home_node >= 0)
  432. {
  433. struct _starpu_unregister_callback_arg arg;
  434. arg.handle = handle;
  435. arg.memory_node = (unsigned)home_node;
  436. arg.terminated = 0;
  437. STARPU_PTHREAD_MUTEX_INIT(&arg.mutex, NULL);
  438. STARPU_PTHREAD_COND_INIT(&arg.cond, NULL);
  439. if (!_starpu_attempt_to_submit_data_request_from_apps(handle, STARPU_R,
  440. _starpu_data_unregister_fetch_data_callback, &arg))
  441. {
  442. /* no one has locked this data yet, so we proceed immediately */
  443. struct _starpu_data_replicate *home_replicate = &handle->per_node[home_node];
  444. int ret = _starpu_fetch_data_on_node(handle, home_replicate, STARPU_R, 0, 0, NULL, NULL);
  445. STARPU_ASSERT(!ret);
  446. }
  447. else
  448. {
  449. STARPU_PTHREAD_MUTEX_LOCK(&arg.mutex);
  450. while (!arg.terminated)
  451. STARPU_PTHREAD_COND_WAIT(&arg.cond, &arg.mutex);
  452. STARPU_PTHREAD_MUTEX_UNLOCK(&arg.mutex);
  453. }
  454. STARPU_PTHREAD_MUTEX_DESTROY(&arg.mutex);
  455. STARPU_PTHREAD_COND_DESTROY(&arg.cond);
  456. _starpu_release_data_on_node(handle, 0, &handle->per_node[home_node]);
  457. }
  458. /* If this handle uses a multiformat interface, we may have to convert
  459. * this piece of data back into the CPU format.
  460. * XXX : This is quite hacky, could we submit a task instead ?
  461. */
  462. if (_starpu_data_is_multiformat_handle(handle) &&
  463. starpu_node_get_kind(handle->mf_node) != STARPU_CPU_RAM)
  464. {
  465. _STARPU_DEBUG("Conversion needed\n");
  466. void *buffers[1];
  467. struct starpu_multiformat_interface *format_interface;
  468. format_interface = (struct starpu_multiformat_interface *) starpu_data_get_interface_on_node(handle, 0);
  469. struct starpu_codelet *cl = NULL;
  470. enum starpu_node_kind node_kind = starpu_node_get_kind(handle->mf_node);
  471. switch (node_kind)
  472. {
  473. #ifdef STARPU_USE_CUDA
  474. case STARPU_CUDA_RAM:
  475. {
  476. struct starpu_multiformat_data_interface_ops *mf_ops;
  477. mf_ops = (struct starpu_multiformat_data_interface_ops *) handle->ops->get_mf_ops(format_interface);
  478. cl = mf_ops->cuda_to_cpu_cl;
  479. break;
  480. }
  481. #endif
  482. #ifdef STARPU_USE_OPENCL
  483. case STARPU_OPENCL_RAM:
  484. {
  485. struct starpu_multiformat_data_interface_ops *mf_ops;
  486. mf_ops = (struct starpu_multiformat_data_interface_ops *) handle->ops->get_mf_ops(format_interface);
  487. cl = mf_ops->opencl_to_cpu_cl;
  488. break;
  489. }
  490. #endif
  491. case STARPU_CPU_RAM: /* Impossible ! */
  492. default:
  493. STARPU_ABORT();
  494. }
  495. buffers[0] = format_interface;
  496. _starpu_cl_func_t func = _starpu_task_get_cpu_nth_implementation(cl, 0);
  497. STARPU_ASSERT(func);
  498. func(buffers, NULL);
  499. }
  500. }
  501. _starpu_spin_lock(&handle->header_lock);
  502. if (!coherent)
  503. {
  504. /* Should we postpone the unregister operation ? */
  505. if ((handle->busy_count > 0) && handle->lazy_unregister)
  506. {
  507. _starpu_spin_unlock(&handle->header_lock);
  508. return;
  509. }
  510. }
  511. /* Tell holders of references that we're starting waiting */
  512. handle->busy_waiting = 1;
  513. _starpu_spin_unlock(&handle->header_lock);
  514. /* Wait for all requests to finish (notably WT requests) */
  515. STARPU_PTHREAD_MUTEX_LOCK(&handle->busy_mutex);
  516. while (1) {
  517. int busy;
  518. /* Note: we here tell valgrind that reading busy_count is as
  519. * safe is if we had the lock held */
  520. _STARPU_VALGRIND_HG_SPIN_LOCK_PRE(&handle->header_lock);
  521. _STARPU_VALGRIND_HG_SPIN_LOCK_POST(&handle->header_lock);
  522. busy = handle->busy_count;
  523. _STARPU_VALGRIND_HG_SPIN_UNLOCK_PRE(&handle->header_lock);
  524. _STARPU_VALGRIND_HG_SPIN_UNLOCK_POST(&handle->header_lock);
  525. if (!busy)
  526. break;
  527. /* This is woken by _starpu_data_check_not_busy, always called
  528. * after decrementing busy_count */
  529. STARPU_PTHREAD_COND_WAIT(&handle->busy_cond, &handle->busy_mutex);
  530. }
  531. STARPU_PTHREAD_MUTEX_UNLOCK(&handle->busy_mutex);
  532. /* Wait for finished requests to release the handle */
  533. _starpu_spin_lock(&handle->header_lock);
  534. size_t size = _starpu_data_get_size(handle);
  535. _starpu_data_free_interfaces(handle);
  536. /* Destroy the data now */
  537. unsigned node;
  538. for (node = 0; node < STARPU_MAXNODES; node++)
  539. {
  540. struct _starpu_data_replicate *local = &handle->per_node[node];
  541. /* free the data copy in a lazy fashion */
  542. if (local->allocated && local->automatically_allocated)
  543. _starpu_request_mem_chunk_removal(handle, local, node, size);
  544. }
  545. unsigned worker;
  546. unsigned nworkers = starpu_worker_get_count();
  547. for (worker = 0; worker < nworkers; worker++)
  548. {
  549. struct _starpu_data_replicate *local = &handle->per_worker[worker];
  550. /* free the data copy in a lazy fashion */
  551. if (local->allocated && local->automatically_allocated)
  552. _starpu_request_mem_chunk_removal(handle, local, starpu_worker_get_memory_node(worker), size);
  553. }
  554. _starpu_memory_stats_free(handle);
  555. _starpu_data_requester_list_delete(handle->req_list);
  556. _starpu_data_requester_list_delete(handle->reduction_req_list);
  557. _starpu_spin_unlock(&handle->header_lock);
  558. _starpu_spin_destroy(&handle->header_lock);
  559. STARPU_PTHREAD_MUTEX_DESTROY(&handle->busy_mutex);
  560. STARPU_PTHREAD_COND_DESTROY(&handle->busy_cond);
  561. STARPU_PTHREAD_MUTEX_DESTROY(&handle->sequential_consistency_mutex);
  562. starpu_data_release_tag(handle);
  563. free(handle);
  564. }
  565. void starpu_data_unregister(starpu_data_handle_t handle)
  566. {
  567. STARPU_ASSERT_MSG(!handle->lazy_unregister, "data must not be unregistered twice");
  568. _starpu_data_unregister(handle, 1);
  569. }
  570. void starpu_data_unregister_no_coherency(starpu_data_handle_t handle)
  571. {
  572. _starpu_data_unregister(handle, 0);
  573. }
  574. void starpu_data_unregister_submit(starpu_data_handle_t handle)
  575. {
  576. _starpu_spin_lock(&handle->header_lock);
  577. STARPU_ASSERT_MSG(!handle->lazy_unregister, "data must not be unregistered twice");
  578. handle->lazy_unregister = 1;
  579. _starpu_spin_unlock(&handle->header_lock);
  580. _starpu_data_unregister(handle, 0);
  581. }
  582. static void _starpu_data_invalidate(void *data)
  583. {
  584. starpu_data_handle_t handle = data;
  585. size_t size = _starpu_data_get_size(handle);
  586. _starpu_spin_lock(&handle->header_lock);
  587. unsigned node;
  588. for (node = 0; node < STARPU_MAXNODES; node++)
  589. {
  590. struct _starpu_data_replicate *local = &handle->per_node[node];
  591. if (local->mc && local->allocated && local->automatically_allocated)
  592. /* free the data copy in a lazy fashion */
  593. _starpu_request_mem_chunk_removal(handle, local, node, size);
  594. local->state = STARPU_INVALID;
  595. }
  596. unsigned worker;
  597. unsigned nworkers = starpu_worker_get_count();
  598. for (worker = 0; worker < nworkers; worker++)
  599. {
  600. struct _starpu_data_replicate *local = &handle->per_worker[worker];
  601. if (local->mc && local->allocated && local->automatically_allocated)
  602. /* free the data copy in a lazy fashion */
  603. _starpu_request_mem_chunk_removal(handle, local, starpu_worker_get_memory_node(worker), size);
  604. local->state = STARPU_INVALID;
  605. }
  606. _starpu_spin_unlock(&handle->header_lock);
  607. starpu_data_release(handle);
  608. }
  609. void starpu_data_invalidate(starpu_data_handle_t handle)
  610. {
  611. STARPU_ASSERT(handle);
  612. starpu_data_acquire(handle, STARPU_W);
  613. _starpu_data_invalidate(handle);
  614. }
  615. void starpu_data_invalidate_submit(starpu_data_handle_t handle)
  616. {
  617. STARPU_ASSERT(handle);
  618. starpu_data_acquire_cb(handle, STARPU_W, _starpu_data_invalidate, handle);
  619. }
  620. enum starpu_data_interface_id starpu_handle_get_interface_id(starpu_data_handle_t handle)
  621. {
  622. return handle->ops->interfaceid;
  623. }
  624. void *starpu_data_get_interface_on_node(starpu_data_handle_t handle, unsigned memory_node)
  625. {
  626. return handle->per_node[memory_node].data_interface;
  627. }
  628. int starpu_data_interface_get_next_id(void)
  629. {
  630. _data_interface_number += 1;
  631. return _data_interface_number-1;
  632. }
  633. int starpu_handle_pack_data(starpu_data_handle_t handle, void **ptr, starpu_ssize_t *count)
  634. {
  635. STARPU_ASSERT(handle->ops->pack_data);
  636. return handle->ops->pack_data(handle, _starpu_memory_node_get_local_key(), ptr, count);
  637. }
  638. int starpu_handle_unpack_data(starpu_data_handle_t handle, void *ptr, size_t count)
  639. {
  640. STARPU_ASSERT(handle->ops->unpack_data);
  641. int ret;
  642. ret = handle->ops->unpack_data(handle, _starpu_memory_node_get_local_key(), ptr, count);
  643. free(ptr);
  644. return ret;
  645. }
  646. size_t starpu_handle_get_size(starpu_data_handle_t handle)
  647. {
  648. return handle->ops->get_size(handle);
  649. }