data_interface.c 16 KB

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