data_interface.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-2016 Université de Bordeaux
  4. * Copyright (C) 2010, 2011, 2012, 2013, 2014, 2015 CNRS
  5. * Copyright (C) 2014 INRIA
  6. *
  7. * StarPU is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation; either version 2.1 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * StarPU is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. *
  16. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  17. */
  18. #include <stdint.h>
  19. #include <datawizard/datawizard.h>
  20. #include <datawizard/memory_nodes.h>
  21. #include <core/dependencies/data_concurrency.h>
  22. #include <common/uthash.h>
  23. #include <common/starpu_spinlock.h>
  24. #include <core/task.h>
  25. #include <core/workers.h>
  26. #include <datawizard/memstats.h>
  27. #ifdef STARPU_OPENMP
  28. #include <util/openmp_runtime_support.h>
  29. #endif
  30. /* Entry in the `registered_handles' hash table. */
  31. struct handle_entry
  32. {
  33. UT_hash_handle hh;
  34. void *pointer;
  35. starpu_data_handle_t handle;
  36. };
  37. /* Hash table mapping host pointers to data handles. */
  38. static int nregistered, maxnregistered;
  39. static struct handle_entry *registered_handles;
  40. static struct _starpu_spinlock registered_handles_lock;
  41. static int _data_interface_number = STARPU_MAX_INTERFACE_ID;
  42. starpu_arbiter_t _starpu_global_arbiter;
  43. static void _starpu_data_unregister(starpu_data_handle_t handle, unsigned coherent, unsigned nowait);
  44. void _starpu_data_interface_init(void)
  45. {
  46. _starpu_spin_init(&registered_handles_lock);
  47. /* Just for testing purpose */
  48. if (starpu_get_env_number_default("STARPU_GLOBAL_ARBITER", 0) > 0)
  49. _starpu_global_arbiter = starpu_arbiter_create();
  50. }
  51. void _starpu_data_interface_shutdown()
  52. {
  53. struct handle_entry *entry, *tmp;
  54. if (registered_handles)
  55. {
  56. _STARPU_DISP("[warning] The application has not unregistered all data handles.\n");
  57. }
  58. _starpu_spin_destroy(&registered_handles_lock);
  59. HASH_ITER(hh, registered_handles, entry, tmp)
  60. {
  61. HASH_DEL(registered_handles, entry);
  62. nregistered--;
  63. free(entry);
  64. }
  65. if (starpu_get_env_number_default("STARPU_MAX_MEMORY_USE", 0))
  66. _STARPU_DISP("Memory used for %d data handles: %lu MiB\n", maxnregistered, (unsigned long) (maxnregistered * sizeof(struct _starpu_data_state)) >> 20);
  67. STARPU_ASSERT(nregistered == 0);
  68. registered_handles = NULL;
  69. }
  70. #ifdef STARPU_OPENMP
  71. void _starpu_omp_unregister_region_handles(struct starpu_omp_region *region)
  72. {
  73. _starpu_spin_lock(&region->registered_handles_lock);
  74. struct handle_entry *entry, *tmp;
  75. HASH_ITER(hh, (region->registered_handles), entry, tmp)
  76. {
  77. entry->handle->removed_from_context_hash = 1;
  78. HASH_DEL(region->registered_handles, entry);
  79. starpu_data_unregister(entry->handle);
  80. free(entry);
  81. }
  82. _starpu_spin_unlock(&region->registered_handles_lock);
  83. }
  84. void _starpu_omp_unregister_task_handles(struct starpu_omp_task *task)
  85. {
  86. struct handle_entry *entry, *tmp;
  87. HASH_ITER(hh, task->registered_handles, entry, tmp)
  88. {
  89. entry->handle->removed_from_context_hash = 1;
  90. HASH_DEL(task->registered_handles, entry);
  91. starpu_data_unregister(entry->handle);
  92. free(entry);
  93. }
  94. }
  95. #endif
  96. struct starpu_data_interface_ops *_starpu_data_interface_get_ops(unsigned interface_id)
  97. {
  98. switch (interface_id)
  99. {
  100. case STARPU_MATRIX_INTERFACE_ID:
  101. return &starpu_interface_matrix_ops;
  102. case STARPU_BLOCK_INTERFACE_ID:
  103. return &starpu_interface_block_ops;
  104. case STARPU_VECTOR_INTERFACE_ID:
  105. return &starpu_interface_vector_ops;
  106. case STARPU_CSR_INTERFACE_ID:
  107. return &starpu_interface_csr_ops;
  108. case STARPU_BCSR_INTERFACE_ID:
  109. return &starpu_interface_bcsr_ops;
  110. case STARPU_VARIABLE_INTERFACE_ID:
  111. return &starpu_interface_variable_ops;
  112. case STARPU_VOID_INTERFACE_ID:
  113. return &starpu_interface_void_ops;
  114. case STARPU_MULTIFORMAT_INTERFACE_ID:
  115. return &starpu_interface_multiformat_ops;
  116. default:
  117. STARPU_ABORT();
  118. return NULL;
  119. }
  120. }
  121. /* Register the mapping from PTR to HANDLE. If PTR is already mapped to
  122. * some handle, the new mapping shadows the previous one. */
  123. void _starpu_data_register_ram_pointer(starpu_data_handle_t handle, void *ptr)
  124. {
  125. struct handle_entry *entry;
  126. entry = (struct handle_entry *) malloc(sizeof(*entry));
  127. STARPU_ASSERT(entry != NULL);
  128. entry->pointer = ptr;
  129. entry->handle = handle;
  130. #ifdef STARPU_OPENMP
  131. struct starpu_omp_task *task = _starpu_omp_get_task();
  132. if (task)
  133. {
  134. if (task->flags & STARPU_OMP_TASK_FLAGS_IMPLICIT)
  135. {
  136. struct starpu_omp_region *parallel_region = task->owner_region;
  137. _starpu_spin_lock(&parallel_region->registered_handles_lock);
  138. HASH_ADD_PTR(parallel_region->registered_handles, pointer, entry);
  139. _starpu_spin_unlock(&parallel_region->registered_handles_lock);
  140. }
  141. else
  142. {
  143. HASH_ADD_PTR(task->registered_handles, pointer, entry);
  144. }
  145. }
  146. else
  147. #endif
  148. {
  149. _starpu_spin_lock(&registered_handles_lock);
  150. nregistered++;
  151. if (nregistered > maxnregistered)
  152. maxnregistered = nregistered;
  153. HASH_ADD_PTR(registered_handles, pointer, entry);
  154. _starpu_spin_unlock(&registered_handles_lock);
  155. }
  156. }
  157. starpu_data_handle_t starpu_data_lookup(const void *ptr)
  158. {
  159. starpu_data_handle_t result;
  160. #ifdef STARPU_OPENMP
  161. struct starpu_omp_task *task = _starpu_omp_get_task();
  162. if (task)
  163. {
  164. if (task->flags & STARPU_OMP_TASK_FLAGS_IMPLICIT)
  165. {
  166. struct starpu_omp_region *parallel_region = task->owner_region;
  167. _starpu_spin_lock(&parallel_region->registered_handles_lock);
  168. {
  169. struct handle_entry *entry;
  170. HASH_FIND_PTR(parallel_region->registered_handles, &ptr, entry);
  171. if(STARPU_UNLIKELY(entry == NULL))
  172. result = NULL;
  173. else
  174. result = entry->handle;
  175. }
  176. _starpu_spin_unlock(&parallel_region->registered_handles_lock);
  177. }
  178. else
  179. {
  180. struct handle_entry *entry;
  181. HASH_FIND_PTR(task->registered_handles, &ptr, entry);
  182. if(STARPU_UNLIKELY(entry == NULL))
  183. result = NULL;
  184. else
  185. result = entry->handle;
  186. }
  187. }
  188. else
  189. #endif
  190. {
  191. _starpu_spin_lock(&registered_handles_lock);
  192. {
  193. struct handle_entry *entry;
  194. HASH_FIND_PTR(registered_handles, &ptr, entry);
  195. if(STARPU_UNLIKELY(entry == NULL))
  196. result = NULL;
  197. else
  198. result = entry->handle;
  199. }
  200. _starpu_spin_unlock(&registered_handles_lock);
  201. }
  202. return result;
  203. }
  204. /*
  205. * Start monitoring a piece of data
  206. */
  207. static void _starpu_register_new_data(starpu_data_handle_t handle,
  208. unsigned home_node, uint32_t wt_mask)
  209. {
  210. void *ptr;
  211. STARPU_ASSERT(handle);
  212. /* initialize the new lock */
  213. _starpu_data_requester_list_init(&handle->req_list);
  214. handle->refcnt = 0;
  215. handle->unlocking_reqs = 0;
  216. handle->busy_count = 0;
  217. handle->busy_waiting = 0;
  218. STARPU_PTHREAD_MUTEX_INIT(&handle->busy_mutex, NULL);
  219. STARPU_PTHREAD_COND_INIT(&handle->busy_cond, NULL);
  220. _starpu_spin_init(&handle->header_lock);
  221. /* first take care to properly lock the data */
  222. _starpu_spin_lock(&handle->header_lock);
  223. /* there is no hierarchy yet */
  224. handle->nchildren = 0;
  225. handle->nplans = 0;
  226. handle->switch_cl = NULL;
  227. handle->partitioned = 0;
  228. handle->readonly = 0;
  229. handle->root_handle = handle;
  230. handle->father_handle = NULL;
  231. handle->sibling_index = 0; /* could be anything for the root */
  232. handle->depth = 1; /* the tree is just a node yet */
  233. handle->mpi_data = NULL; /* invalid until set */
  234. handle->is_not_important = 0;
  235. handle->sequential_consistency =
  236. starpu_data_get_default_sequential_consistency_flag();
  237. STARPU_PTHREAD_MUTEX_INIT(&handle->sequential_consistency_mutex, NULL);
  238. handle->last_submitted_mode = STARPU_R;
  239. handle->last_sync_task = NULL;
  240. handle->last_submitted_accessors.task = NULL;
  241. handle->last_submitted_accessors.next = &handle->last_submitted_accessors;
  242. handle->last_submitted_accessors.prev = &handle->last_submitted_accessors;
  243. handle->post_sync_tasks = NULL;
  244. /* Tell helgrind that the race in _starpu_unlock_post_sync_tasks is fine */
  245. STARPU_HG_DISABLE_CHECKING(handle->post_sync_tasks_cnt);
  246. handle->post_sync_tasks_cnt = 0;
  247. /* By default, there are no methods available to perform a reduction */
  248. handle->redux_cl = NULL;
  249. handle->init_cl = NULL;
  250. handle->reduction_refcnt = 0;
  251. _starpu_data_requester_list_init(&handle->reduction_req_list);
  252. handle->reduction_tmp_handles = NULL;
  253. handle->write_invalidation_req = NULL;
  254. #ifdef STARPU_USE_FXT
  255. handle->last_submitted_ghost_sync_id_is_valid = 0;
  256. handle->last_submitted_ghost_sync_id = 0;
  257. handle->last_submitted_ghost_accessors_id = NULL;
  258. #endif
  259. handle->wt_mask = wt_mask;
  260. /* Store some values directly in the handle not to recompute them all
  261. * the time. */
  262. handle->footprint = _starpu_compute_data_footprint(handle);
  263. handle->home_node = home_node;
  264. if (_starpu_global_arbiter)
  265. /* Just for testing purpose */
  266. starpu_data_assign_arbiter(handle, _starpu_global_arbiter);
  267. else
  268. handle->arbiter = NULL;
  269. _starpu_data_requester_list_init(&handle->arbitered_req_list);
  270. handle->last_locality = -1;
  271. /* that new data is invalid from all nodes perpective except for the
  272. * home node */
  273. unsigned node;
  274. for (node = 0; node < STARPU_MAXNODES; node++)
  275. {
  276. struct _starpu_data_replicate *replicate;
  277. replicate = &handle->per_node[node];
  278. replicate->memory_node = node;
  279. replicate->relaxed_coherency = 0;
  280. replicate->refcnt = 0;
  281. if (node == home_node)
  282. {
  283. /* this is the home node with the only valid copy */
  284. replicate->state = STARPU_OWNER;
  285. replicate->allocated = 1;
  286. replicate->automatically_allocated = 0;
  287. replicate->initialized = 1;
  288. }
  289. else
  290. {
  291. /* the value is not available here yet */
  292. replicate->state = STARPU_INVALID;
  293. replicate->allocated = 0;
  294. replicate->initialized = 0;
  295. }
  296. }
  297. handle->per_worker = NULL;
  298. /* now the data is available ! */
  299. _starpu_spin_unlock(&handle->header_lock);
  300. ptr = starpu_data_handle_to_pointer(handle, STARPU_MAIN_RAM);
  301. if (ptr != NULL)
  302. {
  303. _starpu_data_register_ram_pointer(handle, ptr);
  304. }
  305. }
  306. void
  307. _starpu_data_initialize_per_worker(starpu_data_handle_t handle)
  308. {
  309. unsigned worker;
  310. unsigned nworkers = starpu_worker_get_count();
  311. _starpu_spin_checklocked(&handle->header_lock);
  312. handle->per_worker = calloc(nworkers, sizeof(*handle->per_worker));
  313. size_t interfacesize = handle->ops->interface_size;
  314. for (worker = 0; worker < nworkers; worker++)
  315. {
  316. struct _starpu_data_replicate *replicate;
  317. unsigned node;
  318. replicate = &handle->per_worker[worker];
  319. replicate->allocated = 0;
  320. replicate->automatically_allocated = 0;
  321. replicate->state = STARPU_INVALID;
  322. replicate->refcnt = 0;
  323. replicate->handle = handle;
  324. replicate->requested = 0;
  325. for (node = 0; node < STARPU_MAXNODES; node++)
  326. {
  327. replicate->request[node] = NULL;
  328. }
  329. /* Assuming being used for SCRATCH for now, patched when entering REDUX mode */
  330. replicate->relaxed_coherency = 1;
  331. replicate->initialized = 0;
  332. replicate->memory_node = starpu_worker_get_memory_node(worker);
  333. replicate->data_interface = calloc(1, interfacesize);
  334. STARPU_ASSERT(replicate->data_interface);
  335. /* duplicate the content of the interface on node 0 */
  336. memcpy(replicate->data_interface, handle->per_node[STARPU_MAIN_RAM].data_interface, interfacesize);
  337. }
  338. }
  339. void starpu_data_ptr_register(starpu_data_handle_t handle, unsigned node)
  340. {
  341. struct _starpu_data_replicate *replicate = &handle->per_node[node];
  342. _starpu_spin_lock(&handle->header_lock);
  343. STARPU_ASSERT_MSG(replicate->allocated == 0, "starpu_data_ptr_register must be called right after starpu_data_register");
  344. replicate->allocated = 1;
  345. replicate->automatically_allocated = 0;
  346. _starpu_spin_unlock(&handle->header_lock);
  347. }
  348. int _starpu_data_handle_init(starpu_data_handle_t handle, struct starpu_data_interface_ops *interface_ops, unsigned int mf_node)
  349. {
  350. unsigned node;
  351. /* Tell helgrind that our access to busy_count in
  352. * starpu_data_unregister is actually safe */
  353. STARPU_HG_DISABLE_CHECKING(handle->busy_count);
  354. handle->magic = 42;
  355. handle->ops = interface_ops;
  356. handle->mf_node = mf_node;
  357. handle->mpi_data = NULL;
  358. size_t interfacesize = interface_ops->interface_size;
  359. _starpu_memory_stats_init(handle);
  360. for (node = 0; node < STARPU_MAXNODES; node++)
  361. {
  362. _starpu_memory_stats_init_per_node(handle, node);
  363. struct _starpu_data_replicate *replicate;
  364. replicate = &handle->per_node[node];
  365. /* relaxed_coherency = 0 */
  366. replicate->handle = handle;
  367. replicate->data_interface = calloc(1, interfacesize);
  368. STARPU_ASSERT(replicate->data_interface);
  369. }
  370. return 0;
  371. }
  372. static
  373. starpu_data_handle_t _starpu_data_handle_allocate(struct starpu_data_interface_ops *interface_ops, unsigned int mf_node)
  374. {
  375. starpu_data_handle_t handle = (starpu_data_handle_t) calloc(1, sizeof(struct _starpu_data_state));
  376. STARPU_ASSERT(handle);
  377. _starpu_data_handle_init(handle, interface_ops, mf_node);
  378. return handle;
  379. }
  380. void starpu_data_register(starpu_data_handle_t *handleptr, unsigned home_node,
  381. void *data_interface,
  382. struct starpu_data_interface_ops *ops)
  383. {
  384. starpu_data_handle_t handle = _starpu_data_handle_allocate(ops, home_node);
  385. _STARPU_TRACE_HANDLE_DATA_REGISTER(handle);
  386. STARPU_ASSERT(handleptr);
  387. *handleptr = handle;
  388. /* fill the interface fields with the appropriate method */
  389. STARPU_ASSERT(ops->register_data_handle);
  390. ops->register_data_handle(handle, home_node, data_interface);
  391. _starpu_register_new_data(handle, home_node, 0);
  392. }
  393. void starpu_data_register_same(starpu_data_handle_t *handledst, starpu_data_handle_t handlesrc)
  394. {
  395. void *local_interface = starpu_data_get_interface_on_node(handlesrc, STARPU_MAIN_RAM);
  396. starpu_data_register(handledst, -1, local_interface, handlesrc->ops);
  397. }
  398. void *starpu_data_handle_to_pointer(starpu_data_handle_t handle, unsigned node)
  399. {
  400. /* Check whether the operation is supported and the node has actually
  401. * been allocated. */
  402. if (handle->ops->handle_to_pointer
  403. && starpu_data_test_if_allocated_on_node(handle, node))
  404. {
  405. return handle->ops->handle_to_pointer(handle, node);
  406. }
  407. return NULL;
  408. }
  409. void *starpu_data_get_local_ptr(starpu_data_handle_t handle)
  410. {
  411. return starpu_data_handle_to_pointer(handle,
  412. _starpu_memory_node_get_local_key());
  413. }
  414. struct starpu_data_interface_ops* starpu_data_get_interface_ops(starpu_data_handle_t handle)
  415. {
  416. return handle->ops;
  417. }
  418. /*
  419. * Stop monitoring a piece of data
  420. */
  421. void _starpu_data_unregister_ram_pointer(starpu_data_handle_t handle)
  422. {
  423. const void *ram_ptr = starpu_data_handle_to_pointer(handle, STARPU_MAIN_RAM);
  424. #ifdef STARPU_OPENMP
  425. if (handle->removed_from_context_hash)
  426. return;
  427. #endif
  428. if (ram_ptr != NULL)
  429. {
  430. /* Remove the PTR -> HANDLE mapping. If a mapping from PTR
  431. * to another handle existed before (e.g., when using
  432. * filters), it becomes visible again. */
  433. struct handle_entry *entry;
  434. #ifdef STARPU_OPENMP
  435. struct starpu_omp_task *task = _starpu_omp_get_task();
  436. if (task)
  437. {
  438. if (task->flags & STARPU_OMP_TASK_FLAGS_IMPLICIT)
  439. {
  440. struct starpu_omp_region *parallel_region = task->owner_region;
  441. _starpu_spin_lock(&parallel_region->registered_handles_lock);
  442. HASH_FIND_PTR(parallel_region->registered_handles, &ram_ptr, entry);
  443. STARPU_ASSERT(entry != NULL);
  444. HASH_DEL(registered_handles, entry);
  445. _starpu_spin_unlock(&parallel_region->registered_handles_lock);
  446. }
  447. else
  448. {
  449. HASH_FIND_PTR(task->registered_handles, &ram_ptr, entry);
  450. STARPU_ASSERT(entry != NULL);
  451. HASH_DEL(task->registered_handles, entry);
  452. }
  453. }
  454. else
  455. #endif
  456. {
  457. _starpu_spin_lock(&registered_handles_lock);
  458. HASH_FIND_PTR(registered_handles, &ram_ptr, entry);
  459. STARPU_ASSERT(entry != NULL);
  460. nregistered--;
  461. HASH_DEL(registered_handles, entry);
  462. _starpu_spin_unlock(&registered_handles_lock);
  463. }
  464. free(entry);
  465. }
  466. }
  467. void _starpu_data_free_interfaces(starpu_data_handle_t handle)
  468. {
  469. unsigned node;
  470. unsigned worker;
  471. unsigned nworkers = starpu_worker_get_count();
  472. for (node = 0; node < STARPU_MAXNODES; node++)
  473. free(handle->per_node[node].data_interface);
  474. if (handle->per_worker)
  475. {
  476. for (worker = 0; worker < nworkers; worker++)
  477. free(handle->per_worker[worker].data_interface);
  478. free(handle->per_worker);
  479. }
  480. }
  481. struct _starpu_unregister_callback_arg
  482. {
  483. unsigned memory_node;
  484. starpu_data_handle_t handle;
  485. unsigned terminated;
  486. starpu_pthread_mutex_t mutex;
  487. starpu_pthread_cond_t cond;
  488. };
  489. /* Check whether we should tell starpu_data_unregister that the data handle is
  490. * not busy any more.
  491. * The header is supposed to be locked.
  492. * This may free the handle, if it was lazily unregistered (1 is returned in
  493. * that case). The handle pointer thus becomes invalid for the caller.
  494. *
  495. * Note: we inline some of the tests in the _starpu_data_check_not_busy macro.
  496. */
  497. int __starpu_data_check_not_busy(starpu_data_handle_t handle)
  498. {
  499. if (STARPU_LIKELY(handle->busy_count))
  500. return 0;
  501. /* Not busy any more, perhaps have to unregister etc. */
  502. if (STARPU_UNLIKELY(handle->busy_waiting))
  503. {
  504. STARPU_PTHREAD_MUTEX_LOCK(&handle->busy_mutex);
  505. STARPU_PTHREAD_COND_BROADCAST(&handle->busy_cond);
  506. STARPU_PTHREAD_MUTEX_UNLOCK(&handle->busy_mutex);
  507. }
  508. /* The handle has been destroyed in between (eg. this was a temporary
  509. * handle created for a reduction.) */
  510. if (STARPU_UNLIKELY(handle->lazy_unregister))
  511. {
  512. handle->lazy_unregister = 0;
  513. _starpu_spin_unlock(&handle->header_lock);
  514. _starpu_data_unregister(handle, 0, 1);
  515. /* Warning: in case we unregister the handle, we must be sure
  516. * that the caller will not try to unlock the header after
  517. * !*/
  518. return 1;
  519. }
  520. return 0;
  521. }
  522. static
  523. void _starpu_check_if_valid_and_fetch_data_on_node(starpu_data_handle_t handle, struct _starpu_data_replicate *replicate, const char *origin)
  524. {
  525. unsigned node;
  526. unsigned nnodes = starpu_memory_nodes_get_count();
  527. int valid = 0;
  528. for (node = 0; node < nnodes; node++)
  529. {
  530. if (handle->per_node[node].state != STARPU_INVALID)
  531. {
  532. /* we found a copy ! */
  533. valid = 1;
  534. }
  535. }
  536. if (valid)
  537. {
  538. int ret = _starpu_fetch_data_on_node(handle, replicate, STARPU_R, 0, 0, 0, NULL, NULL, 0, origin);
  539. STARPU_ASSERT(!ret);
  540. _starpu_release_data_on_node(handle, 0, &handle->per_node[handle->home_node]);
  541. }
  542. else
  543. {
  544. _starpu_spin_lock(&handle->header_lock);
  545. if (!_starpu_notify_data_dependencies(handle))
  546. _starpu_spin_unlock(&handle->header_lock);
  547. }
  548. }
  549. static void _starpu_data_unregister_fetch_data_callback(void *_arg)
  550. {
  551. struct _starpu_unregister_callback_arg *arg = (struct _starpu_unregister_callback_arg *) _arg;
  552. starpu_data_handle_t handle = arg->handle;
  553. STARPU_ASSERT(handle);
  554. struct _starpu_data_replicate *replicate = &handle->per_node[arg->memory_node];
  555. _starpu_check_if_valid_and_fetch_data_on_node(handle, replicate, "_starpu_data_unregister_fetch_data_callback");
  556. /* unlock the caller */
  557. STARPU_PTHREAD_MUTEX_LOCK(&arg->mutex);
  558. arg->terminated = 1;
  559. STARPU_PTHREAD_COND_SIGNAL(&arg->cond);
  560. STARPU_PTHREAD_MUTEX_UNLOCK(&arg->mutex);
  561. }
  562. /* Unregister the data handle, perhaps we don't need to update the home_node
  563. * (in that case coherent is set to 0)
  564. * nowait is for internal use when we already know for sure that we won't have to wait.
  565. */
  566. static void _starpu_data_unregister(starpu_data_handle_t handle, unsigned coherent, unsigned nowait)
  567. {
  568. STARPU_ASSERT(handle);
  569. /* Prevent any further unregistration */
  570. handle->magic = 0;
  571. STARPU_ASSERT_MSG(handle->nchildren == 0, "data %p needs to be unpartitioned before unregistration", handle);
  572. STARPU_ASSERT_MSG(handle->nplans == 0, "data %p needs its partition plans to be cleaned before unregistration", handle);
  573. STARPU_ASSERT_MSG(handle->partitioned == 0, "data %p needs its partitioned plans to be unpartitioned before unregistration", handle);
  574. /* TODO: also check that it has the latest coherency */
  575. STARPU_ASSERT(!(nowait && handle->busy_count != 0));
  576. int sequential_consistency = handle->sequential_consistency;
  577. if (sequential_consistency && !nowait)
  578. {
  579. STARPU_ASSERT_MSG(_starpu_worker_may_perform_blocking_calls(), "starpu_data_unregister must not be called from a task or callback, perhaps you can use starpu_data_unregister_submit instead");
  580. /* If sequential consistency is enabled, wait until data is available */
  581. _starpu_data_wait_until_available(handle, STARPU_RW, "starpu_data_unregister");
  582. }
  583. if (coherent && !nowait)
  584. {
  585. STARPU_ASSERT_MSG(_starpu_worker_may_perform_blocking_calls(), "starpu_data_unregister must not be called from a task or callback, perhaps you can use starpu_data_unregister_submit instead");
  586. /* Fetch data in the home of the data to ensure we have a valid copy
  587. * where we registered it */
  588. int home_node = handle->home_node;
  589. if (home_node >= 0)
  590. {
  591. struct _starpu_unregister_callback_arg arg;
  592. arg.handle = handle;
  593. arg.memory_node = (unsigned)home_node;
  594. arg.terminated = 0;
  595. STARPU_PTHREAD_MUTEX_INIT(&arg.mutex, NULL);
  596. STARPU_PTHREAD_COND_INIT(&arg.cond, NULL);
  597. if (!_starpu_attempt_to_submit_data_request_from_apps(handle, STARPU_R,
  598. _starpu_data_unregister_fetch_data_callback, &arg))
  599. {
  600. /* no one has locked this data yet, so we proceed immediately */
  601. struct _starpu_data_replicate *home_replicate = &handle->per_node[home_node];
  602. _starpu_check_if_valid_and_fetch_data_on_node(handle, home_replicate, "_starpu_data_unregister");
  603. }
  604. else
  605. {
  606. STARPU_PTHREAD_MUTEX_LOCK(&arg.mutex);
  607. while (!arg.terminated)
  608. STARPU_PTHREAD_COND_WAIT(&arg.cond, &arg.mutex);
  609. STARPU_PTHREAD_MUTEX_UNLOCK(&arg.mutex);
  610. }
  611. STARPU_PTHREAD_MUTEX_DESTROY(&arg.mutex);
  612. STARPU_PTHREAD_COND_DESTROY(&arg.cond);
  613. }
  614. /* If this handle uses a multiformat interface, we may have to convert
  615. * this piece of data back into the CPU format.
  616. * XXX : This is quite hacky, could we submit a task instead ?
  617. */
  618. if (_starpu_data_is_multiformat_handle(handle) &&
  619. ( starpu_node_get_kind(handle->mf_node) != STARPU_CPU_RAM
  620. && starpu_node_get_kind(handle->mf_node) != STARPU_SCC_RAM
  621. && starpu_node_get_kind(handle->mf_node) != STARPU_SCC_SHM
  622. ))
  623. {
  624. _STARPU_DEBUG("Conversion needed\n");
  625. void *buffers[1];
  626. struct starpu_multiformat_interface *format_interface;
  627. format_interface = (struct starpu_multiformat_interface *) starpu_data_get_interface_on_node(handle, STARPU_MAIN_RAM);
  628. struct starpu_codelet *cl = NULL;
  629. enum starpu_node_kind node_kind = starpu_node_get_kind(handle->mf_node);
  630. switch (node_kind)
  631. {
  632. #ifdef STARPU_USE_CUDA
  633. case STARPU_CUDA_RAM:
  634. {
  635. struct starpu_multiformat_data_interface_ops *mf_ops;
  636. mf_ops = (struct starpu_multiformat_data_interface_ops *) handle->ops->get_mf_ops(format_interface);
  637. cl = mf_ops->cuda_to_cpu_cl;
  638. break;
  639. }
  640. #endif
  641. #ifdef STARPU_USE_OPENCL
  642. case STARPU_OPENCL_RAM:
  643. {
  644. struct starpu_multiformat_data_interface_ops *mf_ops;
  645. mf_ops = (struct starpu_multiformat_data_interface_ops *) handle->ops->get_mf_ops(format_interface);
  646. cl = mf_ops->opencl_to_cpu_cl;
  647. break;
  648. }
  649. #endif
  650. #ifdef STARPU_USE_MIC
  651. case STARPU_MIC_RAM:
  652. {
  653. struct starpu_multiformat_data_interface_ops *mf_ops;
  654. mf_ops = (struct starpu_multiformat_data_interface_ops *) handle->ops->get_mf_ops(format_interface);
  655. cl = mf_ops->mic_to_cpu_cl;
  656. break;
  657. }
  658. #endif
  659. case STARPU_CPU_RAM: /* Impossible ! */
  660. case STARPU_SCC_RAM: /* Impossible ! */
  661. case STARPU_SCC_SHM: /* Impossible ! */
  662. default:
  663. STARPU_ABORT();
  664. }
  665. buffers[0] = format_interface;
  666. _starpu_cl_func_t func = _starpu_task_get_cpu_nth_implementation(cl, 0);
  667. STARPU_ASSERT(func);
  668. func(buffers, NULL);
  669. }
  670. }
  671. _starpu_spin_lock(&handle->header_lock);
  672. if (!coherent)
  673. {
  674. /* Should we postpone the unregister operation ? */
  675. if ((handle->busy_count > 0) && handle->lazy_unregister)
  676. {
  677. _starpu_spin_unlock(&handle->header_lock);
  678. return;
  679. }
  680. }
  681. /* Tell holders of references that we're starting waiting */
  682. handle->busy_waiting = 1;
  683. _starpu_spin_unlock(&handle->header_lock);
  684. retry_busy:
  685. /* Wait for all requests to finish (notably WT requests) */
  686. STARPU_PTHREAD_MUTEX_LOCK(&handle->busy_mutex);
  687. while (1)
  688. {
  689. /* Here helgrind would shout that this an unprotected access,
  690. * but this is actually fine: all threads who do busy_count--
  691. * are supposed to call _starpu_data_check_not_busy, which will
  692. * wake us up through the busy_mutex/busy_cond. */
  693. if (!handle->busy_count)
  694. break;
  695. /* This is woken by _starpu_data_check_not_busy, always called
  696. * after decrementing busy_count */
  697. STARPU_PTHREAD_COND_WAIT(&handle->busy_cond, &handle->busy_mutex);
  698. }
  699. STARPU_PTHREAD_MUTEX_UNLOCK(&handle->busy_mutex);
  700. /* Wait for finished requests to release the handle */
  701. _starpu_spin_lock(&handle->header_lock);
  702. if (handle->busy_count)
  703. {
  704. /* Bad luck: some request went in in between, wait again... */
  705. _starpu_spin_unlock(&handle->header_lock);
  706. goto retry_busy;
  707. }
  708. size_t size = _starpu_data_get_size(handle);
  709. _starpu_data_unregister_ram_pointer(handle);
  710. /* Destroy the data now */
  711. unsigned node;
  712. for (node = 0; node < STARPU_MAXNODES; node++)
  713. {
  714. struct _starpu_data_replicate *local = &handle->per_node[node];
  715. /* free the data copy in a lazy fashion */
  716. if (local->allocated && local->automatically_allocated)
  717. _starpu_request_mem_chunk_removal(handle, local, node, size);
  718. }
  719. unsigned worker;
  720. unsigned nworkers = starpu_worker_get_count();
  721. if (handle->per_worker)
  722. for (worker = 0; worker < nworkers; worker++)
  723. {
  724. struct _starpu_data_replicate *local = &handle->per_worker[worker];
  725. /* free the data copy in a lazy fashion */
  726. if (local->allocated && local->automatically_allocated)
  727. _starpu_request_mem_chunk_removal(handle, local, starpu_worker_get_memory_node(worker), size);
  728. }
  729. _starpu_data_free_interfaces(handle);
  730. _starpu_memory_stats_free(handle);
  731. _starpu_spin_unlock(&handle->header_lock);
  732. _starpu_spin_destroy(&handle->header_lock);
  733. _starpu_data_clear_implicit(handle);
  734. STARPU_PTHREAD_MUTEX_DESTROY(&handle->busy_mutex);
  735. STARPU_PTHREAD_COND_DESTROY(&handle->busy_cond);
  736. STARPU_PTHREAD_MUTEX_DESTROY(&handle->sequential_consistency_mutex);
  737. STARPU_HG_ENABLE_CHECKING(handle->post_sync_tasks_cnt);
  738. STARPU_HG_ENABLE_CHECKING(handle->busy_count);
  739. if (handle->switch_cl)
  740. {
  741. free(handle->switch_cl->dyn_nodes);
  742. free(handle->switch_cl);
  743. }
  744. free(handle);
  745. }
  746. void starpu_data_unregister(starpu_data_handle_t handle)
  747. {
  748. STARPU_ASSERT_MSG(handle->magic == 42, "data %p is invalid (was it already registered?)", handle);
  749. STARPU_ASSERT_MSG(!handle->lazy_unregister, "data %p can not be unregistered twice", handle);
  750. if (handle->unregister_hook)
  751. {
  752. handle->unregister_hook(handle);
  753. }
  754. _starpu_data_unregister(handle, 1, 0);
  755. }
  756. void starpu_data_unregister_no_coherency(starpu_data_handle_t handle)
  757. {
  758. STARPU_ASSERT_MSG(handle->magic == 42, "data %p is invalid (was it already registered?)", handle);
  759. if (handle->unregister_hook)
  760. {
  761. handle->unregister_hook(handle);
  762. }
  763. _starpu_data_unregister(handle, 0, 0);
  764. }
  765. static void _starpu_data_unregister_submit_cb(void *arg)
  766. {
  767. starpu_data_handle_t handle = arg;
  768. _starpu_spin_lock(&handle->header_lock);
  769. handle->lazy_unregister = 1;
  770. /* The handle should be busy since we are working on it.
  771. * when we releases the handle below, it will be destroyed by
  772. * _starpu_data_check_not_busy */
  773. STARPU_ASSERT(handle->busy_count);
  774. _starpu_spin_unlock(&handle->header_lock);
  775. starpu_data_release_on_node(handle, -1);
  776. }
  777. void starpu_data_unregister_submit(starpu_data_handle_t handle)
  778. {
  779. STARPU_ASSERT_MSG(handle->magic == 42, "data %p is invalid (was it already registered?)", handle);
  780. STARPU_ASSERT_MSG(!handle->lazy_unregister, "data %p can not be unregistered twice", handle);
  781. if (handle->unregister_hook)
  782. {
  783. handle->unregister_hook(handle);
  784. }
  785. /* Wait for all task dependencies on this handle before putting it for free */
  786. starpu_data_acquire_on_node_cb(handle, -1, STARPU_RW, _starpu_data_unregister_submit_cb, handle);
  787. }
  788. static void _starpu_data_invalidate(void *data)
  789. {
  790. starpu_data_handle_t handle = data;
  791. size_t size = _starpu_data_get_size(handle);
  792. _starpu_spin_lock(&handle->header_lock);
  793. _STARPU_DEBUG("Really invalidating data %p\n", data);
  794. #ifdef STARPU_DEBUG
  795. {
  796. /* There shouldn't be any pending request since we acquired the data in W mode */
  797. unsigned i, j, nnodes = starpu_memory_nodes_get_count();
  798. for (i = 0; i < nnodes; i++)
  799. for (j = 0; j < nnodes; j++)
  800. STARPU_ASSERT_MSG(!handle->per_node[i].request[j], "request for handle %p pending from %d to %d while invalidating data!", handle, j, i);
  801. }
  802. #endif
  803. unsigned node;
  804. for (node = 0; node < STARPU_MAXNODES; node++)
  805. {
  806. struct _starpu_data_replicate *local = &handle->per_node[node];
  807. if (local->mc && local->allocated && local->automatically_allocated)
  808. {
  809. if (node == STARPU_MAIN_RAM)
  810. _starpu_data_unregister_ram_pointer(handle);
  811. /* free the data copy in a lazy fashion */
  812. _starpu_request_mem_chunk_removal(handle, local, node, size);
  813. }
  814. if (local->state != STARPU_INVALID)
  815. _STARPU_TRACE_DATA_INVALIDATE(handle, node);
  816. local->state = STARPU_INVALID;
  817. }
  818. unsigned worker;
  819. unsigned nworkers = starpu_worker_get_count();
  820. if (handle->per_worker)
  821. for (worker = 0; worker < nworkers; worker++)
  822. {
  823. struct _starpu_data_replicate *local = &handle->per_worker[worker];
  824. if (local->mc && local->allocated && local->automatically_allocated)
  825. /* free the data copy in a lazy fashion */
  826. _starpu_request_mem_chunk_removal(handle, local, starpu_worker_get_memory_node(worker), size);
  827. local->state = STARPU_INVALID;
  828. }
  829. _starpu_spin_unlock(&handle->header_lock);
  830. starpu_data_release_on_node(handle, -1);
  831. }
  832. void starpu_data_invalidate(starpu_data_handle_t handle)
  833. {
  834. STARPU_ASSERT(handle);
  835. starpu_data_acquire_on_node(handle, -1, STARPU_W);
  836. _starpu_data_invalidate(handle);
  837. }
  838. void starpu_data_invalidate_submit(starpu_data_handle_t handle)
  839. {
  840. STARPU_ASSERT(handle);
  841. starpu_data_acquire_on_node_cb(handle, -1, STARPU_W, _starpu_data_invalidate, handle);
  842. }
  843. enum starpu_data_interface_id starpu_data_get_interface_id(starpu_data_handle_t handle)
  844. {
  845. return handle->ops->interfaceid;
  846. }
  847. void *starpu_data_get_interface_on_node(starpu_data_handle_t handle, unsigned memory_node)
  848. {
  849. return handle->per_node[memory_node].data_interface;
  850. }
  851. int starpu_data_interface_get_next_id(void)
  852. {
  853. _data_interface_number += 1;
  854. return _data_interface_number-1;
  855. }
  856. int starpu_data_pack(starpu_data_handle_t handle, void **ptr, starpu_ssize_t *count)
  857. {
  858. STARPU_ASSERT(handle->ops->pack_data);
  859. return handle->ops->pack_data(handle, _starpu_memory_node_get_local_key(), ptr, count);
  860. }
  861. int starpu_data_unpack(starpu_data_handle_t handle, void *ptr, size_t count)
  862. {
  863. STARPU_ASSERT(handle->ops->unpack_data);
  864. int ret;
  865. ret = handle->ops->unpack_data(handle, _starpu_memory_node_get_local_key(), ptr, count);
  866. starpu_free_flags(ptr, count, 0);
  867. return ret;
  868. }
  869. size_t starpu_data_get_size(starpu_data_handle_t handle)
  870. {
  871. return handle->ops->get_size(handle);
  872. }