memalloc.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108
  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_cuda.h>
  21. #include <starpu_opencl.h>
  22. /* This per-node RW-locks protect mc_list and memchunk_cache entries */
  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_get_local_memory_node(), 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. _starpu_data_check_not_busy(handle);
  155. break;
  156. case STARPU_SHARED:
  157. /* some other node may have the copy */
  158. src_replicate->state = STARPU_INVALID;
  159. /* count the number of copies */
  160. cnt = 0;
  161. for (i = 0; i < STARPU_MAXNODES; i++)
  162. {
  163. if (handle->per_node[i].state == STARPU_SHARED)
  164. {
  165. cnt++;
  166. last = i;
  167. }
  168. }
  169. STARPU_ASSERT(cnt > 0);
  170. if (cnt == 1)
  171. handle->per_node[last].state = STARPU_OWNER;
  172. break;
  173. case STARPU_INVALID:
  174. /* nothing to be done */
  175. break;
  176. default:
  177. STARPU_ABORT();
  178. break;
  179. }
  180. }
  181. else
  182. {
  183. /* lock all sub-subtrees children */
  184. unsigned child;
  185. for (child = 0; child < handle->nchildren; child++)
  186. {
  187. starpu_data_handle_t child_handle = starpu_data_get_child(handle, child);
  188. transfer_subtree_to_node(child_handle, src_node, dst_node);
  189. }
  190. }
  191. }
  192. static size_t free_memory_on_node(struct _starpu_mem_chunk *mc, uint32_t node)
  193. {
  194. size_t freed = 0;
  195. STARPU_ASSERT(mc->ops);
  196. STARPU_ASSERT(mc->ops->free_data_on_node);
  197. starpu_data_handle_t handle = mc->data;
  198. /* Does this memory chunk refers to a handle that does not exist
  199. * anymore ? */
  200. unsigned data_was_deleted = mc->data_was_deleted;
  201. struct _starpu_data_replicate *replicate = mc->replicate;
  202. // while (_starpu_spin_trylock(&handle->header_lock))
  203. // _starpu_datawizard_progress(_starpu_get_local_memory_node());
  204. #ifdef STARPU_DEVEL
  205. #warning can we block here ?
  206. #endif
  207. // _starpu_spin_lock(&handle->header_lock);
  208. if (mc->automatically_allocated &&
  209. (!handle || data_was_deleted || replicate->refcnt == 0))
  210. {
  211. if (handle && !data_was_deleted)
  212. STARPU_ASSERT(replicate->allocated);
  213. #if defined(STARPU_USE_CUDA) && defined(HAVE_CUDA_MEMCPY_PEER) && !defined(STARPU_SIMGRID)
  214. if (starpu_node_get_kind(node) == STARPU_CUDA_RAM)
  215. {
  216. /* To facilitate the design of interface, we set the
  217. * proper CUDA device in case it is needed. This avoids
  218. * having to set it again in the free method of each
  219. * interface. */
  220. starpu_cuda_set_device(_starpu_memory_node_to_devid(node));
  221. }
  222. #endif
  223. mc->ops->free_data_on_node(mc->chunk_interface, node);
  224. if (handle && !data_was_deleted)
  225. {
  226. replicate->allocated = 0;
  227. /* XXX why do we need that ? */
  228. replicate->automatically_allocated = 0;
  229. }
  230. freed = mc->size;
  231. if (handle && !data_was_deleted)
  232. STARPU_ASSERT(replicate->refcnt == 0);
  233. }
  234. // _starpu_spin_unlock(&handle->header_lock);
  235. return freed;
  236. }
  237. static size_t do_free_mem_chunk(struct _starpu_mem_chunk *mc, unsigned node)
  238. {
  239. size_t size;
  240. mc->replicate->mc=NULL;
  241. /* free the actual buffer */
  242. size = free_memory_on_node(mc, node);
  243. /* remove the mem_chunk from the list */
  244. _starpu_mem_chunk_list_erase(mc_list[node], mc);
  245. free(mc->chunk_interface);
  246. _starpu_mem_chunk_delete(mc);
  247. return size;
  248. }
  249. /* This function is called for memory chunks that are possibly in used (ie. not
  250. * in the cache). They should therefore still be associated to a handle. */
  251. static size_t try_to_free_mem_chunk(struct _starpu_mem_chunk *mc, unsigned node)
  252. {
  253. size_t freed = 0;
  254. starpu_data_handle_t handle;
  255. handle = mc->data;
  256. STARPU_ASSERT(handle);
  257. /* This data should be written through to this node, avoid dropping it! */
  258. if (handle->wt_mask & (1<<node))
  259. return 0;
  260. /* REDUX memchunk */
  261. if (mc->relaxed_coherency == 2)
  262. {
  263. /* TODO: reduce it back to e.g. main memory */
  264. }
  265. else
  266. /* Either it's a "relaxed coherency" memchunk (SCRATCH), or it's a
  267. * memchunk that could be used with filters. */
  268. if (mc->relaxed_coherency == 1)
  269. {
  270. STARPU_ASSERT(mc->replicate);
  271. while (_starpu_spin_trylock(&handle->header_lock))
  272. _starpu_datawizard_progress(_starpu_get_local_memory_node(), 0);
  273. if (mc->replicate->refcnt == 0)
  274. {
  275. /* Note taht there is no need to transfer any data or
  276. * to update the status in terms of MSI protocol
  277. * because this memchunk is associated to a replicate
  278. * in "relaxed coherency" mode. */
  279. freed = do_free_mem_chunk(mc, node);
  280. }
  281. _starpu_spin_unlock(&handle->header_lock);
  282. }
  283. else
  284. {
  285. /* try to lock all the leafs of the subtree */
  286. lock_all_subtree(handle);
  287. /* check if they are all "free" */
  288. if (may_free_subtree(handle, node))
  289. {
  290. STARPU_ASSERT(handle->per_node[node].refcnt == 0);
  291. #ifdef STARPU_MEMORY_STATS
  292. if (handle->per_node[node].state == STARPU_OWNER)
  293. _starpu_memory_handle_stats_invalidated(handle, node);
  294. /* else XXX Considering only owner to invalidate */
  295. #endif
  296. /* in case there was nobody using that buffer, throw it
  297. * away after writing it back to main memory */
  298. transfer_subtree_to_node(handle, node, 0);
  299. #ifdef STARPU_MEMORY_STATS
  300. _starpu_memory_handle_stats_loaded_owner(handle, 0);
  301. #endif
  302. STARPU_ASSERT(handle->per_node[node].refcnt == 0);
  303. /* now the actual buffer may be freed */
  304. freed = do_free_mem_chunk(mc, node);
  305. }
  306. /* unlock the leafs */
  307. unlock_all_subtree(handle);
  308. }
  309. return freed;
  310. }
  311. #ifdef STARPU_USE_ALLOCATION_CACHE
  312. /* We assume that mc_rwlock[node] is taken. is_already_in_mc_list indicates
  313. * that the mc is already in the list of buffers that are possibly used, and
  314. * therefore not in the cache. */
  315. static void reuse_mem_chunk(unsigned node, struct _starpu_data_replicate *new_replicate, struct _starpu_mem_chunk *mc, unsigned is_already_in_mc_list)
  316. {
  317. /* we found an appropriate mem chunk: so we get it out
  318. * of the "to free" list, and reassign it to the new
  319. * piece of data */
  320. if (!is_already_in_mc_list)
  321. {
  322. _starpu_mem_chunk_list_erase(memchunk_cache[node], mc);
  323. }
  324. struct _starpu_data_replicate *old_replicate = mc->replicate;
  325. old_replicate->allocated = 0;
  326. old_replicate->automatically_allocated = 0;
  327. old_replicate->initialized = 0;
  328. new_replicate->allocated = 1;
  329. new_replicate->automatically_allocated = 1;
  330. new_replicate->initialized = 0;
  331. STARPU_ASSERT(new_replicate->data_interface);
  332. STARPU_ASSERT(mc->chunk_interface);
  333. memcpy(new_replicate->data_interface, mc->chunk_interface, old_replicate->handle->ops->interface_size);
  334. mc->data = new_replicate->handle;
  335. mc->data_was_deleted = 0;
  336. /* mc->ops, mc->footprint and mc->interface should be
  337. * unchanged ! */
  338. /* reinsert the mem chunk in the list of active memory chunks */
  339. if (!is_already_in_mc_list)
  340. {
  341. _starpu_mem_chunk_list_push_front(mc_list[node], mc);
  342. }
  343. }
  344. 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)
  345. {
  346. unsigned success = 0;
  347. starpu_data_handle_t old_data;
  348. old_data = mc->data;
  349. STARPU_ASSERT(old_data);
  350. /* try to lock all the leafs of the subtree */
  351. lock_all_subtree(old_data);
  352. /* check if they are all "free" */
  353. if (may_free_subtree(old_data, node))
  354. {
  355. success = 1;
  356. /* in case there was nobody using that buffer, throw it
  357. * away after writing it back to main memory */
  358. transfer_subtree_to_node(old_data, node, 0);
  359. /* now replace the previous data */
  360. reuse_mem_chunk(node, replicate, mc, is_already_in_mc_list);
  361. }
  362. /* unlock the leafs */
  363. unlock_all_subtree(old_data);
  364. return success;
  365. }
  366. static int _starpu_data_interface_compare(void *data_interface_a, struct starpu_data_interface_ops *ops_a,
  367. void *data_interface_b, struct starpu_data_interface_ops *ops_b)
  368. {
  369. if (ops_a->interfaceid != ops_b->interfaceid)
  370. return -1;
  371. int ret = ops_a->compare(data_interface_a, data_interface_b);
  372. return ret;
  373. }
  374. /* This function must be called with mc_rwlock[node] taken in write mode */
  375. static struct _starpu_mem_chunk *_starpu_memchunk_cache_lookup_locked(uint32_t node, starpu_data_handle_t handle)
  376. {
  377. uint32_t footprint = _starpu_compute_data_footprint(handle);
  378. /* go through all buffers in the cache */
  379. struct _starpu_mem_chunk *mc;
  380. for (mc = _starpu_mem_chunk_list_begin(memchunk_cache[node]);
  381. mc != _starpu_mem_chunk_list_end(memchunk_cache[node]);
  382. mc = _starpu_mem_chunk_list_next(mc))
  383. {
  384. if (mc->footprint == footprint)
  385. {
  386. /* Is that a false hit ? (this is _very_ unlikely) */
  387. if (_starpu_data_interface_compare(handle->per_node[node].data_interface, handle->ops, mc->chunk_interface, mc->ops))
  388. continue;
  389. /* Cache hit */
  390. /* Remove from the cache */
  391. _starpu_mem_chunk_list_erase(memchunk_cache[node], mc);
  392. return mc;
  393. }
  394. }
  395. /* This is a cache miss */
  396. return NULL;
  397. }
  398. /* this function looks for a memory chunk that matches a given footprint in the
  399. * list of mem chunk that need to be freed. This function must be called with
  400. * mc_rwlock[node] taken in write mode. */
  401. static unsigned try_to_find_reusable_mem_chunk(unsigned node, starpu_data_handle_t data, struct _starpu_data_replicate *replicate, uint32_t footprint)
  402. {
  403. struct _starpu_mem_chunk *mc, *next_mc;
  404. /* go through all buffers in the cache */
  405. mc = _starpu_memchunk_cache_lookup_locked(node, data);
  406. if (mc)
  407. {
  408. /* We found an entry in the cache so we can reuse it */
  409. reuse_mem_chunk(node, replicate, mc, 0);
  410. return 1;
  411. }
  412. /* now look for some non essential data in the active list */
  413. for (mc = _starpu_mem_chunk_list_begin(mc_list[node]);
  414. mc != _starpu_mem_chunk_list_end(mc_list[node]);
  415. mc = next_mc)
  416. {
  417. /* there is a risk that the memory chunk is freed before next
  418. * iteration starts: so we compute the next element of the list
  419. * now */
  420. next_mc = _starpu_mem_chunk_list_next(mc);
  421. if (mc->data->is_not_important && (mc->footprint == footprint))
  422. {
  423. // fprintf(stderr, "found a candidate ...\n");
  424. if (try_to_reuse_mem_chunk(mc, node, replicate, 1))
  425. return 1;
  426. }
  427. }
  428. return 0;
  429. }
  430. #endif
  431. /*
  432. * Free the memory chuncks that are explicitely tagged to be freed. The
  433. * mc_rwlock[node] rw-lock should be taken prior to calling this function.
  434. */
  435. static size_t flush_memchunk_cache(uint32_t node, size_t reclaim)
  436. {
  437. struct _starpu_mem_chunk *mc, *next_mc;
  438. size_t freed = 0;
  439. for (mc = _starpu_mem_chunk_list_begin(memchunk_cache[node]);
  440. mc != _starpu_mem_chunk_list_end(memchunk_cache[node]);
  441. mc = next_mc)
  442. {
  443. next_mc = _starpu_mem_chunk_list_next(mc);
  444. freed += free_memory_on_node(mc, node);
  445. _starpu_mem_chunk_list_erase(memchunk_cache[node], mc);
  446. free(mc->chunk_interface);
  447. _starpu_mem_chunk_delete(mc);
  448. if (reclaim && freed>reclaim)
  449. break;
  450. }
  451. return freed;
  452. }
  453. /*
  454. * Try to free the buffers currently in use on the memory node. If the force
  455. * flag is set, the memory is freed regardless of coherency concerns (this
  456. * should only be used at the termination of StarPU for instance). The
  457. * mc_rwlock[node] rw-lock should be taken prior to calling this function.
  458. */
  459. static size_t free_potentially_in_use_mc(uint32_t node, unsigned force, size_t reclaim)
  460. {
  461. size_t freed = 0;
  462. struct _starpu_mem_chunk *mc, *next_mc;
  463. for (mc = _starpu_mem_chunk_list_begin(mc_list[node]);
  464. mc != _starpu_mem_chunk_list_end(mc_list[node]);
  465. mc = next_mc)
  466. {
  467. /* there is a risk that the memory chunk is freed
  468. before next iteration starts: so we compute the next
  469. element of the list now */
  470. next_mc = _starpu_mem_chunk_list_next(mc);
  471. if (!force)
  472. {
  473. freed += try_to_free_mem_chunk(mc, node);
  474. #if 1
  475. if (reclaim && freed > reclaim)
  476. break;
  477. #endif
  478. }
  479. else
  480. {
  481. /* We must free the memory now: note that data
  482. * coherency is not maintained in that case ! */
  483. freed += do_free_mem_chunk(mc, node);
  484. }
  485. }
  486. return freed;
  487. }
  488. static size_t reclaim_memory_generic(uint32_t node, unsigned force, size_t reclaim)
  489. {
  490. size_t freed = 0;
  491. _STARPU_PTHREAD_RWLOCK_WRLOCK(&mc_rwlock[node]);
  492. starpu_lru(node);
  493. /* remove all buffers for which there was a removal request */
  494. freed += flush_memchunk_cache(node, reclaim);
  495. /* try to free all allocated data potentially in use */
  496. if (reclaim && freed<reclaim)
  497. freed += free_potentially_in_use_mc(node, force, reclaim);
  498. _STARPU_PTHREAD_RWLOCK_UNLOCK(&mc_rwlock[node]);
  499. return freed;
  500. }
  501. /*
  502. * This function frees all the memory that was implicitely allocated by StarPU
  503. * (for the data replicates). This is not ensuring data coherency, and should
  504. * only be called while StarPU is getting shut down.
  505. */
  506. size_t _starpu_free_all_automatically_allocated_buffers(uint32_t node)
  507. {
  508. return reclaim_memory_generic(node, 1, 0);
  509. }
  510. static struct _starpu_mem_chunk *_starpu_memchunk_init(struct _starpu_data_replicate *replicate, size_t interface_size, unsigned automatically_allocated)
  511. {
  512. struct _starpu_mem_chunk *mc = _starpu_mem_chunk_new();
  513. starpu_data_handle_t handle = replicate->handle;
  514. STARPU_ASSERT(handle);
  515. STARPU_ASSERT(handle->ops);
  516. mc->data = handle;
  517. mc->footprint = _starpu_compute_data_footprint(handle);
  518. mc->ops = handle->ops;
  519. mc->data_was_deleted = 0;
  520. mc->automatically_allocated = automatically_allocated;
  521. mc->relaxed_coherency = replicate->relaxed_coherency;
  522. mc->replicate = replicate;
  523. mc->replicate->mc = mc;
  524. /* Save a copy of the interface */
  525. mc->chunk_interface = malloc(interface_size);
  526. STARPU_ASSERT(mc->chunk_interface);
  527. memcpy(mc->chunk_interface, replicate->data_interface, interface_size);
  528. return mc;
  529. }
  530. static void register_mem_chunk(struct _starpu_data_replicate *replicate, unsigned automatically_allocated)
  531. {
  532. unsigned dst_node = replicate->memory_node;
  533. struct _starpu_mem_chunk *mc;
  534. /* the interface was already filled by ops->allocate_data_on_node */
  535. size_t interface_size = replicate->handle->ops->interface_size;
  536. /* Put this memchunk in the list of memchunk in use */
  537. mc = _starpu_memchunk_init(replicate, interface_size, automatically_allocated);
  538. _STARPU_PTHREAD_RWLOCK_WRLOCK(&mc_rwlock[dst_node]);
  539. _starpu_mem_chunk_list_push_back(mc_list[dst_node], mc);
  540. _STARPU_PTHREAD_RWLOCK_UNLOCK(&mc_rwlock[dst_node]);
  541. }
  542. /* This function is called when the handle is destroyed (eg. when calling
  543. * unregister or unpartition). It puts all the memchunks that refer to the
  544. * specified handle into the cache.
  545. * handle_deleted specifies whether the handle is deleted or not (and thus we
  546. * need to update it)
  547. */
  548. void _starpu_request_mem_chunk_removal(starpu_data_handle_t handle, unsigned node, int handle_deleted)
  549. {
  550. _STARPU_PTHREAD_RWLOCK_WRLOCK(&mc_rwlock[node]);
  551. size_t size = _starpu_data_get_size(handle);
  552. /* TODO: expensive, handle should its own list of chunks? */
  553. /* iterate over the list of memory chunks and remove the entry */
  554. struct _starpu_mem_chunk *mc, *next_mc;
  555. for (mc = _starpu_mem_chunk_list_begin(mc_list[node]);
  556. mc != _starpu_mem_chunk_list_end(mc_list[node]);
  557. mc = next_mc)
  558. {
  559. next_mc = _starpu_mem_chunk_list_next(mc);
  560. if (mc->data == handle)
  561. {
  562. /* we found the data */
  563. mc->size = size;
  564. mc->data_was_deleted = handle_deleted;
  565. /* remove it from the main list */
  566. _starpu_mem_chunk_list_erase(mc_list[node], mc);
  567. /* We would never flush the node 0 cache, unless
  568. * malloc() returns NULL, which is very unlikely... */
  569. /* This is particularly important when
  570. * STARPU_USE_ALLOCATION_CACHE is not enabled, as we
  571. * wouldn't even re-use these allocations! */
  572. if (starpu_node_get_kind(node) == STARPU_CPU_RAM)
  573. {
  574. free_memory_on_node(mc, node);
  575. free(mc->chunk_interface);
  576. _starpu_mem_chunk_delete(mc);
  577. }
  578. else
  579. /* put it in the list of buffers to be removed */
  580. _starpu_mem_chunk_list_push_front(memchunk_cache[node], mc);
  581. /* Note that we do not stop here because there can be
  582. * multiple replicates associated to the same handle on
  583. * the same memory node. */
  584. }
  585. }
  586. /* there was no corresponding buffer ... */
  587. _STARPU_PTHREAD_RWLOCK_UNLOCK(&mc_rwlock[node]);
  588. }
  589. static size_t _starpu_get_global_mem_size(int dst_node)
  590. {
  591. enum starpu_node_kind kind = starpu_node_get_kind(dst_node);
  592. size_t global_mem_size;
  593. switch(kind)
  594. {
  595. case STARPU_CPU_RAM:
  596. {
  597. /* We should probably never get here : if there is no
  598. * space left in RAM, the operating system should swap
  599. * to disk for us. */
  600. STARPU_ABORT();
  601. }
  602. #ifdef STARPU_USE_CUDA
  603. case STARPU_CUDA_RAM:
  604. {
  605. int devid = _starpu_memory_node_to_devid(dst_node);
  606. global_mem_size = starpu_cuda_get_global_mem_size(devid);
  607. break;
  608. }
  609. #endif
  610. #ifdef STARPU_USE_OPENCL
  611. case STARPU_OPENCL_RAM:
  612. {
  613. int devid = _starpu_memory_node_to_devid(dst_node);
  614. global_mem_size = starpu_opencl_get_global_mem_size(devid);
  615. break;
  616. }
  617. #endif
  618. default:
  619. STARPU_ABORT();
  620. }
  621. return global_mem_size;
  622. }
  623. #ifdef STARPU_SIMGRID
  624. static _starpu_pthread_mutex_t cuda_alloc_mutex = _STARPU_PTHREAD_MUTEX_INITIALIZER;
  625. static _starpu_pthread_mutex_t opencl_alloc_mutex = _STARPU_PTHREAD_MUTEX_INITIALIZER;
  626. #endif
  627. uintptr_t
  628. starpu_allocate_buffer_on_node(uint32_t dst_node, size_t size)
  629. {
  630. uintptr_t addr = 0;
  631. #ifdef STARPU_DEVEL
  632. #warning TODO: we need to use starpu_malloc
  633. #endif
  634. switch(starpu_node_get_kind(dst_node))
  635. {
  636. case STARPU_CPU_RAM:
  637. {
  638. addr = (uintptr_t)malloc(size);
  639. _starpu_memory_manager_add_size(size);
  640. break;
  641. }
  642. #if defined(STARPU_USE_CUDA) || defined(STARPU_SIMGRID)
  643. case STARPU_CUDA_RAM:
  644. #ifdef STARPU_SIMGRID
  645. #ifdef STARPU_DEVEL
  646. #warning TODO: record used memory, using a simgrid property to know the available memory
  647. #endif
  648. /* Sleep 10µs for the allocation */
  649. _STARPU_PTHREAD_MUTEX_LOCK(&cuda_alloc_mutex);
  650. MSG_process_sleep(0.000010);
  651. addr = 1;
  652. _STARPU_PTHREAD_MUTEX_UNLOCK(&cuda_alloc_mutex);
  653. #else
  654. cudaError_t status = cudaMalloc((void **)&addr, size);
  655. if (!addr || (status != cudaSuccess))
  656. {
  657. if (STARPU_UNLIKELY(status != cudaErrorMemoryAllocation))
  658. STARPU_CUDA_REPORT_ERROR(status);
  659. addr = 0;
  660. }
  661. #endif
  662. break;
  663. #endif
  664. #if defined(STARPU_USE_OPENCL) || defined(STARPU_SIMGRID)
  665. case STARPU_OPENCL_RAM:
  666. {
  667. #ifdef STARPU_SIMGRID
  668. /* Sleep 10µs for the allocation */
  669. _STARPU_PTHREAD_MUTEX_LOCK(&opencl_alloc_mutex);
  670. MSG_process_sleep(0.000010);
  671. addr = 1;
  672. _STARPU_PTHREAD_MUTEX_UNLOCK(&opencl_alloc_mutex);
  673. #else
  674. int ret;
  675. cl_mem ptr;
  676. ret = starpu_opencl_allocate_memory(&ptr, size, CL_MEM_READ_WRITE);
  677. if (ret)
  678. addr = 0;
  679. else
  680. addr = (uintptr_t)ptr;
  681. break;
  682. #endif
  683. }
  684. #endif
  685. default:
  686. STARPU_ABORT();
  687. }
  688. return addr;
  689. }
  690. void
  691. starpu_free_buffer_on_node(uint32_t dst_node, uintptr_t addr, size_t size)
  692. {
  693. enum starpu_node_kind kind = starpu_node_get_kind(dst_node);
  694. switch(kind)
  695. {
  696. #ifdef STARPU_DEVEL
  697. #warning TODO we need to call starpu_free
  698. #endif
  699. case STARPU_CPU_RAM:
  700. free((void*)addr);
  701. _starpu_memory_manager_sub_size(size);
  702. break;
  703. #if defined(STARPU_USE_CUDA) || defined(STARPU_SIMGRID)
  704. case STARPU_CUDA_RAM:
  705. {
  706. #ifdef STARPU_SIMGRID
  707. _STARPU_PTHREAD_MUTEX_LOCK(&cuda_alloc_mutex);
  708. /* Sleep 10µs for the free */
  709. MSG_process_sleep(0.000010);
  710. _STARPU_PTHREAD_MUTEX_UNLOCK(&cuda_alloc_mutex);
  711. #else
  712. cudaError_t err;
  713. err = cudaFree((void*)addr);
  714. if (STARPU_UNLIKELY(err != cudaSuccess))
  715. STARPU_CUDA_REPORT_ERROR(err);
  716. #endif
  717. break;
  718. }
  719. #endif
  720. #if defined(STARPU_USE_OPENCL) || defined(STARPU_SIMGRID)
  721. case STARPU_OPENCL_RAM:
  722. {
  723. #ifdef STARPU_SIMGRID
  724. _STARPU_PTHREAD_MUTEX_LOCK(&opencl_alloc_mutex);
  725. /* Sleep 10µs for the free */
  726. MSG_process_sleep(0.000010);
  727. _STARPU_PTHREAD_MUTEX_UNLOCK(&opencl_alloc_mutex);
  728. #else
  729. cl_int err;
  730. err = clReleaseMemObject((void*)addr);
  731. if (STARPU_UNLIKELY(err != CL_SUCCESS))
  732. STARPU_OPENCL_REPORT_ERROR(err);
  733. #endif
  734. break;
  735. }
  736. #endif
  737. default:
  738. STARPU_ABORT();
  739. }
  740. }
  741. /*
  742. * In order to allocate a piece of data, we try to reuse existing buffers if
  743. * its possible.
  744. * 1 - we try to reuse a memchunk that is explicitely unused.
  745. * 2 - we go through the list of memory chunks and find one that is not
  746. * referenced and that has the same footprint to reuse it.
  747. * 3 - we call the usual driver's alloc method
  748. * 4 - we go through the list of memory chunks and release those that are
  749. * not referenced (or part of those).
  750. *
  751. */
  752. static ssize_t _starpu_allocate_interface(starpu_data_handle_t handle, struct _starpu_data_replicate *replicate, uint32_t dst_node, unsigned is_prefetch)
  753. {
  754. unsigned attempts = 0;
  755. ssize_t allocated_memory;
  756. _starpu_spin_checklocked(&handle->header_lock);
  757. _starpu_data_allocation_inc_stats(dst_node);
  758. #ifdef STARPU_USE_ALLOCATION_CACHE
  759. /* perhaps we can directly reuse a buffer in the free-list */
  760. uint32_t footprint = _starpu_compute_data_footprint(handle);
  761. _STARPU_TRACE_START_ALLOC_REUSE(dst_node);
  762. _STARPU_PTHREAD_RWLOCK_WRLOCK(&mc_rwlock[dst_node]);
  763. if (try_to_find_reusable_mem_chunk(dst_node, handle, replicate, footprint))
  764. {
  765. _STARPU_PTHREAD_RWLOCK_UNLOCK(&mc_rwlock[dst_node]);
  766. _starpu_allocation_cache_hit(dst_node);
  767. ssize_t data_size = _starpu_data_get_size(handle);
  768. return data_size;
  769. }
  770. _STARPU_PTHREAD_RWLOCK_UNLOCK(&mc_rwlock[dst_node]);
  771. _STARPU_TRACE_END_ALLOC_REUSE(dst_node);
  772. #endif
  773. do
  774. {
  775. STARPU_ASSERT(handle->ops);
  776. STARPU_ASSERT(handle->ops->allocate_data_on_node);
  777. _STARPU_TRACE_START_ALLOC(dst_node);
  778. STARPU_ASSERT(replicate->data_interface);
  779. #if defined(STARPU_USE_CUDA) && defined(HAVE_CUDA_MEMCPY_PEER) && !defined(STARPU_SIMGRID)
  780. if (starpu_node_get_kind(dst_node) == STARPU_CUDA_RAM)
  781. {
  782. /* To facilitate the design of interface, we set the
  783. * proper CUDA device in case it is needed. This avoids
  784. * having to set it again in the malloc method of each
  785. * interface. */
  786. starpu_cuda_set_device(_starpu_memory_node_to_devid(dst_node));
  787. }
  788. #endif
  789. allocated_memory = handle->ops->allocate_data_on_node(replicate->data_interface, dst_node);
  790. _STARPU_TRACE_END_ALLOC(dst_node);
  791. if (allocated_memory == -ENOMEM)
  792. {
  793. size_t reclaim = 0.25*_starpu_get_global_mem_size(dst_node);
  794. size_t handle_size = handle->ops->get_size(handle);
  795. if (starpu_memstrategy_data_size_coefficient*handle_size > reclaim)
  796. reclaim = starpu_memstrategy_data_size_coefficient*handle_size;
  797. /* Take temporary reference on the replicate */
  798. replicate->refcnt++;
  799. handle->busy_count++;
  800. _starpu_spin_unlock(&handle->header_lock);
  801. _STARPU_TRACE_START_MEMRECLAIM(dst_node);
  802. if (is_prefetch)
  803. {
  804. _STARPU_PTHREAD_RWLOCK_WRLOCK(&mc_rwlock[dst_node]);
  805. flush_memchunk_cache(dst_node, reclaim);
  806. _STARPU_PTHREAD_RWLOCK_UNLOCK(&mc_rwlock[dst_node]);
  807. }
  808. else
  809. reclaim_memory_generic(dst_node, 0, reclaim);
  810. _STARPU_TRACE_END_MEMRECLAIM(dst_node);
  811. while (_starpu_spin_trylock(&handle->header_lock))
  812. _starpu_datawizard_progress(_starpu_get_local_memory_node(), 0);
  813. replicate->refcnt--;
  814. STARPU_ASSERT(replicate->refcnt >= 0);
  815. STARPU_ASSERT(handle->busy_count > 0);
  816. handle->busy_count--;
  817. _starpu_data_check_not_busy(handle);
  818. }
  819. }
  820. while((allocated_memory == -ENOMEM) && attempts++ < 2);
  821. return allocated_memory;
  822. }
  823. int _starpu_allocate_memory_on_node(starpu_data_handle_t handle, struct _starpu_data_replicate *replicate, unsigned is_prefetch)
  824. {
  825. ssize_t allocated_memory;
  826. unsigned dst_node = replicate->memory_node;
  827. STARPU_ASSERT(handle);
  828. /* A buffer is already allocated on the node */
  829. if (replicate->allocated)
  830. return 0;
  831. STARPU_ASSERT(replicate->data_interface);
  832. allocated_memory = _starpu_allocate_interface(handle, replicate, dst_node, is_prefetch);
  833. /* perhaps we could really not handle that capacity misses */
  834. if (allocated_memory == -ENOMEM)
  835. return -ENOMEM;
  836. register_mem_chunk(replicate, 1);
  837. replicate->allocated = 1;
  838. replicate->automatically_allocated = 1;
  839. if (dst_node == 0)
  840. {
  841. void *ptr = starpu_handle_to_pointer(handle, 0);
  842. if (ptr != NULL)
  843. {
  844. _starpu_data_register_ram_pointer(handle, ptr);
  845. }
  846. }
  847. return 0;
  848. }
  849. unsigned starpu_data_test_if_allocated_on_node(starpu_data_handle_t handle, uint32_t memory_node)
  850. {
  851. return handle->per_node[memory_node].allocated;
  852. }
  853. void _starpu_memchunk_recently_used(struct _starpu_mem_chunk *mc, unsigned node)
  854. {
  855. _starpu_spin_lock(&lru_rwlock[node]);
  856. struct _starpu_mem_chunk_lru *mc_lru=_starpu_mem_chunk_lru_new();
  857. mc_lru->mc=mc;
  858. _starpu_mem_chunk_lru_list_push_front(starpu_lru_list[node],mc_lru);
  859. _starpu_spin_unlock(&lru_rwlock[node]);
  860. }
  861. /* The mc_rwlock[node] rw-lock should be taken prior to calling this function.*/
  862. static void _starpu_memchunk_recently_used_move(struct _starpu_mem_chunk *mc, unsigned node)
  863. {
  864. /* XXX Sometimes the memchunk is not in the list... */
  865. struct _starpu_mem_chunk *mc_iter;
  866. for (mc_iter = _starpu_mem_chunk_list_begin(mc_list[node]);
  867. mc_iter != _starpu_mem_chunk_list_end(mc_list[node]);
  868. mc_iter = _starpu_mem_chunk_list_next(mc_iter) )
  869. {
  870. if (mc_iter==mc)
  871. {
  872. _starpu_mem_chunk_list_erase(mc_list[node], mc);
  873. _starpu_mem_chunk_list_push_back(mc_list[node], mc);
  874. return;
  875. }
  876. }
  877. }
  878. static void starpu_lru(unsigned node)
  879. {
  880. _starpu_spin_lock(&lru_rwlock[node]);
  881. while (!_starpu_mem_chunk_lru_list_empty(starpu_lru_list[node]))
  882. {
  883. struct _starpu_mem_chunk_lru *mc_lru=_starpu_mem_chunk_lru_list_front(starpu_lru_list[node]);
  884. _starpu_memchunk_recently_used_move(mc_lru->mc, node);
  885. _starpu_mem_chunk_lru_list_erase(starpu_lru_list[node], mc_lru);
  886. _starpu_mem_chunk_lru_delete(mc_lru);
  887. }
  888. _starpu_spin_unlock(&lru_rwlock[node]);
  889. }
  890. #ifdef STARPU_MEMORY_STATS
  891. void _starpu_memory_display_stats_by_node(int node)
  892. {
  893. _STARPU_PTHREAD_RWLOCK_WRLOCK(&mc_rwlock[node]);
  894. if (!_starpu_mem_chunk_list_empty(mc_list[node]))
  895. {
  896. struct _starpu_mem_chunk *mc;
  897. fprintf(stderr, "#-------\n");
  898. fprintf(stderr, "Data on Node #%d\n",node);
  899. for (mc = _starpu_mem_chunk_list_begin(mc_list[node]);
  900. mc != _starpu_mem_chunk_list_end(mc_list[node]);
  901. mc = _starpu_mem_chunk_list_next(mc))
  902. {
  903. _starpu_memory_display_handle_stats(mc->data);
  904. }
  905. }
  906. _STARPU_PTHREAD_RWLOCK_UNLOCK(&mc_rwlock[node]);
  907. }
  908. #endif
  909. void starpu_memory_display_stats(void)
  910. {
  911. #ifdef STARPU_MEMORY_STATS
  912. unsigned node;
  913. fprintf(stderr, "\n#---------------------\n");
  914. fprintf(stderr, "Memory stats :\n");
  915. for (node = 0; node < STARPU_MAXNODES; node++)
  916. {
  917. _starpu_memory_display_stats_by_node(node);
  918. }
  919. fprintf(stderr, "\n#---------------------\n");
  920. #endif
  921. }