data_interface.c 19 KB

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