memalloc.c 27 KB

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