data_interface.c 31 KB

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