data_interface.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  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_handle_to_pointer(starpu_data_handle_t handle, uint32_t node)
  240. {
  241. /* Check whether the operation is supported and the node has actually
  242. * been allocated. */
  243. if (handle->ops->handle_to_pointer
  244. && starpu_data_test_if_allocated_on_node(handle, node))
  245. {
  246. return handle->ops->handle_to_pointer(handle, node);
  247. }
  248. return NULL;
  249. }
  250. void *starpu_handle_get_local_ptr(starpu_data_handle_t handle)
  251. {
  252. return starpu_handle_to_pointer(handle,
  253. _starpu_get_local_memory_node());
  254. }
  255. int starpu_data_get_rank(starpu_data_handle_t handle)
  256. {
  257. return handle->rank;
  258. }
  259. int starpu_data_set_rank(starpu_data_handle_t handle, int rank)
  260. {
  261. handle->rank = rank;
  262. return 0;
  263. }
  264. int starpu_data_get_tag(starpu_data_handle_t handle)
  265. {
  266. return handle->tag;
  267. }
  268. int starpu_data_set_tag(starpu_data_handle_t handle, int tag)
  269. {
  270. handle->tag = tag;
  271. return 0;
  272. }
  273. /*
  274. * Stop monitoring a piece of data
  275. */
  276. void _starpu_data_free_interfaces(starpu_data_handle_t handle)
  277. {
  278. const void *ram_ptr;
  279. unsigned node;
  280. unsigned worker;
  281. unsigned nworkers = starpu_worker_get_count();
  282. ram_ptr = starpu_handle_to_pointer(handle, 0);
  283. for (node = 0; node < STARPU_MAXNODES; node++)
  284. free(handle->per_node[node].data_interface);
  285. for (worker = 0; worker < nworkers; worker++)
  286. free(handle->per_worker[worker].data_interface);
  287. if (ram_ptr != NULL)
  288. {
  289. /* Remove the PTR -> HANDLE mapping. If a mapping from PTR
  290. * to another handle existed before (e.g., when using
  291. * filters), it becomes visible again. */
  292. struct handle_entry *entry;
  293. _starpu_spin_lock(&registered_handles_lock);
  294. HASH_FIND_PTR(registered_handles, &ram_ptr, entry);
  295. STARPU_ASSERT(entry != NULL);
  296. HASH_DEL(registered_handles, entry);
  297. free(entry);
  298. _starpu_spin_unlock(&registered_handles_lock);
  299. }
  300. }
  301. struct _starpu_unregister_callback_arg
  302. {
  303. unsigned memory_node;
  304. starpu_data_handle_t handle;
  305. unsigned terminated;
  306. pthread_mutex_t mutex;
  307. pthread_cond_t cond;
  308. };
  309. /* Check whether we should tell starpu_data_unregister that the data handle is
  310. * not busy any more.
  311. * The header is supposed to be locked */
  312. void _starpu_data_check_not_busy(starpu_data_handle_t handle)
  313. {
  314. if (!handle->busy_count && handle->busy_waiting)
  315. {
  316. _STARPU_PTHREAD_MUTEX_LOCK(&handle->busy_mutex);
  317. _STARPU_PTHREAD_COND_BROADCAST(&handle->busy_cond);
  318. _STARPU_PTHREAD_MUTEX_UNLOCK(&handle->busy_mutex);
  319. }
  320. }
  321. static void _starpu_data_unregister_fetch_data_callback(void *_arg)
  322. {
  323. int ret;
  324. struct _starpu_unregister_callback_arg *arg = (struct _starpu_unregister_callback_arg *) _arg;
  325. starpu_data_handle_t handle = arg->handle;
  326. STARPU_ASSERT(handle);
  327. struct _starpu_data_replicate *replicate = &handle->per_node[arg->memory_node];
  328. ret = _starpu_fetch_data_on_node(handle, replicate, STARPU_R, 0, 0, NULL, NULL);
  329. STARPU_ASSERT(!ret);
  330. /* unlock the caller */
  331. _STARPU_PTHREAD_MUTEX_LOCK(&arg->mutex);
  332. arg->terminated = 1;
  333. _STARPU_PTHREAD_COND_SIGNAL(&arg->cond);
  334. _STARPU_PTHREAD_MUTEX_UNLOCK(&arg->mutex);
  335. }
  336. /* Unregister the data handle, perhaps we don't need to update the home_node
  337. * (in that case coherent is set to 0) */
  338. static void _starpu_data_unregister(starpu_data_handle_t handle, unsigned coherent)
  339. {
  340. STARPU_ASSERT(handle);
  341. if (coherent)
  342. {
  343. /* If sequential consistency is enabled, wait until data is available */
  344. _starpu_data_wait_until_available(handle, STARPU_RW);
  345. /* Fetch data in the home of the data to ensure we have a valid copy
  346. * where we registered it */
  347. int home_node = handle->home_node;
  348. if (home_node >= 0)
  349. {
  350. struct _starpu_unregister_callback_arg arg;
  351. arg.handle = handle;
  352. arg.memory_node = (unsigned)home_node;
  353. arg.terminated = 0;
  354. _STARPU_PTHREAD_MUTEX_INIT(&arg.mutex, NULL);
  355. _STARPU_PTHREAD_COND_INIT(&arg.cond, NULL);
  356. if (!_starpu_attempt_to_submit_data_request_from_apps(handle, STARPU_R,
  357. _starpu_data_unregister_fetch_data_callback, &arg))
  358. {
  359. /* no one has locked this data yet, so we proceed immediately */
  360. struct _starpu_data_replicate *home_replicate = &handle->per_node[home_node];
  361. int ret = _starpu_fetch_data_on_node(handle, home_replicate, STARPU_R, 0, 0, NULL, NULL);
  362. STARPU_ASSERT(!ret);
  363. }
  364. else
  365. {
  366. _STARPU_PTHREAD_MUTEX_LOCK(&arg.mutex);
  367. while (!arg.terminated)
  368. _STARPU_PTHREAD_COND_WAIT(&arg.cond, &arg.mutex);
  369. _STARPU_PTHREAD_MUTEX_UNLOCK(&arg.mutex);
  370. }
  371. _starpu_release_data_on_node(handle, 0, &handle->per_node[home_node]);
  372. }
  373. /* If this handle uses a multiformat interface, we may have to convert
  374. * this piece of data back into the CPU format.
  375. * XXX : This is quite hacky, could we submit a task instead ?
  376. */
  377. if (_starpu_data_is_multiformat_handle(handle) &&
  378. starpu_node_get_kind(handle->mf_node) != STARPU_CPU_RAM)
  379. {
  380. _STARPU_DEBUG("Conversion needed\n");
  381. void *buffers[1];
  382. struct starpu_multiformat_interface *format_interface;
  383. format_interface = (struct starpu_multiformat_interface *) starpu_data_get_interface_on_node(handle, 0);
  384. struct starpu_codelet *cl;
  385. enum starpu_node_kind node_kind = starpu_node_get_kind(handle->mf_node);
  386. struct starpu_multiformat_data_interface_ops *mf_ops;
  387. mf_ops = (struct starpu_multiformat_data_interface_ops *) handle->ops->get_mf_ops(format_interface);
  388. switch (node_kind)
  389. {
  390. #ifdef STARPU_USE_CUDA
  391. case STARPU_CUDA_RAM:
  392. cl = mf_ops->cuda_to_cpu_cl;
  393. break;
  394. #endif
  395. #ifdef STARPU_USE_OPENCL
  396. case STARPU_OPENCL_RAM:
  397. cl = mf_ops->opencl_to_cpu_cl;
  398. break;
  399. #endif
  400. case STARPU_CPU_RAM: /* Impossible ! */
  401. case STARPU_SPU_LS: /* Not supported */
  402. default:
  403. STARPU_ASSERT(0);
  404. }
  405. buffers[0] = format_interface;
  406. _starpu_cl_func_t func = _starpu_task_get_cpu_nth_implementation(cl, 0);
  407. STARPU_ASSERT(func);
  408. func(buffers, NULL);
  409. }
  410. }
  411. else
  412. {
  413. /* Should we postpone the unregister operation ? */
  414. if ((handle->refcnt > 0) && handle->lazy_unregister)
  415. return;
  416. }
  417. _starpu_spin_lock(&handle->header_lock);
  418. /* Tell holders of references that we're starting waiting */
  419. handle->busy_waiting = 1;
  420. _starpu_spin_unlock(&handle->header_lock);
  421. /* Wait for all requests to finish (notably WT requests) */
  422. _STARPU_PTHREAD_MUTEX_LOCK(&handle->busy_mutex);
  423. while (handle->busy_count)
  424. _STARPU_PTHREAD_COND_WAIT(&handle->busy_cond, &handle->busy_mutex);
  425. /* Wait for finished requests to release the handle */
  426. _starpu_spin_lock(&handle->header_lock);
  427. _starpu_data_free_interfaces(handle);
  428. /* Destroy the data now */
  429. unsigned node;
  430. for (node = 0; node < STARPU_MAXNODES; node++)
  431. {
  432. /* free the data copy in a lazy fashion */
  433. _starpu_request_mem_chunk_removal(handle, node);
  434. }
  435. _starpu_data_requester_list_delete(handle->req_list);
  436. _starpu_data_requester_list_delete(handle->reduction_req_list);
  437. free(handle);
  438. }
  439. void starpu_data_unregister(starpu_data_handle_t handle)
  440. {
  441. _starpu_data_unregister(handle, 1);
  442. }
  443. void starpu_data_unregister_no_coherency(starpu_data_handle_t handle)
  444. {
  445. _starpu_data_unregister(handle, 0);
  446. }
  447. void starpu_data_invalidate(starpu_data_handle_t handle)
  448. {
  449. STARPU_ASSERT(handle);
  450. starpu_data_acquire(handle, STARPU_W);
  451. _starpu_spin_lock(&handle->header_lock);
  452. unsigned node;
  453. for (node = 0; node < STARPU_MAXNODES; node++)
  454. {
  455. struct _starpu_data_replicate *local = &handle->per_node[node];
  456. if (local->allocated && local->automatically_allocated)
  457. {
  458. /* free the data copy in a lazy fashion */
  459. _starpu_request_mem_chunk_removal(handle, node);
  460. }
  461. local->state = STARPU_INVALID;
  462. }
  463. _starpu_spin_unlock(&handle->header_lock);
  464. starpu_data_release(handle);
  465. }
  466. enum starpu_data_interface_id starpu_handle_get_interface_id(starpu_data_handle_t handle)
  467. {
  468. return handle->ops->interfaceid;
  469. }
  470. void *starpu_data_get_interface_on_node(starpu_data_handle_t handle, unsigned memory_node)
  471. {
  472. return handle->per_node[memory_node].data_interface;
  473. }
  474. int starpu_data_interface_get_next_id()
  475. {
  476. _data_interface_number += 1;
  477. return _data_interface_number-1;
  478. }