data_interface.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-2021 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
  4. *
  5. * StarPU is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation; either version 2.1 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * StarPU is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. *
  14. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  15. */
  16. #include <stdint.h>
  17. #include <stdarg.h>
  18. #include <datawizard/datawizard.h>
  19. #include <datawizard/memory_nodes.h>
  20. #include <datawizard/memstats.h>
  21. #include <datawizard/malloc.h>
  22. #include <core/dependencies/data_concurrency.h>
  23. #include <common/uthash.h>
  24. #include <common/starpu_spinlock.h>
  25. #include <core/task.h>
  26. #include <core/workers.h>
  27. #ifdef STARPU_OPENMP
  28. #include <util/openmp_runtime_support.h>
  29. #endif
  30. static struct starpu_data_interface_ops **_id_to_ops_array;
  31. static unsigned _id_to_ops_array_size;
  32. /* Entry in the `registered_handles' hash table. */
  33. struct handle_entry
  34. {
  35. UT_hash_handle hh;
  36. void *pointer;
  37. starpu_data_handle_t handle;
  38. };
  39. /* Hash table mapping host pointers to data handles. */
  40. static int nregistered, maxnregistered;
  41. static struct handle_entry *registered_handles;
  42. static struct _starpu_spinlock registered_handles_lock;
  43. static int _data_interface_number = STARPU_MAX_INTERFACE_ID;
  44. starpu_arbiter_t _starpu_global_arbiter;
  45. static void _starpu_data_unregister(starpu_data_handle_t handle, unsigned coherent, unsigned nowait);
  46. void _starpu_data_interface_init(void)
  47. {
  48. _starpu_spin_init(&registered_handles_lock);
  49. /* Just for testing purpose */
  50. if (starpu_get_env_number_default("STARPU_GLOBAL_ARBITER", 0) > 0)
  51. _starpu_global_arbiter = starpu_arbiter_create();
  52. }
  53. void _starpu_data_interface_shutdown()
  54. {
  55. struct handle_entry *entry=NULL, *tmp=NULL;
  56. if (registered_handles)
  57. {
  58. _STARPU_DISP("[warning] The application has not unregistered all data handles.\n");
  59. }
  60. _starpu_spin_destroy(&registered_handles_lock);
  61. free(_id_to_ops_array);
  62. _id_to_ops_array = NULL;
  63. _id_to_ops_array_size = 0;
  64. HASH_ITER(hh, registered_handles, entry, tmp)
  65. {
  66. HASH_DEL(registered_handles, entry);
  67. nregistered--;
  68. free(entry);
  69. }
  70. if (starpu_get_env_number_default("STARPU_MAX_MEMORY_USE", 0))
  71. _STARPU_DISP("Memory used for %d data handles: %lu MiB\n", maxnregistered, (unsigned long) (maxnregistered * sizeof(struct _starpu_data_state)) >> 20);
  72. STARPU_ASSERT(nregistered == 0);
  73. registered_handles = NULL;
  74. }
  75. #ifdef STARPU_OPENMP
  76. void _starpu_omp_unregister_region_handles(struct starpu_omp_region *region)
  77. {
  78. _starpu_spin_lock(&region->registered_handles_lock);
  79. struct handle_entry *entry=NULL, *tmp=NULL;
  80. HASH_ITER(hh, (region->registered_handles), entry, tmp)
  81. {
  82. entry->handle->removed_from_context_hash = 1;
  83. HASH_DEL(region->registered_handles, entry);
  84. starpu_data_unregister(entry->handle);
  85. free(entry);
  86. }
  87. _starpu_spin_unlock(&region->registered_handles_lock);
  88. }
  89. void _starpu_omp_unregister_task_handles(struct starpu_omp_task *task)
  90. {
  91. struct handle_entry *entry=NULL, *tmp=NULL;
  92. HASH_ITER(hh, task->registered_handles, entry, tmp)
  93. {
  94. entry->handle->removed_from_context_hash = 1;
  95. HASH_DEL(task->registered_handles, entry);
  96. starpu_data_unregister(entry->handle);
  97. free(entry);
  98. }
  99. }
  100. #endif
  101. struct starpu_data_interface_ops *_starpu_data_interface_get_ops(unsigned interface_id)
  102. {
  103. switch (interface_id)
  104. {
  105. case STARPU_MATRIX_INTERFACE_ID:
  106. return &starpu_interface_matrix_ops;
  107. case STARPU_BLOCK_INTERFACE_ID:
  108. return &starpu_interface_block_ops;
  109. case STARPU_VECTOR_INTERFACE_ID:
  110. return &starpu_interface_vector_ops;
  111. case STARPU_CSR_INTERFACE_ID:
  112. return &starpu_interface_csr_ops;
  113. case STARPU_BCSR_INTERFACE_ID:
  114. return &starpu_interface_bcsr_ops;
  115. case STARPU_VARIABLE_INTERFACE_ID:
  116. return &starpu_interface_variable_ops;
  117. case STARPU_VOID_INTERFACE_ID:
  118. return &starpu_interface_void_ops;
  119. case STARPU_MULTIFORMAT_INTERFACE_ID:
  120. return &starpu_interface_multiformat_ops;
  121. default:
  122. {
  123. if (interface_id-STARPU_MAX_INTERFACE_ID > _id_to_ops_array_size || _id_to_ops_array[interface_id-STARPU_MAX_INTERFACE_ID]==NULL)
  124. {
  125. _STARPU_MSG("There is no 'struct starpu_data_interface_ops' registered for interface %d\n", interface_id);
  126. STARPU_ABORT();
  127. return NULL;
  128. }
  129. else
  130. return _id_to_ops_array[interface_id-STARPU_MAX_INTERFACE_ID];
  131. }
  132. }
  133. }
  134. /* Register the mapping from PTR to HANDLE. If PTR is already mapped to
  135. * some handle, the new mapping shadows the previous one. */
  136. void _starpu_data_register_ram_pointer(starpu_data_handle_t handle, void *ptr)
  137. {
  138. struct handle_entry *entry;
  139. _STARPU_MALLOC(entry, sizeof(*entry));
  140. entry->pointer = ptr;
  141. entry->handle = handle;
  142. #ifdef STARPU_OPENMP
  143. struct starpu_omp_task *task = _starpu_omp_get_task();
  144. if (task)
  145. {
  146. if (task->flags & STARPU_OMP_TASK_FLAGS_IMPLICIT)
  147. {
  148. struct starpu_omp_region *parallel_region = task->owner_region;
  149. _starpu_spin_lock(&parallel_region->registered_handles_lock);
  150. HASH_ADD_PTR(parallel_region->registered_handles, pointer, entry);
  151. _starpu_spin_unlock(&parallel_region->registered_handles_lock);
  152. }
  153. else
  154. {
  155. HASH_ADD_PTR(task->registered_handles, pointer, entry);
  156. }
  157. }
  158. else
  159. #endif
  160. {
  161. struct handle_entry *old_entry;
  162. _starpu_spin_lock(&registered_handles_lock);
  163. HASH_FIND_PTR(registered_handles, &ptr, old_entry);
  164. if (old_entry)
  165. {
  166. /* Already registered this pointer, avoid undefined
  167. * behavior of duplicate in hash table */
  168. _starpu_spin_unlock(&registered_handles_lock);
  169. free(entry);
  170. }
  171. else
  172. {
  173. nregistered++;
  174. if (nregistered > maxnregistered)
  175. maxnregistered = nregistered;
  176. HASH_ADD_PTR(registered_handles, pointer, entry);
  177. _starpu_spin_unlock(&registered_handles_lock);
  178. }
  179. }
  180. }
  181. starpu_data_handle_t starpu_data_lookup(const void *ptr)
  182. {
  183. starpu_data_handle_t result;
  184. #ifdef STARPU_OPENMP
  185. struct starpu_omp_task *task = _starpu_omp_get_task();
  186. if (task)
  187. {
  188. if (task->flags & STARPU_OMP_TASK_FLAGS_IMPLICIT)
  189. {
  190. struct starpu_omp_region *parallel_region = task->owner_region;
  191. _starpu_spin_lock(&parallel_region->registered_handles_lock);
  192. {
  193. struct handle_entry *entry;
  194. HASH_FIND_PTR(parallel_region->registered_handles, &ptr, entry);
  195. if(STARPU_UNLIKELY(entry == NULL))
  196. result = NULL;
  197. else
  198. result = entry->handle;
  199. }
  200. _starpu_spin_unlock(&parallel_region->registered_handles_lock);
  201. }
  202. else
  203. {
  204. struct handle_entry *entry;
  205. HASH_FIND_PTR(task->registered_handles, &ptr, entry);
  206. if(STARPU_UNLIKELY(entry == NULL))
  207. result = NULL;
  208. else
  209. result = entry->handle;
  210. }
  211. }
  212. else
  213. #endif
  214. {
  215. _starpu_spin_lock(&registered_handles_lock);
  216. {
  217. struct handle_entry *entry;
  218. HASH_FIND_PTR(registered_handles, &ptr, entry);
  219. if(STARPU_UNLIKELY(entry == NULL))
  220. result = NULL;
  221. else
  222. result = entry->handle;
  223. }
  224. _starpu_spin_unlock(&registered_handles_lock);
  225. }
  226. return result;
  227. }
  228. /*
  229. * Start monitoring a piece of data
  230. */
  231. static void _starpu_register_new_data(starpu_data_handle_t handle,
  232. int home_node, uint32_t wt_mask)
  233. {
  234. void *ptr;
  235. STARPU_ASSERT(handle);
  236. /* first take care to properly lock the data */
  237. _starpu_spin_lock(&handle->header_lock);
  238. handle->root_handle = handle;
  239. //handle->father_handle = NULL;
  240. //handle->nsiblings = 0;
  241. //handle->siblings = NULL;
  242. //handle->sibling_index = 0; /* could be anything for the root */
  243. handle->depth = 1; /* the tree is just a node yet */
  244. handle->active = 1;
  245. /* Store some values directly in the handle not to recompute them all
  246. * the time. */
  247. handle->footprint = _starpu_compute_data_footprint(handle);
  248. handle->home_node = home_node;
  249. handle->wt_mask = wt_mask;
  250. //handle->aliases = 0;
  251. //handle->readonly_dup = NULL;
  252. //handle->readonly_dup_of = NULL;
  253. //handle->is_not_important = 0;
  254. handle->sequential_consistency =
  255. starpu_data_get_default_sequential_consistency_flag();
  256. handle->initialized = home_node != -1;
  257. //handle->readonly = 0;
  258. handle->ooc = 1;
  259. /* By default, there are no methods available to perform a reduction */
  260. //handle->redux_cl = NULL;
  261. //handle->init_cl = NULL;
  262. /* that new data is invalid from all nodes perpective except for the
  263. * home node */
  264. unsigned node;
  265. for (node = 0; node < STARPU_MAXNODES; node++)
  266. {
  267. struct _starpu_data_replicate *replicate;
  268. replicate = &handle->per_node[node];
  269. replicate->memory_node = node;
  270. //replicate->relaxed_coherency = 0;
  271. //replicate->refcnt = 0;
  272. if ((int) node == home_node)
  273. {
  274. /* this is the home node with the only valid copy */
  275. replicate->state = STARPU_OWNER;
  276. replicate->allocated = 1;
  277. //replicate->automatically_allocated = 0;
  278. replicate->initialized = 1;
  279. }
  280. else
  281. {
  282. /* the value is not available here yet */
  283. replicate->state = STARPU_INVALID;
  284. //replicate->allocated = 0;
  285. //replicate->initialized = 0;
  286. }
  287. }
  288. /* now the data is available ! */
  289. _starpu_spin_unlock(&handle->header_lock);
  290. for (node = 0; node < STARPU_MAXNODES; node++)
  291. {
  292. if (starpu_node_get_kind(node) != STARPU_CPU_RAM)
  293. continue;
  294. ptr = starpu_data_handle_to_pointer(handle, node);
  295. if (ptr != NULL)
  296. _starpu_data_register_ram_pointer(handle, ptr);
  297. }
  298. }
  299. void
  300. _starpu_data_initialize_per_worker(starpu_data_handle_t handle)
  301. {
  302. unsigned worker;
  303. unsigned nworkers = starpu_worker_get_count();
  304. _starpu_spin_checklocked(&handle->header_lock);
  305. _STARPU_CALLOC(handle->per_worker, nworkers, sizeof(*handle->per_worker));
  306. size_t interfacesize = handle->ops->interface_size;
  307. for (worker = 0; worker < nworkers; worker++)
  308. {
  309. struct _starpu_data_replicate *replicate;
  310. unsigned node;
  311. replicate = &handle->per_worker[worker];
  312. replicate->allocated = 0;
  313. replicate->automatically_allocated = 0;
  314. replicate->state = STARPU_INVALID;
  315. replicate->refcnt = 0;
  316. replicate->handle = handle;
  317. replicate->requested = 0;
  318. for (node = 0; node < STARPU_MAXNODES; node++)
  319. {
  320. replicate->request[node] = NULL;
  321. }
  322. /* Assuming being used for SCRATCH for now, patched when entering REDUX mode */
  323. replicate->relaxed_coherency = 1;
  324. replicate->initialized = 0;
  325. replicate->memory_node = starpu_worker_get_memory_node(worker);
  326. _STARPU_CALLOC(replicate->data_interface, 1, interfacesize);
  327. /* duplicate the content of the interface on node 0 */
  328. memcpy(replicate->data_interface, handle->per_node[STARPU_MAIN_RAM].data_interface, interfacesize);
  329. }
  330. }
  331. void starpu_data_ptr_register(starpu_data_handle_t handle, unsigned node)
  332. {
  333. struct _starpu_data_replicate *replicate = &handle->per_node[node];
  334. _starpu_spin_lock(&handle->header_lock);
  335. STARPU_ASSERT_MSG(replicate->allocated == 0, "starpu_data_ptr_register must be called right after starpu_data_register");
  336. replicate->allocated = 1;
  337. replicate->automatically_allocated = 0;
  338. _starpu_spin_unlock(&handle->header_lock);
  339. }
  340. int _starpu_data_handle_init(starpu_data_handle_t handle, struct starpu_data_interface_ops *interface_ops, unsigned int mf_node)
  341. {
  342. unsigned node;
  343. /* Tell helgrind that our access to busy_count in
  344. * starpu_data_unregister is actually safe */
  345. STARPU_HG_DISABLE_CHECKING(handle->busy_count);
  346. handle->magic = 42;
  347. /* When not specified, the fields are initialized in _starpu_register_new_data and _starpu_data_partition */
  348. _starpu_data_requester_prio_list_init0(&handle->req_list);
  349. //handle->refcnt = 0;
  350. //handle->unlocking_reqs = 0;
  351. //handle->current_mode = STARPU_NONE;
  352. _starpu_spin_init(&handle->header_lock);
  353. //handle->busy_count = 0;
  354. //handle->busy_waiting = 0;
  355. STARPU_PTHREAD_MUTEX_INIT0(&handle->busy_mutex, NULL);
  356. STARPU_PTHREAD_COND_INIT0(&handle->busy_cond, NULL);
  357. //handle->root_handle
  358. //handle->father_handle
  359. //handle->active_children = NULL;
  360. //handle->active_nchildren = 0;
  361. //handle->active_readonly_children = NULL;
  362. //handle->active_readonly_nchildren = NULL;
  363. //handle->nactive_readonly_children = 0;
  364. //handle->nsiblings
  365. //handle->siblings
  366. //handle->sibling_index
  367. //handle->depth
  368. /* there is no hierarchy yet */
  369. //handle->children = NULL;
  370. //handle->nchildren = 0;
  371. //handle->nplans = 0;
  372. //handle->switch_cl = NULL;
  373. //handle->switch_cl_nparts = 0;
  374. //handle->partitioned = 0;
  375. //handle->part_readonly = 0;
  376. //handle->active
  377. //handle->active_ro = 0;
  378. //handle->per_node below
  379. handle->ops = interface_ops;
  380. size_t interfacesize = interface_ops->interface_size;
  381. for (node = 0; node < STARPU_MAXNODES; node++)
  382. {
  383. _starpu_memory_stats_init_per_node(handle, node);
  384. struct _starpu_data_replicate *replicate;
  385. replicate = &handle->per_node[node];
  386. /* relaxed_coherency = 0 */
  387. replicate->handle = handle;
  388. _STARPU_CALLOC(replicate->data_interface, 1, interfacesize);
  389. if (handle->ops->init) handle->ops->init(replicate->data_interface);
  390. }
  391. //handle->per_worker = NULL;
  392. //handle->ops above
  393. //handle->footprint
  394. //handle->home_node
  395. //handle->wt_mask
  396. //handle->aliases = 0;
  397. //handle->is_not_important
  398. //handle->sequential_consistency
  399. //handle->initialized
  400. //handle->readonly
  401. //handle->ooc
  402. //handle->lazy_unregister = 0;
  403. //handle->partition_automatic_disabled = 0;
  404. //handle->removed_from_context_hash = 0;
  405. STARPU_PTHREAD_MUTEX_INIT0(&handle->sequential_consistency_mutex, NULL);
  406. handle->last_submitted_mode = STARPU_R;
  407. //handle->last_sync_task = NULL;
  408. //handle->last_submitted_accessors.task = NULL;
  409. handle->last_submitted_accessors.next = &handle->last_submitted_accessors;
  410. handle->last_submitted_accessors.prev = &handle->last_submitted_accessors;
  411. #ifdef STARPU_USE_FXT
  412. //handle->last_submitted_ghost_sync_id_is_valid = 0;
  413. //handle->last_submitted_ghost_sync_id = 0;
  414. //handle->last_submitted_ghost_accessors_id = NULL;
  415. #endif
  416. //handle->post_sync_tasks = NULL;
  417. /* Tell helgrind that the race in _starpu_unlock_post_sync_tasks is fine */
  418. STARPU_HG_DISABLE_CHECKING(handle->post_sync_tasks_cnt);
  419. //handle->post_sync_tasks_cnt = 0;
  420. //handle->redux_cl
  421. //handle->init_cl
  422. //handle->reduction_refcnt = 0;
  423. _starpu_data_requester_prio_list_init0(&handle->reduction_req_list);
  424. //handle->reduction_tmp_handles = NULL;
  425. //handle->write_invalidation_req = NULL;
  426. //handle->mpi_data = NULL; /* invalid until set */
  427. _starpu_memory_stats_init(handle);
  428. handle->mf_node = mf_node;
  429. //handle->unregister_hook = NULL;
  430. if (_starpu_global_arbiter)
  431. /* Just for testing purpose */
  432. starpu_data_assign_arbiter(handle, _starpu_global_arbiter);
  433. else
  434. {
  435. //handle->arbiter = NULL;
  436. }
  437. _starpu_data_requester_prio_list_init0(&handle->arbitered_req_list);
  438. handle->last_locality = -1;
  439. //handle->dimensions = 0;
  440. //handle->coordinates = {};
  441. //handle->user_data = NULL;
  442. return 0;
  443. }
  444. static
  445. starpu_data_handle_t _starpu_data_handle_allocate(struct starpu_data_interface_ops *interface_ops, unsigned int mf_node)
  446. {
  447. starpu_data_handle_t handle;
  448. _STARPU_CALLOC(handle, 1, sizeof(struct _starpu_data_state));
  449. _starpu_data_handle_init(handle, interface_ops, mf_node);
  450. return handle;
  451. }
  452. void starpu_data_register(starpu_data_handle_t *handleptr, int home_node,
  453. void *data_interface,
  454. struct starpu_data_interface_ops *ops)
  455. {
  456. starpu_data_handle_t handle = _starpu_data_handle_allocate(ops, home_node);
  457. STARPU_ASSERT(handleptr);
  458. *handleptr = handle;
  459. /* check the interfaceid is set */
  460. STARPU_ASSERT(ops->interfaceid != STARPU_UNKNOWN_INTERFACE_ID);
  461. /* fill the interface fields with the appropriate method */
  462. STARPU_ASSERT(ops->register_data_handle);
  463. ops->register_data_handle(handle, home_node, data_interface);
  464. if ((unsigned)ops->interfaceid >= STARPU_MAX_INTERFACE_ID)
  465. {
  466. if ((unsigned)ops->interfaceid > _id_to_ops_array_size)
  467. {
  468. if (!_id_to_ops_array_size)
  469. {
  470. _id_to_ops_array_size = 16;
  471. }
  472. else
  473. {
  474. _id_to_ops_array_size *= 2;
  475. }
  476. _STARPU_REALLOC(_id_to_ops_array, _id_to_ops_array_size * sizeof(struct starpu_data_interface_ops *));
  477. }
  478. _id_to_ops_array[ops->interfaceid-STARPU_MAX_INTERFACE_ID] = ops;
  479. }
  480. _starpu_register_new_data(handle, home_node, 0);
  481. _STARPU_TRACE_HANDLE_DATA_REGISTER(handle);
  482. }
  483. void starpu_data_register_same(starpu_data_handle_t *handledst, starpu_data_handle_t handlesrc)
  484. {
  485. void *local_interface = starpu_data_get_interface_on_node(handlesrc, STARPU_MAIN_RAM);
  486. starpu_data_register(handledst, -1, local_interface, handlesrc->ops);
  487. }
  488. void *starpu_data_handle_to_pointer(starpu_data_handle_t handle, unsigned node)
  489. {
  490. /* Check whether the operation is supported and the node has actually
  491. * been allocated. */
  492. if (!starpu_data_test_if_allocated_on_node(handle, node))
  493. return NULL;
  494. if (handle->ops->to_pointer)
  495. {
  496. return handle->ops->to_pointer(starpu_data_get_interface_on_node(handle, node), node);
  497. }
  498. /* Deprecated */
  499. if (handle->ops->handle_to_pointer)
  500. {
  501. return handle->ops->handle_to_pointer(handle, node);
  502. }
  503. return NULL;
  504. }
  505. int starpu_data_pointer_is_inside(starpu_data_handle_t handle, unsigned node, void *ptr)
  506. {
  507. /* Check whether the operation is supported and the node has actually
  508. * been allocated. */
  509. if (!starpu_data_test_if_allocated_on_node(handle, node))
  510. return 0;
  511. if (handle->ops->pointer_is_inside)
  512. {
  513. return handle->ops->pointer_is_inside(starpu_data_get_interface_on_node(handle, node), node, ptr);
  514. }
  515. /* Don't know :/ */
  516. return -1;
  517. }
  518. void *starpu_data_get_local_ptr(starpu_data_handle_t handle)
  519. {
  520. return starpu_data_handle_to_pointer(handle, starpu_worker_get_local_memory_node());
  521. }
  522. struct starpu_data_interface_ops* starpu_data_get_interface_ops(starpu_data_handle_t handle)
  523. {
  524. return handle->ops;
  525. }
  526. /*
  527. * Stop monitoring a piece of data
  528. */
  529. void _starpu_data_unregister_ram_pointer(starpu_data_handle_t handle, unsigned node)
  530. {
  531. if (starpu_node_get_kind(node) != STARPU_CPU_RAM)
  532. return;
  533. #ifdef STARPU_OPENMP
  534. if (handle->removed_from_context_hash)
  535. return;
  536. #endif
  537. const void *ram_ptr = starpu_data_handle_to_pointer(handle, node);
  538. if (ram_ptr != NULL)
  539. {
  540. /* Remove the PTR -> HANDLE mapping. If a mapping from PTR
  541. * to another handle existed before (e.g., when using
  542. * filters), it becomes visible again. */
  543. struct handle_entry *entry;
  544. #ifdef STARPU_OPENMP
  545. struct starpu_omp_task *task = _starpu_omp_get_task();
  546. if (task)
  547. {
  548. if (task->flags & STARPU_OMP_TASK_FLAGS_IMPLICIT)
  549. {
  550. struct starpu_omp_region *parallel_region = task->owner_region;
  551. _starpu_spin_lock(&parallel_region->registered_handles_lock);
  552. HASH_FIND_PTR(parallel_region->registered_handles, &ram_ptr, entry);
  553. STARPU_ASSERT(entry != NULL);
  554. HASH_DEL(registered_handles, entry);
  555. _starpu_spin_unlock(&parallel_region->registered_handles_lock);
  556. }
  557. else
  558. {
  559. HASH_FIND_PTR(task->registered_handles, &ram_ptr, entry);
  560. STARPU_ASSERT(entry != NULL);
  561. HASH_DEL(task->registered_handles, entry);
  562. }
  563. }
  564. else
  565. #endif
  566. {
  567. _starpu_spin_lock(&registered_handles_lock);
  568. HASH_FIND_PTR(registered_handles, &ram_ptr, entry);
  569. if (entry)
  570. {
  571. if (entry->handle == handle)
  572. {
  573. nregistered--;
  574. HASH_DEL(registered_handles, entry);
  575. }
  576. else
  577. /* don't free it, it's not ours */
  578. entry = NULL;
  579. }
  580. _starpu_spin_unlock(&registered_handles_lock);
  581. }
  582. free(entry);
  583. }
  584. }
  585. void _starpu_data_free_interfaces(starpu_data_handle_t handle)
  586. {
  587. unsigned node;
  588. unsigned nworkers = starpu_worker_get_count();
  589. for (node = 0; node < STARPU_MAXNODES; node++)
  590. free(handle->per_node[node].data_interface);
  591. if (handle->per_worker)
  592. {
  593. unsigned worker;
  594. for (worker = 0; worker < nworkers; worker++)
  595. free(handle->per_worker[worker].data_interface);
  596. free(handle->per_worker);
  597. }
  598. }
  599. struct _starpu_unregister_callback_arg
  600. {
  601. unsigned memory_node;
  602. starpu_data_handle_t handle;
  603. unsigned terminated;
  604. starpu_pthread_mutex_t mutex;
  605. starpu_pthread_cond_t cond;
  606. };
  607. /* Check whether we should tell starpu_data_unregister that the data handle is
  608. * not busy any more.
  609. * The header is supposed to be locked.
  610. * This may free the handle, if it was lazily unregistered (1 is returned in
  611. * that case). The handle pointer thus becomes invalid for the caller.
  612. *
  613. * Note: we inline some of the tests in the _starpu_data_check_not_busy macro.
  614. */
  615. int __starpu_data_check_not_busy(starpu_data_handle_t handle)
  616. {
  617. if (STARPU_LIKELY(handle->busy_count))
  618. return 0;
  619. /* Not busy any more, perhaps have to unregister etc. */
  620. if (STARPU_UNLIKELY(handle->busy_waiting))
  621. {
  622. STARPU_PTHREAD_MUTEX_LOCK(&handle->busy_mutex);
  623. STARPU_PTHREAD_COND_BROADCAST(&handle->busy_cond);
  624. STARPU_PTHREAD_MUTEX_UNLOCK(&handle->busy_mutex);
  625. }
  626. /* The handle has been destroyed in between (eg. this was a temporary
  627. * handle created for a reduction.) */
  628. if (STARPU_UNLIKELY(handle->lazy_unregister))
  629. {
  630. handle->lazy_unregister = 0;
  631. _starpu_spin_unlock(&handle->header_lock);
  632. _starpu_data_unregister(handle, 0, 1);
  633. /* Warning: in case we unregister the handle, we must be sure
  634. * that the caller will not try to unlock the header after
  635. * !*/
  636. return 1;
  637. }
  638. return 0;
  639. }
  640. static
  641. void _starpu_check_if_valid_and_fetch_data_on_node(starpu_data_handle_t handle, struct _starpu_data_replicate *replicate, const char *origin)
  642. {
  643. unsigned node;
  644. unsigned nnodes = starpu_memory_nodes_get_count();
  645. int valid = 0;
  646. for (node = 0; node < nnodes; node++)
  647. {
  648. if (handle->per_node[node].state != STARPU_INVALID)
  649. {
  650. /* we found a copy ! */
  651. valid = 1;
  652. }
  653. }
  654. if (valid)
  655. {
  656. int ret = _starpu_fetch_data_on_node(handle, handle->home_node, replicate, STARPU_R, 0, STARPU_FETCH, 0, NULL, NULL, 0, origin);
  657. STARPU_ASSERT(!ret);
  658. _starpu_release_data_on_node(handle, 0, STARPU_NONE, replicate);
  659. }
  660. else
  661. {
  662. _starpu_spin_lock(&handle->header_lock);
  663. if (!_starpu_notify_data_dependencies(handle, STARPU_NONE))
  664. _starpu_spin_unlock(&handle->header_lock);
  665. }
  666. }
  667. static void _starpu_data_unregister_fetch_data_callback(void *_arg)
  668. {
  669. struct _starpu_unregister_callback_arg *arg = (struct _starpu_unregister_callback_arg *) _arg;
  670. starpu_data_handle_t handle = arg->handle;
  671. STARPU_ASSERT(handle);
  672. struct _starpu_data_replicate *replicate = &handle->per_node[arg->memory_node];
  673. _starpu_check_if_valid_and_fetch_data_on_node(handle, replicate, "_starpu_data_unregister_fetch_data_callback");
  674. /* unlock the caller */
  675. STARPU_PTHREAD_MUTEX_LOCK(&arg->mutex);
  676. arg->terminated = 1;
  677. STARPU_PTHREAD_COND_SIGNAL(&arg->cond);
  678. STARPU_PTHREAD_MUTEX_UNLOCK(&arg->mutex);
  679. }
  680. void _starpu_data_set_unregister_hook(starpu_data_handle_t handle, _starpu_data_handle_unregister_hook func)
  681. {
  682. STARPU_ASSERT(handle->unregister_hook == NULL);
  683. handle->unregister_hook = func;
  684. }
  685. /*
  686. * We are about to unregister this R/O data. There might be still other aliases,
  687. * in which case this returns 0. If not, users are not supposed to see it
  688. * any more, so detach it from their sight and return 1 to let unregistration happen.
  689. */
  690. static int _starpu_ro_data_detach(starpu_data_handle_t handle)
  691. {
  692. _starpu_spin_lock(&handle->header_lock);
  693. if (handle->aliases)
  694. {
  695. handle->aliases--;
  696. _starpu_spin_unlock(&handle->header_lock);
  697. return 0;
  698. }
  699. if (handle->readonly_dup)
  700. {
  701. STARPU_ASSERT(handle->readonly_dup->readonly_dup_of == handle);
  702. handle->readonly_dup->readonly_dup_of = NULL;
  703. handle->readonly_dup = NULL;
  704. }
  705. if (handle->readonly_dup_of)
  706. {
  707. STARPU_ASSERT(handle->readonly_dup_of->readonly_dup == handle);
  708. handle->readonly_dup_of->readonly_dup = NULL;
  709. handle->readonly_dup_of = NULL;
  710. }
  711. /* So that unregistration can use write dependencies to wait for
  712. * anything to finish */
  713. handle->readonly = 0;
  714. _starpu_spin_unlock(&handle->header_lock);
  715. return 1;
  716. }
  717. /* Unregister the data handle, perhaps we don't need to update the home_node
  718. * (in that case coherent is set to 0)
  719. * nowait is for internal use when we already know for sure that we won't have to wait.
  720. */
  721. static void _starpu_data_unregister(starpu_data_handle_t handle, unsigned coherent, unsigned nowait)
  722. {
  723. STARPU_ASSERT(handle);
  724. STARPU_ASSERT_MSG(handle->nchildren == 0, "data %p needs to be unpartitioned before unregistration", handle);
  725. STARPU_ASSERT_MSG(handle->nplans == 0, "data %p needs its partition plans to be cleaned before unregistration", handle);
  726. STARPU_ASSERT_MSG(handle->partitioned == 0, "data %p needs its partitioned plans to be unpartitioned before unregistration", handle);
  727. /* TODO: also check that it has the latest coherency */
  728. STARPU_ASSERT(!(nowait && handle->busy_count != 0));
  729. if (!_starpu_ro_data_detach(handle))
  730. return;
  731. int sequential_consistency = handle->sequential_consistency;
  732. if (sequential_consistency && !nowait)
  733. {
  734. /* We will acquire it in write mode to catch all dependencies,
  735. * but possibly it's not actually initialized. Fake it to avoid
  736. getting caught doing it */
  737. handle->initialized = 1;
  738. 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");
  739. /* If sequential consistency is enabled, wait until data is available */
  740. _starpu_data_wait_until_available(handle, handle->readonly?STARPU_R:STARPU_RW, "starpu_data_unregister");
  741. }
  742. if (coherent && !nowait)
  743. {
  744. 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");
  745. /* Fetch data in the home of the data to ensure we have a valid copy
  746. * where we registered it */
  747. int home_node = handle->home_node;
  748. if (home_node >= 0)
  749. {
  750. struct _starpu_unregister_callback_arg arg = { 0 };
  751. arg.handle = handle;
  752. arg.memory_node = (unsigned)home_node;
  753. arg.terminated = 0;
  754. STARPU_PTHREAD_MUTEX_INIT0(&arg.mutex, NULL);
  755. STARPU_PTHREAD_COND_INIT0(&arg.cond, NULL);
  756. if (!_starpu_attempt_to_submit_data_request_from_apps(handle, STARPU_R,
  757. _starpu_data_unregister_fetch_data_callback, &arg))
  758. {
  759. /* no one has locked this data yet, so we proceed immediately */
  760. struct _starpu_data_replicate *home_replicate = &handle->per_node[home_node];
  761. _starpu_check_if_valid_and_fetch_data_on_node(handle, home_replicate, "_starpu_data_unregister");
  762. }
  763. else
  764. {
  765. STARPU_PTHREAD_MUTEX_LOCK(&arg.mutex);
  766. while (!arg.terminated)
  767. STARPU_PTHREAD_COND_WAIT(&arg.cond, &arg.mutex);
  768. STARPU_PTHREAD_MUTEX_UNLOCK(&arg.mutex);
  769. }
  770. STARPU_PTHREAD_MUTEX_DESTROY(&arg.mutex);
  771. STARPU_PTHREAD_COND_DESTROY(&arg.cond);
  772. }
  773. /* If this handle uses a multiformat interface, we may have to convert
  774. * this piece of data back into the CPU format.
  775. * XXX : This is quite hacky, could we submit a task instead ?
  776. */
  777. if (_starpu_data_is_multiformat_handle(handle) && (starpu_node_get_kind(handle->mf_node) != STARPU_CPU_RAM))
  778. {
  779. _STARPU_DEBUG("Conversion needed\n");
  780. void *buffers[1];
  781. struct starpu_multiformat_interface *format_interface;
  782. home_node = handle->home_node;
  783. if (home_node < 0 || (starpu_node_get_kind(home_node) != STARPU_CPU_RAM))
  784. home_node = STARPU_MAIN_RAM;
  785. format_interface = (struct starpu_multiformat_interface *) starpu_data_get_interface_on_node(handle, home_node);
  786. struct starpu_codelet *cl = NULL;
  787. enum starpu_node_kind node_kind = starpu_node_get_kind(handle->mf_node);
  788. switch (node_kind)
  789. {
  790. #ifdef STARPU_USE_CUDA
  791. case STARPU_CUDA_RAM:
  792. {
  793. struct starpu_multiformat_data_interface_ops *mf_ops;
  794. mf_ops = (struct starpu_multiformat_data_interface_ops *) handle->ops->get_mf_ops(format_interface);
  795. cl = mf_ops->cuda_to_cpu_cl;
  796. break;
  797. }
  798. #endif
  799. #ifdef STARPU_USE_OPENCL
  800. case STARPU_OPENCL_RAM:
  801. {
  802. struct starpu_multiformat_data_interface_ops *mf_ops;
  803. mf_ops = (struct starpu_multiformat_data_interface_ops *) handle->ops->get_mf_ops(format_interface);
  804. cl = mf_ops->opencl_to_cpu_cl;
  805. break;
  806. }
  807. #endif
  808. #ifdef STARPU_USE_MIC
  809. case STARPU_MIC_RAM:
  810. {
  811. struct starpu_multiformat_data_interface_ops *mf_ops;
  812. mf_ops = (struct starpu_multiformat_data_interface_ops *) handle->ops->get_mf_ops(format_interface);
  813. cl = mf_ops->mic_to_cpu_cl;
  814. break;
  815. }
  816. #endif
  817. case STARPU_CPU_RAM: /* Impossible ! */
  818. default:
  819. STARPU_ABORT();
  820. }
  821. buffers[0] = format_interface;
  822. _starpu_cl_func_t func = _starpu_task_get_cpu_nth_implementation(cl, 0);
  823. STARPU_ASSERT(func);
  824. func(buffers, NULL);
  825. }
  826. }
  827. /* Prevent any further unregistration */
  828. handle->magic = 0;
  829. _starpu_spin_lock(&handle->header_lock);
  830. if (!coherent)
  831. {
  832. /* Should we postpone the unregister operation ? */
  833. if ((handle->busy_count > 0) && handle->lazy_unregister)
  834. {
  835. _starpu_spin_unlock(&handle->header_lock);
  836. return;
  837. }
  838. }
  839. /* Tell holders of references that we're starting waiting */
  840. handle->busy_waiting = 1;
  841. _starpu_spin_unlock(&handle->header_lock);
  842. retry_busy:
  843. /* Wait for all requests to finish (notably WT requests) */
  844. STARPU_PTHREAD_MUTEX_LOCK(&handle->busy_mutex);
  845. while (1)
  846. {
  847. /* Here helgrind would shout that this an unprotected access,
  848. * but this is actually fine: all threads who do busy_count--
  849. * are supposed to call _starpu_data_check_not_busy, which will
  850. * wake us up through the busy_mutex/busy_cond. */
  851. if (!handle->busy_count)
  852. break;
  853. /* This is woken by _starpu_data_check_not_busy, always called
  854. * after decrementing busy_count */
  855. STARPU_PTHREAD_COND_WAIT(&handle->busy_cond, &handle->busy_mutex);
  856. }
  857. STARPU_PTHREAD_MUTEX_UNLOCK(&handle->busy_mutex);
  858. /* Unregister MPI things after having waiting for MPI reqs etc. to settle down */
  859. if (handle->unregister_hook)
  860. {
  861. handle->unregister_hook(handle);
  862. handle->unregister_hook = NULL;
  863. }
  864. /* Wait for finished requests to release the handle */
  865. _starpu_spin_lock(&handle->header_lock);
  866. if (handle->busy_count)
  867. {
  868. /* Bad luck: some request went in in between, wait again... */
  869. _starpu_spin_unlock(&handle->header_lock);
  870. goto retry_busy;
  871. }
  872. size_t size = _starpu_data_get_alloc_size(handle);
  873. /* Destroy the data now */
  874. unsigned node;
  875. for (node = 0; node < STARPU_MAXNODES; node++)
  876. {
  877. struct _starpu_data_replicate *local = &handle->per_node[node];
  878. if (local->allocated)
  879. {
  880. _starpu_data_unregister_ram_pointer(handle, node);
  881. /* free the data copy in a lazy fashion */
  882. if (local->automatically_allocated)
  883. _starpu_request_mem_chunk_removal(handle, local, node, size);
  884. }
  885. }
  886. if (handle->per_worker)
  887. {
  888. unsigned worker;
  889. unsigned nworkers = starpu_worker_get_count();
  890. for (worker = 0; worker < nworkers; worker++)
  891. {
  892. struct _starpu_data_replicate *local = &handle->per_worker[worker];
  893. /* free the data copy in a lazy fashion */
  894. if (local->allocated && local->automatically_allocated)
  895. _starpu_request_mem_chunk_removal(handle, local, starpu_worker_get_memory_node(worker), size);
  896. }
  897. }
  898. _starpu_data_free_interfaces(handle);
  899. _starpu_memory_stats_free(handle);
  900. _starpu_spin_unlock(&handle->header_lock);
  901. _starpu_spin_destroy(&handle->header_lock);
  902. _starpu_data_clear_implicit(handle);
  903. free(handle->active_readonly_children);
  904. free(handle->active_readonly_nchildren);
  905. STARPU_PTHREAD_MUTEX_DESTROY(&handle->busy_mutex);
  906. STARPU_PTHREAD_COND_DESTROY(&handle->busy_cond);
  907. STARPU_PTHREAD_MUTEX_DESTROY(&handle->sequential_consistency_mutex);
  908. STARPU_HG_ENABLE_CHECKING(handle->post_sync_tasks_cnt);
  909. STARPU_HG_ENABLE_CHECKING(handle->busy_count);
  910. _starpu_data_requester_prio_list_deinit(&handle->req_list);
  911. _starpu_data_requester_prio_list_deinit(&handle->reduction_req_list);
  912. if (handle->switch_cl)
  913. {
  914. free(handle->switch_cl->dyn_nodes);
  915. free(handle->switch_cl);
  916. }
  917. _STARPU_TRACE_HANDLE_DATA_UNREGISTER(handle);
  918. free(handle);
  919. }
  920. void starpu_data_unregister(starpu_data_handle_t handle)
  921. {
  922. STARPU_ASSERT_MSG(handle->magic == 42, "data %p is invalid (was it already registered?)", handle);
  923. STARPU_ASSERT_MSG(!handle->lazy_unregister, "data %p can not be unregistered twice", handle);
  924. _starpu_data_unregister(handle, 1, 0);
  925. }
  926. void starpu_data_unregister_no_coherency(starpu_data_handle_t handle)
  927. {
  928. STARPU_ASSERT_MSG(handle->magic == 42, "data %p is invalid (was it already registered?)", handle);
  929. _starpu_data_unregister(handle, 0, 0);
  930. }
  931. static void _starpu_data_unregister_submit_cb(void *arg)
  932. {
  933. starpu_data_handle_t handle = arg;
  934. _starpu_spin_lock(&handle->header_lock);
  935. handle->lazy_unregister = 1;
  936. /* The handle should be busy since we are working on it.
  937. * when we releases the handle below, it will be destroyed by
  938. * _starpu_data_check_not_busy */
  939. STARPU_ASSERT(handle->busy_count);
  940. _starpu_spin_unlock(&handle->header_lock);
  941. starpu_data_release_on_node(handle, STARPU_ACQUIRE_NO_NODE_LOCK_ALL);
  942. }
  943. void starpu_data_unregister_submit(starpu_data_handle_t handle)
  944. {
  945. STARPU_ASSERT_MSG(handle->magic == 42, "data %p is invalid (was it already registered?)", handle);
  946. STARPU_ASSERT_MSG(!handle->lazy_unregister, "data %p can not be unregistered twice", handle);
  947. if (!_starpu_ro_data_detach(handle))
  948. return;
  949. /* Wait for all task dependencies on this handle before putting it for free */
  950. starpu_data_acquire_on_node_cb(handle, STARPU_ACQUIRE_NO_NODE_LOCK_ALL, handle->initialized?STARPU_RW:STARPU_W, _starpu_data_unregister_submit_cb, handle);
  951. }
  952. static void _starpu_data_invalidate(void *data)
  953. {
  954. starpu_data_handle_t handle = data;
  955. size_t size = _starpu_data_get_alloc_size(handle);
  956. _starpu_spin_lock(&handle->header_lock);
  957. //_STARPU_DEBUG("Really invalidating data %p\n", data);
  958. #ifdef STARPU_DEBUG
  959. {
  960. /* There shouldn't be any pending request since we acquired the data in W mode */
  961. unsigned i, j, nnodes = starpu_memory_nodes_get_count();
  962. for (i = 0; i < nnodes; i++)
  963. for (j = 0; j < nnodes; j++)
  964. STARPU_ASSERT_MSG(!handle->per_node[i].request[j], "request for handle %p pending from %u to %u while invalidating data!", handle, j, i);
  965. }
  966. #endif
  967. unsigned node;
  968. for (node = 0; node < STARPU_MAXNODES; node++)
  969. {
  970. struct _starpu_data_replicate *local = &handle->per_node[node];
  971. if (local->mc && local->allocated && local->automatically_allocated)
  972. {
  973. _starpu_data_unregister_ram_pointer(handle, node);
  974. /* free the data copy in a lazy fashion */
  975. _starpu_request_mem_chunk_removal(handle, local, node, size);
  976. }
  977. if (local->state != STARPU_INVALID)
  978. _STARPU_TRACE_DATA_STATE_INVALID(handle, node);
  979. local->state = STARPU_INVALID;
  980. }
  981. if (handle->per_worker)
  982. {
  983. unsigned worker;
  984. unsigned nworkers = starpu_worker_get_count();
  985. for (worker = 0; worker < nworkers; worker++)
  986. {
  987. struct _starpu_data_replicate *local = &handle->per_worker[worker];
  988. if (local->mc && local->allocated && local->automatically_allocated)
  989. /* free the data copy in a lazy fashion */
  990. _starpu_request_mem_chunk_removal(handle, local, starpu_worker_get_memory_node(worker), size);
  991. local->state = STARPU_INVALID;
  992. }
  993. }
  994. _starpu_spin_unlock(&handle->header_lock);
  995. starpu_data_release_on_node(handle, STARPU_ACQUIRE_NO_NODE_LOCK_ALL);
  996. }
  997. void starpu_data_invalidate(starpu_data_handle_t handle)
  998. {
  999. STARPU_ASSERT(handle);
  1000. starpu_data_acquire_on_node(handle, STARPU_ACQUIRE_NO_NODE_LOCK_ALL, STARPU_W);
  1001. _starpu_data_invalidate(handle);
  1002. handle->initialized = 0;
  1003. }
  1004. void starpu_data_invalidate_submit(starpu_data_handle_t handle)
  1005. {
  1006. STARPU_ASSERT(handle);
  1007. starpu_data_acquire_on_node_cb(handle, STARPU_ACQUIRE_NO_NODE_LOCK_ALL, STARPU_W, _starpu_data_invalidate, handle);
  1008. handle->initialized = 0;
  1009. }
  1010. enum starpu_data_interface_id starpu_data_get_interface_id(starpu_data_handle_t handle)
  1011. {
  1012. return handle->ops->interfaceid;
  1013. }
  1014. void *starpu_data_get_interface_on_node(starpu_data_handle_t handle, unsigned memory_node)
  1015. {
  1016. return handle->per_node[memory_node].data_interface;
  1017. }
  1018. int starpu_data_interface_get_next_id(void)
  1019. {
  1020. _data_interface_number += 1;
  1021. return _data_interface_number-1;
  1022. }
  1023. int starpu_data_pack_node(starpu_data_handle_t handle, unsigned node, void **ptr, starpu_ssize_t *count)
  1024. {
  1025. STARPU_ASSERT_MSG(handle->ops->pack_data, "The datatype interface %s (%d) does not have a pack operation", handle->ops->name, handle->ops->interfaceid);
  1026. return handle->ops->pack_data(handle, node, ptr, count);
  1027. }
  1028. int starpu_data_pack(starpu_data_handle_t handle, void **ptr, starpu_ssize_t *count)
  1029. {
  1030. return starpu_data_pack_node(handle, starpu_worker_get_local_memory_node(), ptr, count);
  1031. }
  1032. int starpu_data_unpack_node(starpu_data_handle_t handle, unsigned node, void *ptr, size_t count)
  1033. {
  1034. STARPU_ASSERT_MSG(handle->ops->unpack_data, "The datatype interface %s (%d) does not have an unpack operation", handle->ops->name, handle->ops->interfaceid);
  1035. int ret;
  1036. ret = handle->ops->unpack_data(handle, node, ptr, count);
  1037. return ret;
  1038. }
  1039. int starpu_data_unpack(starpu_data_handle_t handle, void *ptr, size_t count)
  1040. {
  1041. return starpu_data_unpack_node(handle, starpu_worker_get_local_memory_node(), ptr, count);
  1042. }
  1043. size_t starpu_data_get_size(starpu_data_handle_t handle)
  1044. {
  1045. return handle->ops->get_size(handle);
  1046. }
  1047. size_t starpu_data_get_alloc_size(starpu_data_handle_t handle)
  1048. {
  1049. if (handle->ops->get_alloc_size)
  1050. return handle->ops->get_alloc_size(handle);
  1051. else
  1052. return handle->ops->get_size(handle);
  1053. }
  1054. void starpu_data_set_name(starpu_data_handle_t handle STARPU_ATTRIBUTE_UNUSED, const char *name STARPU_ATTRIBUTE_UNUSED)
  1055. {
  1056. _STARPU_TRACE_DATA_NAME(handle, name);
  1057. }
  1058. int starpu_data_get_home_node(starpu_data_handle_t handle)
  1059. {
  1060. return handle->home_node;
  1061. }
  1062. void starpu_data_set_coordinates_array(starpu_data_handle_t handle, unsigned dimensions, int dims[])
  1063. {
  1064. unsigned i;
  1065. unsigned max_dimensions = sizeof(handle->coordinates)/sizeof(handle->coordinates[0]);
  1066. if (dimensions > max_dimensions)
  1067. dimensions = max_dimensions;
  1068. handle->dimensions = dimensions;
  1069. for (i = 0; i < dimensions; i++)
  1070. handle->coordinates[i] = dims[i];
  1071. _STARPU_TRACE_DATA_COORDINATES(handle, dimensions, dims);
  1072. }
  1073. void starpu_data_set_coordinates(starpu_data_handle_t handle, unsigned dimensions, ...)
  1074. {
  1075. int dims[dimensions];
  1076. unsigned i;
  1077. va_list varg_list;
  1078. va_start(varg_list, dimensions);
  1079. for (i = 0; i < dimensions; i++)
  1080. dims[i] = va_arg(varg_list, int);
  1081. va_end(varg_list);
  1082. starpu_data_set_coordinates_array(handle, dimensions, dims);
  1083. }
  1084. unsigned starpu_data_get_coordinates_array(starpu_data_handle_t handle, unsigned dimensions, int dims[])
  1085. {
  1086. unsigned i;
  1087. if (dimensions > handle->dimensions)
  1088. dimensions = handle->dimensions;
  1089. for (i = 0; i < dimensions; i++)
  1090. dims[i] = handle->coordinates[i];
  1091. return dimensions;
  1092. }
  1093. void starpu_data_print(starpu_data_handle_t handle, unsigned node, FILE *stream)
  1094. {
  1095. if (handle->ops == NULL)
  1096. fprintf(stream, "Undefined");
  1097. else
  1098. {
  1099. switch (handle->ops->interfaceid)
  1100. {
  1101. case(STARPU_MATRIX_INTERFACE_ID):
  1102. fprintf(stream, "Matrix");
  1103. break;
  1104. case(STARPU_BLOCK_INTERFACE_ID):
  1105. fprintf(stream, "Block");
  1106. break;
  1107. case(STARPU_VECTOR_INTERFACE_ID):
  1108. fprintf(stream, "Vector");
  1109. break;
  1110. case(STARPU_CSR_INTERFACE_ID):
  1111. fprintf(stream, "CSR");
  1112. break;
  1113. case(STARPU_BCSR_INTERFACE_ID):
  1114. fprintf(stream, "BCSR");
  1115. break;
  1116. case(STARPU_VARIABLE_INTERFACE_ID):
  1117. fprintf(stream, "Variable");
  1118. break;
  1119. case(STARPU_VOID_INTERFACE_ID):
  1120. fprintf(stream, "Void");
  1121. break;
  1122. case(STARPU_MULTIFORMAT_INTERFACE_ID):
  1123. fprintf(stream, "Multfiformat");
  1124. break;
  1125. case(STARPU_COO_INTERFACE_ID):
  1126. fprintf(stream, "COO");
  1127. break;
  1128. case(STARPU_TENSOR_INTERFACE_ID):
  1129. fprintf(stream, "Tensor");
  1130. break;
  1131. case(STARPU_UNKNOWN_INTERFACE_ID ):
  1132. fprintf(stream, "UNKNOWN");
  1133. break;
  1134. default:
  1135. fprintf(stream, "User interface with id %d", handle->ops->interfaceid);
  1136. break;
  1137. }
  1138. }
  1139. void *data_interface = NULL;
  1140. if (starpu_data_test_if_allocated_on_node(handle, node))
  1141. data_interface = starpu_data_get_interface_on_node(handle, node);
  1142. if (starpu_data_test_if_allocated_on_node(handle, handle->home_node))
  1143. data_interface = starpu_data_get_interface_on_node(handle, handle->home_node);
  1144. if (handle->ops && handle->ops->describe && data_interface)
  1145. {
  1146. char buffer[1024];
  1147. handle->ops->describe(data_interface, buffer, sizeof(buffer));
  1148. fprintf(stream, " %s\n", buffer);
  1149. }
  1150. else
  1151. fprintf(stream, "\n");
  1152. }