data_interface.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  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. {
  42. HASH_DEL(registered_handles, entry);
  43. free(entry);
  44. }
  45. registered_handles = NULL;
  46. }
  47. /* Register the mapping from PTR to HANDLE. If PTR is already mapped to
  48. * some handle, the new mapping shadows the previous one. */
  49. void _starpu_data_register_ram_pointer(starpu_data_handle_t handle, void *ptr)
  50. {
  51. struct handle_entry *entry;
  52. entry = (struct handle_entry *) malloc(sizeof(*entry));
  53. STARPU_ASSERT(entry != NULL);
  54. entry->pointer = ptr;
  55. entry->handle = handle;
  56. _starpu_spin_lock(&registered_handles_lock);
  57. HASH_ADD_PTR(registered_handles, pointer, entry);
  58. _starpu_spin_unlock(&registered_handles_lock);
  59. }
  60. starpu_data_handle_t starpu_data_lookup(const void *ptr)
  61. {
  62. starpu_data_handle_t result;
  63. _starpu_spin_lock(&registered_handles_lock);
  64. {
  65. struct handle_entry *entry;
  66. HASH_FIND_PTR(registered_handles, &ptr, entry);
  67. if(STARPU_UNLIKELY(entry == NULL))
  68. result = NULL;
  69. else
  70. result = entry->handle;
  71. }
  72. _starpu_spin_unlock(&registered_handles_lock);
  73. return result;
  74. }
  75. /*
  76. * Start monitoring a piece of data
  77. */
  78. static void _starpu_register_new_data(starpu_data_handle_t handle,
  79. uint32_t home_node, uint32_t wt_mask)
  80. {
  81. void *ptr;
  82. STARPU_ASSERT(handle);
  83. /* initialize the new lock */
  84. handle->req_list = _starpu_data_requester_list_new();
  85. handle->refcnt = 0;
  86. handle->busy_count = 0;
  87. handle->busy_waiting = 0;
  88. _STARPU_PTHREAD_MUTEX_INIT(&handle->busy_mutex, NULL);
  89. _STARPU_PTHREAD_COND_INIT(&handle->busy_cond, NULL);
  90. _starpu_spin_init(&handle->header_lock);
  91. /* first take care to properly lock the data */
  92. _starpu_spin_lock(&handle->header_lock);
  93. /* there is no hierarchy yet */
  94. handle->nchildren = 0;
  95. handle->root_handle = handle;
  96. handle->father_handle = NULL;
  97. handle->sibling_index = 0; /* could be anything for the root */
  98. handle->depth = 1; /* the tree is just a node yet */
  99. handle->rank = -1; /* invalid until set */
  100. handle->tag = -1; /* invalid until set */
  101. handle->is_not_important = 0;
  102. handle->sequential_consistency =
  103. starpu_data_get_default_sequential_consistency_flag();
  104. _STARPU_PTHREAD_MUTEX_INIT(&handle->sequential_consistency_mutex, NULL);
  105. handle->last_submitted_mode = STARPU_R;
  106. handle->last_submitted_writer = NULL;
  107. handle->last_submitted_readers = NULL;
  108. handle->post_sync_tasks = NULL;
  109. handle->post_sync_tasks_cnt = 0;
  110. /* By default, there are no methods available to perform a reduction */
  111. handle->redux_cl = NULL;
  112. handle->init_cl = NULL;
  113. handle->reduction_refcnt = 0;
  114. handle->reduction_req_list = _starpu_data_requester_list_new();
  115. #ifdef STARPU_USE_FXT
  116. handle->last_submitted_ghost_writer_id_is_valid = 0;
  117. handle->last_submitted_ghost_writer_id = 0;
  118. handle->last_submitted_ghost_readers_id = NULL;
  119. #endif
  120. handle->wt_mask = wt_mask;
  121. /* Store some values directly in the handle not to recompute them all
  122. * the time. */
  123. handle->data_size = handle->ops->get_size(handle);
  124. handle->footprint = _starpu_compute_data_footprint(handle);
  125. handle->home_node = home_node;
  126. /* that new data is invalid from all nodes perpective except for the
  127. * home node */
  128. unsigned node;
  129. for (node = 0; node < STARPU_MAXNODES; node++)
  130. {
  131. struct _starpu_data_replicate *replicate;
  132. replicate = &handle->per_node[node];
  133. replicate->memory_node = node;
  134. replicate->relaxed_coherency = 0;
  135. replicate->refcnt = 0;
  136. if (node == home_node)
  137. {
  138. /* this is the home node with the only valid copy */
  139. replicate->state = STARPU_OWNER;
  140. replicate->allocated = 1;
  141. replicate->automatically_allocated = 0;
  142. }
  143. else
  144. {
  145. /* the value is not available here yet */
  146. replicate->state = STARPU_INVALID;
  147. replicate->allocated = 0;
  148. }
  149. }
  150. unsigned worker;
  151. unsigned nworkers = starpu_worker_get_count();
  152. for (worker = 0; worker < nworkers; worker++)
  153. {
  154. struct _starpu_data_replicate *replicate;
  155. replicate = &handle->per_worker[worker];
  156. replicate->allocated = 0;
  157. replicate->automatically_allocated = 0;
  158. replicate->state = STARPU_INVALID;
  159. replicate->refcnt = 0;
  160. replicate->handle = handle;
  161. for (node = 0; node < STARPU_MAXNODES; node++)
  162. {
  163. replicate->requested[node] = 0;
  164. replicate->request[node] = NULL;
  165. }
  166. replicate->relaxed_coherency = 1;
  167. replicate->initialized = 0;
  168. replicate->memory_node = starpu_worker_get_memory_node(worker);
  169. /* duplicate the content of the interface on node 0 */
  170. memcpy(replicate->data_interface, handle->per_node[0].data_interface, handle->ops->interface_size);
  171. }
  172. /* now the data is available ! */
  173. _starpu_spin_unlock(&handle->header_lock);
  174. ptr = starpu_handle_to_pointer(handle, 0);
  175. if (ptr != NULL)
  176. {
  177. _starpu_data_register_ram_pointer(handle, ptr);
  178. }
  179. }
  180. static starpu_data_handle_t _starpu_data_handle_allocate(struct starpu_data_interface_ops *interface_ops)
  181. {
  182. starpu_data_handle_t handle = (starpu_data_handle_t) calloc(1, sizeof(struct _starpu_data_state));
  183. STARPU_ASSERT(handle);
  184. handle->ops = interface_ops;
  185. size_t interfacesize = interface_ops->interface_size;
  186. unsigned node;
  187. for (node = 0; node < STARPU_MAXNODES; node++)
  188. {
  189. #ifdef STARPU_MEMORY_STATUS
  190. /* Stats initilization */
  191. handle->stats_direct_access[node]=0;
  192. handle->stats_loaded_shared[node]=0;
  193. handle->stats_shared_to_owner[node]=0;
  194. handle->stats_loaded_owner[node]=0;
  195. handle->stats_invalidated[node]=0;
  196. #endif
  197. struct _starpu_data_replicate *replicate;
  198. replicate = &handle->per_node[node];
  199. /* relaxed_coherency = 0 */
  200. replicate->handle = handle;
  201. replicate->data_interface = calloc(1, interfacesize);
  202. STARPU_ASSERT(replicate->data_interface);
  203. }
  204. unsigned worker;
  205. unsigned nworkers = starpu_worker_get_count();
  206. for (worker = 0; worker < nworkers; worker++)
  207. {
  208. struct _starpu_data_replicate *replicate;
  209. replicate = &handle->per_worker[worker];
  210. replicate->handle = handle;
  211. replicate->data_interface = calloc(1, interfacesize);
  212. STARPU_ASSERT(replicate->data_interface);
  213. }
  214. return handle;
  215. }
  216. void starpu_data_register(starpu_data_handle_t *handleptr, uint32_t home_node,
  217. void *data_interface,
  218. struct starpu_data_interface_ops *ops)
  219. {
  220. starpu_data_handle_t handle =
  221. _starpu_data_handle_allocate(ops);
  222. STARPU_ASSERT(handleptr);
  223. *handleptr = handle;
  224. handle->mf_node = home_node;
  225. /* fill the interface fields with the appropriate method */
  226. ops->register_data_handle(handle, home_node, data_interface);
  227. _starpu_register_new_data(handle, home_node, 0);
  228. }
  229. void *starpu_handle_to_pointer(starpu_data_handle_t handle, uint32_t node)
  230. {
  231. /* Check whether the operation is supported and the node has actually
  232. * been allocated. */
  233. if (handle->ops->handle_to_pointer
  234. && starpu_data_test_if_allocated_on_node(handle, node))
  235. {
  236. return handle->ops->handle_to_pointer(handle, node);
  237. }
  238. return NULL;
  239. }
  240. void *starpu_handle_get_local_ptr(starpu_data_handle_t handle)
  241. {
  242. return starpu_handle_to_pointer(handle,
  243. _starpu_get_local_memory_node());
  244. }
  245. int starpu_data_get_rank(starpu_data_handle_t handle)
  246. {
  247. return handle->rank;
  248. }
  249. int starpu_data_set_rank(starpu_data_handle_t handle, int rank)
  250. {
  251. handle->rank = rank;
  252. return 0;
  253. }
  254. int starpu_data_get_tag(starpu_data_handle_t handle)
  255. {
  256. return handle->tag;
  257. }
  258. int starpu_data_set_tag(starpu_data_handle_t handle, int tag)
  259. {
  260. handle->tag = tag;
  261. return 0;
  262. }
  263. /*
  264. * Stop monitoring a piece of data
  265. */
  266. void _starpu_data_free_interfaces(starpu_data_handle_t handle)
  267. {
  268. const void *ram_ptr;
  269. unsigned node;
  270. unsigned worker;
  271. unsigned nworkers = starpu_worker_get_count();
  272. ram_ptr = starpu_handle_to_pointer(handle, 0);
  273. for (node = 0; node < STARPU_MAXNODES; node++)
  274. free(handle->per_node[node].data_interface);
  275. for (worker = 0; worker < nworkers; worker++)
  276. free(handle->per_worker[worker].data_interface);
  277. if (ram_ptr != NULL)
  278. {
  279. /* Remove the PTR -> HANDLE mapping. If a mapping from PTR
  280. * to another handle existed before (e.g., when using
  281. * filters), it becomes visible again. */
  282. struct handle_entry *entry;
  283. _starpu_spin_lock(&registered_handles_lock);
  284. HASH_FIND_PTR(registered_handles, &ram_ptr, entry);
  285. STARPU_ASSERT(entry != NULL);
  286. HASH_DEL(registered_handles, entry);
  287. free(entry);
  288. _starpu_spin_unlock(&registered_handles_lock);
  289. }
  290. }
  291. struct _starpu_unregister_callback_arg
  292. {
  293. unsigned memory_node;
  294. starpu_data_handle_t handle;
  295. unsigned terminated;
  296. pthread_mutex_t mutex;
  297. pthread_cond_t cond;
  298. };
  299. /* Check whether we should tell starpu_data_unregister that the data handle is
  300. * not busy any more.
  301. * The header is supposed to be locked */
  302. void _starpu_data_check_not_busy(starpu_data_handle_t handle)
  303. {
  304. if (!handle->busy_count && handle->busy_waiting)
  305. {
  306. _STARPU_PTHREAD_MUTEX_LOCK(&handle->busy_mutex);
  307. _STARPU_PTHREAD_COND_BROADCAST(&handle->busy_cond);
  308. _STARPU_PTHREAD_MUTEX_UNLOCK(&handle->busy_mutex);
  309. }
  310. }
  311. static void _starpu_data_unregister_fetch_data_callback(void *_arg)
  312. {
  313. int ret;
  314. struct _starpu_unregister_callback_arg *arg = (struct _starpu_unregister_callback_arg *) _arg;
  315. starpu_data_handle_t handle = arg->handle;
  316. STARPU_ASSERT(handle);
  317. struct _starpu_data_replicate *replicate = &handle->per_node[arg->memory_node];
  318. ret = _starpu_fetch_data_on_node(handle, replicate, STARPU_R, 0, NULL, NULL);
  319. STARPU_ASSERT(!ret);
  320. /* unlock the caller */
  321. _STARPU_PTHREAD_MUTEX_LOCK(&arg->mutex);
  322. arg->terminated = 1;
  323. _STARPU_PTHREAD_COND_SIGNAL(&arg->cond);
  324. _STARPU_PTHREAD_MUTEX_UNLOCK(&arg->mutex);
  325. }
  326. /* Unregister the data handle, perhaps we don't need to update the home_node
  327. * (in that case coherent is set to 0) */
  328. static void _starpu_data_unregister(starpu_data_handle_t handle, unsigned coherent)
  329. {
  330. STARPU_ASSERT(handle);
  331. if (coherent)
  332. {
  333. /* If sequential consistency is enabled, wait until data is available */
  334. _starpu_data_wait_until_available(handle, STARPU_RW);
  335. /* Fetch data in the home of the data to ensure we have a valid copy
  336. * where we registered it */
  337. int home_node = handle->home_node;
  338. if (home_node >= 0)
  339. {
  340. struct _starpu_unregister_callback_arg arg;
  341. arg.handle = handle;
  342. arg.memory_node = (unsigned)home_node;
  343. arg.terminated = 0;
  344. _STARPU_PTHREAD_MUTEX_INIT(&arg.mutex, NULL);
  345. _STARPU_PTHREAD_COND_INIT(&arg.cond, NULL);
  346. if (!_starpu_attempt_to_submit_data_request_from_apps(handle, STARPU_R,
  347. _starpu_data_unregister_fetch_data_callback, &arg))
  348. {
  349. /* no one has locked this data yet, so we proceed immediately */
  350. struct _starpu_data_replicate *home_replicate = &handle->per_node[home_node];
  351. int ret = _starpu_fetch_data_on_node(handle, home_replicate, STARPU_R, 0, NULL, NULL);
  352. STARPU_ASSERT(!ret);
  353. }
  354. else
  355. {
  356. _STARPU_PTHREAD_MUTEX_LOCK(&arg.mutex);
  357. while (!arg.terminated)
  358. _STARPU_PTHREAD_COND_WAIT(&arg.cond, &arg.mutex);
  359. _STARPU_PTHREAD_MUTEX_UNLOCK(&arg.mutex);
  360. }
  361. _starpu_release_data_on_node(handle, 0, &handle->per_node[home_node]);
  362. }
  363. /* If this handle uses the multiformat interface, we may have to convert
  364. * this piece of data back into the CPU format.
  365. * XXX : This is quite hacky, could we submit a task instead ?
  366. */
  367. unsigned int id = starpu_get_handle_interface_id(handle);
  368. if (id == STARPU_MULTIFORMAT_INTERFACE_ID &&
  369. _starpu_get_node_kind(handle->mf_node) != STARPU_CPU_RAM)
  370. {
  371. _STARPU_DEBUG("Conversion needed\n");
  372. void *buffers[1];
  373. struct starpu_multiformat_interface *format_interface;
  374. format_interface = starpu_data_get_interface_on_node(handle, 0);
  375. struct starpu_codelet *cl;
  376. enum _starpu_node_kind node_kind = _starpu_get_node_kind(handle->mf_node);
  377. switch (node_kind)
  378. {
  379. #ifdef STARPU_USE_CUDA
  380. case STARPU_CUDA_RAM:
  381. cl = format_interface->ops->cuda_to_cpu_cl;
  382. break;
  383. #endif
  384. #ifdef STARPU_USE_OPENCL
  385. case STARPU_OPENCL_RAM:
  386. cl = format_interface->ops->opencl_to_cpu_cl;
  387. break;
  388. #endif
  389. case STARPU_CPU_RAM: /* Impossible ! */
  390. case STARPU_SPU_LS: /* Not supported */
  391. default:
  392. STARPU_ASSERT(0);
  393. }
  394. buffers[0] = format_interface;
  395. _starpu_cl_func_t func = _starpu_task_get_cpu_nth_implementation(cl, 0);
  396. STARPU_ASSERT(func);
  397. func(buffers, NULL);
  398. }
  399. }
  400. else
  401. {
  402. /* Should we postpone the unregister operation ? */
  403. if ((handle->refcnt > 0) && handle->lazy_unregister)
  404. return;
  405. }
  406. _starpu_spin_lock(&handle->header_lock);
  407. /* Tell holders of references that we're starting waiting */
  408. handle->busy_waiting = 1;
  409. _starpu_spin_unlock(&handle->header_lock);
  410. /* Wait for all requests to finish (notably WT requests) */
  411. _STARPU_PTHREAD_MUTEX_LOCK(&handle->busy_mutex);
  412. while (handle->busy_count)
  413. _STARPU_PTHREAD_COND_WAIT(&handle->busy_cond, &handle->busy_mutex);
  414. /* Wait for finished requests to release the handle */
  415. _starpu_spin_lock(&handle->header_lock);
  416. _starpu_data_free_interfaces(handle);
  417. /* Destroy the data now */
  418. unsigned node;
  419. for (node = 0; node < STARPU_MAXNODES; node++)
  420. {
  421. struct _starpu_data_replicate *local = &handle->per_node[node];
  422. if (local->allocated && local->automatically_allocated)
  423. {
  424. /* free the data copy in a lazy fashion */
  425. _starpu_request_mem_chunk_removal(handle, node);
  426. }
  427. }
  428. _starpu_data_requester_list_delete(handle->req_list);
  429. _starpu_data_requester_list_delete(handle->reduction_req_list);
  430. free(handle);
  431. }
  432. void starpu_data_unregister(starpu_data_handle_t handle)
  433. {
  434. _starpu_data_unregister(handle, 1);
  435. }
  436. void starpu_data_unregister_no_coherency(starpu_data_handle_t handle)
  437. {
  438. _starpu_data_unregister(handle, 0);
  439. }
  440. void starpu_data_invalidate(starpu_data_handle_t handle)
  441. {
  442. STARPU_ASSERT(handle);
  443. starpu_data_acquire(handle, STARPU_W);
  444. _starpu_spin_lock(&handle->header_lock);
  445. unsigned node;
  446. for (node = 0; node < STARPU_MAXNODES; node++)
  447. {
  448. struct _starpu_data_replicate *local = &handle->per_node[node];
  449. if (local->allocated && local->automatically_allocated)
  450. {
  451. /* free the data copy in a lazy fashion */
  452. _starpu_request_mem_chunk_removal(handle, node);
  453. }
  454. local->state = STARPU_INVALID;
  455. }
  456. _starpu_spin_unlock(&handle->header_lock);
  457. starpu_data_release(handle);
  458. }
  459. unsigned starpu_get_handle_interface_id(starpu_data_handle_t handle)
  460. {
  461. return handle->ops->interfaceid;
  462. }
  463. void *starpu_data_get_interface_on_node(starpu_data_handle_t handle, unsigned memory_node)
  464. {
  465. return handle->per_node[memory_node].data_interface;
  466. }