data_interface.c 24 KB

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