data_interface.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388
  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. //replicate->nb_tasks_prefetch = 0;
  273. if ((int) node == home_node)
  274. {
  275. /* this is the home node with the only valid copy */
  276. replicate->state = STARPU_OWNER;
  277. replicate->allocated = 1;
  278. //replicate->automatically_allocated = 0;
  279. replicate->initialized = 1;
  280. }
  281. else
  282. {
  283. /* the value is not available here yet */
  284. replicate->state = STARPU_INVALID;
  285. //replicate->allocated = 0;
  286. //replicate->initialized = 0;
  287. }
  288. }
  289. /* now the data is available ! */
  290. _starpu_spin_unlock(&handle->header_lock);
  291. for (node = 0; node < STARPU_MAXNODES; node++)
  292. {
  293. if (starpu_node_get_kind(node) != STARPU_CPU_RAM)
  294. continue;
  295. ptr = starpu_data_handle_to_pointer(handle, node);
  296. if (ptr != NULL)
  297. _starpu_data_register_ram_pointer(handle, ptr);
  298. }
  299. }
  300. void
  301. _starpu_data_initialize_per_worker(starpu_data_handle_t handle)
  302. {
  303. unsigned worker;
  304. unsigned nworkers = starpu_worker_get_count();
  305. _starpu_spin_checklocked(&handle->header_lock);
  306. _STARPU_CALLOC(handle->per_worker, nworkers, sizeof(*handle->per_worker));
  307. size_t interfacesize = handle->ops->interface_size;
  308. for (worker = 0; worker < nworkers; worker++)
  309. {
  310. struct _starpu_data_replicate *replicate;
  311. //unsigned node;
  312. replicate = &handle->per_worker[worker];
  313. //replicate->allocated = 0;
  314. //replicate->automatically_allocated = 0;
  315. replicate->state = STARPU_INVALID;
  316. //replicate->refcnt = 0;
  317. replicate->handle = handle;
  318. //replicate->nb_tasks_prefetch = 0;
  319. //for (node = 0; node < STARPU_MAXNODES; node++)
  320. //{
  321. // replicate->request[node] = NULL;
  322. // replicate->last_request[node] = NULL;
  323. //}
  324. //replicate->load_request = NULL;
  325. /* Assuming being used for SCRATCH for now, patched when entering REDUX mode */
  326. replicate->relaxed_coherency = 1;
  327. //replicate->initialized = 0;
  328. replicate->memory_node = starpu_worker_get_memory_node(worker);
  329. _STARPU_CALLOC(replicate->data_interface, 1, interfacesize);
  330. /* duplicate the content of the interface on node 0 */
  331. memcpy(replicate->data_interface, handle->per_node[STARPU_MAIN_RAM].data_interface, interfacesize);
  332. }
  333. }
  334. void starpu_data_ptr_register(starpu_data_handle_t handle, unsigned node)
  335. {
  336. struct _starpu_data_replicate *replicate = &handle->per_node[node];
  337. _starpu_spin_lock(&handle->header_lock);
  338. STARPU_ASSERT_MSG(replicate->allocated == 0, "starpu_data_ptr_register must be called right after starpu_data_register");
  339. replicate->allocated = 1;
  340. replicate->automatically_allocated = 0;
  341. _starpu_spin_unlock(&handle->header_lock);
  342. }
  343. int _starpu_data_handle_init(starpu_data_handle_t handle, struct starpu_data_interface_ops *interface_ops, unsigned int mf_node)
  344. {
  345. unsigned node;
  346. /* Tell helgrind that our access to busy_count in
  347. * starpu_data_unregister is actually safe */
  348. STARPU_HG_DISABLE_CHECKING(handle->busy_count);
  349. handle->magic = 42;
  350. /* When not specified, the fields are initialized in _starpu_register_new_data and _starpu_data_partition */
  351. _starpu_data_requester_prio_list_init0(&handle->req_list);
  352. //handle->refcnt = 0;
  353. //handle->unlocking_reqs = 0;
  354. //handle->current_mode = STARPU_NONE;
  355. _starpu_spin_init(&handle->header_lock);
  356. //handle->busy_count = 0;
  357. //handle->busy_waiting = 0;
  358. STARPU_PTHREAD_MUTEX_INIT0(&handle->busy_mutex, NULL);
  359. STARPU_PTHREAD_COND_INIT0(&handle->busy_cond, NULL);
  360. //handle->root_handle
  361. //handle->father_handle
  362. //handle->active_children = NULL;
  363. //handle->active_nchildren = 0;
  364. //handle->active_readonly_children = NULL;
  365. //handle->active_readonly_nchildren = NULL;
  366. //handle->nactive_readonly_children = 0;
  367. //handle->nsiblings
  368. //handle->siblings
  369. //handle->sibling_index
  370. //handle->depth
  371. /* there is no hierarchy yet */
  372. //handle->children = NULL;
  373. //handle->nchildren = 0;
  374. //handle->nplans = 0;
  375. //handle->switch_cl = NULL;
  376. //handle->switch_cl_nparts = 0;
  377. //handle->partitioned = 0;
  378. //handle->part_readonly = 0;
  379. //handle->active
  380. //handle->active_ro = 0;
  381. //handle->per_node below
  382. handle->ops = interface_ops;
  383. size_t interfacesize = interface_ops->interface_size;
  384. for (node = 0; node < STARPU_MAXNODES; node++)
  385. {
  386. _starpu_memory_stats_init_per_node(handle, node);
  387. struct _starpu_data_replicate *replicate;
  388. replicate = &handle->per_node[node];
  389. /* relaxed_coherency = 0 */
  390. replicate->handle = handle;
  391. _STARPU_CALLOC(replicate->data_interface, 1, interfacesize);
  392. if (handle->ops->init) handle->ops->init(replicate->data_interface);
  393. }
  394. //handle->per_worker = NULL;
  395. //handle->ops above
  396. //handle->footprint
  397. //handle->home_node
  398. //handle->wt_mask
  399. //handle->aliases = 0;
  400. //handle->is_not_important
  401. //handle->sequential_consistency
  402. //handle->initialized
  403. //handle->readonly
  404. //handle->ooc
  405. //handle->lazy_unregister = 0;
  406. //handle->partition_automatic_disabled = 0;
  407. //handle->removed_from_context_hash = 0;
  408. STARPU_PTHREAD_MUTEX_INIT0(&handle->sequential_consistency_mutex, NULL);
  409. handle->last_submitted_mode = STARPU_R;
  410. //handle->last_sync_task = NULL;
  411. //handle->last_submitted_accessors.task = NULL;
  412. handle->last_submitted_accessors.next = &handle->last_submitted_accessors;
  413. handle->last_submitted_accessors.prev = &handle->last_submitted_accessors;
  414. #ifdef STARPU_USE_FXT
  415. //handle->last_submitted_ghost_sync_id_is_valid = 0;
  416. //handle->last_submitted_ghost_sync_id = 0;
  417. //handle->last_submitted_ghost_accessors_id = NULL;
  418. #endif
  419. //handle->post_sync_tasks = NULL;
  420. /* Tell helgrind that the race in _starpu_unlock_post_sync_tasks is fine */
  421. STARPU_HG_DISABLE_CHECKING(handle->post_sync_tasks_cnt);
  422. //handle->post_sync_tasks_cnt = 0;
  423. //handle->redux_cl
  424. //handle->init_cl
  425. //handle->reduction_refcnt = 0;
  426. _starpu_data_requester_prio_list_init0(&handle->reduction_req_list);
  427. //handle->reduction_tmp_handles = NULL;
  428. //handle->write_invalidation_req = NULL;
  429. //handle->mpi_data = NULL; /* invalid until set */
  430. _starpu_memory_stats_init(handle);
  431. handle->mf_node = mf_node;
  432. //handle->unregister_hook = NULL;
  433. if (_starpu_global_arbiter)
  434. /* Just for testing purpose */
  435. starpu_data_assign_arbiter(handle, _starpu_global_arbiter);
  436. else
  437. {
  438. //handle->arbiter = NULL;
  439. }
  440. _starpu_data_requester_prio_list_init0(&handle->arbitered_req_list);
  441. handle->last_locality = -1;
  442. //handle->dimensions = 0;
  443. //handle->coordinates = {};
  444. //handle->user_data = NULL;
  445. return 0;
  446. }
  447. static
  448. starpu_data_handle_t _starpu_data_handle_allocate(struct starpu_data_interface_ops *interface_ops, unsigned int mf_node)
  449. {
  450. starpu_data_handle_t handle;
  451. _STARPU_CALLOC(handle, 1, sizeof(struct _starpu_data_state));
  452. _starpu_data_handle_init(handle, interface_ops, mf_node);
  453. return handle;
  454. }
  455. void starpu_data_register(starpu_data_handle_t *handleptr, int home_node,
  456. void *data_interface,
  457. struct starpu_data_interface_ops *ops)
  458. {
  459. starpu_data_handle_t handle = _starpu_data_handle_allocate(ops, home_node);
  460. STARPU_ASSERT(handleptr);
  461. *handleptr = handle;
  462. /* check the interfaceid is set */
  463. STARPU_ASSERT(ops->interfaceid != STARPU_UNKNOWN_INTERFACE_ID);
  464. /* fill the interface fields with the appropriate method */
  465. STARPU_ASSERT(ops->register_data_handle);
  466. ops->register_data_handle(handle, home_node, data_interface);
  467. if ((unsigned)ops->interfaceid >= STARPU_MAX_INTERFACE_ID)
  468. {
  469. if ((unsigned)ops->interfaceid > _id_to_ops_array_size)
  470. {
  471. if (!_id_to_ops_array_size)
  472. {
  473. _id_to_ops_array_size = 16;
  474. }
  475. else
  476. {
  477. _id_to_ops_array_size *= 2;
  478. }
  479. _STARPU_REALLOC(_id_to_ops_array, _id_to_ops_array_size * sizeof(struct starpu_data_interface_ops *));
  480. }
  481. _id_to_ops_array[ops->interfaceid-STARPU_MAX_INTERFACE_ID] = ops;
  482. }
  483. _starpu_register_new_data(handle, home_node, 0);
  484. _STARPU_TRACE_HANDLE_DATA_REGISTER(handle);
  485. }
  486. void starpu_data_register_same(starpu_data_handle_t *handledst, starpu_data_handle_t handlesrc)
  487. {
  488. void *local_interface = starpu_data_get_interface_on_node(handlesrc, STARPU_MAIN_RAM);
  489. starpu_data_register(handledst, -1, local_interface, handlesrc->ops);
  490. }
  491. void *starpu_data_handle_to_pointer(starpu_data_handle_t handle, unsigned node)
  492. {
  493. /* Check whether the operation is supported and the node has actually
  494. * been allocated. */
  495. if (!starpu_data_test_if_allocated_on_node(handle, node))
  496. return NULL;
  497. if (handle->ops->to_pointer)
  498. {
  499. return handle->ops->to_pointer(starpu_data_get_interface_on_node(handle, node), node);
  500. }
  501. /* Deprecated */
  502. if (handle->ops->handle_to_pointer)
  503. {
  504. return handle->ops->handle_to_pointer(handle, node);
  505. }
  506. return NULL;
  507. }
  508. int starpu_data_pointer_is_inside(starpu_data_handle_t handle, unsigned node, void *ptr)
  509. {
  510. /* Check whether the operation is supported and the node has actually
  511. * been allocated. */
  512. if (!starpu_data_test_if_allocated_on_node(handle, node))
  513. return 0;
  514. if (handle->ops->pointer_is_inside)
  515. {
  516. return handle->ops->pointer_is_inside(starpu_data_get_interface_on_node(handle, node), node, ptr);
  517. }
  518. /* Don't know :/ */
  519. return -1;
  520. }
  521. void *starpu_data_get_local_ptr(starpu_data_handle_t handle)
  522. {
  523. return starpu_data_handle_to_pointer(handle, starpu_worker_get_local_memory_node());
  524. }
  525. struct starpu_data_interface_ops* starpu_data_get_interface_ops(starpu_data_handle_t handle)
  526. {
  527. return handle->ops;
  528. }
  529. /*
  530. * Stop monitoring a piece of data
  531. */
  532. void _starpu_data_unregister_ram_pointer(starpu_data_handle_t handle, unsigned node)
  533. {
  534. if (starpu_node_get_kind(node) != STARPU_CPU_RAM)
  535. return;
  536. #ifdef STARPU_OPENMP
  537. if (handle->removed_from_context_hash)
  538. return;
  539. #endif
  540. const void *ram_ptr = starpu_data_handle_to_pointer(handle, node);
  541. if (ram_ptr != NULL)
  542. {
  543. /* Remove the PTR -> HANDLE mapping. If a mapping from PTR
  544. * to another handle existed before (e.g., when using
  545. * filters), it becomes visible again. */
  546. struct handle_entry *entry;
  547. #ifdef STARPU_OPENMP
  548. struct starpu_omp_task *task = _starpu_omp_get_task();
  549. if (task)
  550. {
  551. if (task->flags & STARPU_OMP_TASK_FLAGS_IMPLICIT)
  552. {
  553. struct starpu_omp_region *parallel_region = task->owner_region;
  554. _starpu_spin_lock(&parallel_region->registered_handles_lock);
  555. HASH_FIND_PTR(parallel_region->registered_handles, &ram_ptr, entry);
  556. STARPU_ASSERT(entry != NULL);
  557. HASH_DEL(registered_handles, entry);
  558. _starpu_spin_unlock(&parallel_region->registered_handles_lock);
  559. }
  560. else
  561. {
  562. HASH_FIND_PTR(task->registered_handles, &ram_ptr, entry);
  563. STARPU_ASSERT(entry != NULL);
  564. HASH_DEL(task->registered_handles, entry);
  565. }
  566. }
  567. else
  568. #endif
  569. {
  570. _starpu_spin_lock(&registered_handles_lock);
  571. HASH_FIND_PTR(registered_handles, &ram_ptr, entry);
  572. if (entry)
  573. {
  574. if (entry->handle == handle)
  575. {
  576. nregistered--;
  577. HASH_DEL(registered_handles, entry);
  578. }
  579. else
  580. /* don't free it, it's not ours */
  581. entry = NULL;
  582. }
  583. _starpu_spin_unlock(&registered_handles_lock);
  584. }
  585. free(entry);
  586. }
  587. }
  588. void _starpu_data_free_interfaces(starpu_data_handle_t handle)
  589. {
  590. unsigned node;
  591. unsigned nworkers = starpu_worker_get_count();
  592. for (node = 0; node < STARPU_MAXNODES; node++)
  593. free(handle->per_node[node].data_interface);
  594. if (handle->per_worker)
  595. {
  596. unsigned worker;
  597. for (worker = 0; worker < nworkers; worker++)
  598. free(handle->per_worker[worker].data_interface);
  599. free(handle->per_worker);
  600. }
  601. }
  602. struct _starpu_unregister_callback_arg
  603. {
  604. unsigned memory_node;
  605. starpu_data_handle_t handle;
  606. unsigned terminated;
  607. starpu_pthread_mutex_t mutex;
  608. starpu_pthread_cond_t cond;
  609. };
  610. /* Check whether we should tell starpu_data_unregister that the data handle is
  611. * not busy any more.
  612. * The header is supposed to be locked.
  613. * This may free the handle, if it was lazily unregistered (1 is returned in
  614. * that case). The handle pointer thus becomes invalid for the caller.
  615. *
  616. * Note: we inline some of the tests in the _starpu_data_check_not_busy macro.
  617. */
  618. int __starpu_data_check_not_busy(starpu_data_handle_t handle)
  619. {
  620. if (STARPU_LIKELY(handle->busy_count))
  621. return 0;
  622. /* Not busy any more, perhaps have to unregister etc. */
  623. if (STARPU_UNLIKELY(handle->busy_waiting))
  624. {
  625. STARPU_PTHREAD_MUTEX_LOCK(&handle->busy_mutex);
  626. STARPU_PTHREAD_COND_BROADCAST(&handle->busy_cond);
  627. STARPU_PTHREAD_MUTEX_UNLOCK(&handle->busy_mutex);
  628. }
  629. /* The handle has been destroyed in between (eg. this was a temporary
  630. * handle created for a reduction.) */
  631. if (STARPU_UNLIKELY(handle->lazy_unregister))
  632. {
  633. handle->lazy_unregister = 0;
  634. _starpu_spin_unlock(&handle->header_lock);
  635. _starpu_data_unregister(handle, 0, 1);
  636. /* Warning: in case we unregister the handle, we must be sure
  637. * that the caller will not try to unlock the header after
  638. * !*/
  639. return 1;
  640. }
  641. return 0;
  642. }
  643. static
  644. void _starpu_check_if_valid_and_fetch_data_on_node(starpu_data_handle_t handle, struct _starpu_data_replicate *replicate, const char *origin)
  645. {
  646. unsigned node;
  647. unsigned nnodes = starpu_memory_nodes_get_count();
  648. int valid = 0;
  649. for (node = 0; node < nnodes; node++)
  650. {
  651. if (handle->per_node[node].state != STARPU_INVALID)
  652. {
  653. /* we found a copy ! */
  654. valid = 1;
  655. }
  656. }
  657. if (valid)
  658. {
  659. int ret = _starpu_fetch_data_on_node(handle, handle->home_node, replicate, STARPU_R, 0, NULL, STARPU_FETCH, 0, NULL, NULL, 0, origin);
  660. STARPU_ASSERT(!ret);
  661. _starpu_release_data_on_node(handle, 0, STARPU_NONE, replicate);
  662. }
  663. else
  664. {
  665. _starpu_spin_lock(&handle->header_lock);
  666. if (!_starpu_notify_data_dependencies(handle, STARPU_NONE))
  667. _starpu_spin_unlock(&handle->header_lock);
  668. }
  669. }
  670. static void _starpu_data_unregister_fetch_data_callback(void *_arg)
  671. {
  672. struct _starpu_unregister_callback_arg *arg = (struct _starpu_unregister_callback_arg *) _arg;
  673. starpu_data_handle_t handle = arg->handle;
  674. STARPU_ASSERT(handle);
  675. struct _starpu_data_replicate *replicate = &handle->per_node[arg->memory_node];
  676. _starpu_check_if_valid_and_fetch_data_on_node(handle, replicate, "_starpu_data_unregister_fetch_data_callback");
  677. /* unlock the caller */
  678. STARPU_PTHREAD_MUTEX_LOCK(&arg->mutex);
  679. arg->terminated = 1;
  680. STARPU_PTHREAD_COND_SIGNAL(&arg->cond);
  681. STARPU_PTHREAD_MUTEX_UNLOCK(&arg->mutex);
  682. }
  683. void _starpu_data_set_unregister_hook(starpu_data_handle_t handle, _starpu_data_handle_unregister_hook func)
  684. {
  685. STARPU_ASSERT(handle->unregister_hook == NULL);
  686. handle->unregister_hook = func;
  687. }
  688. /*
  689. * We are about to unregister this R/O data. There might be still other aliases,
  690. * in which case this returns 0. If not, users are not supposed to see it
  691. * any more, so detach it from their sight and return 1 to let unregistration happen.
  692. */
  693. static int _starpu_ro_data_detach(starpu_data_handle_t handle)
  694. {
  695. _starpu_spin_lock(&handle->header_lock);
  696. if (handle->aliases)
  697. {
  698. handle->aliases--;
  699. _starpu_spin_unlock(&handle->header_lock);
  700. return 0;
  701. }
  702. if (handle->readonly_dup)
  703. {
  704. STARPU_ASSERT(handle->readonly_dup->readonly_dup_of == handle);
  705. handle->readonly_dup->readonly_dup_of = NULL;
  706. handle->readonly_dup = NULL;
  707. }
  708. if (handle->readonly_dup_of)
  709. {
  710. STARPU_ASSERT(handle->readonly_dup_of->readonly_dup == handle);
  711. handle->readonly_dup_of->readonly_dup = NULL;
  712. handle->readonly_dup_of = NULL;
  713. }
  714. /* So that unregistration can use write dependencies to wait for
  715. * anything to finish */
  716. handle->readonly = 0;
  717. _starpu_spin_unlock(&handle->header_lock);
  718. return 1;
  719. }
  720. /* Unregister the data handle, perhaps we don't need to update the home_node
  721. * (in that case coherent is set to 0)
  722. * nowait is for internal use when we already know for sure that we won't have to wait.
  723. */
  724. static void _starpu_data_unregister(starpu_data_handle_t handle, unsigned coherent, unsigned nowait)
  725. {
  726. STARPU_ASSERT(handle);
  727. STARPU_ASSERT_MSG(handle->nchildren == 0, "data %p needs to be unpartitioned before unregistration", handle);
  728. STARPU_ASSERT_MSG(handle->nplans == 0, "data %p needs its partition plans to be cleaned before unregistration", handle);
  729. STARPU_ASSERT_MSG(handle->partitioned == 0, "data %p needs its partitioned plans to be unpartitioned before unregistration", handle);
  730. /* TODO: also check that it has the latest coherency */
  731. STARPU_ASSERT(!(nowait && handle->busy_count != 0));
  732. if (!_starpu_ro_data_detach(handle))
  733. return;
  734. int sequential_consistency = handle->sequential_consistency;
  735. if (sequential_consistency && !nowait)
  736. {
  737. /* We will acquire it in write mode to catch all dependencies,
  738. * but possibly it's not actually initialized. Fake it to avoid
  739. getting caught doing it */
  740. handle->initialized = 1;
  741. 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");
  742. /* If sequential consistency is enabled, wait until data is available */
  743. _starpu_data_wait_until_available(handle, handle->readonly?STARPU_R:STARPU_RW, "starpu_data_unregister");
  744. }
  745. if (coherent && !nowait)
  746. {
  747. 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");
  748. /* Fetch data in the home of the data to ensure we have a valid copy
  749. * where we registered it */
  750. int home_node = handle->home_node;
  751. if (home_node >= 0)
  752. {
  753. struct _starpu_unregister_callback_arg arg = { 0 };
  754. arg.handle = handle;
  755. arg.memory_node = (unsigned)home_node;
  756. arg.terminated = 0;
  757. STARPU_PTHREAD_MUTEX_INIT0(&arg.mutex, NULL);
  758. STARPU_PTHREAD_COND_INIT0(&arg.cond, NULL);
  759. if (!_starpu_attempt_to_submit_data_request_from_apps(handle, STARPU_R,
  760. _starpu_data_unregister_fetch_data_callback, &arg))
  761. {
  762. /* no one has locked this data yet, so we proceed immediately */
  763. struct _starpu_data_replicate *home_replicate = &handle->per_node[home_node];
  764. _starpu_check_if_valid_and_fetch_data_on_node(handle, home_replicate, "_starpu_data_unregister");
  765. }
  766. else
  767. {
  768. STARPU_PTHREAD_MUTEX_LOCK(&arg.mutex);
  769. while (!arg.terminated)
  770. STARPU_PTHREAD_COND_WAIT(&arg.cond, &arg.mutex);
  771. STARPU_PTHREAD_MUTEX_UNLOCK(&arg.mutex);
  772. }
  773. STARPU_PTHREAD_MUTEX_DESTROY(&arg.mutex);
  774. STARPU_PTHREAD_COND_DESTROY(&arg.cond);
  775. }
  776. /* If this handle uses a multiformat interface, we may have to convert
  777. * this piece of data back into the CPU format.
  778. * XXX : This is quite hacky, could we submit a task instead ?
  779. */
  780. if (_starpu_data_is_multiformat_handle(handle) && (starpu_node_get_kind(handle->mf_node) != STARPU_CPU_RAM))
  781. {
  782. _STARPU_DEBUG("Conversion needed\n");
  783. void *buffers[1];
  784. struct starpu_multiformat_interface *format_interface;
  785. home_node = handle->home_node;
  786. if (home_node < 0 || (starpu_node_get_kind(home_node) != STARPU_CPU_RAM))
  787. home_node = STARPU_MAIN_RAM;
  788. format_interface = (struct starpu_multiformat_interface *) starpu_data_get_interface_on_node(handle, home_node);
  789. struct starpu_codelet *cl = NULL;
  790. enum starpu_node_kind node_kind = starpu_node_get_kind(handle->mf_node);
  791. switch (node_kind)
  792. {
  793. #ifdef STARPU_USE_CUDA
  794. case STARPU_CUDA_RAM:
  795. {
  796. struct starpu_multiformat_data_interface_ops *mf_ops;
  797. mf_ops = (struct starpu_multiformat_data_interface_ops *) handle->ops->get_mf_ops(format_interface);
  798. cl = mf_ops->cuda_to_cpu_cl;
  799. break;
  800. }
  801. #endif
  802. #ifdef STARPU_USE_OPENCL
  803. case STARPU_OPENCL_RAM:
  804. {
  805. struct starpu_multiformat_data_interface_ops *mf_ops;
  806. mf_ops = (struct starpu_multiformat_data_interface_ops *) handle->ops->get_mf_ops(format_interface);
  807. cl = mf_ops->opencl_to_cpu_cl;
  808. break;
  809. }
  810. #endif
  811. #ifdef STARPU_USE_MIC
  812. case STARPU_MIC_RAM:
  813. {
  814. struct starpu_multiformat_data_interface_ops *mf_ops;
  815. mf_ops = (struct starpu_multiformat_data_interface_ops *) handle->ops->get_mf_ops(format_interface);
  816. cl = mf_ops->mic_to_cpu_cl;
  817. break;
  818. }
  819. #endif
  820. case STARPU_CPU_RAM: /* Impossible ! */
  821. default:
  822. STARPU_ABORT();
  823. }
  824. buffers[0] = format_interface;
  825. _starpu_cl_func_t func = _starpu_task_get_cpu_nth_implementation(cl, 0);
  826. STARPU_ASSERT(func);
  827. func(buffers, NULL);
  828. }
  829. }
  830. /* Prevent any further unregistration */
  831. handle->magic = 0;
  832. _starpu_spin_lock(&handle->header_lock);
  833. if (!coherent)
  834. {
  835. /* Should we postpone the unregister operation ? */
  836. if ((handle->busy_count > 0) && handle->lazy_unregister)
  837. {
  838. _starpu_spin_unlock(&handle->header_lock);
  839. return;
  840. }
  841. }
  842. /* Tell holders of references that we're starting waiting */
  843. handle->busy_waiting = 1;
  844. _starpu_spin_unlock(&handle->header_lock);
  845. retry_busy:
  846. /* Wait for all requests to finish (notably WT requests) */
  847. STARPU_PTHREAD_MUTEX_LOCK(&handle->busy_mutex);
  848. while (1)
  849. {
  850. /* Here helgrind would shout that this an unprotected access,
  851. * but this is actually fine: all threads who do busy_count--
  852. * are supposed to call _starpu_data_check_not_busy, which will
  853. * wake us up through the busy_mutex/busy_cond. */
  854. if (!handle->busy_count)
  855. break;
  856. /* This is woken by _starpu_data_check_not_busy, always called
  857. * after decrementing busy_count */
  858. STARPU_PTHREAD_COND_WAIT(&handle->busy_cond, &handle->busy_mutex);
  859. }
  860. STARPU_PTHREAD_MUTEX_UNLOCK(&handle->busy_mutex);
  861. /* Unregister MPI things after having waiting for MPI reqs etc. to settle down */
  862. if (handle->unregister_hook)
  863. {
  864. handle->unregister_hook(handle);
  865. handle->unregister_hook = NULL;
  866. }
  867. /* Wait for finished requests to release the handle */
  868. _starpu_spin_lock(&handle->header_lock);
  869. if (handle->busy_count)
  870. {
  871. /* Bad luck: some request went in in between, wait again... */
  872. _starpu_spin_unlock(&handle->header_lock);
  873. goto retry_busy;
  874. }
  875. size_t size = _starpu_data_get_alloc_size(handle);
  876. /* Destroy the data now */
  877. unsigned node;
  878. for (node = 0; node < STARPU_MAXNODES; node++)
  879. {
  880. struct _starpu_data_replicate *local = &handle->per_node[node];
  881. STARPU_ASSERT(!local->refcnt);
  882. if (local->allocated)
  883. {
  884. _starpu_data_unregister_ram_pointer(handle, node);
  885. /* free the data copy in a lazy fashion */
  886. if (local->automatically_allocated)
  887. _starpu_request_mem_chunk_removal(handle, local, node, size);
  888. }
  889. }
  890. if (handle->per_worker)
  891. {
  892. unsigned worker;
  893. unsigned nworkers = starpu_worker_get_count();
  894. for (worker = 0; worker < nworkers; worker++)
  895. {
  896. struct _starpu_data_replicate *local = &handle->per_worker[worker];
  897. STARPU_ASSERT(!local->refcnt);
  898. /* free the data copy in a lazy fashion */
  899. if (local->allocated && local->automatically_allocated)
  900. _starpu_request_mem_chunk_removal(handle, local, starpu_worker_get_memory_node(worker), size);
  901. }
  902. }
  903. _starpu_data_free_interfaces(handle);
  904. _starpu_memory_stats_free(handle);
  905. _starpu_spin_unlock(&handle->header_lock);
  906. _starpu_spin_destroy(&handle->header_lock);
  907. _starpu_data_clear_implicit(handle);
  908. free(handle->active_readonly_children);
  909. free(handle->active_readonly_nchildren);
  910. STARPU_PTHREAD_MUTEX_DESTROY(&handle->busy_mutex);
  911. STARPU_PTHREAD_COND_DESTROY(&handle->busy_cond);
  912. STARPU_PTHREAD_MUTEX_DESTROY(&handle->sequential_consistency_mutex);
  913. STARPU_HG_ENABLE_CHECKING(handle->post_sync_tasks_cnt);
  914. STARPU_HG_ENABLE_CHECKING(handle->busy_count);
  915. _starpu_data_requester_prio_list_deinit(&handle->req_list);
  916. _starpu_data_requester_prio_list_deinit(&handle->reduction_req_list);
  917. if (handle->switch_cl)
  918. {
  919. free(handle->switch_cl->dyn_nodes);
  920. free(handle->switch_cl);
  921. }
  922. _STARPU_TRACE_HANDLE_DATA_UNREGISTER(handle);
  923. free(handle);
  924. }
  925. void starpu_data_unregister(starpu_data_handle_t handle)
  926. {
  927. STARPU_ASSERT_MSG(handle->magic == 42, "data %p is invalid (was it already registered?)", handle);
  928. STARPU_ASSERT_MSG(!handle->lazy_unregister, "data %p can not be unregistered twice", handle);
  929. _starpu_data_unregister(handle, 1, 0);
  930. }
  931. void starpu_data_unregister_no_coherency(starpu_data_handle_t handle)
  932. {
  933. STARPU_ASSERT_MSG(handle->magic == 42, "data %p is invalid (was it already registered?)", handle);
  934. _starpu_data_unregister(handle, 0, 0);
  935. }
  936. static void _starpu_data_unregister_submit_cb(void *arg)
  937. {
  938. starpu_data_handle_t handle = arg;
  939. _starpu_spin_lock(&handle->header_lock);
  940. handle->lazy_unregister = 1;
  941. /* The handle should be busy since we are working on it.
  942. * when we releases the handle below, it will be destroyed by
  943. * _starpu_data_check_not_busy */
  944. STARPU_ASSERT(handle->busy_count);
  945. _starpu_spin_unlock(&handle->header_lock);
  946. starpu_data_release_on_node(handle, STARPU_ACQUIRE_NO_NODE_LOCK_ALL);
  947. }
  948. void starpu_data_unregister_submit(starpu_data_handle_t handle)
  949. {
  950. STARPU_ASSERT_MSG(handle->magic == 42, "data %p is invalid (was it already registered?)", handle);
  951. STARPU_ASSERT_MSG(!handle->lazy_unregister, "data %p can not be unregistered twice", handle);
  952. if (!_starpu_ro_data_detach(handle))
  953. return;
  954. /* Wait for all task dependencies on this handle before putting it for free */
  955. 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);
  956. }
  957. static void _starpu_data_invalidate(void *data)
  958. {
  959. starpu_data_handle_t handle = data;
  960. size_t size = _starpu_data_get_alloc_size(handle);
  961. _starpu_spin_lock(&handle->header_lock);
  962. //_STARPU_DEBUG("Really invalidating data %p\n", data);
  963. #ifdef STARPU_DEBUG
  964. {
  965. /* There shouldn't be any pending request since we acquired the data in W mode */
  966. unsigned i, j, nnodes = starpu_memory_nodes_get_count();
  967. for (i = 0; i < nnodes; i++)
  968. for (j = 0; j < nnodes; j++)
  969. STARPU_ASSERT_MSG(!handle->per_node[i].request[j], "request for handle %p pending from %u to %u while invalidating data!", handle, j, i);
  970. }
  971. #endif
  972. unsigned node;
  973. for (node = 0; node < STARPU_MAXNODES; node++)
  974. {
  975. struct _starpu_data_replicate *local = &handle->per_node[node];
  976. if (local->mc && local->allocated && local->automatically_allocated)
  977. {
  978. _starpu_data_unregister_ram_pointer(handle, node);
  979. /* free the data copy in a lazy fashion */
  980. _starpu_request_mem_chunk_removal(handle, local, node, size);
  981. }
  982. if (local->state != STARPU_INVALID)
  983. _STARPU_TRACE_DATA_STATE_INVALID(handle, node);
  984. local->state = STARPU_INVALID;
  985. }
  986. if (handle->per_worker)
  987. {
  988. unsigned worker;
  989. unsigned nworkers = starpu_worker_get_count();
  990. for (worker = 0; worker < nworkers; worker++)
  991. {
  992. struct _starpu_data_replicate *local = &handle->per_worker[worker];
  993. if (local->mc && local->allocated && local->automatically_allocated)
  994. /* free the data copy in a lazy fashion */
  995. _starpu_request_mem_chunk_removal(handle, local, starpu_worker_get_memory_node(worker), size);
  996. local->state = STARPU_INVALID;
  997. }
  998. }
  999. _starpu_spin_unlock(&handle->header_lock);
  1000. starpu_data_release_on_node(handle, STARPU_ACQUIRE_NO_NODE_LOCK_ALL);
  1001. }
  1002. void starpu_data_invalidate(starpu_data_handle_t handle)
  1003. {
  1004. STARPU_ASSERT(handle);
  1005. starpu_data_acquire_on_node(handle, STARPU_ACQUIRE_NO_NODE_LOCK_ALL, STARPU_W);
  1006. _starpu_data_invalidate(handle);
  1007. handle->initialized = 0;
  1008. }
  1009. void starpu_data_invalidate_submit(starpu_data_handle_t handle)
  1010. {
  1011. STARPU_ASSERT(handle);
  1012. starpu_data_acquire_on_node_cb(handle, STARPU_ACQUIRE_NO_NODE_LOCK_ALL, STARPU_W, _starpu_data_invalidate, handle);
  1013. handle->initialized = 0;
  1014. }
  1015. enum starpu_data_interface_id starpu_data_get_interface_id(starpu_data_handle_t handle)
  1016. {
  1017. return handle->ops->interfaceid;
  1018. }
  1019. void *starpu_data_get_interface_on_node(starpu_data_handle_t handle, unsigned memory_node)
  1020. {
  1021. return handle->per_node[memory_node].data_interface;
  1022. }
  1023. int starpu_data_interface_get_next_id(void)
  1024. {
  1025. _data_interface_number += 1;
  1026. return _data_interface_number-1;
  1027. }
  1028. int starpu_data_pack_node(starpu_data_handle_t handle, unsigned node, void **ptr, starpu_ssize_t *count)
  1029. {
  1030. STARPU_ASSERT_MSG(handle->ops->pack_data, "The datatype interface %s (%d) does not have a pack operation", handle->ops->name, handle->ops->interfaceid);
  1031. return handle->ops->pack_data(handle, node, ptr, count);
  1032. }
  1033. int starpu_data_pack(starpu_data_handle_t handle, void **ptr, starpu_ssize_t *count)
  1034. {
  1035. return starpu_data_pack_node(handle, starpu_worker_get_local_memory_node(), ptr, count);
  1036. }
  1037. int starpu_data_peek_node(starpu_data_handle_t handle, unsigned node, void *ptr, size_t count)
  1038. {
  1039. STARPU_ASSERT_MSG(handle->ops->peek_data, "The datatype interface %s (%d) does not have a peek operation", handle->ops->name, handle->ops->interfaceid);
  1040. int ret;
  1041. ret = handle->ops->peek_data(handle, node, ptr, count);
  1042. return ret;
  1043. }
  1044. int starpu_data_peek(starpu_data_handle_t handle, void *ptr, size_t count)
  1045. {
  1046. return starpu_data_peek_node(handle, starpu_worker_get_local_memory_node(), ptr, count);
  1047. }
  1048. int starpu_data_unpack_node(starpu_data_handle_t handle, unsigned node, void *ptr, size_t count)
  1049. {
  1050. STARPU_ASSERT_MSG(handle->ops->unpack_data, "The datatype interface %s (%d) does not have an unpack operation", handle->ops->name, handle->ops->interfaceid);
  1051. int ret;
  1052. ret = handle->ops->unpack_data(handle, node, ptr, count);
  1053. return ret;
  1054. }
  1055. int starpu_data_unpack(starpu_data_handle_t handle, void *ptr, size_t count)
  1056. {
  1057. return starpu_data_unpack_node(handle, starpu_worker_get_local_memory_node(), ptr, count);
  1058. }
  1059. size_t starpu_data_get_size(starpu_data_handle_t handle)
  1060. {
  1061. return handle->ops->get_size(handle);
  1062. }
  1063. size_t starpu_data_get_alloc_size(starpu_data_handle_t handle)
  1064. {
  1065. if (handle->ops->get_alloc_size)
  1066. return handle->ops->get_alloc_size(handle);
  1067. else
  1068. return handle->ops->get_size(handle);
  1069. }
  1070. void starpu_data_set_name(starpu_data_handle_t handle STARPU_ATTRIBUTE_UNUSED, const char *name STARPU_ATTRIBUTE_UNUSED)
  1071. {
  1072. _STARPU_TRACE_DATA_NAME(handle, name);
  1073. }
  1074. int starpu_data_get_home_node(starpu_data_handle_t handle)
  1075. {
  1076. return handle->home_node;
  1077. }
  1078. void starpu_data_set_coordinates_array(starpu_data_handle_t handle, unsigned dimensions, int dims[])
  1079. {
  1080. unsigned i;
  1081. unsigned max_dimensions = sizeof(handle->coordinates)/sizeof(handle->coordinates[0]);
  1082. if (dimensions > max_dimensions)
  1083. dimensions = max_dimensions;
  1084. handle->dimensions = dimensions;
  1085. for (i = 0; i < dimensions; i++)
  1086. handle->coordinates[i] = dims[i];
  1087. _STARPU_TRACE_DATA_COORDINATES(handle, dimensions, dims);
  1088. }
  1089. void starpu_data_set_coordinates(starpu_data_handle_t handle, unsigned dimensions, ...)
  1090. {
  1091. int dims[dimensions];
  1092. unsigned i;
  1093. va_list varg_list;
  1094. va_start(varg_list, dimensions);
  1095. for (i = 0; i < dimensions; i++)
  1096. dims[i] = va_arg(varg_list, int);
  1097. va_end(varg_list);
  1098. starpu_data_set_coordinates_array(handle, dimensions, dims);
  1099. }
  1100. unsigned starpu_data_get_coordinates_array(starpu_data_handle_t handle, unsigned dimensions, int dims[])
  1101. {
  1102. unsigned i;
  1103. if (dimensions > handle->dimensions)
  1104. dimensions = handle->dimensions;
  1105. for (i = 0; i < dimensions; i++)
  1106. dims[i] = handle->coordinates[i];
  1107. return dimensions;
  1108. }
  1109. void starpu_data_print(starpu_data_handle_t handle, unsigned node, FILE *stream)
  1110. {
  1111. if (handle->ops == NULL)
  1112. fprintf(stream, "Undefined");
  1113. else
  1114. {
  1115. switch (handle->ops->interfaceid)
  1116. {
  1117. case(STARPU_MATRIX_INTERFACE_ID):
  1118. fprintf(stream, "Matrix");
  1119. break;
  1120. case(STARPU_BLOCK_INTERFACE_ID):
  1121. fprintf(stream, "Block");
  1122. break;
  1123. case(STARPU_VECTOR_INTERFACE_ID):
  1124. fprintf(stream, "Vector");
  1125. break;
  1126. case(STARPU_CSR_INTERFACE_ID):
  1127. fprintf(stream, "CSR");
  1128. break;
  1129. case(STARPU_BCSR_INTERFACE_ID):
  1130. fprintf(stream, "BCSR");
  1131. break;
  1132. case(STARPU_VARIABLE_INTERFACE_ID):
  1133. fprintf(stream, "Variable");
  1134. break;
  1135. case(STARPU_VOID_INTERFACE_ID):
  1136. fprintf(stream, "Void");
  1137. break;
  1138. case(STARPU_MULTIFORMAT_INTERFACE_ID):
  1139. fprintf(stream, "Multfiformat");
  1140. break;
  1141. case(STARPU_COO_INTERFACE_ID):
  1142. fprintf(stream, "COO");
  1143. break;
  1144. case(STARPU_TENSOR_INTERFACE_ID):
  1145. fprintf(stream, "Tensor");
  1146. break;
  1147. case(STARPU_UNKNOWN_INTERFACE_ID ):
  1148. fprintf(stream, "UNKNOWN");
  1149. break;
  1150. default:
  1151. fprintf(stream, "User interface with id %d", handle->ops->interfaceid);
  1152. break;
  1153. }
  1154. }
  1155. void *data_interface = NULL;
  1156. if (starpu_data_test_if_allocated_on_node(handle, node))
  1157. data_interface = starpu_data_get_interface_on_node(handle, node);
  1158. if (starpu_data_test_if_allocated_on_node(handle, handle->home_node))
  1159. data_interface = starpu_data_get_interface_on_node(handle, handle->home_node);
  1160. if (handle->ops && handle->ops->describe && data_interface)
  1161. {
  1162. char buffer[1024];
  1163. handle->ops->describe(data_interface, buffer, sizeof(buffer));
  1164. fprintf(stream, " %s\n", buffer);
  1165. }
  1166. else
  1167. fprintf(stream, "\n");
  1168. }