memalloc.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126
  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_memory_node_get_local_key(), 0);
  67. }
  68. else
  69. {
  70. /* lock all sub-subtrees children */
  71. unsigned child;
  72. for (child = 0; child < handle->nchildren; child++)
  73. {
  74. starpu_data_handle_t child_handle = starpu_data_get_child(handle, child);
  75. lock_all_subtree(child_handle);
  76. }
  77. }
  78. }
  79. static void unlock_all_subtree(starpu_data_handle_t handle)
  80. {
  81. if (handle->nchildren == 0)
  82. {
  83. /* this is a leaf */
  84. _starpu_spin_unlock(&handle->header_lock);
  85. }
  86. else
  87. {
  88. /* lock all sub-subtrees children
  89. * Note that this is done in the reverse order of the
  90. * lock_all_subtree so that we avoid deadlock */
  91. unsigned i;
  92. for (i =0; i < handle->nchildren; i++)
  93. {
  94. unsigned child = handle->nchildren - 1 - i;
  95. starpu_data_handle_t child_handle = starpu_data_get_child(handle, child);
  96. unlock_all_subtree(child_handle);
  97. }
  98. }
  99. }
  100. static unsigned may_free_subtree(starpu_data_handle_t handle, unsigned node)
  101. {
  102. /* we only free if no one refers to the leaf */
  103. uint32_t refcnt = _starpu_get_data_refcnt(handle, node);
  104. if (refcnt)
  105. return 0;
  106. if (!handle->nchildren)
  107. return 1;
  108. /* look into all sub-subtrees children */
  109. unsigned child;
  110. for (child = 0; child < handle->nchildren; child++)
  111. {
  112. unsigned res;
  113. starpu_data_handle_t child_handle = starpu_data_get_child(handle, child);
  114. res = may_free_subtree(child_handle, node);
  115. if (!res) return 0;
  116. }
  117. /* no problem was found */
  118. return 1;
  119. }
  120. static void transfer_subtree_to_node(starpu_data_handle_t handle, unsigned src_node,
  121. unsigned dst_node)
  122. {
  123. unsigned i;
  124. unsigned last = 0;
  125. unsigned cnt;
  126. int ret;
  127. if (handle->nchildren == 0)
  128. {
  129. struct _starpu_data_replicate *src_replicate = &handle->per_node[src_node];
  130. struct _starpu_data_replicate *dst_replicate = &handle->per_node[dst_node];
  131. /* this is a leaf */
  132. switch(src_replicate->state)
  133. {
  134. case STARPU_OWNER:
  135. /* the local node has the only copy */
  136. /* the owner is now the destination_node */
  137. src_replicate->state = STARPU_INVALID;
  138. dst_replicate->state = STARPU_OWNER;
  139. #ifdef STARPU_DEVEL
  140. #warning we should use requests during memory reclaim
  141. #endif
  142. /* TODO use request !! */
  143. /* Take temporary references on the replicates */
  144. _starpu_spin_checklocked(&handle->header_lock);
  145. src_replicate->refcnt++;
  146. dst_replicate->refcnt++;
  147. handle->busy_count+=2;
  148. ret = _starpu_driver_copy_data_1_to_1(handle, src_replicate, dst_replicate, 0, NULL, 1);
  149. STARPU_ASSERT(ret == 0);
  150. src_replicate->refcnt--;
  151. dst_replicate->refcnt--;
  152. STARPU_ASSERT(handle->busy_count >= 2);
  153. handle->busy_count -= 2;
  154. _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, unsigned 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_memory_node_get_local_key());
  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_get_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_memory_node_get_local_key(), 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(unsigned 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(unsigned 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(unsigned 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(unsigned 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(unsigned 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_get_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_get_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(unsigned dst_node, size_t size)
  629. {
  630. uintptr_t addr = 0;
  631. #ifdef STARPU_USE_CUDA
  632. cudaError_t status;
  633. #endif
  634. if (_starpu_memory_manager_can_allocate_size(size, dst_node) == 0)
  635. return NULL;
  636. #ifdef STARPU_DEVEL
  637. #warning TODO: we need to use starpu_malloc which should itself inquire from the memory manager is there is enough available memory
  638. #endif
  639. switch(starpu_node_get_kind(dst_node))
  640. {
  641. case STARPU_CPU_RAM:
  642. {
  643. addr = (uintptr_t)malloc(size);
  644. break;
  645. }
  646. #if defined(STARPU_USE_CUDA) || defined(STARPU_SIMGRID)
  647. case STARPU_CUDA_RAM:
  648. #ifdef STARPU_SIMGRID
  649. #ifdef STARPU_DEVEL
  650. #warning TODO: record used memory, using a simgrid property to know the available memory
  651. #endif
  652. /* Sleep 10µs for the allocation */
  653. _STARPU_PTHREAD_MUTEX_LOCK(&cuda_alloc_mutex);
  654. MSG_process_sleep(0.000010);
  655. addr = 1;
  656. _STARPU_PTHREAD_MUTEX_UNLOCK(&cuda_alloc_mutex);
  657. #else
  658. status = cudaMalloc((void **)&addr, size);
  659. if (!addr || (status != cudaSuccess))
  660. {
  661. if (STARPU_UNLIKELY(status != cudaErrorMemoryAllocation))
  662. STARPU_CUDA_REPORT_ERROR(status);
  663. addr = 0;
  664. }
  665. #endif
  666. break;
  667. #endif
  668. #if defined(STARPU_USE_OPENCL) || defined(STARPU_SIMGRID)
  669. case STARPU_OPENCL_RAM:
  670. {
  671. #ifdef STARPU_SIMGRID
  672. /* Sleep 10µs for the allocation */
  673. _STARPU_PTHREAD_MUTEX_LOCK(&opencl_alloc_mutex);
  674. MSG_process_sleep(0.000010);
  675. addr = 1;
  676. _STARPU_PTHREAD_MUTEX_UNLOCK(&opencl_alloc_mutex);
  677. #else
  678. int ret;
  679. cl_mem ptr;
  680. ret = starpu_opencl_allocate_memory(&ptr, size, CL_MEM_READ_WRITE);
  681. if (ret)
  682. {
  683. addr = 0;
  684. }
  685. else
  686. {
  687. addr = (uintptr_t)ptr;
  688. }
  689. break;
  690. #endif
  691. }
  692. #endif
  693. default:
  694. STARPU_ABORT();
  695. }
  696. if (addr == 0)
  697. {
  698. // Allocation failed, gives the memory back to the memory manager
  699. _starpu_memory_manager_deallocate_size(size, dst_node);
  700. }
  701. return addr;
  702. }
  703. void
  704. starpu_free_buffer_on_node(unsigned dst_node, uintptr_t addr, size_t size)
  705. {
  706. enum starpu_node_kind kind = starpu_node_get_kind(dst_node);
  707. switch(kind)
  708. {
  709. #ifdef STARPU_DEVEL
  710. #warning TODO we need to call starpu_free
  711. #endif
  712. case STARPU_CPU_RAM:
  713. free((void*)addr);
  714. _starpu_memory_manager_deallocate_size(size, dst_node);
  715. break;
  716. #if defined(STARPU_USE_CUDA) || defined(STARPU_SIMGRID)
  717. case STARPU_CUDA_RAM:
  718. {
  719. #ifdef STARPU_SIMGRID
  720. _STARPU_PTHREAD_MUTEX_LOCK(&cuda_alloc_mutex);
  721. /* Sleep 10µs for the free */
  722. MSG_process_sleep(0.000010);
  723. _STARPU_PTHREAD_MUTEX_UNLOCK(&cuda_alloc_mutex);
  724. #else
  725. cudaError_t err;
  726. err = cudaFree((void*)addr);
  727. if (STARPU_UNLIKELY(err != cudaSuccess))
  728. STARPU_CUDA_REPORT_ERROR(err);
  729. _starpu_memory_manager_deallocate_size(size, dst_node);
  730. #endif
  731. break;
  732. }
  733. #endif
  734. #if defined(STARPU_USE_OPENCL) || defined(STARPU_SIMGRID)
  735. case STARPU_OPENCL_RAM:
  736. {
  737. #ifdef STARPU_SIMGRID
  738. _STARPU_PTHREAD_MUTEX_LOCK(&opencl_alloc_mutex);
  739. /* Sleep 10µs for the free */
  740. MSG_process_sleep(0.000010);
  741. _STARPU_PTHREAD_MUTEX_UNLOCK(&opencl_alloc_mutex);
  742. #else
  743. cl_int err;
  744. err = clReleaseMemObject((void*)addr);
  745. if (STARPU_UNLIKELY(err != CL_SUCCESS))
  746. STARPU_OPENCL_REPORT_ERROR(err);
  747. _starpu_memory_manager_deallocate_size(size, dst_node);
  748. #endif
  749. break;
  750. }
  751. #endif
  752. default:
  753. STARPU_ABORT();
  754. }
  755. }
  756. /*
  757. * In order to allocate a piece of data, we try to reuse existing buffers if
  758. * its possible.
  759. * 1 - we try to reuse a memchunk that is explicitely unused.
  760. * 2 - we go through the list of memory chunks and find one that is not
  761. * referenced and that has the same footprint to reuse it.
  762. * 3 - we call the usual driver's alloc method
  763. * 4 - we go through the list of memory chunks and release those that are
  764. * not referenced (or part of those).
  765. *
  766. */
  767. static ssize_t _starpu_allocate_interface(starpu_data_handle_t handle, struct _starpu_data_replicate *replicate, unsigned dst_node, unsigned is_prefetch)
  768. {
  769. unsigned attempts = 0;
  770. ssize_t allocated_memory;
  771. _starpu_spin_checklocked(&handle->header_lock);
  772. _starpu_data_allocation_inc_stats(dst_node);
  773. #ifdef STARPU_USE_ALLOCATION_CACHE
  774. /* perhaps we can directly reuse a buffer in the free-list */
  775. uint32_t footprint = _starpu_compute_data_footprint(handle);
  776. _STARPU_TRACE_START_ALLOC_REUSE(dst_node);
  777. _STARPU_PTHREAD_RWLOCK_WRLOCK(&mc_rwlock[dst_node]);
  778. if (try_to_find_reusable_mem_chunk(dst_node, handle, replicate, footprint))
  779. {
  780. _STARPU_PTHREAD_RWLOCK_UNLOCK(&mc_rwlock[dst_node]);
  781. _starpu_allocation_cache_hit(dst_node);
  782. ssize_t data_size = _starpu_data_get_size(handle);
  783. return data_size;
  784. }
  785. _STARPU_PTHREAD_RWLOCK_UNLOCK(&mc_rwlock[dst_node]);
  786. _STARPU_TRACE_END_ALLOC_REUSE(dst_node);
  787. #endif
  788. do
  789. {
  790. STARPU_ASSERT(handle->ops);
  791. STARPU_ASSERT(handle->ops->allocate_data_on_node);
  792. _STARPU_TRACE_START_ALLOC(dst_node);
  793. STARPU_ASSERT(replicate->data_interface);
  794. #if defined(STARPU_USE_CUDA) && defined(HAVE_CUDA_MEMCPY_PEER) && !defined(STARPU_SIMGRID)
  795. if (starpu_node_get_kind(dst_node) == STARPU_CUDA_RAM)
  796. {
  797. /* To facilitate the design of interface, we set the
  798. * proper CUDA device in case it is needed. This avoids
  799. * having to set it again in the malloc method of each
  800. * interface. */
  801. starpu_cuda_set_device(_starpu_memory_node_get_devid(dst_node));
  802. }
  803. #endif
  804. allocated_memory = handle->ops->allocate_data_on_node(replicate->data_interface, dst_node);
  805. _STARPU_TRACE_END_ALLOC(dst_node);
  806. if (allocated_memory == -ENOMEM)
  807. {
  808. size_t reclaim = 0.25*_starpu_get_global_mem_size(dst_node);
  809. size_t handle_size = handle->ops->get_size(handle);
  810. if (starpu_memstrategy_data_size_coefficient*handle_size > reclaim)
  811. reclaim = starpu_memstrategy_data_size_coefficient*handle_size;
  812. /* Take temporary reference on the replicate */
  813. replicate->refcnt++;
  814. handle->busy_count++;
  815. _starpu_spin_unlock(&handle->header_lock);
  816. _STARPU_TRACE_START_MEMRECLAIM(dst_node);
  817. if (is_prefetch)
  818. {
  819. _STARPU_PTHREAD_RWLOCK_WRLOCK(&mc_rwlock[dst_node]);
  820. flush_memchunk_cache(dst_node, reclaim);
  821. _STARPU_PTHREAD_RWLOCK_UNLOCK(&mc_rwlock[dst_node]);
  822. }
  823. else
  824. reclaim_memory_generic(dst_node, 0, reclaim);
  825. _STARPU_TRACE_END_MEMRECLAIM(dst_node);
  826. while (_starpu_spin_trylock(&handle->header_lock))
  827. _starpu_datawizard_progress(_starpu_memory_node_get_local_key(), 0);
  828. replicate->refcnt--;
  829. STARPU_ASSERT(replicate->refcnt >= 0);
  830. STARPU_ASSERT(handle->busy_count > 0);
  831. handle->busy_count--;
  832. _starpu_data_check_not_busy(handle);
  833. }
  834. }
  835. while((allocated_memory == -ENOMEM) && attempts++ < 2);
  836. return allocated_memory;
  837. }
  838. int _starpu_allocate_memory_on_node(starpu_data_handle_t handle, struct _starpu_data_replicate *replicate, unsigned is_prefetch)
  839. {
  840. ssize_t allocated_memory;
  841. unsigned dst_node = replicate->memory_node;
  842. STARPU_ASSERT(handle);
  843. /* A buffer is already allocated on the node */
  844. if (replicate->allocated)
  845. return 0;
  846. STARPU_ASSERT(replicate->data_interface);
  847. allocated_memory = _starpu_allocate_interface(handle, replicate, dst_node, is_prefetch);
  848. /* perhaps we could really not handle that capacity misses */
  849. if (allocated_memory == -ENOMEM)
  850. return -ENOMEM;
  851. register_mem_chunk(replicate, 1);
  852. replicate->allocated = 1;
  853. replicate->automatically_allocated = 1;
  854. if (dst_node == 0)
  855. {
  856. void *ptr = starpu_handle_to_pointer(handle, 0);
  857. if (ptr != NULL)
  858. {
  859. _starpu_data_register_ram_pointer(handle, ptr);
  860. }
  861. }
  862. return 0;
  863. }
  864. unsigned starpu_data_test_if_allocated_on_node(starpu_data_handle_t handle, unsigned memory_node)
  865. {
  866. return handle->per_node[memory_node].allocated;
  867. }
  868. void _starpu_memchunk_recently_used(struct _starpu_mem_chunk *mc, unsigned node)
  869. {
  870. _starpu_spin_lock(&lru_rwlock[node]);
  871. struct _starpu_mem_chunk_lru *mc_lru=_starpu_mem_chunk_lru_new();
  872. mc_lru->mc=mc;
  873. _starpu_mem_chunk_lru_list_push_front(starpu_lru_list[node],mc_lru);
  874. _starpu_spin_unlock(&lru_rwlock[node]);
  875. }
  876. /* The mc_rwlock[node] rw-lock should be taken prior to calling this function.*/
  877. static void _starpu_memchunk_recently_used_move(struct _starpu_mem_chunk *mc, unsigned node)
  878. {
  879. /* XXX Sometimes the memchunk is not in the list... */
  880. struct _starpu_mem_chunk *mc_iter;
  881. for (mc_iter = _starpu_mem_chunk_list_begin(mc_list[node]);
  882. mc_iter != _starpu_mem_chunk_list_end(mc_list[node]);
  883. mc_iter = _starpu_mem_chunk_list_next(mc_iter) )
  884. {
  885. if (mc_iter==mc)
  886. {
  887. _starpu_mem_chunk_list_erase(mc_list[node], mc);
  888. _starpu_mem_chunk_list_push_back(mc_list[node], mc);
  889. return;
  890. }
  891. }
  892. }
  893. static void starpu_lru(unsigned node)
  894. {
  895. _starpu_spin_lock(&lru_rwlock[node]);
  896. while (!_starpu_mem_chunk_lru_list_empty(starpu_lru_list[node]))
  897. {
  898. struct _starpu_mem_chunk_lru *mc_lru=_starpu_mem_chunk_lru_list_front(starpu_lru_list[node]);
  899. _starpu_memchunk_recently_used_move(mc_lru->mc, node);
  900. _starpu_mem_chunk_lru_list_erase(starpu_lru_list[node], mc_lru);
  901. _starpu_mem_chunk_lru_delete(mc_lru);
  902. }
  903. _starpu_spin_unlock(&lru_rwlock[node]);
  904. }
  905. #ifdef STARPU_MEMORY_STATS
  906. void _starpu_memory_display_stats_by_node(int node)
  907. {
  908. _STARPU_PTHREAD_RWLOCK_WRLOCK(&mc_rwlock[node]);
  909. if (!_starpu_mem_chunk_list_empty(mc_list[node]))
  910. {
  911. struct _starpu_mem_chunk *mc;
  912. fprintf(stderr, "#-------\n");
  913. fprintf(stderr, "Data on Node #%d\n",node);
  914. for (mc = _starpu_mem_chunk_list_begin(mc_list[node]);
  915. mc != _starpu_mem_chunk_list_end(mc_list[node]);
  916. mc = _starpu_mem_chunk_list_next(mc))
  917. {
  918. if (mc->automatically_allocated == 0)
  919. _starpu_memory_display_handle_stats(mc->data);
  920. }
  921. }
  922. _STARPU_PTHREAD_RWLOCK_UNLOCK(&mc_rwlock[node]);
  923. }
  924. #endif
  925. void starpu_memory_display_stats(void)
  926. {
  927. #ifdef STARPU_MEMORY_STATS
  928. unsigned node;
  929. fprintf(stderr, "\n#---------------------\n");
  930. fprintf(stderr, "Memory stats :\n");
  931. for (node = 0; node < STARPU_MAXNODES; node++)
  932. {
  933. _starpu_memory_display_stats_by_node(node);
  934. }
  935. fprintf(stderr, "\n#---------------------\n");
  936. #endif
  937. }