memalloc.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009, 2010 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 <datawizard/memalloc.h>
  18. #include <datawizard/footprint.h>
  19. /* This per-node RW-locks protect mc_list and memchunk_cache entries */
  20. static pthread_rwlock_t mc_rwlock[STARPU_MAXNODES];
  21. /* Potentially in use memory chunks */
  22. static starpu_mem_chunk_list_t mc_list[STARPU_MAXNODES];
  23. /* Explicitly caches memory chunks that can be reused */
  24. static starpu_mem_chunk_list_t memchunk_cache[STARPU_MAXNODES];
  25. void _starpu_init_mem_chunk_lists(void)
  26. {
  27. unsigned i;
  28. for (i = 0; i < STARPU_MAXNODES; i++)
  29. {
  30. PTHREAD_RWLOCK_INIT(&mc_rwlock[i], NULL);
  31. mc_list[i] = starpu_mem_chunk_list_new();
  32. memchunk_cache[i] = starpu_mem_chunk_list_new();
  33. }
  34. }
  35. void _starpu_deinit_mem_chunk_lists(void)
  36. {
  37. unsigned i;
  38. for (i = 0; i < STARPU_MAXNODES; i++)
  39. {
  40. starpu_mem_chunk_list_delete(mc_list[i]);
  41. starpu_mem_chunk_list_delete(memchunk_cache[i]);
  42. }
  43. }
  44. /*
  45. * Manipulate subtrees
  46. */
  47. static void lock_all_subtree(starpu_data_handle handle)
  48. {
  49. if (handle->nchildren == 0)
  50. {
  51. /* this is a leaf */
  52. while (_starpu_spin_trylock(&handle->header_lock))
  53. _starpu_datawizard_progress(_starpu_get_local_memory_node(), 0);
  54. }
  55. else {
  56. /* lock all sub-subtrees children */
  57. unsigned child;
  58. for (child = 0; child < handle->nchildren; child++)
  59. {
  60. lock_all_subtree(&handle->children[child]);
  61. }
  62. }
  63. }
  64. static void unlock_all_subtree(starpu_data_handle handle)
  65. {
  66. if (handle->nchildren == 0)
  67. {
  68. /* this is a leaf */
  69. _starpu_spin_unlock(&handle->header_lock);
  70. }
  71. else {
  72. /* lock all sub-subtrees children
  73. * Note that this is done in the reverse order of the
  74. * lock_all_subtree so that we avoid deadlock */
  75. unsigned i;
  76. for (i =0; i < handle->nchildren; i++)
  77. {
  78. unsigned child = handle->nchildren - 1 - i;
  79. unlock_all_subtree(&handle->children[child]);
  80. }
  81. }
  82. }
  83. static unsigned may_free_subtree(starpu_data_handle handle, unsigned node)
  84. {
  85. /* we only free if no one refers to the leaf */
  86. uint32_t refcnt = _starpu_get_data_refcnt(handle, node);
  87. if (refcnt)
  88. return 0;
  89. if (!handle->nchildren)
  90. return 1;
  91. /* look into all sub-subtrees children */
  92. unsigned child;
  93. for (child = 0; child < handle->nchildren; child++)
  94. {
  95. unsigned res;
  96. res = may_free_subtree(&handle->children[child], node);
  97. if (!res) return 0;
  98. }
  99. /* no problem was found */
  100. return 1;
  101. }
  102. static void transfer_subtree_to_node(starpu_data_handle handle, unsigned src_node,
  103. unsigned dst_node)
  104. {
  105. unsigned i;
  106. unsigned last = 0;
  107. unsigned cnt;
  108. int ret;
  109. if (handle->nchildren == 0)
  110. {
  111. struct starpu_data_replicate_s *src_replicate = &handle->per_node[src_node];
  112. struct starpu_data_replicate_s *dst_replicate = &handle->per_node[dst_node];
  113. /* this is a leaf */
  114. switch(src_replicate->state) {
  115. case STARPU_OWNER:
  116. /* the local node has the only copy */
  117. /* the owner is now the destination_node */
  118. src_replicate->state = STARPU_INVALID;
  119. dst_replicate->state = STARPU_OWNER;
  120. #warning we should use requests during memory reclaim
  121. /* TODO use request !! */
  122. src_replicate->refcnt++;
  123. dst_replicate->refcnt++;
  124. ret = _starpu_driver_copy_data_1_to_1(handle, src_replicate, dst_replicate, 0, NULL, 1);
  125. STARPU_ASSERT(ret == 0);
  126. src_replicate->refcnt--;
  127. dst_replicate->refcnt--;
  128. break;
  129. case STARPU_SHARED:
  130. /* some other node may have the copy */
  131. src_replicate->state = STARPU_INVALID;
  132. /* count the number of copies */
  133. cnt = 0;
  134. for (i = 0; i < STARPU_MAXNODES; i++)
  135. {
  136. if (handle->per_node[i].state == STARPU_SHARED) {
  137. cnt++;
  138. last = i;
  139. }
  140. }
  141. if (cnt == 1)
  142. handle->per_node[last].state = STARPU_OWNER;
  143. break;
  144. case STARPU_INVALID:
  145. /* nothing to be done */
  146. break;
  147. default:
  148. STARPU_ABORT();
  149. break;
  150. }
  151. }
  152. else {
  153. /* lock all sub-subtrees children */
  154. unsigned child;
  155. for (child = 0; child < handle->nchildren; child++)
  156. {
  157. transfer_subtree_to_node(&handle->children[child],
  158. src_node, dst_node);
  159. }
  160. }
  161. }
  162. static size_t free_memory_on_node(starpu_mem_chunk_t mc, uint32_t node)
  163. {
  164. size_t freed = 0;
  165. STARPU_ASSERT(mc->ops);
  166. STARPU_ASSERT(mc->ops->free_data_on_node);
  167. starpu_data_handle handle = mc->data;
  168. /* Does this memory chunk refers to a handle that does not exist
  169. * anymore ? */
  170. unsigned data_was_deleted = mc->data_was_deleted;
  171. struct starpu_data_replicate_s *replicate = mc->replicate;
  172. // while (_starpu_spin_trylock(&handle->header_lock))
  173. // _starpu_datawizard_progress(_starpu_get_local_memory_node());
  174. #warning can we block here ?
  175. // _starpu_spin_lock(&handle->header_lock);
  176. if (mc->automatically_allocated &&
  177. (!handle || data_was_deleted || replicate->refcnt == 0))
  178. {
  179. if (handle && !data_was_deleted)
  180. STARPU_ASSERT(replicate->allocated);
  181. mc->ops->free_data_on_node(mc->chunk_interface, node);
  182. if (handle && !data_was_deleted)
  183. {
  184. replicate->allocated = 0;
  185. /* XXX why do we need that ? */
  186. replicate->automatically_allocated = 0;
  187. }
  188. freed = mc->size;
  189. if (handle && !data_was_deleted)
  190. STARPU_ASSERT(replicate->refcnt == 0);
  191. }
  192. // _starpu_spin_unlock(&handle->header_lock);
  193. return freed;
  194. }
  195. static size_t do_free_mem_chunk(starpu_mem_chunk_t mc, unsigned node)
  196. {
  197. size_t size;
  198. /* free the actual buffer */
  199. size = free_memory_on_node(mc, node);
  200. /* remove the mem_chunk from the list */
  201. starpu_mem_chunk_list_erase(mc_list[node], mc);
  202. free(mc->chunk_interface);
  203. starpu_mem_chunk_delete(mc);
  204. return size;
  205. }
  206. /* This function is called for memory chunks that are possibly in used (ie. not
  207. * in the cache). They should therefore still be associated to a handle. */
  208. static size_t try_to_free_mem_chunk(starpu_mem_chunk_t mc, unsigned node)
  209. {
  210. size_t freed = 0;
  211. starpu_data_handle handle;
  212. handle = mc->data;
  213. STARPU_ASSERT(handle);
  214. /* Either it's a "relaxed coherency" memchunk, or it's a memchunk that
  215. * could be used with filters. */
  216. if (mc->relaxed_coherency)
  217. {
  218. STARPU_ASSERT(mc->replicate);
  219. while (_starpu_spin_trylock(&handle->header_lock))
  220. _starpu_datawizard_progress(_starpu_get_local_memory_node(), 0);
  221. if (mc->replicate->refcnt == 0)
  222. {
  223. /* Note taht there is no need to transfer any data or
  224. * to update the status in terms of MSI protocol
  225. * because this memchunk is associated to a replicate
  226. * in "relaxed coherency" mode. */
  227. freed = do_free_mem_chunk(mc, node);
  228. }
  229. _starpu_spin_unlock(&handle->header_lock);
  230. }
  231. else {
  232. /* try to lock all the leafs of the subtree */
  233. lock_all_subtree(handle);
  234. /* check if they are all "free" */
  235. if (may_free_subtree(handle, node))
  236. {
  237. STARPU_ASSERT(handle->per_node[node].refcnt == 0);
  238. /* in case there was nobody using that buffer, throw it
  239. * away after writing it back to main memory */
  240. transfer_subtree_to_node(handle, node, 0);
  241. STARPU_ASSERT(handle->per_node[node].refcnt == 0);
  242. /* now the actual buffer may be freed */
  243. freed = do_free_mem_chunk(mc, node);
  244. }
  245. /* unlock the leafs */
  246. unlock_all_subtree(handle);
  247. }
  248. return freed;
  249. }
  250. #ifdef STARPU_USE_ALLOCATION_CACHE
  251. /* We assume that mc_rwlock[node] is taken. is_already_in_mc_list indicates
  252. * that the mc is already in the list of buffers that are possibly used, and
  253. * therefore not in the cache. */
  254. static void reuse_mem_chunk(unsigned node, struct starpu_data_replicate_s *new_replicate, starpu_mem_chunk_t mc, unsigned is_already_in_mc_list)
  255. {
  256. starpu_data_handle old_data;
  257. old_data = mc->data;
  258. /* we found an appropriate mem chunk: so we get it out
  259. * of the "to free" list, and reassign it to the new
  260. * piece of data */
  261. if (!is_already_in_mc_list)
  262. {
  263. starpu_mem_chunk_list_erase(memchunk_cache[node], mc);
  264. }
  265. struct starpu_data_replicate_s *old_replicate = mc->replicate;
  266. old_replicate->allocated = 0;
  267. old_replicate->automatically_allocated = 0;
  268. old_replicate->initialized = 0;
  269. new_replicate->allocated = 1;
  270. new_replicate->automatically_allocated = 1;
  271. new_replicate->initialized = 0;
  272. STARPU_ASSERT(new_replicate->chunk_interface);
  273. STARPU_ASSERT(mc->chunk_interface);
  274. memcpy(new_replicate->chunk_interface, mc->chunk_interface, old_replicate->ops->interface_size);
  275. mc->data = new_replicate->handle;
  276. mc->data_was_deleted = 0;
  277. /* mc->ops, mc->size, mc->footprint and mc->interface should be
  278. * unchanged ! */
  279. /* reinsert the mem chunk in the list of active memory chunks */
  280. if (!is_already_in_mc_list)
  281. {
  282. starpu_mem_chunk_list_push_front(mc_list[node], mc);
  283. }
  284. }
  285. static unsigned try_to_reuse_mem_chunk(starpu_mem_chunk_t mc, unsigned node, starpu_data_handle new_data, unsigned is_already_in_mc_list)
  286. {
  287. unsigned success = 0;
  288. starpu_data_handle old_data;
  289. old_data = mc->data;
  290. STARPU_ASSERT(old_data);
  291. /* try to lock all the leafs of the subtree */
  292. lock_all_subtree(old_data);
  293. /* check if they are all "free" */
  294. if (may_free_subtree(old_data, node))
  295. {
  296. success = 1;
  297. /* in case there was nobody using that buffer, throw it
  298. * away after writing it back to main memory */
  299. transfer_subtree_to_node(old_data, node, 0);
  300. /* now replace the previous data */
  301. reuse_mem_chunk(node, new_data, mc, is_already_in_mc_list);
  302. }
  303. /* unlock the leafs */
  304. unlock_all_subtree(old_data);
  305. return success;
  306. }
  307. static int _starpu_data_interface_compare(void *interface_a, struct starpu_data_interface_ops_t *ops_a,
  308. void *interface_b, struct starpu_data_interface_ops_t *ops_b)
  309. {
  310. if (ops_a->interfaceid != ops_b->interfaceid)
  311. return -1;
  312. int ret = ops_a->compare(interface_a, interface_b);
  313. return ret;
  314. }
  315. /* This function must be called with mc_rwlock[node] taken in write mode */
  316. static starpu_mem_chunk_t _starpu_memchunk_cache_lookup_locked(uint32_t node, starpu_data_handle handle)
  317. {
  318. uint32_t footprint = _starpu_compute_data_footprint(handle);
  319. /* go through all buffers in the cache */
  320. starpu_mem_chunk_t mc;
  321. for (mc = starpu_mem_chunk_list_begin(memchunk_cache[node]);
  322. mc != starpu_mem_chunk_list_end(memchunk_cache[node]);
  323. mc = starpu_mem_chunk_list_next(mc))
  324. {
  325. if (mc->footprint == footprint)
  326. {
  327. /* Is that a false hit ? (this is _very_ unlikely) */
  328. if (_starpu_data_interface_compare(handle->per_node[node].interface, handle->ops, mc->interface, mc->ops))
  329. continue;
  330. /* Cache hit */
  331. /* Remove from the cache */
  332. starpu_mem_chunk_list_erase(memchunk_cache[node], mc);
  333. return mc;
  334. }
  335. }
  336. /* This is a cache miss */
  337. return NULL;
  338. }
  339. /* this function looks for a memory chunk that matches a given footprint in the
  340. * list of mem chunk that need to be freed. This function must be called with
  341. * mc_rwlock[node] taken in write mode. */
  342. static unsigned try_to_find_reusable_mem_chunk(unsigned node, starpu_data_handle data, uint32_t footprint)
  343. {
  344. starpu_mem_chunk_t mc, next_mc;
  345. /* go through all buffers in the cache */
  346. mc = _starpu_memchunk_cache_lookup_locked(node, handle);
  347. if (mc)
  348. {
  349. /* We found an entry in the cache so we can reuse it */
  350. reuse_mem_chunk(node, data, mc, 0);
  351. return 1;
  352. }
  353. /* now look for some non essential data in the active list */
  354. for (mc = starpu_mem_chunk_list_begin(mc_list[node]);
  355. mc != starpu_mem_chunk_list_end(mc_list[node]);
  356. mc = next_mc)
  357. {
  358. /* there is a risk that the memory chunk is freed before next
  359. * iteration starts: so we compute the next element of the list
  360. * now */
  361. next_mc = starpu_mem_chunk_list_next(mc);
  362. if (mc->data->is_not_important && (mc->footprint == footprint))
  363. {
  364. // fprintf(stderr, "found a candidate ...\n");
  365. if (try_to_reuse_mem_chunk(mc, node, data, 1))
  366. return 1;
  367. }
  368. }
  369. return 0;
  370. }
  371. #endif
  372. /*
  373. * Free the memory chuncks that are explicitely tagged to be freed. The
  374. * mc_rwlock[node] rw-lock should be taken prior to calling this function.
  375. */
  376. static size_t flush_memchunk_cache(uint32_t node)
  377. {
  378. starpu_mem_chunk_t mc, next_mc;
  379. size_t freed = 0;
  380. for (mc = starpu_mem_chunk_list_begin(memchunk_cache[node]);
  381. mc != starpu_mem_chunk_list_end(memchunk_cache[node]);
  382. mc = next_mc)
  383. {
  384. next_mc = starpu_mem_chunk_list_next(mc);
  385. freed += free_memory_on_node(mc, node);
  386. starpu_mem_chunk_list_erase(memchunk_cache[node], mc);
  387. free(mc->chunk_interface);
  388. starpu_mem_chunk_delete(mc);
  389. }
  390. return freed;
  391. }
  392. /*
  393. * Try to free the buffers currently in use on the memory node. If the force
  394. * flag is set, the memory is freed regardless of coherency concerns (this
  395. * should only be used at the termination of StarPU for instance). The
  396. * mc_rwlock[node] rw-lock should be taken prior to calling this function.
  397. */
  398. static size_t free_potentially_in_use_mc(uint32_t node, unsigned force)
  399. {
  400. size_t freed = 0;
  401. starpu_mem_chunk_t mc, next_mc;
  402. for (mc = starpu_mem_chunk_list_begin(mc_list[node]);
  403. mc != starpu_mem_chunk_list_end(mc_list[node]);
  404. mc = next_mc)
  405. {
  406. /* there is a risk that the memory chunk is freed
  407. before next iteration starts: so we compute the next
  408. element of the list now */
  409. next_mc = starpu_mem_chunk_list_next(mc);
  410. if (!force)
  411. {
  412. freed += try_to_free_mem_chunk(mc, node);
  413. #if 0
  414. if (freed > toreclaim)
  415. break;
  416. #endif
  417. }
  418. else {
  419. /* We must free the memory now: note that data
  420. * coherency is not maintained in that case ! */
  421. freed += do_free_mem_chunk(mc, node);
  422. }
  423. }
  424. return freed;
  425. }
  426. static size_t reclaim_memory_generic(uint32_t node, unsigned force)
  427. {
  428. size_t freed = 0;
  429. PTHREAD_RWLOCK_WRLOCK(&mc_rwlock[node]);
  430. /* remove all buffers for which there was a removal request */
  431. freed += flush_memchunk_cache(node);
  432. /* try to free all allocated data potentially in use */
  433. freed += free_potentially_in_use_mc(node, force);
  434. PTHREAD_RWLOCK_UNLOCK(&mc_rwlock[node]);
  435. return freed;
  436. }
  437. /*
  438. * This function frees all the memory that was implicitely allocated by StarPU
  439. * (for the data replicates). This is not ensuring data coherency, and should
  440. * only be called while StarPU is getting shut down.
  441. */
  442. size_t _starpu_free_all_automatically_allocated_buffers(uint32_t node)
  443. {
  444. return reclaim_memory_generic(node, 1);
  445. }
  446. static starpu_mem_chunk_t _starpu_memchunk_init(struct starpu_data_replicate_s *replicate, size_t size, size_t interface_size, unsigned automatically_allocated)
  447. {
  448. starpu_mem_chunk_t mc = starpu_mem_chunk_new();
  449. starpu_data_handle handle = replicate->handle;
  450. STARPU_ASSERT(handle);
  451. STARPU_ASSERT(handle->ops);
  452. mc->data = handle;
  453. mc->size = size;
  454. mc->footprint = _starpu_compute_data_footprint(handle);
  455. mc->ops = handle->ops;
  456. mc->data_was_deleted = 0;
  457. mc->automatically_allocated = automatically_allocated;
  458. mc->relaxed_coherency = replicate->relaxed_coherency;
  459. mc->replicate = replicate;
  460. /* Save a copy of the interface */
  461. mc->chunk_interface = malloc(interface_size);
  462. STARPU_ASSERT(mc->chunk_interface);
  463. memcpy(mc->chunk_interface, replicate->data_interface, interface_size);
  464. return mc;
  465. }
  466. static void register_mem_chunk(struct starpu_data_replicate_s *replicate, size_t size, unsigned automatically_allocated)
  467. {
  468. unsigned dst_node = replicate->memory_node;
  469. starpu_mem_chunk_t mc;
  470. /* the interface was already filled by ops->allocate_data_on_node */
  471. size_t interface_size = replicate->handle->ops->interface_size;
  472. /* Put this memchunk in the list of memchunk in use */
  473. mc = _starpu_memchunk_init(replicate, size, interface_size, automatically_allocated);
  474. PTHREAD_RWLOCK_WRLOCK(&mc_rwlock[dst_node]);
  475. starpu_mem_chunk_list_push_front(mc_list[dst_node], mc);
  476. PTHREAD_RWLOCK_UNLOCK(&mc_rwlock[dst_node]);
  477. }
  478. /* This function is called when the handle is destroyed (eg. when calling
  479. * unregister or unpartition). It puts all the memchunks that refer to the
  480. * specified handle into the cache. */
  481. void _starpu_request_mem_chunk_removal(starpu_data_handle handle, unsigned node)
  482. {
  483. PTHREAD_RWLOCK_WRLOCK(&mc_rwlock[node]);
  484. /* iterate over the list of memory chunks and remove the entry */
  485. starpu_mem_chunk_t mc, next_mc;
  486. for (mc = starpu_mem_chunk_list_begin(mc_list[node]);
  487. mc != starpu_mem_chunk_list_end(mc_list[node]);
  488. mc = next_mc)
  489. {
  490. next_mc = starpu_mem_chunk_list_next(mc);
  491. if (mc->data == handle) {
  492. /* we found the data */
  493. mc->data_was_deleted = 1;
  494. /* remove it from the main list */
  495. starpu_mem_chunk_list_erase(mc_list[node], mc);
  496. /* put it in the list of buffers to be removed */
  497. starpu_mem_chunk_list_push_front(memchunk_cache[node], mc);
  498. /* Note that we do not stop here because there can be
  499. * multiple replicates associated to the same handle on
  500. * the same memory node. */
  501. }
  502. }
  503. /* there was no corresponding buffer ... */
  504. PTHREAD_RWLOCK_UNLOCK(&mc_rwlock[node]);
  505. }
  506. /*
  507. * In order to allocate a piece of data, we try to reuse existing buffers if
  508. * its possible.
  509. * 1 - we try to reuse a memchunk that is explicitely unused.
  510. * 2 - we go through the list of memory chunks and find one that is not
  511. * referenced and that has the same footprint to reuse it.
  512. * 3 - we call the usual driver's alloc method
  513. * 4 - we go through the list of memory chunks and release those that are
  514. * not referenced (or part of those).
  515. *
  516. */
  517. static ssize_t _starpu_allocate_interface(starpu_data_handle handle, struct starpu_data_replicate_s *replicate, uint32_t dst_node)
  518. {
  519. unsigned attempts = 0;
  520. ssize_t allocated_memory;
  521. _starpu_data_allocation_inc_stats(dst_node);
  522. #ifdef STARPU_USE_ALLOCATION_CACHE
  523. /* perhaps we can directly reuse a buffer in the free-list */
  524. uint32_t footprint = _starpu_compute_data_footprint(handle);
  525. STARPU_TRACE_START_ALLOC_REUSE(dst_node);
  526. PTHREAD_RWLOCK_WRLOCK(&mc_rwlock[node]);
  527. if (try_to_find_reusable_mem_chunk(dst_node, handle, footprint))
  528. {
  529. PTHREAD_RWLOCK_UNLOCK(&mc_rwlock[node]);
  530. _starpu_allocation_cache_hit(dst_node);
  531. ssize_t data_size = _starpu_data_get_size(handle);
  532. return data_size;
  533. }
  534. PTHREAD_RWLOCK_UNLOCK(&mc_rwlock[node]);
  535. STARPU_TRACE_END_ALLOC_REUSE(dst_node);
  536. #endif
  537. do {
  538. STARPU_ASSERT(handle->ops);
  539. STARPU_ASSERT(handle->ops->allocate_data_on_node);
  540. STARPU_TRACE_START_ALLOC(dst_node);
  541. STARPU_ASSERT(replicate->data_interface);
  542. allocated_memory = handle->ops->allocate_data_on_node(replicate->data_interface, dst_node);
  543. STARPU_TRACE_END_ALLOC(dst_node);
  544. if (allocated_memory == -ENOMEM)
  545. {
  546. replicate->refcnt++;
  547. _starpu_spin_unlock(&handle->header_lock);
  548. STARPU_TRACE_START_MEMRECLAIM(dst_node);
  549. reclaim_memory_generic(dst_node, 0);
  550. STARPU_TRACE_END_MEMRECLAIM(dst_node);
  551. while (_starpu_spin_trylock(&handle->header_lock))
  552. _starpu_datawizard_progress(_starpu_get_local_memory_node(), 0);
  553. replicate->refcnt--;
  554. }
  555. } while((allocated_memory == -ENOMEM) && attempts++ < 2);
  556. return allocated_memory;
  557. }
  558. int _starpu_allocate_memory_on_node(starpu_data_handle handle, struct starpu_data_replicate_s *replicate)
  559. {
  560. ssize_t allocated_memory;
  561. unsigned dst_node = replicate->memory_node;
  562. STARPU_ASSERT(handle);
  563. /* A buffer is already allocated on the node */
  564. if (replicate->allocated)
  565. return 0;
  566. STARPU_ASSERT(replicate->data_interface);
  567. allocated_memory = _starpu_allocate_interface(handle, replicate, dst_node);
  568. /* perhaps we could really not handle that capacity misses */
  569. if (allocated_memory == -ENOMEM)
  570. return -ENOMEM;
  571. register_mem_chunk(replicate, allocated_memory, 1);
  572. replicate->allocated = 1;
  573. replicate->automatically_allocated = 1;
  574. return 0;
  575. }
  576. unsigned starpu_data_test_if_allocated_on_node(starpu_data_handle handle, uint32_t memory_node)
  577. {
  578. return handle->per_node[memory_node].allocated;
  579. }