memalloc.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-2013 Université de Bordeaux 1
  4. * Copyright (C) 2010, 2011, 2012, 2013 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/memory_manager.h>
  18. #include <datawizard/memalloc.h>
  19. #include <datawizard/footprint.h>
  20. #include <starpu.h>
  21. /* This per-node RW-locks protect mc_list and memchunk_cache entries */
  22. /* Note: handle header lock is always taken before this */
  23. static starpu_pthread_rwlock_t mc_rwlock[STARPU_MAXNODES];
  24. /* This per-node spinlock protect lru_list */
  25. static struct _starpu_spinlock lru_rwlock[STARPU_MAXNODES];
  26. /* Last Recently used memory chunkgs */
  27. static struct _starpu_mem_chunk_lru_list *starpu_lru_list[STARPU_MAXNODES];
  28. /* Potentially in use memory chunks */
  29. static struct _starpu_mem_chunk_list *mc_list[STARPU_MAXNODES];
  30. /* Explicitly caches memory chunks that can be reused */
  31. static struct _starpu_mem_chunk_list *memchunk_cache[STARPU_MAXNODES];
  32. /* When reclaiming memory to allocate, we reclaim MAX(what_is_to_reclaim_on_device, data_size_coefficient*data_size) */
  33. const unsigned starpu_memstrategy_data_size_coefficient=2;
  34. static void starpu_lru(unsigned node);
  35. void _starpu_init_mem_chunk_lists(void)
  36. {
  37. unsigned i;
  38. for (i = 0; i < STARPU_MAXNODES; i++)
  39. {
  40. STARPU_PTHREAD_RWLOCK_INIT(&mc_rwlock[i], NULL);
  41. _starpu_spin_init(&lru_rwlock[i]);
  42. mc_list[i] = _starpu_mem_chunk_list_new();
  43. starpu_lru_list[i] = _starpu_mem_chunk_lru_list_new();
  44. memchunk_cache[i] = _starpu_mem_chunk_list_new();
  45. }
  46. }
  47. void _starpu_deinit_mem_chunk_lists(void)
  48. {
  49. unsigned i;
  50. for (i = 0; i < STARPU_MAXNODES; i++)
  51. {
  52. _starpu_mem_chunk_list_delete(mc_list[i]);
  53. _starpu_mem_chunk_list_delete(memchunk_cache[i]);
  54. _starpu_mem_chunk_lru_list_delete(starpu_lru_list[i]);
  55. _starpu_spin_destroy(&lru_rwlock[i]);
  56. STARPU_PTHREAD_RWLOCK_DESTROY(&mc_rwlock[i]);
  57. }
  58. }
  59. /*
  60. * Manipulate subtrees
  61. */
  62. static void lock_all_subtree(starpu_data_handle_t handle)
  63. {
  64. unsigned child;
  65. /* lock parent */
  66. while (_starpu_spin_trylock(&handle->header_lock))
  67. _starpu_datawizard_progress(_starpu_memory_node_get_local_key(), 0);
  68. /* lock all sub-subtrees children */
  69. for (child = 0; child < handle->nchildren; child++)
  70. {
  71. starpu_data_handle_t child_handle = starpu_data_get_child(handle, child);
  72. lock_all_subtree(child_handle);
  73. }
  74. }
  75. static void unlock_all_subtree(starpu_data_handle_t handle)
  76. {
  77. /* lock all sub-subtrees children
  78. * Note that this is done in the reverse order of the
  79. * lock_all_subtree so that we avoid deadlock */
  80. unsigned i;
  81. for (i =0; i < handle->nchildren; i++)
  82. {
  83. unsigned child = handle->nchildren - 1 - i;
  84. starpu_data_handle_t child_handle = starpu_data_get_child(handle, child);
  85. unlock_all_subtree(child_handle);
  86. }
  87. _starpu_spin_unlock(&handle->header_lock);
  88. }
  89. static unsigned may_free_subtree(starpu_data_handle_t handle, unsigned node)
  90. {
  91. /* we only free if no one refers to the leaf */
  92. uint32_t refcnt = _starpu_get_data_refcnt(handle, node);
  93. if (refcnt)
  94. return 0;
  95. if (!handle->nchildren)
  96. return 1;
  97. /* look into all sub-subtrees children */
  98. unsigned child;
  99. for (child = 0; child < handle->nchildren; child++)
  100. {
  101. unsigned res;
  102. starpu_data_handle_t child_handle = starpu_data_get_child(handle, child);
  103. res = may_free_subtree(child_handle, node);
  104. if (!res) return 0;
  105. }
  106. /* no problem was found */
  107. return 1;
  108. }
  109. static void transfer_subtree_to_node(starpu_data_handle_t handle, unsigned src_node,
  110. unsigned dst_node)
  111. {
  112. unsigned i;
  113. unsigned last = 0;
  114. unsigned cnt;
  115. int ret;
  116. if (handle->nchildren == 0)
  117. {
  118. struct _starpu_data_replicate *src_replicate = &handle->per_node[src_node];
  119. struct _starpu_data_replicate *dst_replicate = &handle->per_node[dst_node];
  120. /* this is a leaf */
  121. switch(src_replicate->state)
  122. {
  123. case STARPU_OWNER:
  124. /* the local node has the only copy */
  125. /* the owner is now the destination_node */
  126. src_replicate->state = STARPU_INVALID;
  127. dst_replicate->state = STARPU_OWNER;
  128. #ifdef STARPU_DEVEL
  129. #warning we should use requests during memory reclaim
  130. #endif
  131. /* TODO use request !! */
  132. /* Take temporary references on the replicates */
  133. _starpu_spin_checklocked(&handle->header_lock);
  134. src_replicate->refcnt++;
  135. dst_replicate->refcnt++;
  136. handle->busy_count+=2;
  137. ret = _starpu_driver_copy_data_1_to_1(handle, src_replicate, dst_replicate, 0, NULL, 1);
  138. STARPU_ASSERT(ret == 0);
  139. src_replicate->refcnt--;
  140. dst_replicate->refcnt--;
  141. STARPU_ASSERT(handle->busy_count >= 2);
  142. handle->busy_count -= 2;
  143. ret = _starpu_data_check_not_busy(handle);
  144. STARPU_ASSERT(ret == 0);
  145. break;
  146. case STARPU_SHARED:
  147. /* some other node may have the copy */
  148. src_replicate->state = STARPU_INVALID;
  149. /* count the number of copies */
  150. cnt = 0;
  151. for (i = 0; i < STARPU_MAXNODES; i++)
  152. {
  153. if (handle->per_node[i].state == STARPU_SHARED)
  154. {
  155. cnt++;
  156. last = i;
  157. }
  158. }
  159. STARPU_ASSERT(cnt > 0);
  160. if (cnt == 1)
  161. handle->per_node[last].state = STARPU_OWNER;
  162. break;
  163. case STARPU_INVALID:
  164. /* nothing to be done */
  165. break;
  166. default:
  167. STARPU_ABORT();
  168. break;
  169. }
  170. }
  171. else
  172. {
  173. /* lock all sub-subtrees children */
  174. unsigned child;
  175. for (child = 0; child < handle->nchildren; child++)
  176. {
  177. starpu_data_handle_t child_handle = starpu_data_get_child(handle, child);
  178. transfer_subtree_to_node(child_handle, src_node, dst_node);
  179. }
  180. }
  181. }
  182. static size_t free_memory_on_node(struct _starpu_mem_chunk *mc, unsigned node)
  183. {
  184. size_t freed = 0;
  185. STARPU_ASSERT(mc->ops);
  186. STARPU_ASSERT(mc->ops->free_data_on_node);
  187. starpu_data_handle_t handle = mc->data;
  188. struct _starpu_data_replicate *replicate = mc->replicate;
  189. if (handle)
  190. _starpu_spin_checklocked(&handle->header_lock);
  191. if (mc->automatically_allocated &&
  192. (!handle || replicate->refcnt == 0))
  193. {
  194. if (handle)
  195. STARPU_ASSERT(replicate->allocated);
  196. #if defined(STARPU_USE_CUDA) && defined(HAVE_CUDA_MEMCPY_PEER) && !defined(STARPU_SIMGRID)
  197. if (starpu_node_get_kind(node) == STARPU_CUDA_RAM)
  198. {
  199. /* To facilitate the design of interface, we set the
  200. * proper CUDA device in case it is needed. This avoids
  201. * having to set it again in the free method of each
  202. * interface. */
  203. starpu_cuda_set_device(_starpu_memory_node_get_devid(node));
  204. }
  205. #endif
  206. mc->ops->free_data_on_node(mc->chunk_interface, node);
  207. if (handle)
  208. {
  209. replicate->allocated = 0;
  210. /* XXX why do we need that ? */
  211. replicate->automatically_allocated = 0;
  212. }
  213. freed = mc->size;
  214. if (handle)
  215. STARPU_ASSERT(replicate->refcnt == 0);
  216. }
  217. return freed;
  218. }
  219. static size_t do_free_mem_chunk(struct _starpu_mem_chunk *mc, unsigned node)
  220. {
  221. size_t size;
  222. starpu_data_handle_t handle = mc->data;
  223. if (handle)
  224. _starpu_spin_checklocked(&handle->header_lock);
  225. mc->replicate->mc=NULL;
  226. /* free the actual buffer */
  227. size = free_memory_on_node(mc, node);
  228. /* remove the mem_chunk from the list */
  229. _starpu_mem_chunk_list_erase(mc_list[node], mc);
  230. free(mc->chunk_interface);
  231. _starpu_mem_chunk_delete(mc);
  232. return size;
  233. }
  234. /* This function is called for memory chunks that are possibly in used (ie. not
  235. * in the cache). They should therefore still be associated to a handle. */
  236. static size_t try_to_free_mem_chunk(struct _starpu_mem_chunk *mc, unsigned node)
  237. {
  238. size_t freed = 0;
  239. starpu_data_handle_t handle;
  240. handle = mc->data;
  241. STARPU_ASSERT(handle);
  242. /* This data should be written through to this node, avoid dropping it! */
  243. if (handle->wt_mask & (1<<node))
  244. return 0;
  245. /* REDUX memchunk */
  246. if (mc->relaxed_coherency == 2)
  247. {
  248. /* TODO: reduce it back to e.g. main memory */
  249. }
  250. else
  251. /* Either it's a "relaxed coherency" memchunk (SCRATCH), or it's a
  252. * memchunk that could be used with filters. */
  253. if (mc->relaxed_coherency == 1)
  254. {
  255. STARPU_ASSERT(mc->replicate);
  256. while (_starpu_spin_trylock(&handle->header_lock))
  257. _starpu_datawizard_progress(_starpu_memory_node_get_local_key(), 0);
  258. if (mc->replicate->refcnt == 0)
  259. {
  260. /* Note taht there is no need to transfer any data or
  261. * to update the status in terms of MSI protocol
  262. * because this memchunk is associated to a replicate
  263. * in "relaxed coherency" mode. */
  264. freed = do_free_mem_chunk(mc, node);
  265. }
  266. _starpu_spin_unlock(&handle->header_lock);
  267. }
  268. else
  269. {
  270. /* try to lock all the subtree */
  271. lock_all_subtree(handle);
  272. /* check if they are all "free" */
  273. if (may_free_subtree(handle, node))
  274. {
  275. STARPU_ASSERT(handle->per_node[node].refcnt == 0);
  276. #ifdef STARPU_MEMORY_STATS
  277. if (handle->per_node[node].state == STARPU_OWNER)
  278. _starpu_memory_handle_stats_invalidated(handle, node);
  279. /* else XXX Considering only owner to invalidate */
  280. #endif
  281. /* in case there was nobody using that buffer, throw it
  282. * away after writing it back to main memory */
  283. transfer_subtree_to_node(handle, node, 0);
  284. #ifdef STARPU_MEMORY_STATS
  285. _starpu_memory_handle_stats_loaded_owner(handle, 0);
  286. #endif
  287. STARPU_ASSERT(handle->per_node[node].refcnt == 0);
  288. /* now the actual buffer may be freed */
  289. freed = do_free_mem_chunk(mc, node);
  290. }
  291. /* unlock the leafs */
  292. unlock_all_subtree(handle);
  293. }
  294. return freed;
  295. }
  296. #ifdef STARPU_USE_ALLOCATION_CACHE
  297. /* We assume that mc_rwlock[node] is taken. is_already_in_mc_list indicates
  298. * that the mc is already in the list of buffers that are possibly used, and
  299. * therefore not in the cache. */
  300. static void reuse_mem_chunk(unsigned node, struct _starpu_data_replicate *new_replicate, struct _starpu_mem_chunk *mc, unsigned is_already_in_mc_list)
  301. {
  302. /* we found an appropriate mem chunk: so we get it out
  303. * of the "to free" list, and reassign it to the new
  304. * piece of data */
  305. if (!is_already_in_mc_list)
  306. {
  307. _starpu_mem_chunk_list_erase(memchunk_cache[node], mc);
  308. }
  309. struct _starpu_data_replicate *old_replicate = mc->replicate;
  310. old_replicate->allocated = 0;
  311. old_replicate->automatically_allocated = 0;
  312. old_replicate->initialized = 0;
  313. new_replicate->allocated = 1;
  314. new_replicate->automatically_allocated = 1;
  315. new_replicate->initialized = 0;
  316. STARPU_ASSERT(new_replicate->data_interface);
  317. STARPU_ASSERT(mc->chunk_interface);
  318. memcpy(new_replicate->data_interface, mc->chunk_interface, old_replicate->handle->ops->interface_size);
  319. mc->data = new_replicate->handle;
  320. /* mc->ops, mc->footprint and mc->interface should be
  321. * unchanged ! */
  322. /* reinsert the mem chunk in the list of active memory chunks */
  323. if (!is_already_in_mc_list)
  324. {
  325. _starpu_mem_chunk_list_push_front(mc_list[node], mc);
  326. }
  327. }
  328. static unsigned try_to_reuse_mem_chunk(struct _starpu_mem_chunk *mc, unsigned node, struct _starpu_data_replicate *replicate, unsigned is_already_in_mc_list)
  329. {
  330. unsigned success = 0;
  331. starpu_data_handle_t old_data;
  332. old_data = mc->data;
  333. STARPU_ASSERT(old_data);
  334. /* try to lock all the subtree */
  335. lock_all_subtree(old_data);
  336. /* check if they are all "free" */
  337. if (may_free_subtree(old_data, node))
  338. {
  339. success = 1;
  340. /* in case there was nobody using that buffer, throw it
  341. * away after writing it back to main memory */
  342. transfer_subtree_to_node(old_data, node, 0);
  343. /* now replace the previous data */
  344. reuse_mem_chunk(node, replicate, mc, is_already_in_mc_list);
  345. }
  346. /* unlock the leafs */
  347. unlock_all_subtree(old_data);
  348. return success;
  349. }
  350. static int _starpu_data_interface_compare(void *data_interface_a, struct starpu_data_interface_ops *ops_a,
  351. void *data_interface_b, struct starpu_data_interface_ops *ops_b)
  352. {
  353. if (ops_a->interfaceid != ops_b->interfaceid)
  354. return -1;
  355. int ret = ops_a->compare(data_interface_a, data_interface_b);
  356. return ret;
  357. }
  358. /* This function must be called with mc_rwlock[node] taken in write mode */
  359. static struct _starpu_mem_chunk *_starpu_memchunk_cache_lookup_locked(unsigned node, starpu_data_handle_t handle)
  360. {
  361. uint32_t footprint = _starpu_compute_data_footprint(handle);
  362. /* go through all buffers in the cache */
  363. struct _starpu_mem_chunk *mc;
  364. for (mc = _starpu_mem_chunk_list_begin(memchunk_cache[node]);
  365. mc != _starpu_mem_chunk_list_end(memchunk_cache[node]);
  366. mc = _starpu_mem_chunk_list_next(mc))
  367. {
  368. if (mc->footprint == footprint)
  369. {
  370. /* Is that a false hit ? (this is _very_ unlikely) */
  371. if (_starpu_data_interface_compare(handle->per_node[node].data_interface, handle->ops, mc->chunk_interface, mc->ops))
  372. continue;
  373. /* Cache hit */
  374. /* Remove from the cache */
  375. _starpu_mem_chunk_list_erase(memchunk_cache[node], mc);
  376. return mc;
  377. }
  378. }
  379. /* This is a cache miss */
  380. return NULL;
  381. }
  382. /* this function looks for a memory chunk that matches a given footprint in the
  383. * list of mem chunk that need to be freed. This function must be called with
  384. * mc_rwlock[node] taken in write mode. */
  385. static unsigned try_to_find_reusable_mem_chunk(unsigned node, starpu_data_handle_t data, struct _starpu_data_replicate *replicate, uint32_t footprint)
  386. {
  387. struct _starpu_mem_chunk *mc, *next_mc;
  388. /* go through all buffers in the cache */
  389. mc = _starpu_memchunk_cache_lookup_locked(node, data);
  390. if (mc)
  391. {
  392. /* We found an entry in the cache so we can reuse it */
  393. reuse_mem_chunk(node, replicate, mc, 0);
  394. return 1;
  395. }
  396. /* now look for some non essential data in the active list */
  397. for (mc = _starpu_mem_chunk_list_begin(mc_list[node]);
  398. mc != _starpu_mem_chunk_list_end(mc_list[node]);
  399. mc = next_mc)
  400. {
  401. /* there is a risk that the memory chunk is freed before next
  402. * iteration starts: so we compute the next element of the list
  403. * now */
  404. next_mc = _starpu_mem_chunk_list_next(mc);
  405. if (mc->data->is_not_important && (mc->footprint == footprint))
  406. {
  407. // fprintf(stderr, "found a candidate ...\n");
  408. if (try_to_reuse_mem_chunk(mc, node, replicate, 1))
  409. return 1;
  410. }
  411. }
  412. return 0;
  413. }
  414. #endif
  415. /*
  416. * Free the memory chuncks that are explicitely tagged to be freed. The
  417. * mc_rwlock[node] rw-lock should be taken prior to calling this function.
  418. */
  419. static size_t flush_memchunk_cache(unsigned node, size_t reclaim)
  420. {
  421. struct _starpu_mem_chunk *mc;
  422. size_t freed = 0;
  423. STARPU_PTHREAD_RWLOCK_WRLOCK(&mc_rwlock[node]);
  424. while (!_starpu_mem_chunk_list_empty(memchunk_cache[node])) {
  425. mc = _starpu_mem_chunk_list_pop_front(memchunk_cache[node]);
  426. STARPU_PTHREAD_RWLOCK_UNLOCK(&mc_rwlock[node]);
  427. starpu_data_handle_t handle = mc->data;
  428. if (handle)
  429. while (_starpu_spin_trylock(&handle->header_lock))
  430. _starpu_datawizard_progress(_starpu_memory_node_get_local_key(), 0);
  431. freed += free_memory_on_node(mc, node);
  432. if (handle)
  433. _starpu_spin_unlock(&handle->header_lock);
  434. free(mc->chunk_interface);
  435. _starpu_mem_chunk_delete(mc);
  436. STARPU_PTHREAD_RWLOCK_WRLOCK(&mc_rwlock[node]);
  437. if (reclaim && freed>reclaim)
  438. break;
  439. }
  440. STARPU_PTHREAD_RWLOCK_UNLOCK(&mc_rwlock[node]);
  441. return freed;
  442. }
  443. /*
  444. * Try to free the buffers currently in use on the memory node. If the force
  445. * flag is set, the memory is freed regardless of coherency concerns (this
  446. * should only be used at the termination of StarPU for instance). The
  447. * mc_rwlock[node] rw-lock should be taken prior to calling this function.
  448. */
  449. static size_t free_potentially_in_use_mc(unsigned node, unsigned force, size_t reclaim)
  450. {
  451. size_t freed = 0;
  452. struct _starpu_mem_chunk *mc, *next_mc = NULL;
  453. /*
  454. * We have to unlock mc_rwlock before locking header_lock, so we have
  455. * to be careful with the list. We try to do just one pass, by
  456. * remembering the next mc to be tried. If it gets dropped, we restart
  457. * from zero. So we continue until we go through the whole list without
  458. * finding anything to free.
  459. */
  460. while (1)
  461. {
  462. STARPU_PTHREAD_RWLOCK_WRLOCK(&mc_rwlock[node]);
  463. /* A priori, start from the beginning */
  464. mc = _starpu_mem_chunk_list_begin(mc_list[node]);
  465. if (next_mc)
  466. /* Unless we might restart from where we were */
  467. for (mc = _starpu_mem_chunk_list_begin(mc_list[node]);
  468. mc != _starpu_mem_chunk_list_end(mc_list[node]);
  469. mc = _starpu_mem_chunk_list_next(mc))
  470. if (mc == next_mc)
  471. /* Yes, restart from there. */
  472. break;
  473. if (mc == _starpu_mem_chunk_list_end(mc_list[node]))
  474. {
  475. /* But it was the last one of the list :/ */
  476. STARPU_PTHREAD_RWLOCK_UNLOCK(&mc_rwlock[node]);
  477. break;
  478. }
  479. /* Remember where to try next */
  480. next_mc = _starpu_mem_chunk_list_next(mc);
  481. STARPU_PTHREAD_RWLOCK_UNLOCK(&mc_rwlock[node]);
  482. if (!force)
  483. {
  484. freed += try_to_free_mem_chunk(mc, node);
  485. if (reclaim && freed > reclaim)
  486. break;
  487. }
  488. else
  489. {
  490. starpu_data_handle_t handle = mc->data;
  491. _starpu_spin_lock(&handle->header_lock);
  492. /* We must free the memory now, because we are
  493. * terminating the drivers: note that data coherency is
  494. * not maintained in that case ! */
  495. freed += do_free_mem_chunk(mc, node);
  496. _starpu_spin_unlock(&handle->header_lock);
  497. }
  498. }
  499. return freed;
  500. }
  501. size_t _starpu_memory_reclaim_generic(unsigned node, unsigned force, size_t reclaim)
  502. {
  503. size_t freed = 0;
  504. starpu_lru(node);
  505. /* remove all buffers for which there was a removal request */
  506. freed += flush_memchunk_cache(node, reclaim);
  507. /* try to free all allocated data potentially in use */
  508. if (reclaim && freed<reclaim)
  509. freed += free_potentially_in_use_mc(node, force, reclaim);
  510. return freed;
  511. }
  512. /*
  513. * This function frees all the memory that was implicitely allocated by StarPU
  514. * (for the data replicates). This is not ensuring data coherency, and should
  515. * only be called while StarPU is getting shut down.
  516. */
  517. size_t _starpu_free_all_automatically_allocated_buffers(unsigned node)
  518. {
  519. return _starpu_memory_reclaim_generic(node, 1, 0);
  520. }
  521. static struct _starpu_mem_chunk *_starpu_memchunk_init(struct _starpu_data_replicate *replicate, size_t interface_size, unsigned automatically_allocated)
  522. {
  523. struct _starpu_mem_chunk *mc = _starpu_mem_chunk_new();
  524. starpu_data_handle_t handle = replicate->handle;
  525. STARPU_ASSERT(handle);
  526. STARPU_ASSERT(handle->ops);
  527. mc->data = handle;
  528. mc->footprint = _starpu_compute_data_footprint(handle);
  529. mc->ops = handle->ops;
  530. mc->automatically_allocated = automatically_allocated;
  531. mc->relaxed_coherency = replicate->relaxed_coherency;
  532. mc->replicate = replicate;
  533. mc->replicate->mc = mc;
  534. /* Save a copy of the interface */
  535. mc->chunk_interface = malloc(interface_size);
  536. STARPU_ASSERT(mc->chunk_interface);
  537. memcpy(mc->chunk_interface, replicate->data_interface, interface_size);
  538. return mc;
  539. }
  540. static void register_mem_chunk(struct _starpu_data_replicate *replicate, unsigned automatically_allocated)
  541. {
  542. unsigned dst_node = replicate->memory_node;
  543. struct _starpu_mem_chunk *mc;
  544. /* the interface was already filled by ops->allocate_data_on_node */
  545. size_t interface_size = replicate->handle->ops->interface_size;
  546. /* Put this memchunk in the list of memchunk in use */
  547. mc = _starpu_memchunk_init(replicate, interface_size, automatically_allocated);
  548. STARPU_PTHREAD_RWLOCK_WRLOCK(&mc_rwlock[dst_node]);
  549. _starpu_mem_chunk_list_push_back(mc_list[dst_node], mc);
  550. STARPU_PTHREAD_RWLOCK_UNLOCK(&mc_rwlock[dst_node]);
  551. }
  552. /* This function is called when the handle is destroyed (eg. when calling
  553. * unregister or unpartition). It puts all the memchunks that refer to the
  554. * specified handle into the cache.
  555. */
  556. void _starpu_request_mem_chunk_removal(starpu_data_handle_t handle, struct _starpu_data_replicate *replicate, unsigned node, size_t size)
  557. {
  558. struct _starpu_mem_chunk *mc = replicate->mc;
  559. STARPU_ASSERT(mc->data == handle);
  560. /* Record the allocated size, so that later in memory
  561. * reclaiming we can estimate how much memory we free
  562. * by freeing this. */
  563. mc->size = size;
  564. /* This memchunk doesn't have to do with the data any more. */
  565. replicate->mc = NULL;
  566. replicate->allocated = 0;
  567. replicate->automatically_allocated = 0;
  568. STARPU_PTHREAD_RWLOCK_WRLOCK(&mc_rwlock[node]);
  569. mc->data = NULL;
  570. /* remove it from the main list */
  571. _starpu_mem_chunk_list_erase(mc_list[node], mc);
  572. STARPU_PTHREAD_RWLOCK_UNLOCK(&mc_rwlock[node]);
  573. /* We would never flush the node 0 cache, unless
  574. * malloc() returns NULL, which is very unlikely... */
  575. /* This is particularly important when
  576. * STARPU_USE_ALLOCATION_CACHE is not enabled, as we
  577. * wouldn't even re-use these allocations! */
  578. if (starpu_node_get_kind(node) == STARPU_CPU_RAM)
  579. {
  580. free_memory_on_node(mc, node);
  581. free(mc->chunk_interface);
  582. _starpu_mem_chunk_delete(mc);
  583. }
  584. else
  585. /* put it in the list of buffers to be removed */
  586. _starpu_mem_chunk_list_push_front(memchunk_cache[node], mc);
  587. }
  588. /*
  589. * In order to allocate a piece of data, we try to reuse existing buffers if
  590. * its possible.
  591. * 1 - we try to reuse a memchunk that is explicitely unused.
  592. * 2 - we go through the list of memory chunks and find one that is not
  593. * referenced and that has the same footprint to reuse it.
  594. * 3 - we call the usual driver's alloc method
  595. * 4 - we go through the list of memory chunks and release those that are
  596. * not referenced (or part of those).
  597. *
  598. */
  599. static starpu_ssize_t _starpu_allocate_interface(starpu_data_handle_t handle, struct _starpu_data_replicate *replicate, unsigned dst_node, unsigned is_prefetch)
  600. {
  601. unsigned attempts = 0;
  602. starpu_ssize_t allocated_memory;
  603. int ret;
  604. _starpu_spin_checklocked(&handle->header_lock);
  605. _starpu_data_allocation_inc_stats(dst_node);
  606. #ifdef STARPU_USE_ALLOCATION_CACHE
  607. /* perhaps we can directly reuse a buffer in the free-list */
  608. uint32_t footprint = _starpu_compute_data_footprint(handle);
  609. _STARPU_TRACE_START_ALLOC_REUSE(dst_node);
  610. STARPU_PTHREAD_RWLOCK_WRLOCK(&mc_rwlock[dst_node]);
  611. if (try_to_find_reusable_mem_chunk(dst_node, handle, replicate, footprint))
  612. {
  613. STARPU_PTHREAD_RWLOCK_UNLOCK(&mc_rwlock[dst_node]);
  614. _starpu_allocation_cache_hit(dst_node);
  615. starpu_ssize_t data_size = _starpu_data_get_size(handle);
  616. return data_size;
  617. }
  618. STARPU_PTHREAD_RWLOCK_UNLOCK(&mc_rwlock[dst_node]);
  619. _STARPU_TRACE_END_ALLOC_REUSE(dst_node);
  620. #endif
  621. do
  622. {
  623. STARPU_ASSERT(handle->ops);
  624. STARPU_ASSERT(handle->ops->allocate_data_on_node);
  625. _STARPU_TRACE_START_ALLOC(dst_node);
  626. STARPU_ASSERT(replicate->data_interface);
  627. #if defined(STARPU_USE_CUDA) && defined(HAVE_CUDA_MEMCPY_PEER) && !defined(STARPU_SIMGRID)
  628. if (starpu_node_get_kind(dst_node) == STARPU_CUDA_RAM)
  629. {
  630. /* To facilitate the design of interface, we set the
  631. * proper CUDA device in case it is needed. This avoids
  632. * having to set it again in the malloc method of each
  633. * interface. */
  634. starpu_cuda_set_device(_starpu_memory_node_get_devid(dst_node));
  635. }
  636. #endif
  637. allocated_memory = handle->ops->allocate_data_on_node(replicate->data_interface, dst_node);
  638. _STARPU_TRACE_END_ALLOC(dst_node);
  639. if (allocated_memory == -ENOMEM)
  640. {
  641. size_t reclaim = 0.25*_starpu_memory_manager_get_global_memory_size(dst_node);
  642. size_t handle_size = handle->ops->get_size(handle);
  643. if (starpu_memstrategy_data_size_coefficient*handle_size > reclaim)
  644. reclaim = starpu_memstrategy_data_size_coefficient*handle_size;
  645. /* Take temporary reference on the replicate */
  646. replicate->refcnt++;
  647. handle->busy_count++;
  648. _starpu_spin_unlock(&handle->header_lock);
  649. _STARPU_TRACE_START_MEMRECLAIM(dst_node);
  650. if (is_prefetch)
  651. {
  652. flush_memchunk_cache(dst_node, reclaim);
  653. }
  654. else
  655. _starpu_memory_reclaim_generic(dst_node, 0, reclaim);
  656. _STARPU_TRACE_END_MEMRECLAIM(dst_node);
  657. while (_starpu_spin_trylock(&handle->header_lock))
  658. _starpu_datawizard_progress(_starpu_memory_node_get_local_key(), 0);
  659. replicate->refcnt--;
  660. STARPU_ASSERT(replicate->refcnt >= 0);
  661. STARPU_ASSERT(handle->busy_count > 0);
  662. handle->busy_count--;
  663. ret = _starpu_data_check_not_busy(handle);
  664. STARPU_ASSERT(ret == 0);
  665. }
  666. }
  667. while((allocated_memory == -ENOMEM) && attempts++ < 2);
  668. return allocated_memory;
  669. }
  670. int _starpu_allocate_memory_on_node(starpu_data_handle_t handle, struct _starpu_data_replicate *replicate, unsigned is_prefetch)
  671. {
  672. starpu_ssize_t allocated_memory;
  673. unsigned dst_node = replicate->memory_node;
  674. STARPU_ASSERT(handle);
  675. /* A buffer is already allocated on the node */
  676. if (replicate->allocated)
  677. return 0;
  678. STARPU_ASSERT(replicate->data_interface);
  679. allocated_memory = _starpu_allocate_interface(handle, replicate, dst_node, is_prefetch);
  680. /* perhaps we could really not handle that capacity misses */
  681. if (allocated_memory == -ENOMEM)
  682. return -ENOMEM;
  683. register_mem_chunk(replicate, 1);
  684. replicate->allocated = 1;
  685. replicate->automatically_allocated = 1;
  686. if (dst_node == 0)
  687. {
  688. void *ptr = starpu_data_handle_to_pointer(handle, 0);
  689. if (ptr != NULL)
  690. {
  691. _starpu_data_register_ram_pointer(handle, ptr);
  692. }
  693. }
  694. return 0;
  695. }
  696. unsigned starpu_data_test_if_allocated_on_node(starpu_data_handle_t handle, unsigned memory_node)
  697. {
  698. return handle->per_node[memory_node].allocated;
  699. }
  700. void _starpu_memchunk_recently_used(struct _starpu_mem_chunk *mc, unsigned node)
  701. {
  702. _starpu_spin_lock(&lru_rwlock[node]);
  703. struct _starpu_mem_chunk_lru *mc_lru=_starpu_mem_chunk_lru_new();
  704. mc_lru->mc=mc;
  705. _starpu_mem_chunk_lru_list_push_front(starpu_lru_list[node],mc_lru);
  706. _starpu_spin_unlock(&lru_rwlock[node]);
  707. }
  708. /* The mc_rwlock[node] rw-lock should be taken prior to calling this function.*/
  709. static void _starpu_memchunk_recently_used_move(struct _starpu_mem_chunk *mc, unsigned node)
  710. {
  711. /* XXX Sometimes the memchunk is not in the list... */
  712. struct _starpu_mem_chunk *mc_iter;
  713. for (mc_iter = _starpu_mem_chunk_list_begin(mc_list[node]);
  714. mc_iter != _starpu_mem_chunk_list_end(mc_list[node]);
  715. mc_iter = _starpu_mem_chunk_list_next(mc_iter) )
  716. {
  717. if (mc_iter==mc)
  718. {
  719. _starpu_mem_chunk_list_erase(mc_list[node], mc);
  720. _starpu_mem_chunk_list_push_back(mc_list[node], mc);
  721. return;
  722. }
  723. }
  724. }
  725. static void starpu_lru(unsigned node)
  726. {
  727. STARPU_PTHREAD_RWLOCK_WRLOCK(&mc_rwlock[node]);
  728. _starpu_spin_lock(&lru_rwlock[node]);
  729. while (!_starpu_mem_chunk_lru_list_empty(starpu_lru_list[node]))
  730. {
  731. struct _starpu_mem_chunk_lru *mc_lru=_starpu_mem_chunk_lru_list_front(starpu_lru_list[node]);
  732. _starpu_memchunk_recently_used_move(mc_lru->mc, node);
  733. _starpu_mem_chunk_lru_list_erase(starpu_lru_list[node], mc_lru);
  734. _starpu_mem_chunk_lru_delete(mc_lru);
  735. }
  736. _starpu_spin_unlock(&lru_rwlock[node]);
  737. STARPU_PTHREAD_RWLOCK_UNLOCK(&mc_rwlock[node]);
  738. }
  739. #ifdef STARPU_MEMORY_STATS
  740. void _starpu_memory_display_stats_by_node(int node)
  741. {
  742. STARPU_PTHREAD_RWLOCK_WRLOCK(&mc_rwlock[node]);
  743. if (!_starpu_mem_chunk_list_empty(mc_list[node]))
  744. {
  745. struct _starpu_mem_chunk *mc;
  746. fprintf(stderr, "#-------\n");
  747. fprintf(stderr, "Data on Node #%d\n",node);
  748. for (mc = _starpu_mem_chunk_list_begin(mc_list[node]);
  749. mc != _starpu_mem_chunk_list_end(mc_list[node]);
  750. mc = _starpu_mem_chunk_list_next(mc))
  751. {
  752. if (mc->automatically_allocated == 0)
  753. _starpu_memory_display_handle_stats(mc->data);
  754. }
  755. }
  756. STARPU_PTHREAD_RWLOCK_UNLOCK(&mc_rwlock[node]);
  757. }
  758. #endif
  759. void starpu_data_display_memory_stats(void)
  760. {
  761. #ifdef STARPU_MEMORY_STATS
  762. unsigned node;
  763. fprintf(stderr, "\n#---------------------\n");
  764. fprintf(stderr, "Memory stats :\n");
  765. for (node = 0; node < STARPU_MAXNODES; node++)
  766. {
  767. _starpu_memory_display_stats_by_node(node);
  768. }
  769. fprintf(stderr, "\n#---------------------\n");
  770. #endif
  771. }