data_interface.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695
  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. void _starpu_data_interface_init(void)
  37. {
  38. _starpu_spin_init(&registered_handles_lock);
  39. }
  40. void _starpu_data_interface_shutdown()
  41. {
  42. struct handle_entry *entry, *tmp;
  43. _starpu_spin_destroy(&registered_handles_lock);
  44. HASH_ITER(hh, registered_handles, entry, tmp)
  45. {
  46. HASH_DEL(registered_handles, entry);
  47. free(entry);
  48. }
  49. registered_handles = NULL;
  50. }
  51. /* Register the mapping from PTR to HANDLE. If PTR is already mapped to
  52. * some handle, the new mapping shadows the previous one. */
  53. void _starpu_data_register_ram_pointer(starpu_data_handle_t handle, void *ptr)
  54. {
  55. struct handle_entry *entry;
  56. entry = (struct handle_entry *) malloc(sizeof(*entry));
  57. STARPU_ASSERT(entry != NULL);
  58. entry->pointer = ptr;
  59. entry->handle = handle;
  60. _starpu_spin_lock(&registered_handles_lock);
  61. HASH_ADD_PTR(registered_handles, pointer, entry);
  62. _starpu_spin_unlock(&registered_handles_lock);
  63. }
  64. starpu_data_handle_t starpu_data_lookup(const void *ptr)
  65. {
  66. starpu_data_handle_t result;
  67. _starpu_spin_lock(&registered_handles_lock);
  68. {
  69. struct handle_entry *entry;
  70. HASH_FIND_PTR(registered_handles, &ptr, entry);
  71. if(STARPU_UNLIKELY(entry == NULL))
  72. result = NULL;
  73. else
  74. result = entry->handle;
  75. }
  76. _starpu_spin_unlock(&registered_handles_lock);
  77. return result;
  78. }
  79. int
  80. _starpu_data_is_multiformat_handle(starpu_data_handle_t handle)
  81. {
  82. return handle->ops->is_multiformat;
  83. }
  84. /*
  85. * Start monitoring a piece of data
  86. */
  87. static void _starpu_register_new_data(starpu_data_handle_t handle,
  88. unsigned home_node, uint32_t wt_mask)
  89. {
  90. void *ptr;
  91. STARPU_ASSERT(handle);
  92. /* initialize the new lock */
  93. handle->req_list = _starpu_data_requester_list_new();
  94. handle->refcnt = 0;
  95. handle->busy_count = 0;
  96. handle->busy_waiting = 0;
  97. _STARPU_PTHREAD_MUTEX_INIT(&handle->busy_mutex, NULL);
  98. _STARPU_PTHREAD_COND_INIT(&handle->busy_cond, NULL);
  99. _starpu_spin_init(&handle->header_lock);
  100. /* first take care to properly lock the data */
  101. _starpu_spin_lock(&handle->header_lock);
  102. /* there is no hierarchy yet */
  103. handle->nchildren = 0;
  104. handle->root_handle = handle;
  105. handle->father_handle = NULL;
  106. handle->sibling_index = 0; /* could be anything for the root */
  107. handle->depth = 1; /* the tree is just a node yet */
  108. handle->rank = -1; /* invalid until set */
  109. handle->tag = -1; /* invalid until set */
  110. handle->is_not_important = 0;
  111. handle->sequential_consistency =
  112. starpu_data_get_default_sequential_consistency_flag();
  113. _STARPU_PTHREAD_MUTEX_INIT(&handle->sequential_consistency_mutex, NULL);
  114. handle->last_submitted_mode = STARPU_R;
  115. handle->last_submitted_writer = NULL;
  116. handle->last_submitted_readers = NULL;
  117. handle->post_sync_tasks = NULL;
  118. handle->post_sync_tasks_cnt = 0;
  119. /* By default, there are no methods available to perform a reduction */
  120. handle->redux_cl = NULL;
  121. handle->init_cl = NULL;
  122. handle->reduction_refcnt = 0;
  123. handle->reduction_req_list = _starpu_data_requester_list_new();
  124. #ifdef STARPU_USE_FXT
  125. handle->last_submitted_ghost_writer_id_is_valid = 0;
  126. handle->last_submitted_ghost_writer_id = 0;
  127. handle->last_submitted_ghost_readers_id = NULL;
  128. #endif
  129. handle->wt_mask = wt_mask;
  130. /* Store some values directly in the handle not to recompute them all
  131. * the time. */
  132. handle->footprint = _starpu_compute_data_footprint(handle);
  133. handle->home_node = home_node;
  134. /* that new data is invalid from all nodes perpective except for the
  135. * home node */
  136. unsigned node;
  137. for (node = 0; node < STARPU_MAXNODES; node++)
  138. {
  139. struct _starpu_data_replicate *replicate;
  140. replicate = &handle->per_node[node];
  141. replicate->memory_node = node;
  142. replicate->relaxed_coherency = 0;
  143. replicate->refcnt = 0;
  144. if (node == home_node)
  145. {
  146. /* this is the home node with the only valid copy */
  147. replicate->state = STARPU_OWNER;
  148. replicate->allocated = 1;
  149. replicate->automatically_allocated = 0;
  150. }
  151. else
  152. {
  153. /* the value is not available here yet */
  154. replicate->state = STARPU_INVALID;
  155. replicate->allocated = 0;
  156. }
  157. }
  158. unsigned worker;
  159. unsigned nworkers = starpu_worker_get_count();
  160. for (worker = 0; worker < nworkers; worker++)
  161. {
  162. struct _starpu_data_replicate *replicate;
  163. replicate = &handle->per_worker[worker];
  164. replicate->allocated = 0;
  165. replicate->automatically_allocated = 0;
  166. replicate->state = STARPU_INVALID;
  167. replicate->refcnt = 0;
  168. replicate->handle = handle;
  169. for (node = 0; node < STARPU_MAXNODES; node++)
  170. {
  171. replicate->requested[node] = 0;
  172. replicate->request[node] = NULL;
  173. }
  174. /* Assuming being used for SCRATCH for now, patched when entering REDUX mode */
  175. replicate->relaxed_coherency = 1;
  176. replicate->initialized = 0;
  177. replicate->memory_node = starpu_worker_get_memory_node(worker);
  178. /* duplicate the content of the interface on node 0 */
  179. memcpy(replicate->data_interface, handle->per_node[0].data_interface, handle->ops->interface_size);
  180. }
  181. /* now the data is available ! */
  182. _starpu_spin_unlock(&handle->header_lock);
  183. ptr = starpu_handle_to_pointer(handle, 0);
  184. if (ptr != NULL)
  185. {
  186. _starpu_data_register_ram_pointer(handle, ptr);
  187. }
  188. }
  189. int _starpu_data_handle_init(starpu_data_handle_t handle, struct starpu_data_interface_ops *interface_ops, unsigned int mf_node)
  190. {
  191. unsigned node;
  192. unsigned worker;
  193. handle->ops = interface_ops;
  194. handle->mf_node = mf_node;
  195. size_t interfacesize = interface_ops->interface_size;
  196. _starpu_memory_stats_init(handle);
  197. for (node = 0; node < STARPU_MAXNODES; node++)
  198. {
  199. _starpu_memory_stats_init_per_node(handle, node);
  200. struct _starpu_data_replicate *replicate;
  201. replicate = &handle->per_node[node];
  202. /* relaxed_coherency = 0 */
  203. replicate->handle = handle;
  204. replicate->data_interface = calloc(1, interfacesize);
  205. STARPU_ASSERT(replicate->data_interface);
  206. }
  207. unsigned nworkers = starpu_worker_get_count();
  208. for (worker = 0; worker < nworkers; worker++)
  209. {
  210. struct _starpu_data_replicate *replicate;
  211. replicate = &handle->per_worker[worker];
  212. replicate->handle = handle;
  213. replicate->data_interface = calloc(1, interfacesize);
  214. STARPU_ASSERT(replicate->data_interface);
  215. }
  216. handle->tag = -1;
  217. handle->rank = -1;
  218. return 0;
  219. }
  220. static
  221. starpu_data_handle_t _starpu_data_handle_allocate(struct starpu_data_interface_ops *interface_ops, unsigned int mf_node)
  222. {
  223. starpu_data_handle_t handle = (starpu_data_handle_t) calloc(1, sizeof(struct _starpu_data_state));
  224. STARPU_ASSERT(handle);
  225. _starpu_data_handle_init(handle, interface_ops, mf_node);
  226. return handle;
  227. }
  228. void starpu_data_register(starpu_data_handle_t *handleptr, unsigned home_node,
  229. void *data_interface,
  230. struct starpu_data_interface_ops *ops)
  231. {
  232. starpu_data_handle_t handle = _starpu_data_handle_allocate(ops, home_node);
  233. STARPU_ASSERT(handleptr);
  234. *handleptr = handle;
  235. /* fill the interface fields with the appropriate method */
  236. STARPU_ASSERT(ops->register_data_handle);
  237. ops->register_data_handle(handle, home_node, data_interface);
  238. _starpu_register_new_data(handle, home_node, 0);
  239. }
  240. void starpu_data_register_same(starpu_data_handle_t *handledst, starpu_data_handle_t handlesrc)
  241. {
  242. void *local_interface = starpu_data_get_interface_on_node(handlesrc, 0);
  243. starpu_data_register(handledst, -1, local_interface, handlesrc->ops);
  244. }
  245. void *starpu_handle_to_pointer(starpu_data_handle_t handle, unsigned node)
  246. {
  247. /* Check whether the operation is supported and the node has actually
  248. * been allocated. */
  249. if (handle->ops->handle_to_pointer
  250. && starpu_data_test_if_allocated_on_node(handle, node))
  251. {
  252. return handle->ops->handle_to_pointer(handle, node);
  253. }
  254. return NULL;
  255. }
  256. void *starpu_handle_get_local_ptr(starpu_data_handle_t handle)
  257. {
  258. return starpu_handle_to_pointer(handle,
  259. _starpu_memory_node_get_local_key());
  260. }
  261. int starpu_data_get_rank(starpu_data_handle_t handle)
  262. {
  263. return handle->rank;
  264. }
  265. int starpu_data_set_rank(starpu_data_handle_t handle, int rank)
  266. {
  267. handle->rank = rank;
  268. return 0;
  269. }
  270. int starpu_data_get_tag(starpu_data_handle_t handle)
  271. {
  272. return handle->tag;
  273. }
  274. int starpu_data_set_tag(starpu_data_handle_t handle, int tag)
  275. {
  276. handle->tag = tag;
  277. return 0;
  278. }
  279. /*
  280. * Stop monitoring a piece of data
  281. */
  282. void _starpu_data_free_interfaces(starpu_data_handle_t handle)
  283. {
  284. const void *ram_ptr;
  285. unsigned node;
  286. unsigned worker;
  287. unsigned nworkers = starpu_worker_get_count();
  288. ram_ptr = starpu_handle_to_pointer(handle, 0);
  289. for (node = 0; node < STARPU_MAXNODES; node++)
  290. free(handle->per_node[node].data_interface);
  291. for (worker = 0; worker < nworkers; worker++)
  292. free(handle->per_worker[worker].data_interface);
  293. if (ram_ptr != NULL)
  294. {
  295. /* Remove the PTR -> HANDLE mapping. If a mapping from PTR
  296. * to another handle existed before (e.g., when using
  297. * filters), it becomes visible again. */
  298. struct handle_entry *entry;
  299. _starpu_spin_lock(&registered_handles_lock);
  300. HASH_FIND_PTR(registered_handles, &ram_ptr, entry);
  301. STARPU_ASSERT(entry != NULL);
  302. HASH_DEL(registered_handles, entry);
  303. free(entry);
  304. _starpu_spin_unlock(&registered_handles_lock);
  305. }
  306. }
  307. struct _starpu_unregister_callback_arg
  308. {
  309. unsigned memory_node;
  310. starpu_data_handle_t handle;
  311. unsigned terminated;
  312. _starpu_pthread_mutex_t mutex;
  313. _starpu_pthread_cond_t cond;
  314. };
  315. /* Check whether we should tell starpu_data_unregister that the data handle is
  316. * not busy any more.
  317. * The header is supposed to be locked.
  318. * This may free the handle, if it was lazily unregistered (1 is returned in
  319. * that case). The handle pointer thus becomes invalid for the caller.
  320. */
  321. int _starpu_data_check_not_busy(starpu_data_handle_t handle)
  322. {
  323. if (!handle->busy_count && handle->busy_waiting)
  324. {
  325. _STARPU_PTHREAD_MUTEX_LOCK(&handle->busy_mutex);
  326. _STARPU_PTHREAD_COND_BROADCAST(&handle->busy_cond);
  327. _STARPU_PTHREAD_MUTEX_UNLOCK(&handle->busy_mutex);
  328. }
  329. /* The handle has been destroyed in between (eg. this was a temporary
  330. * handle created for a reduction.) */
  331. if (handle->lazy_unregister && handle->busy_count == 0)
  332. {
  333. _starpu_spin_unlock(&handle->header_lock);
  334. starpu_data_unregister_no_coherency(handle);
  335. /* Warning: in case we unregister the handle, we must be sure
  336. * that the caller will not try to unlock the header after
  337. * !*/
  338. return 1;
  339. }
  340. return 0;
  341. }
  342. static void _starpu_data_unregister_fetch_data_callback(void *_arg)
  343. {
  344. int ret;
  345. struct _starpu_unregister_callback_arg *arg = (struct _starpu_unregister_callback_arg *) _arg;
  346. starpu_data_handle_t handle = arg->handle;
  347. STARPU_ASSERT(handle);
  348. struct _starpu_data_replicate *replicate = &handle->per_node[arg->memory_node];
  349. ret = _starpu_fetch_data_on_node(handle, replicate, STARPU_R, 0, 0, NULL, NULL);
  350. STARPU_ASSERT(!ret);
  351. /* unlock the caller */
  352. _STARPU_PTHREAD_MUTEX_LOCK(&arg->mutex);
  353. arg->terminated = 1;
  354. _STARPU_PTHREAD_COND_SIGNAL(&arg->cond);
  355. _STARPU_PTHREAD_MUTEX_UNLOCK(&arg->mutex);
  356. }
  357. /* Unregister the data handle, perhaps we don't need to update the home_node
  358. * (in that case coherent is set to 0) */
  359. static void _starpu_data_unregister(starpu_data_handle_t handle, unsigned coherent)
  360. {
  361. STARPU_ASSERT(handle);
  362. STARPU_ASSERT_MSG(handle->nchildren == 0, "data needs to be unpartitioned before unregistration");
  363. if (coherent)
  364. {
  365. /* If sequential consistency is enabled, wait until data is available */
  366. _starpu_data_wait_until_available(handle, STARPU_RW);
  367. /* Fetch data in the home of the data to ensure we have a valid copy
  368. * where we registered it */
  369. int home_node = handle->home_node;
  370. if (home_node >= 0)
  371. {
  372. struct _starpu_unregister_callback_arg arg;
  373. arg.handle = handle;
  374. arg.memory_node = (unsigned)home_node;
  375. arg.terminated = 0;
  376. _STARPU_PTHREAD_MUTEX_INIT(&arg.mutex, NULL);
  377. _STARPU_PTHREAD_COND_INIT(&arg.cond, NULL);
  378. if (!_starpu_attempt_to_submit_data_request_from_apps(handle, STARPU_R,
  379. _starpu_data_unregister_fetch_data_callback, &arg))
  380. {
  381. /* no one has locked this data yet, so we proceed immediately */
  382. struct _starpu_data_replicate *home_replicate = &handle->per_node[home_node];
  383. int ret = _starpu_fetch_data_on_node(handle, home_replicate, STARPU_R, 0, 0, NULL, NULL);
  384. STARPU_ASSERT(!ret);
  385. }
  386. else
  387. {
  388. _STARPU_PTHREAD_MUTEX_LOCK(&arg.mutex);
  389. while (!arg.terminated)
  390. _STARPU_PTHREAD_COND_WAIT(&arg.cond, &arg.mutex);
  391. _STARPU_PTHREAD_MUTEX_UNLOCK(&arg.mutex);
  392. }
  393. _starpu_release_data_on_node(handle, 0, &handle->per_node[home_node]);
  394. }
  395. /* If this handle uses a multiformat interface, we may have to convert
  396. * this piece of data back into the CPU format.
  397. * XXX : This is quite hacky, could we submit a task instead ?
  398. */
  399. if (_starpu_data_is_multiformat_handle(handle) &&
  400. starpu_node_get_kind(handle->mf_node) != STARPU_CPU_RAM)
  401. {
  402. _STARPU_DEBUG("Conversion needed\n");
  403. void *buffers[1];
  404. struct starpu_multiformat_interface *format_interface;
  405. format_interface = (struct starpu_multiformat_interface *) starpu_data_get_interface_on_node(handle, 0);
  406. struct starpu_codelet *cl = NULL;
  407. enum starpu_node_kind node_kind = starpu_node_get_kind(handle->mf_node);
  408. switch (node_kind)
  409. {
  410. #ifdef STARPU_USE_CUDA
  411. case STARPU_CUDA_RAM:
  412. {
  413. struct starpu_multiformat_data_interface_ops *mf_ops;
  414. mf_ops = (struct starpu_multiformat_data_interface_ops *) handle->ops->get_mf_ops(format_interface);
  415. cl = mf_ops->cuda_to_cpu_cl;
  416. break;
  417. }
  418. #endif
  419. #ifdef STARPU_USE_OPENCL
  420. case STARPU_OPENCL_RAM:
  421. {
  422. struct starpu_multiformat_data_interface_ops *mf_ops;
  423. mf_ops = (struct starpu_multiformat_data_interface_ops *) handle->ops->get_mf_ops(format_interface);
  424. cl = mf_ops->opencl_to_cpu_cl;
  425. break;
  426. }
  427. #endif
  428. case STARPU_CPU_RAM: /* Impossible ! */
  429. default:
  430. STARPU_ABORT();
  431. }
  432. buffers[0] = format_interface;
  433. _starpu_cl_func_t func = _starpu_task_get_cpu_nth_implementation(cl, 0);
  434. STARPU_ASSERT(func);
  435. func(buffers, NULL);
  436. }
  437. }
  438. _starpu_spin_lock(&handle->header_lock);
  439. if (!coherent)
  440. {
  441. /* Should we postpone the unregister operation ? */
  442. if ((handle->busy_count > 0) && handle->lazy_unregister)
  443. {
  444. _starpu_spin_unlock(&handle->header_lock);
  445. return;
  446. }
  447. }
  448. /* Tell holders of references that we're starting waiting */
  449. handle->busy_waiting = 1;
  450. _starpu_spin_unlock(&handle->header_lock);
  451. /* Wait for all requests to finish (notably WT requests) */
  452. _STARPU_PTHREAD_MUTEX_LOCK(&handle->busy_mutex);
  453. while (handle->busy_count)
  454. _STARPU_PTHREAD_COND_WAIT(&handle->busy_cond, &handle->busy_mutex);
  455. _STARPU_PTHREAD_MUTEX_UNLOCK(&handle->busy_mutex);
  456. /* Wait for finished requests to release the handle */
  457. _starpu_spin_lock(&handle->header_lock);
  458. _starpu_data_free_interfaces(handle);
  459. /* Destroy the data now */
  460. size_t size = _starpu_data_get_size(handle);
  461. unsigned node;
  462. for (node = 0; node < STARPU_MAXNODES; node++)
  463. {
  464. struct _starpu_data_replicate *local = &handle->per_node[node];
  465. /* free the data copy in a lazy fashion */
  466. if (local->allocated && local->automatically_allocated)
  467. _starpu_request_mem_chunk_removal(handle, local, node, size);
  468. }
  469. unsigned worker;
  470. unsigned nworkers = starpu_worker_get_count();
  471. for (worker = 0; worker < nworkers; worker++)
  472. {
  473. struct _starpu_data_replicate *local = &handle->per_worker[worker];
  474. /* free the data copy in a lazy fashion */
  475. if (local->allocated && local->automatically_allocated)
  476. _starpu_request_mem_chunk_removal(handle, local, starpu_worker_get_memory_node(worker), size);
  477. }
  478. _starpu_memory_stats_free(handle);
  479. _starpu_data_requester_list_delete(handle->req_list);
  480. _starpu_data_requester_list_delete(handle->reduction_req_list);
  481. _starpu_spin_unlock(&handle->header_lock);
  482. _starpu_spin_destroy(&handle->header_lock);
  483. free(handle);
  484. }
  485. void starpu_data_unregister(starpu_data_handle_t handle)
  486. {
  487. STARPU_ASSERT_MSG(!handle->lazy_unregister, "data must not be unregistered twice");
  488. _starpu_data_unregister(handle, 1);
  489. }
  490. void starpu_data_unregister_no_coherency(starpu_data_handle_t handle)
  491. {
  492. _starpu_data_unregister(handle, 0);
  493. }
  494. void starpu_data_unregister_submit(starpu_data_handle_t handle)
  495. {
  496. _starpu_spin_lock(&handle->header_lock);
  497. STARPU_ASSERT_MSG(!handle->lazy_unregister, "data must not be unregistered twice");
  498. handle->lazy_unregister = 1;
  499. _starpu_spin_unlock(&handle->header_lock);
  500. _starpu_data_unregister(handle, 0);
  501. }
  502. static void _starpu_data_invalidate(void *data)
  503. {
  504. starpu_data_handle_t handle = data;
  505. size_t size = _starpu_data_get_size(handle);
  506. _starpu_spin_lock(&handle->header_lock);
  507. unsigned node;
  508. for (node = 0; node < STARPU_MAXNODES; node++)
  509. {
  510. struct _starpu_data_replicate *local = &handle->per_node[node];
  511. if (local->mc && local->allocated && local->automatically_allocated)
  512. /* free the data copy in a lazy fashion */
  513. _starpu_request_mem_chunk_removal(handle, local, node, size);
  514. local->state = STARPU_INVALID;
  515. }
  516. unsigned worker;
  517. unsigned nworkers = starpu_worker_get_count();
  518. for (worker = 0; worker < nworkers; worker++)
  519. {
  520. struct _starpu_data_replicate *local = &handle->per_worker[worker];
  521. if (local->mc && local->allocated && local->automatically_allocated)
  522. /* free the data copy in a lazy fashion */
  523. _starpu_request_mem_chunk_removal(handle, local, starpu_worker_get_memory_node(worker), size);
  524. local->state = STARPU_INVALID;
  525. }
  526. _starpu_spin_unlock(&handle->header_lock);
  527. starpu_data_release(handle);
  528. }
  529. void starpu_data_invalidate(starpu_data_handle_t handle)
  530. {
  531. STARPU_ASSERT(handle);
  532. starpu_data_acquire(handle, STARPU_W);
  533. _starpu_data_invalidate(handle);
  534. }
  535. void starpu_data_invalidate_submit(starpu_data_handle_t handle)
  536. {
  537. STARPU_ASSERT(handle);
  538. starpu_data_acquire_cb(handle, STARPU_W, _starpu_data_invalidate, handle);
  539. }
  540. enum starpu_data_interface_id starpu_handle_get_interface_id(starpu_data_handle_t handle)
  541. {
  542. return handle->ops->interfaceid;
  543. }
  544. void *starpu_data_get_interface_on_node(starpu_data_handle_t handle, unsigned memory_node)
  545. {
  546. return handle->per_node[memory_node].data_interface;
  547. }
  548. int starpu_data_interface_get_next_id(void)
  549. {
  550. _data_interface_number += 1;
  551. return _data_interface_number-1;
  552. }
  553. int starpu_handle_pack_data(starpu_data_handle_t handle, void **ptr, ssize_t *count)
  554. {
  555. STARPU_ASSERT(handle->ops->pack_data);
  556. return handle->ops->pack_data(handle, _starpu_memory_node_get_local_key(), ptr, count);
  557. }
  558. int starpu_handle_unpack_data(starpu_data_handle_t handle, void *ptr, size_t count)
  559. {
  560. STARPU_ASSERT(handle->ops->unpack_data);
  561. int ret;
  562. ret = handle->ops->unpack_data(handle, _starpu_memory_node_get_local_key(), ptr, count);
  563. free(ptr);
  564. return ret;
  565. }
  566. size_t starpu_handle_get_size(starpu_data_handle_t handle)
  567. {
  568. return handle->ops->get_size(handle);
  569. }