data_interface.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009, 2010, 2011 Université de Bordeaux 1
  4. * Copyright (C) 2010, 2011 Centre National de la Recherche Scientifique
  5. *
  6. * StarPU is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation; either version 2.1 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * StarPU is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. *
  15. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  16. */
  17. #include <stdint.h>
  18. #include <datawizard/datawizard.h>
  19. #include <core/dependencies/data_concurrency.h>
  20. #include <common/uthash.h>
  21. #include <common/starpu_spinlock.h>
  22. /* Entry in the `registered_handles' hash table. */
  23. struct handle_entry
  24. {
  25. UT_hash_handle hh;
  26. void *pointer;
  27. starpu_data_handle_t handle;
  28. };
  29. /* Hash table mapping host pointers to data handles. */
  30. static struct handle_entry *registered_handles;
  31. static struct _starpu_spinlock registered_handles_lock;
  32. void _starpu_data_interface_init()
  33. {
  34. _starpu_spin_init(&registered_handles_lock);
  35. }
  36. void _starpu_data_interface_shutdown()
  37. {
  38. struct handle_entry *entry, *tmp;
  39. _starpu_spin_destroy(&registered_handles_lock);
  40. HASH_ITER(hh, registered_handles, entry, tmp) {
  41. HASH_DEL(registered_handles, entry);
  42. free(entry);
  43. }
  44. registered_handles = NULL;
  45. }
  46. /* Register the mapping from PTR to HANDLE. If PTR is already mapped to
  47. * some handle, the new mapping shadows the previous one. */
  48. void _starpu_data_register_ram_pointer(starpu_data_handle_t handle, void *ptr)
  49. {
  50. struct handle_entry *entry;
  51. entry = (struct handle_entry *) malloc(sizeof(*entry));
  52. STARPU_ASSERT(entry != NULL);
  53. entry->pointer = ptr;
  54. entry->handle = handle;
  55. _starpu_spin_lock(&registered_handles_lock);
  56. HASH_ADD_PTR(registered_handles, pointer, entry);
  57. _starpu_spin_unlock(&registered_handles_lock);
  58. }
  59. starpu_data_handle_t starpu_data_lookup(const void *ptr)
  60. {
  61. starpu_data_handle_t result;
  62. _starpu_spin_lock(&registered_handles_lock);
  63. {
  64. struct handle_entry *entry;
  65. HASH_FIND_PTR(registered_handles, &ptr, entry);
  66. if(STARPU_UNLIKELY(entry == NULL))
  67. result = NULL;
  68. else
  69. result = entry->handle;
  70. }
  71. _starpu_spin_unlock(&registered_handles_lock);
  72. return result;
  73. }
  74. /*
  75. * Start monitoring a piece of data
  76. */
  77. static void _starpu_register_new_data(starpu_data_handle_t handle,
  78. uint32_t home_node, uint32_t wt_mask)
  79. {
  80. void *ptr;
  81. STARPU_ASSERT(handle);
  82. /* initialize the new lock */
  83. handle->req_list = _starpu_data_requester_list_new();
  84. handle->refcnt = 0;
  85. handle->busy_count = 0;
  86. handle->busy_waiting = 0;
  87. _STARPU_PTHREAD_MUTEX_INIT(&handle->busy_mutex, NULL);
  88. _STARPU_PTHREAD_COND_INIT(&handle->busy_cond, NULL);
  89. _starpu_spin_init(&handle->header_lock);
  90. /* first take care to properly lock the data */
  91. _starpu_spin_lock(&handle->header_lock);
  92. /* there is no hierarchy yet */
  93. handle->nchildren = 0;
  94. handle->root_handle = handle;
  95. handle->father_handle = NULL;
  96. handle->sibling_index = 0; /* could be anything for the root */
  97. handle->depth = 1; /* the tree is just a node yet */
  98. handle->rank = -1; /* invalid until set */
  99. handle->tag = -1; /* invalid until set */
  100. handle->is_not_important = 0;
  101. handle->sequential_consistency =
  102. starpu_data_get_default_sequential_consistency_flag();
  103. _STARPU_PTHREAD_MUTEX_INIT(&handle->sequential_consistency_mutex, NULL);
  104. handle->last_submitted_mode = STARPU_R;
  105. handle->last_submitted_writer = NULL;
  106. handle->last_submitted_readers = NULL;
  107. handle->post_sync_tasks = NULL;
  108. handle->post_sync_tasks_cnt = 0;
  109. /* By default, there are no methods available to perform a reduction */
  110. handle->redux_cl = NULL;
  111. handle->init_cl = NULL;
  112. handle->reduction_refcnt = 0;
  113. handle->reduction_req_list = _starpu_data_requester_list_new();
  114. #ifdef STARPU_USE_FXT
  115. handle->last_submitted_ghost_writer_id_is_valid = 0;
  116. handle->last_submitted_ghost_writer_id = 0;
  117. handle->last_submitted_ghost_readers_id = NULL;
  118. #endif
  119. handle->wt_mask = wt_mask;
  120. /* Store some values directly in the handle not to recompute them all
  121. * the time. */
  122. handle->data_size = handle->ops->get_size(handle);
  123. handle->footprint = _starpu_compute_data_footprint(handle);
  124. handle->home_node = home_node;
  125. /* that new data is invalid from all nodes perpective except for the
  126. * home node */
  127. unsigned node;
  128. for (node = 0; node < STARPU_MAXNODES; node++)
  129. {
  130. struct _starpu_data_replicate *replicate;
  131. replicate = &handle->per_node[node];
  132. replicate->memory_node = node;
  133. replicate->relaxed_coherency = 0;
  134. replicate->refcnt = 0;
  135. if (node == home_node) {
  136. /* this is the home node with the only valid copy */
  137. replicate->state = STARPU_OWNER;
  138. replicate->allocated = 1;
  139. replicate->automatically_allocated = 0;
  140. }
  141. else {
  142. /* the value is not available here yet */
  143. replicate->state = STARPU_INVALID;
  144. replicate->allocated = 0;
  145. }
  146. }
  147. unsigned worker;
  148. unsigned nworkers = starpu_worker_get_count();
  149. for (worker = 0; worker < nworkers; worker++)
  150. {
  151. struct _starpu_data_replicate *replicate;
  152. replicate = &handle->per_worker[worker];
  153. replicate->allocated = 0;
  154. replicate->automatically_allocated = 0;
  155. replicate->state = STARPU_INVALID;
  156. replicate->refcnt = 0;
  157. replicate->handle = handle;
  158. for (node = 0; node < STARPU_MAXNODES; node++)
  159. {
  160. replicate->requested[node] = 0;
  161. replicate->request[node] = NULL;
  162. }
  163. replicate->relaxed_coherency = 1;
  164. replicate->initialized = 0;
  165. replicate->memory_node = starpu_worker_get_memory_node(worker);
  166. /* duplicate the content of the interface on node 0 */
  167. memcpy(replicate->data_interface, handle->per_node[0].data_interface, handle->ops->interface_size);
  168. }
  169. /* now the data is available ! */
  170. _starpu_spin_unlock(&handle->header_lock);
  171. ptr = starpu_handle_to_pointer(handle, 0);
  172. if (ptr != NULL)
  173. {
  174. _starpu_data_register_ram_pointer(handle, ptr);
  175. }
  176. }
  177. static starpu_data_handle_t _starpu_data_handle_allocate(struct starpu_data_interface_ops *interface_ops)
  178. {
  179. starpu_data_handle_t handle = (starpu_data_handle_t) calloc(1, sizeof(struct _starpu_data_state));
  180. STARPU_ASSERT(handle);
  181. handle->ops = interface_ops;
  182. size_t interfacesize = interface_ops->interface_size;
  183. unsigned node;
  184. for (node = 0; node < STARPU_MAXNODES; node++)
  185. {
  186. #ifdef STARPU_MEMORY_STATUS
  187. /* Stats initilization */
  188. handle->stats_direct_access[node]=0;
  189. handle->stats_loaded_shared[node]=0;
  190. handle->stats_shared_to_owner[node]=0;
  191. handle->stats_loaded_owner[node]=0;
  192. handle->stats_invalidated[node]=0;
  193. #endif
  194. struct _starpu_data_replicate *replicate;
  195. replicate = &handle->per_node[node];
  196. /* relaxed_coherency = 0 */
  197. replicate->handle = handle;
  198. replicate->data_interface = calloc(1, interfacesize);
  199. STARPU_ASSERT(replicate->data_interface);
  200. }
  201. unsigned worker;
  202. unsigned nworkers = starpu_worker_get_count();
  203. for (worker = 0; worker < nworkers; worker++)
  204. {
  205. struct _starpu_data_replicate *replicate;
  206. replicate = &handle->per_worker[worker];
  207. replicate->handle = handle;
  208. replicate->data_interface = calloc(1, interfacesize);
  209. STARPU_ASSERT(replicate->data_interface);
  210. }
  211. return handle;
  212. }
  213. void starpu_data_register(starpu_data_handle_t *handleptr, uint32_t home_node,
  214. void *data_interface,
  215. struct starpu_data_interface_ops *ops)
  216. {
  217. starpu_data_handle_t handle =
  218. _starpu_data_handle_allocate(ops);
  219. STARPU_ASSERT(handleptr);
  220. *handleptr = handle;
  221. /* fill the interface fields with the appropriate method */
  222. ops->register_data_handle(handle, home_node, data_interface);
  223. _starpu_register_new_data(handle, home_node, 0);
  224. }
  225. void *starpu_handle_to_pointer(starpu_data_handle_t handle, uint32_t node)
  226. {
  227. /* Check whether the operation is supported and the node has actually
  228. * been allocated. */
  229. if (handle->ops->handle_to_pointer
  230. && starpu_data_test_if_allocated_on_node(handle, node))
  231. {
  232. return handle->ops->handle_to_pointer(handle, node);
  233. }
  234. return NULL;
  235. }
  236. void *starpu_handle_get_local_ptr(starpu_data_handle_t handle)
  237. {
  238. return starpu_handle_to_pointer(handle,
  239. _starpu_get_local_memory_node());
  240. }
  241. int starpu_data_get_rank(starpu_data_handle_t handle)
  242. {
  243. return handle->rank;
  244. }
  245. int starpu_data_set_rank(starpu_data_handle_t handle, int rank)
  246. {
  247. handle->rank = rank;
  248. return 0;
  249. }
  250. int starpu_data_get_tag(starpu_data_handle_t handle)
  251. {
  252. return handle->tag;
  253. }
  254. int starpu_data_set_tag(starpu_data_handle_t handle, int tag)
  255. {
  256. handle->tag = tag;
  257. return 0;
  258. }
  259. /*
  260. * Stop monitoring a piece of data
  261. */
  262. void _starpu_data_free_interfaces(starpu_data_handle_t handle)
  263. {
  264. const void *ram_ptr;
  265. unsigned node;
  266. unsigned worker;
  267. unsigned nworkers = starpu_worker_get_count();
  268. ram_ptr = starpu_handle_to_pointer(handle, 0);
  269. for (node = 0; node < STARPU_MAXNODES; node++)
  270. free(handle->per_node[node].data_interface);
  271. for (worker = 0; worker < nworkers; worker++)
  272. free(handle->per_worker[worker].data_interface);
  273. if (ram_ptr != NULL)
  274. {
  275. /* Remove the PTR -> HANDLE mapping. If a mapping from PTR
  276. * to another handle existed before (e.g., when using
  277. * filters), it becomes visible again. */
  278. struct handle_entry *entry;
  279. _starpu_spin_lock(&registered_handles_lock);
  280. HASH_FIND_PTR(registered_handles, &ram_ptr, entry);
  281. STARPU_ASSERT(entry != NULL);
  282. HASH_DEL(registered_handles, entry);
  283. free(entry);
  284. _starpu_spin_unlock(&registered_handles_lock);
  285. }
  286. }
  287. struct _starpu_unregister_callback_arg {
  288. unsigned memory_node;
  289. starpu_data_handle_t handle;
  290. unsigned terminated;
  291. pthread_mutex_t mutex;
  292. pthread_cond_t cond;
  293. };
  294. /* Check whether we should tell starpu_data_unregister that the data handle is
  295. * not busy any more.
  296. * The header is supposed to be locked */
  297. void _starpu_data_check_not_busy(starpu_data_handle_t handle)
  298. {
  299. if (!handle->busy_count && handle->busy_waiting) {
  300. _STARPU_PTHREAD_MUTEX_LOCK(&handle->busy_mutex);
  301. _STARPU_PTHREAD_COND_BROADCAST(&handle->busy_cond);
  302. _STARPU_PTHREAD_MUTEX_UNLOCK(&handle->busy_mutex);
  303. }
  304. }
  305. static void _starpu_data_unregister_fetch_data_callback(void *_arg)
  306. {
  307. int ret;
  308. struct _starpu_unregister_callback_arg *arg = (struct _starpu_unregister_callback_arg *) _arg;
  309. starpu_data_handle_t handle = arg->handle;
  310. STARPU_ASSERT(handle);
  311. struct _starpu_data_replicate *replicate = &handle->per_node[arg->memory_node];
  312. ret = _starpu_fetch_data_on_node(handle, replicate, STARPU_R, 0, NULL, NULL);
  313. STARPU_ASSERT(!ret);
  314. /* unlock the caller */
  315. _STARPU_PTHREAD_MUTEX_LOCK(&arg->mutex);
  316. arg->terminated = 1;
  317. _STARPU_PTHREAD_COND_SIGNAL(&arg->cond);
  318. _STARPU_PTHREAD_MUTEX_UNLOCK(&arg->mutex);
  319. }
  320. /* Unregister the data handle, perhaps we don't need to update the home_node
  321. * (in that case coherent is set to 0) */
  322. static void _starpu_data_unregister(starpu_data_handle_t handle, unsigned coherent)
  323. {
  324. STARPU_ASSERT(handle);
  325. if (coherent)
  326. {
  327. /* If sequential consistency is enabled, wait until data is available */
  328. _starpu_data_wait_until_available(handle, STARPU_RW);
  329. /* Fetch data in the home of the data to ensure we have a valid copy
  330. * where we registered it */
  331. int home_node = handle->home_node;
  332. if (home_node >= 0)
  333. {
  334. struct _starpu_unregister_callback_arg arg;
  335. arg.handle = handle;
  336. arg.memory_node = (unsigned)home_node;
  337. arg.terminated = 0;
  338. _STARPU_PTHREAD_MUTEX_INIT(&arg.mutex, NULL);
  339. _STARPU_PTHREAD_COND_INIT(&arg.cond, NULL);
  340. if (!_starpu_attempt_to_submit_data_request_from_apps(handle, STARPU_R,
  341. _starpu_data_unregister_fetch_data_callback, &arg))
  342. {
  343. /* no one has locked this data yet, so we proceed immediately */
  344. struct _starpu_data_replicate *home_replicate = &handle->per_node[home_node];
  345. int ret = _starpu_fetch_data_on_node(handle, home_replicate, STARPU_R, 0, NULL, NULL);
  346. STARPU_ASSERT(!ret);
  347. }
  348. else {
  349. _STARPU_PTHREAD_MUTEX_LOCK(&arg.mutex);
  350. while (!arg.terminated)
  351. _STARPU_PTHREAD_COND_WAIT(&arg.cond, &arg.mutex);
  352. _STARPU_PTHREAD_MUTEX_UNLOCK(&arg.mutex);
  353. }
  354. _starpu_release_data_on_node(handle, 0, &handle->per_node[home_node]);
  355. }
  356. }
  357. else {
  358. /* Should we postpone the unregister operation ? */
  359. if ((handle->refcnt > 0) && handle->lazy_unregister)
  360. return;
  361. }
  362. _starpu_spin_lock(&handle->header_lock);
  363. /* Tell holders of references that we're starting waiting */
  364. handle->busy_waiting = 1;
  365. _starpu_spin_unlock(&handle->header_lock);
  366. /* Wait for all requests to finish (notably WT requests) */
  367. _STARPU_PTHREAD_MUTEX_LOCK(&handle->busy_mutex);
  368. while (handle->busy_count)
  369. _STARPU_PTHREAD_COND_WAIT(&handle->busy_cond, &handle->busy_mutex);
  370. /* Wait for finished requests to release the handle */
  371. _starpu_spin_lock(&handle->header_lock);
  372. _starpu_data_free_interfaces(handle);
  373. /* Destroy the data now */
  374. unsigned node;
  375. for (node = 0; node < STARPU_MAXNODES; node++)
  376. {
  377. struct _starpu_data_replicate *local = &handle->per_node[node];
  378. if (local->allocated && local->automatically_allocated){
  379. /* free the data copy in a lazy fashion */
  380. _starpu_request_mem_chunk_removal(handle, node);
  381. }
  382. }
  383. _starpu_data_requester_list_delete(handle->req_list);
  384. _starpu_data_requester_list_delete(handle->reduction_req_list);
  385. free(handle);
  386. }
  387. void starpu_data_unregister(starpu_data_handle_t handle)
  388. {
  389. _starpu_data_unregister(handle, 1);
  390. }
  391. void starpu_data_unregister_no_coherency(starpu_data_handle_t handle)
  392. {
  393. _starpu_data_unregister(handle, 0);
  394. }
  395. void starpu_data_invalidate(starpu_data_handle_t handle)
  396. {
  397. STARPU_ASSERT(handle);
  398. starpu_data_acquire(handle, STARPU_W);
  399. _starpu_spin_lock(&handle->header_lock);
  400. unsigned node;
  401. for (node = 0; node < STARPU_MAXNODES; node++)
  402. {
  403. struct _starpu_data_replicate *local = &handle->per_node[node];
  404. if (local->allocated && local->automatically_allocated){
  405. /* free the data copy in a lazy fashion */
  406. _starpu_request_mem_chunk_removal(handle, node);
  407. }
  408. local->state = STARPU_INVALID;
  409. }
  410. _starpu_spin_unlock(&handle->header_lock);
  411. starpu_data_release(handle);
  412. }
  413. unsigned starpu_get_handle_interface_id(starpu_data_handle_t handle)
  414. {
  415. return handle->ops->interfaceid;
  416. }
  417. void *starpu_data_get_interface_on_node(starpu_data_handle_t handle, unsigned memory_node)
  418. {
  419. return handle->per_node[memory_node].data_interface;
  420. }