data_interface.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845
  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)),
  339. "There is already a data handle %p registered with the tag %d\n", starpu_data_get_data_handle_from_tag(tag), tag);
  340. entry->tag = tag;
  341. entry->handle = handle;
  342. _starpu_spin_lock(&registered_tag_handles_lock);
  343. HASH_ADD_INT(registered_tag_handles, tag, entry);
  344. _starpu_spin_unlock(&registered_tag_handles_lock);
  345. handle->tag = tag;
  346. return 0;
  347. }
  348. int starpu_data_release_tag(starpu_data_handle_t handle)
  349. {
  350. struct handle_tag_entry *tag_entry;
  351. if (handle->tag != -1)
  352. {
  353. _starpu_spin_lock(&registered_tag_handles_lock);
  354. HASH_FIND_INT(registered_tag_handles, &handle->tag, tag_entry);
  355. STARPU_ASSERT_MSG((tag_entry != NULL),"Data handle %p with tag %d isn't in the hashmap !",handle,handle->tag);
  356. HASH_DEL(registered_tag_handles, tag_entry);
  357. free(tag_entry);
  358. _starpu_spin_unlock(&registered_tag_handles_lock);
  359. }
  360. return 0;
  361. }
  362. struct starpu_data_interface_ops* starpu_data_get_interface_ops(starpu_data_handle_t handle)
  363. {
  364. return handle->ops;
  365. }
  366. /*
  367. * Stop monitoring a piece of data
  368. */
  369. void _starpu_data_free_interfaces(starpu_data_handle_t handle)
  370. {
  371. const void *ram_ptr;
  372. unsigned node;
  373. unsigned worker;
  374. unsigned nworkers = starpu_worker_get_count();
  375. ram_ptr = starpu_data_handle_to_pointer(handle, 0);
  376. for (node = 0; node < STARPU_MAXNODES; node++)
  377. free(handle->per_node[node].data_interface);
  378. for (worker = 0; worker < nworkers; worker++)
  379. free(handle->per_worker[worker].data_interface);
  380. if (ram_ptr != NULL)
  381. {
  382. /* Remove the PTR -> HANDLE mapping. If a mapping from PTR
  383. * to another handle existed before (e.g., when using
  384. * filters), it becomes visible again. */
  385. struct handle_entry *entry;
  386. _starpu_spin_lock(&registered_handles_lock);
  387. HASH_FIND_PTR(registered_handles, &ram_ptr, entry);
  388. STARPU_ASSERT(entry != NULL);
  389. HASH_DEL(registered_handles, entry);
  390. free(entry);
  391. _starpu_spin_unlock(&registered_handles_lock);
  392. }
  393. }
  394. struct _starpu_unregister_callback_arg
  395. {
  396. unsigned memory_node;
  397. starpu_data_handle_t handle;
  398. unsigned terminated;
  399. starpu_pthread_mutex_t mutex;
  400. starpu_pthread_cond_t cond;
  401. };
  402. /* Check whether we should tell starpu_data_unregister that the data handle is
  403. * not busy any more.
  404. * The header is supposed to be locked.
  405. * This may free the handle, if it was lazily unregistered (1 is returned in
  406. * that case). The handle pointer thus becomes invalid for the caller.
  407. */
  408. int _starpu_data_check_not_busy(starpu_data_handle_t handle)
  409. {
  410. if (!handle->busy_count && handle->busy_waiting)
  411. {
  412. STARPU_PTHREAD_MUTEX_LOCK(&handle->busy_mutex);
  413. STARPU_PTHREAD_COND_BROADCAST(&handle->busy_cond);
  414. STARPU_PTHREAD_MUTEX_UNLOCK(&handle->busy_mutex);
  415. }
  416. /* The handle has been destroyed in between (eg. this was a temporary
  417. * handle created for a reduction.) */
  418. if (handle->lazy_unregister && handle->busy_count == 0)
  419. {
  420. _starpu_spin_unlock(&handle->header_lock);
  421. starpu_data_unregister_no_coherency(handle);
  422. /* Warning: in case we unregister the handle, we must be sure
  423. * that the caller will not try to unlock the header after
  424. * !*/
  425. return 1;
  426. }
  427. return 0;
  428. }
  429. static void _starpu_data_unregister_fetch_data_callback(void *_arg)
  430. {
  431. int ret;
  432. struct _starpu_unregister_callback_arg *arg = (struct _starpu_unregister_callback_arg *) _arg;
  433. starpu_data_handle_t handle = arg->handle;
  434. STARPU_ASSERT(handle);
  435. struct _starpu_data_replicate *replicate = &handle->per_node[arg->memory_node];
  436. ret = _starpu_fetch_data_on_node(handle, replicate, STARPU_R, 0, 0, NULL, NULL);
  437. STARPU_ASSERT(!ret);
  438. /* unlock the caller */
  439. STARPU_PTHREAD_MUTEX_LOCK(&arg->mutex);
  440. arg->terminated = 1;
  441. STARPU_PTHREAD_COND_SIGNAL(&arg->cond);
  442. STARPU_PTHREAD_MUTEX_UNLOCK(&arg->mutex);
  443. }
  444. /* Unregister the data handle, perhaps we don't need to update the home_node
  445. * (in that case coherent is set to 0) */
  446. static void _starpu_data_unregister(starpu_data_handle_t handle, unsigned coherent)
  447. {
  448. STARPU_ASSERT(handle);
  449. STARPU_ASSERT_MSG(handle->nchildren == 0, "data %p needs to be unpartitioned before unregistration", handle);
  450. if (coherent)
  451. {
  452. /* If sequential consistency is enabled, wait until data is available */
  453. _starpu_data_wait_until_available(handle, STARPU_RW);
  454. /* Fetch data in the home of the data to ensure we have a valid copy
  455. * where we registered it */
  456. int home_node = handle->home_node;
  457. if (home_node >= 0)
  458. {
  459. struct _starpu_unregister_callback_arg arg;
  460. arg.handle = handle;
  461. arg.memory_node = (unsigned)home_node;
  462. arg.terminated = 0;
  463. STARPU_PTHREAD_MUTEX_INIT(&arg.mutex, NULL);
  464. STARPU_PTHREAD_COND_INIT(&arg.cond, NULL);
  465. if (!_starpu_attempt_to_submit_data_request_from_apps(handle, STARPU_R,
  466. _starpu_data_unregister_fetch_data_callback, &arg))
  467. {
  468. /* no one has locked this data yet, so we proceed immediately */
  469. struct _starpu_data_replicate *home_replicate = &handle->per_node[home_node];
  470. int ret = _starpu_fetch_data_on_node(handle, home_replicate, STARPU_R, 0, 0, NULL, NULL);
  471. STARPU_ASSERT(!ret);
  472. }
  473. else
  474. {
  475. STARPU_PTHREAD_MUTEX_LOCK(&arg.mutex);
  476. while (!arg.terminated)
  477. STARPU_PTHREAD_COND_WAIT(&arg.cond, &arg.mutex);
  478. STARPU_PTHREAD_MUTEX_UNLOCK(&arg.mutex);
  479. }
  480. STARPU_PTHREAD_MUTEX_DESTROY(&arg.mutex);
  481. STARPU_PTHREAD_COND_DESTROY(&arg.cond);
  482. _starpu_release_data_on_node(handle, 0, &handle->per_node[home_node]);
  483. }
  484. /* If this handle uses a multiformat interface, we may have to convert
  485. * this piece of data back into the CPU format.
  486. * XXX : This is quite hacky, could we submit a task instead ?
  487. */
  488. if (_starpu_data_is_multiformat_handle(handle) &&
  489. ( starpu_node_get_kind(handle->mf_node) != STARPU_CPU_RAM
  490. && starpu_node_get_kind(handle->mf_node) != STARPU_SCC_RAM
  491. && starpu_node_get_kind(handle->mf_node) != STARPU_SCC_SHM
  492. ))
  493. {
  494. _STARPU_DEBUG("Conversion needed\n");
  495. void *buffers[1];
  496. struct starpu_multiformat_interface *format_interface;
  497. format_interface = (struct starpu_multiformat_interface *) starpu_data_get_interface_on_node(handle, 0);
  498. struct starpu_codelet *cl = NULL;
  499. enum starpu_node_kind node_kind = starpu_node_get_kind(handle->mf_node);
  500. switch (node_kind)
  501. {
  502. #ifdef STARPU_USE_CUDA
  503. case STARPU_CUDA_RAM:
  504. {
  505. struct starpu_multiformat_data_interface_ops *mf_ops;
  506. mf_ops = (struct starpu_multiformat_data_interface_ops *) handle->ops->get_mf_ops(format_interface);
  507. cl = mf_ops->cuda_to_cpu_cl;
  508. break;
  509. }
  510. #endif
  511. #ifdef STARPU_USE_OPENCL
  512. case STARPU_OPENCL_RAM:
  513. {
  514. struct starpu_multiformat_data_interface_ops *mf_ops;
  515. mf_ops = (struct starpu_multiformat_data_interface_ops *) handle->ops->get_mf_ops(format_interface);
  516. cl = mf_ops->opencl_to_cpu_cl;
  517. break;
  518. }
  519. #endif
  520. #ifdef STARPU_USE_MIC
  521. case STARPU_MIC_RAM:
  522. {
  523. struct starpu_multiformat_data_interface_ops *mf_ops;
  524. mf_ops = (struct starpu_multiformat_data_interface_ops *) handle->ops->get_mf_ops(format_interface);
  525. cl = mf_ops->mic_to_cpu_cl;
  526. break;
  527. }
  528. #endif
  529. case STARPU_CPU_RAM: /* Impossible ! */
  530. case STARPU_SCC_RAM: /* Impossible ! */
  531. case STARPU_SCC_SHM: /* Impossible ! */
  532. default:
  533. STARPU_ABORT();
  534. }
  535. buffers[0] = format_interface;
  536. _starpu_cl_func_t func = _starpu_task_get_cpu_nth_implementation(cl, 0);
  537. STARPU_ASSERT(func);
  538. func(buffers, NULL);
  539. }
  540. }
  541. _starpu_spin_lock(&handle->header_lock);
  542. if (!coherent)
  543. {
  544. /* Should we postpone the unregister operation ? */
  545. if ((handle->busy_count > 0) && handle->lazy_unregister)
  546. {
  547. _starpu_spin_unlock(&handle->header_lock);
  548. return;
  549. }
  550. }
  551. /* Tell holders of references that we're starting waiting */
  552. handle->busy_waiting = 1;
  553. _starpu_spin_unlock(&handle->header_lock);
  554. /* Wait for all requests to finish (notably WT requests) */
  555. /* Note: we here tell valgrind that reading busy_count is as
  556. * safe is if we had the lock held */
  557. _STARPU_VALGRIND_HG_SPIN_LOCK_PRE(&handle->header_lock);
  558. _STARPU_VALGRIND_HG_SPIN_LOCK_POST(&handle->header_lock);
  559. STARPU_PTHREAD_MUTEX_LOCK(&handle->busy_mutex);
  560. while (1) {
  561. int busy;
  562. busy = handle->busy_count;
  563. if (!busy)
  564. break;
  565. /* This is woken by _starpu_data_check_not_busy, always called
  566. * after decrementing busy_count */
  567. STARPU_PTHREAD_COND_WAIT(&handle->busy_cond, &handle->busy_mutex);
  568. }
  569. STARPU_PTHREAD_MUTEX_UNLOCK(&handle->busy_mutex);
  570. _STARPU_VALGRIND_HG_SPIN_UNLOCK_PRE(&handle->header_lock);
  571. _STARPU_VALGRIND_HG_SPIN_UNLOCK_POST(&handle->header_lock);
  572. /* Wait for finished requests to release the handle */
  573. _starpu_spin_lock(&handle->header_lock);
  574. size_t size = _starpu_data_get_size(handle);
  575. _starpu_data_free_interfaces(handle);
  576. /* Destroy the data now */
  577. unsigned node;
  578. for (node = 0; node < STARPU_MAXNODES; node++)
  579. {
  580. struct _starpu_data_replicate *local = &handle->per_node[node];
  581. /* free the data copy in a lazy fashion */
  582. if (local->allocated && local->automatically_allocated)
  583. _starpu_request_mem_chunk_removal(handle, local, node, size);
  584. }
  585. unsigned worker;
  586. unsigned nworkers = starpu_worker_get_count();
  587. for (worker = 0; worker < nworkers; worker++)
  588. {
  589. struct _starpu_data_replicate *local = &handle->per_worker[worker];
  590. /* free the data copy in a lazy fashion */
  591. if (local->allocated && local->automatically_allocated)
  592. _starpu_request_mem_chunk_removal(handle, local, starpu_worker_get_memory_node(worker), size);
  593. }
  594. _starpu_memory_stats_free(handle);
  595. _starpu_data_requester_list_delete(handle->req_list);
  596. _starpu_data_requester_list_delete(handle->reduction_req_list);
  597. _starpu_spin_unlock(&handle->header_lock);
  598. _starpu_spin_destroy(&handle->header_lock);
  599. STARPU_PTHREAD_MUTEX_DESTROY(&handle->busy_mutex);
  600. STARPU_PTHREAD_COND_DESTROY(&handle->busy_cond);
  601. STARPU_PTHREAD_MUTEX_DESTROY(&handle->sequential_consistency_mutex);
  602. starpu_data_release_tag(handle);
  603. free(handle);
  604. }
  605. void starpu_data_unregister(starpu_data_handle_t handle)
  606. {
  607. STARPU_ASSERT_MSG(!handle->lazy_unregister, "data %p can not be unregistered twice", handle);
  608. _starpu_data_unregister(handle, 1);
  609. }
  610. void starpu_data_unregister_no_coherency(starpu_data_handle_t handle)
  611. {
  612. _starpu_data_unregister(handle, 0);
  613. }
  614. void starpu_data_unregister_submit(starpu_data_handle_t handle)
  615. {
  616. _starpu_spin_lock(&handle->header_lock);
  617. STARPU_ASSERT_MSG(!handle->lazy_unregister, "data %p can not be unregistered twice", handle);
  618. handle->lazy_unregister = 1;
  619. _starpu_spin_unlock(&handle->header_lock);
  620. _starpu_data_unregister(handle, 0);
  621. }
  622. static void _starpu_data_invalidate(void *data)
  623. {
  624. starpu_data_handle_t handle = data;
  625. size_t size = _starpu_data_get_size(handle);
  626. _starpu_spin_lock(&handle->header_lock);
  627. unsigned node;
  628. for (node = 0; node < STARPU_MAXNODES; node++)
  629. {
  630. struct _starpu_data_replicate *local = &handle->per_node[node];
  631. if (local->mc && local->allocated && local->automatically_allocated)
  632. /* free the data copy in a lazy fashion */
  633. _starpu_request_mem_chunk_removal(handle, local, node, size);
  634. local->state = STARPU_INVALID;
  635. }
  636. unsigned worker;
  637. unsigned nworkers = starpu_worker_get_count();
  638. for (worker = 0; worker < nworkers; worker++)
  639. {
  640. struct _starpu_data_replicate *local = &handle->per_worker[worker];
  641. if (local->mc && local->allocated && local->automatically_allocated)
  642. /* free the data copy in a lazy fashion */
  643. _starpu_request_mem_chunk_removal(handle, local, starpu_worker_get_memory_node(worker), size);
  644. local->state = STARPU_INVALID;
  645. }
  646. _starpu_spin_unlock(&handle->header_lock);
  647. starpu_data_release(handle);
  648. }
  649. void starpu_data_invalidate(starpu_data_handle_t handle)
  650. {
  651. STARPU_ASSERT(handle);
  652. starpu_data_acquire(handle, STARPU_W);
  653. _starpu_data_invalidate(handle);
  654. }
  655. void starpu_data_invalidate_submit(starpu_data_handle_t handle)
  656. {
  657. STARPU_ASSERT(handle);
  658. starpu_data_acquire_cb(handle, STARPU_W, _starpu_data_invalidate, handle);
  659. }
  660. enum starpu_data_interface_id starpu_data_get_interface_id(starpu_data_handle_t handle)
  661. {
  662. return handle->ops->interfaceid;
  663. }
  664. void *starpu_data_get_interface_on_node(starpu_data_handle_t handle, unsigned memory_node)
  665. {
  666. return handle->per_node[memory_node].data_interface;
  667. }
  668. int starpu_data_interface_get_next_id(void)
  669. {
  670. _data_interface_number += 1;
  671. return _data_interface_number-1;
  672. }
  673. int starpu_data_pack(starpu_data_handle_t handle, void **ptr, starpu_ssize_t *count)
  674. {
  675. STARPU_ASSERT(handle->ops->pack_data);
  676. return handle->ops->pack_data(handle, _starpu_memory_node_get_local_key(), ptr, count);
  677. }
  678. int starpu_data_unpack(starpu_data_handle_t handle, void *ptr, size_t count)
  679. {
  680. STARPU_ASSERT(handle->ops->unpack_data);
  681. int ret;
  682. ret = handle->ops->unpack_data(handle, _starpu_memory_node_get_local_key(), ptr, count);
  683. free(ptr);
  684. return ret;
  685. }
  686. size_t starpu_data_get_size(starpu_data_handle_t handle)
  687. {
  688. return handle->ops->get_size(handle);
  689. }