memalloc.c 27 KB

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