data_interface.c 16 KB

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