memalloc.c 27 KB

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