data_interface.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  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. int asynchronous_copy_disabled = starpu_asynchronous_copy_disabled();
  236. if (STARPU_UNLIKELY(asynchronous_copy_disabled))
  237. {
  238. #ifdef STARPU_USE_CUDA
  239. ops->copy_methods->ram_to_cuda_async = NULL;
  240. ops->copy_methods->cuda_to_ram_async = NULL;
  241. ops->copy_methods->cuda_to_cuda_async = NULL;
  242. #endif
  243. #ifdef STARPU_USE_OPENCL
  244. ops->copy_methods->ram_to_opencl_async = NULL;
  245. ops->copy_methods->opencl_to_ram_async = NULL;
  246. ops->copy_methods->opencl_to_opencl_async = NULL;
  247. #endif
  248. }
  249. #ifdef STARPU_USE_CUDA
  250. int asynchronous_cuda_copy_disabled = starpu_asynchronous_cuda_copy_disabled();
  251. if (STARPU_UNLIKELY(asynchronous_cuda_copy_disabled))
  252. {
  253. ops->copy_methods->ram_to_cuda_async = NULL;
  254. ops->copy_methods->cuda_to_ram_async = NULL;
  255. ops->copy_methods->cuda_to_cuda_async = NULL;
  256. }
  257. #endif
  258. #ifdef STARPU_USE_OPENCL
  259. int asynchronous_opencl_copy_disabled = starpu_asynchronous_opencl_copy_disabled();
  260. if (STARPU_UNLIKELY(asynchronous_opencl_copy_disabled))
  261. {
  262. ops->copy_methods->ram_to_opencl_async = NULL;
  263. ops->copy_methods->opencl_to_ram_async = NULL;
  264. ops->copy_methods->opencl_to_opencl_async = NULL;
  265. }
  266. #endif
  267. /* fill the interface fields with the appropriate method */
  268. STARPU_ASSERT(ops->register_data_handle);
  269. ops->register_data_handle(handle, home_node, data_interface);
  270. _starpu_register_new_data(handle, home_node, 0);
  271. }
  272. void starpu_data_register_same(starpu_data_handle_t *handledst, starpu_data_handle_t handlesrc)
  273. {
  274. void *local_interface = starpu_data_get_interface_on_node(handlesrc, 0);
  275. starpu_data_register(handledst, -1, local_interface, handlesrc->ops);
  276. }
  277. void *starpu_handle_to_pointer(starpu_data_handle_t handle, unsigned node)
  278. {
  279. /* Check whether the operation is supported and the node has actually
  280. * been allocated. */
  281. if (handle->ops->handle_to_pointer
  282. && starpu_data_test_if_allocated_on_node(handle, node))
  283. {
  284. return handle->ops->handle_to_pointer(handle, node);
  285. }
  286. return NULL;
  287. }
  288. void *starpu_handle_get_local_ptr(starpu_data_handle_t handle)
  289. {
  290. return starpu_handle_to_pointer(handle,
  291. _starpu_memory_node_get_local_key());
  292. }
  293. int starpu_data_get_rank(starpu_data_handle_t handle)
  294. {
  295. return handle->rank;
  296. }
  297. int starpu_data_set_rank(starpu_data_handle_t handle, int rank)
  298. {
  299. handle->rank = rank;
  300. return 0;
  301. }
  302. int starpu_data_get_tag(starpu_data_handle_t handle)
  303. {
  304. return handle->tag;
  305. }
  306. int starpu_data_set_tag(starpu_data_handle_t handle, int tag)
  307. {
  308. handle->tag = tag;
  309. return 0;
  310. }
  311. /*
  312. * Stop monitoring a piece of data
  313. */
  314. void _starpu_data_free_interfaces(starpu_data_handle_t handle)
  315. {
  316. const void *ram_ptr;
  317. unsigned node;
  318. unsigned worker;
  319. unsigned nworkers = starpu_worker_get_count();
  320. ram_ptr = starpu_handle_to_pointer(handle, 0);
  321. for (node = 0; node < STARPU_MAXNODES; node++)
  322. free(handle->per_node[node].data_interface);
  323. for (worker = 0; worker < nworkers; worker++)
  324. free(handle->per_worker[worker].data_interface);
  325. if (ram_ptr != NULL)
  326. {
  327. /* Remove the PTR -> HANDLE mapping. If a mapping from PTR
  328. * to another handle existed before (e.g., when using
  329. * filters), it becomes visible again. */
  330. struct handle_entry *entry;
  331. _starpu_spin_lock(&registered_handles_lock);
  332. HASH_FIND_PTR(registered_handles, &ram_ptr, entry);
  333. STARPU_ASSERT(entry != NULL);
  334. HASH_DEL(registered_handles, entry);
  335. free(entry);
  336. _starpu_spin_unlock(&registered_handles_lock);
  337. }
  338. }
  339. struct _starpu_unregister_callback_arg
  340. {
  341. unsigned memory_node;
  342. starpu_data_handle_t handle;
  343. unsigned terminated;
  344. _starpu_pthread_mutex_t mutex;
  345. _starpu_pthread_cond_t cond;
  346. };
  347. /* Check whether we should tell starpu_data_unregister that the data handle is
  348. * not busy any more.
  349. * The header is supposed to be locked.
  350. * This may free the handle, if it was lazily unregistered (1 is returned in
  351. * that case). The handle pointer thus becomes invalid for the caller.
  352. */
  353. int _starpu_data_check_not_busy(starpu_data_handle_t handle)
  354. {
  355. if (!handle->busy_count && handle->busy_waiting)
  356. {
  357. _STARPU_PTHREAD_MUTEX_LOCK(&handle->busy_mutex);
  358. _STARPU_PTHREAD_COND_BROADCAST(&handle->busy_cond);
  359. _STARPU_PTHREAD_MUTEX_UNLOCK(&handle->busy_mutex);
  360. }
  361. /* The handle has been destroyed in between (eg. this was a temporary
  362. * handle created for a reduction.) */
  363. if (handle->lazy_unregister && handle->busy_count == 0)
  364. {
  365. _starpu_spin_unlock(&handle->header_lock);
  366. starpu_data_unregister_no_coherency(handle);
  367. /* Warning: in case we unregister the handle, we must be sure
  368. * that the caller will not try to unlock the header after
  369. * !*/
  370. return 1;
  371. }
  372. return 0;
  373. }
  374. static void _starpu_data_unregister_fetch_data_callback(void *_arg)
  375. {
  376. int ret;
  377. struct _starpu_unregister_callback_arg *arg = (struct _starpu_unregister_callback_arg *) _arg;
  378. starpu_data_handle_t handle = arg->handle;
  379. STARPU_ASSERT(handle);
  380. struct _starpu_data_replicate *replicate = &handle->per_node[arg->memory_node];
  381. ret = _starpu_fetch_data_on_node(handle, replicate, STARPU_R, 0, 0, NULL, NULL);
  382. STARPU_ASSERT(!ret);
  383. /* unlock the caller */
  384. _STARPU_PTHREAD_MUTEX_LOCK(&arg->mutex);
  385. arg->terminated = 1;
  386. _STARPU_PTHREAD_COND_SIGNAL(&arg->cond);
  387. _STARPU_PTHREAD_MUTEX_UNLOCK(&arg->mutex);
  388. }
  389. /* Unregister the data handle, perhaps we don't need to update the home_node
  390. * (in that case coherent is set to 0) */
  391. static void _starpu_data_unregister(starpu_data_handle_t handle, unsigned coherent)
  392. {
  393. STARPU_ASSERT(handle);
  394. STARPU_ASSERT_MSG(handle->nchildren == 0, "data needs to be unpartitioned before unregistration");
  395. if (coherent)
  396. {
  397. /* If sequential consistency is enabled, wait until data is available */
  398. _starpu_data_wait_until_available(handle, STARPU_RW);
  399. /* Fetch data in the home of the data to ensure we have a valid copy
  400. * where we registered it */
  401. int home_node = handle->home_node;
  402. if (home_node >= 0)
  403. {
  404. struct _starpu_unregister_callback_arg arg;
  405. arg.handle = handle;
  406. arg.memory_node = (unsigned)home_node;
  407. arg.terminated = 0;
  408. _STARPU_PTHREAD_MUTEX_INIT(&arg.mutex, NULL);
  409. _STARPU_PTHREAD_COND_INIT(&arg.cond, NULL);
  410. if (!_starpu_attempt_to_submit_data_request_from_apps(handle, STARPU_R,
  411. _starpu_data_unregister_fetch_data_callback, &arg))
  412. {
  413. /* no one has locked this data yet, so we proceed immediately */
  414. struct _starpu_data_replicate *home_replicate = &handle->per_node[home_node];
  415. int ret = _starpu_fetch_data_on_node(handle, home_replicate, STARPU_R, 0, 0, NULL, NULL);
  416. STARPU_ASSERT(!ret);
  417. }
  418. else
  419. {
  420. _STARPU_PTHREAD_MUTEX_LOCK(&arg.mutex);
  421. while (!arg.terminated)
  422. _STARPU_PTHREAD_COND_WAIT(&arg.cond, &arg.mutex);
  423. _STARPU_PTHREAD_MUTEX_UNLOCK(&arg.mutex);
  424. }
  425. _starpu_release_data_on_node(handle, 0, &handle->per_node[home_node]);
  426. }
  427. /* If this handle uses a multiformat interface, we may have to convert
  428. * this piece of data back into the CPU format.
  429. * XXX : This is quite hacky, could we submit a task instead ?
  430. */
  431. if (_starpu_data_is_multiformat_handle(handle) &&
  432. starpu_node_get_kind(handle->mf_node) != STARPU_CPU_RAM)
  433. {
  434. _STARPU_DEBUG("Conversion needed\n");
  435. void *buffers[1];
  436. struct starpu_multiformat_interface *format_interface;
  437. format_interface = (struct starpu_multiformat_interface *) starpu_data_get_interface_on_node(handle, 0);
  438. struct starpu_codelet *cl = NULL;
  439. enum starpu_node_kind node_kind = starpu_node_get_kind(handle->mf_node);
  440. switch (node_kind)
  441. {
  442. #ifdef STARPU_USE_CUDA
  443. case STARPU_CUDA_RAM:
  444. {
  445. struct starpu_multiformat_data_interface_ops *mf_ops;
  446. mf_ops = (struct starpu_multiformat_data_interface_ops *) handle->ops->get_mf_ops(format_interface);
  447. cl = mf_ops->cuda_to_cpu_cl;
  448. break;
  449. }
  450. #endif
  451. #ifdef STARPU_USE_OPENCL
  452. case STARPU_OPENCL_RAM:
  453. {
  454. struct starpu_multiformat_data_interface_ops *mf_ops;
  455. mf_ops = (struct starpu_multiformat_data_interface_ops *) handle->ops->get_mf_ops(format_interface);
  456. cl = mf_ops->opencl_to_cpu_cl;
  457. break;
  458. }
  459. #endif
  460. case STARPU_CPU_RAM: /* Impossible ! */
  461. default:
  462. STARPU_ABORT();
  463. }
  464. buffers[0] = format_interface;
  465. _starpu_cl_func_t func = _starpu_task_get_cpu_nth_implementation(cl, 0);
  466. STARPU_ASSERT(func);
  467. func(buffers, NULL);
  468. }
  469. }
  470. _starpu_spin_lock(&handle->header_lock);
  471. if (!coherent)
  472. {
  473. /* Should we postpone the unregister operation ? */
  474. if ((handle->busy_count > 0) && handle->lazy_unregister)
  475. {
  476. _starpu_spin_unlock(&handle->header_lock);
  477. return;
  478. }
  479. }
  480. /* Tell holders of references that we're starting waiting */
  481. handle->busy_waiting = 1;
  482. _starpu_spin_unlock(&handle->header_lock);
  483. /* Wait for all requests to finish (notably WT requests) */
  484. _STARPU_PTHREAD_MUTEX_LOCK(&handle->busy_mutex);
  485. while (handle->busy_count)
  486. _STARPU_PTHREAD_COND_WAIT(&handle->busy_cond, &handle->busy_mutex);
  487. _STARPU_PTHREAD_MUTEX_UNLOCK(&handle->busy_mutex);
  488. /* Wait for finished requests to release the handle */
  489. _starpu_spin_lock(&handle->header_lock);
  490. /* Destroy the data now */
  491. unsigned node;
  492. for (node = 0; node < STARPU_MAXNODES; node++)
  493. {
  494. /* free the data copy in a lazy fashion */
  495. _starpu_request_mem_chunk_removal(handle, node, 1);
  496. }
  497. _starpu_data_free_interfaces(handle);
  498. _starpu_memory_stats_free(handle);
  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. _starpu_spin_destroy(&handle->header_lock);
  503. free(handle);
  504. }
  505. void starpu_data_unregister(starpu_data_handle_t handle)
  506. {
  507. STARPU_ASSERT_MSG(!handle->lazy_unregister, "data must not be unregistered twice");
  508. _starpu_data_unregister(handle, 1);
  509. }
  510. void starpu_data_unregister_no_coherency(starpu_data_handle_t handle)
  511. {
  512. _starpu_data_unregister(handle, 0);
  513. }
  514. void starpu_data_unregister_submit(starpu_data_handle_t handle) {
  515. _starpu_spin_lock(&handle->header_lock);
  516. STARPU_ASSERT_MSG(!handle->lazy_unregister, "data must not be unregistered twice");
  517. handle->lazy_unregister = 1;
  518. _starpu_spin_unlock(&handle->header_lock);
  519. _starpu_data_unregister(handle, 0);
  520. }
  521. static void _starpu_data_invalidate(void *data)
  522. {
  523. starpu_data_handle_t handle = data;
  524. _starpu_spin_lock(&handle->header_lock);
  525. unsigned node;
  526. for (node = 0; node < STARPU_MAXNODES; node++)
  527. {
  528. struct _starpu_data_replicate *local = &handle->per_node[node];
  529. if (local->allocated && local->automatically_allocated)
  530. {
  531. /* free the data copy in a lazy fashion */
  532. _starpu_request_mem_chunk_removal(handle, node, 0);
  533. }
  534. local->state = STARPU_INVALID;
  535. }
  536. _starpu_spin_unlock(&handle->header_lock);
  537. starpu_data_release(handle);
  538. }
  539. void starpu_data_invalidate(starpu_data_handle_t handle)
  540. {
  541. STARPU_ASSERT(handle);
  542. starpu_data_acquire(handle, STARPU_W);
  543. _starpu_data_invalidate(handle);
  544. }
  545. void starpu_data_invalidate_submit(starpu_data_handle_t handle)
  546. {
  547. STARPU_ASSERT(handle);
  548. starpu_data_acquire_cb(handle, STARPU_W, _starpu_data_invalidate, handle);
  549. }
  550. enum starpu_data_interface_id starpu_handle_get_interface_id(starpu_data_handle_t handle)
  551. {
  552. return handle->ops->interfaceid;
  553. }
  554. void *starpu_data_get_interface_on_node(starpu_data_handle_t handle, unsigned memory_node)
  555. {
  556. return handle->per_node[memory_node].data_interface;
  557. }
  558. int starpu_data_interface_get_next_id(void)
  559. {
  560. _data_interface_number += 1;
  561. return _data_interface_number-1;
  562. }
  563. int starpu_handle_pack_data(starpu_data_handle_t handle, void **ptr, size_t *count)
  564. {
  565. STARPU_ASSERT(handle->ops->pack_data);
  566. return handle->ops->pack_data(handle, _starpu_memory_node_get_local_key(), ptr, count);
  567. }
  568. int starpu_handle_unpack_data(starpu_data_handle_t handle, void *ptr, size_t count)
  569. {
  570. STARPU_ASSERT(handle->ops->unpack_data);
  571. int ret;
  572. ret = handle->ops->unpack_data(handle, _starpu_memory_node_get_local_key(), ptr, count);
  573. free(ptr);
  574. return ret;
  575. }
  576. size_t starpu_handle_get_size(starpu_data_handle_t handle)
  577. {
  578. return handle->ops->get_size(handle);
  579. }