memalloc.c 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-2016 Université de Bordeaux
  4. * Copyright (C) 2010, 2011, 2012, 2013, 2014, 2015, 2016 CNRS
  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/memory_nodes.h>
  19. #include <datawizard/memalloc.h>
  20. #include <datawizard/footprint.h>
  21. #include <core/disk.h>
  22. #include <starpu.h>
  23. #include <common/uthash.h>
  24. /* Minimum percentage of available memory in each node */
  25. static unsigned minimum_p;
  26. static unsigned target_p;
  27. /* Minimum percentage of number of clean buffer in each node */
  28. static unsigned minimum_clean_p;
  29. static unsigned target_clean_p;
  30. /* Whether CPU memory has been explicitly limited by user */
  31. static int limit_cpu_mem;
  32. /* This per-node RW-locks protect mc_list and memchunk_cache entries */
  33. /* Note: handle header lock is always taken before this (normal add/remove case) */
  34. static struct _starpu_spinlock mc_lock[STARPU_MAXNODES];
  35. /* Potentially in use memory chunks. The beginning of the list is clean (home
  36. * node has a copy of the data, or the data is being transferred there), the
  37. * remainder of the list may not be clean. */
  38. static struct _starpu_mem_chunk_list mc_list[STARPU_MAXNODES];
  39. /* This is a shortcut inside the mc_list to the first potentially dirty MC. All
  40. * MC before this are clean, MC before this only *may* be clean. */
  41. static struct _starpu_mem_chunk *mc_dirty_head[STARPU_MAXNODES];
  42. /* Number of elements in mc_list, number of elements in the clean part of
  43. * mc_list plus the non-automatically allocated elements (which are thus always
  44. * considered as clean) */
  45. static unsigned mc_nb[STARPU_MAXNODES], mc_clean_nb[STARPU_MAXNODES];
  46. /* TODO: no home doesn't mean always clean, should push to larger memory nodes */
  47. #define MC_LIST_PUSH_BACK(node, mc) do { \
  48. _starpu_mem_chunk_list_push_back(&mc_list[node], mc); \
  49. if ((mc)->clean || (mc)->home) \
  50. /* This is clean */ \
  51. mc_clean_nb[node]++; \
  52. else if (!mc_dirty_head[node]) \
  53. /* This is the only dirty element for now */ \
  54. mc_dirty_head[node] = mc; \
  55. mc_nb[node]++; \
  56. } while(0)
  57. /* Put new clean mc at the end of the clean part of mc_list, i.e. just before mc_dirty_head (if any) */
  58. #define MC_LIST_PUSH_CLEAN(node, mc) do { \
  59. if (mc_dirty_head[node]) \
  60. _starpu_mem_chunk_list_insert_before(&mc_list[node], mc, mc_dirty_head[node]); \
  61. else \
  62. _starpu_mem_chunk_list_push_back(&mc_list[node], mc); \
  63. /* This is clean */ \
  64. mc_clean_nb[node]++; \
  65. mc_nb[node]++; \
  66. } while (0)
  67. #define MC_LIST_ERASE(node, mc) do { \
  68. if ((mc)->clean || (mc)->home) \
  69. mc_clean_nb[node]--; /* One clean element less */ \
  70. if ((mc) == mc_dirty_head[node]) \
  71. /* This was the dirty head */ \
  72. mc_dirty_head[node] = _starpu_mem_chunk_list_next((mc)); \
  73. /* One element less */ \
  74. mc_nb[node]--; \
  75. /* Remove element */ \
  76. _starpu_mem_chunk_list_erase(&mc_list[node], (mc)); \
  77. /* Notify whoever asked for it */ \
  78. if ((mc)->remove_notify) \
  79. { \
  80. *((mc)->remove_notify) = NULL; \
  81. (mc)->remove_notify = NULL; \
  82. } \
  83. } while (0)
  84. /* Explicitly caches memory chunks that can be reused */
  85. struct mc_cache_entry
  86. {
  87. UT_hash_handle hh;
  88. struct _starpu_mem_chunk_list list;
  89. uint32_t footprint;
  90. };
  91. static struct mc_cache_entry *mc_cache[STARPU_MAXNODES];
  92. static int mc_cache_nb[STARPU_MAXNODES];
  93. static starpu_ssize_t mc_cache_size[STARPU_MAXNODES];
  94. /* Whether some thread is currently tidying this node */
  95. static unsigned tidying[STARPU_MAXNODES];
  96. /* Whether some thread is currently reclaiming memory for this node */
  97. static unsigned reclaiming[STARPU_MAXNODES];
  98. int _starpu_is_reclaiming(unsigned node)
  99. {
  100. return tidying[node] || reclaiming[node];
  101. }
  102. /* When reclaiming memory to allocate, we reclaim MAX(what_is_to_reclaim_on_device, data_size_coefficient*data_size) */
  103. const unsigned starpu_memstrategy_data_size_coefficient=2;
  104. static int get_better_disk_can_accept_size(starpu_data_handle_t handle, unsigned node);
  105. static unsigned choose_target(starpu_data_handle_t handle, unsigned node);
  106. void _starpu_init_mem_chunk_lists(void)
  107. {
  108. unsigned i;
  109. for (i = 0; i < STARPU_MAXNODES; i++)
  110. {
  111. _starpu_spin_init(&mc_lock[i]);
  112. _starpu_mem_chunk_list_init(&mc_list[i]);
  113. STARPU_HG_DISABLE_CHECKING(mc_cache_size[i]);
  114. STARPU_HG_DISABLE_CHECKING(mc_nb[i]);
  115. STARPU_HG_DISABLE_CHECKING(mc_clean_nb[i]);
  116. }
  117. minimum_p = starpu_get_env_number_default("STARPU_MINIMUM_AVAILABLE_MEM", 5);
  118. target_p = starpu_get_env_number_default("STARPU_TARGET_AVAILABLE_MEM", 10);
  119. minimum_clean_p = starpu_get_env_number_default("STARPU_MINIMUM_CLEAN_BUFFERS", 5);
  120. target_clean_p = starpu_get_env_number_default("STARPU_TARGET_CLEAN_BUFFERS", 10);
  121. limit_cpu_mem = starpu_get_env_number("STARPU_LIMIT_CPU_MEM");
  122. }
  123. void _starpu_deinit_mem_chunk_lists(void)
  124. {
  125. unsigned i;
  126. for (i = 0; i < STARPU_MAXNODES; i++)
  127. {
  128. struct mc_cache_entry *entry, *tmp;
  129. STARPU_ASSERT(mc_nb[i] == 0);
  130. STARPU_ASSERT(mc_clean_nb[i] == 0);
  131. STARPU_ASSERT(mc_dirty_head[i] == NULL);
  132. HASH_ITER(hh, mc_cache[i], entry, tmp)
  133. {
  134. STARPU_ASSERT (_starpu_mem_chunk_list_empty(&entry->list));
  135. HASH_DEL(mc_cache[i], entry);
  136. free(entry);
  137. }
  138. STARPU_ASSERT(mc_cache_nb[i] == 0);
  139. STARPU_ASSERT(mc_cache_size[i] == 0);
  140. _starpu_spin_destroy(&mc_lock[i]);
  141. }
  142. }
  143. /*
  144. * Manipulate subtrees
  145. */
  146. static void unlock_all_subtree(starpu_data_handle_t handle)
  147. {
  148. /* lock all sub-subtrees children
  149. * Note that this is done in the reverse order of the
  150. * lock_all_subtree so that we avoid deadlock */
  151. unsigned i;
  152. for (i =0; i < handle->nchildren; i++)
  153. {
  154. unsigned child = handle->nchildren - 1 - i;
  155. starpu_data_handle_t child_handle = starpu_data_get_child(handle, child);
  156. unlock_all_subtree(child_handle);
  157. }
  158. _starpu_spin_unlock(&handle->header_lock);
  159. }
  160. static int lock_all_subtree(starpu_data_handle_t handle)
  161. {
  162. int child;
  163. /* lock parent */
  164. if (_starpu_spin_trylock(&handle->header_lock))
  165. /* the handle is busy, abort */
  166. return 0;
  167. /* lock all sub-subtrees children */
  168. for (child = 0; child < (int) handle->nchildren; child++)
  169. {
  170. if (!lock_all_subtree(starpu_data_get_child(handle, child)))
  171. {
  172. /* Some child is busy, abort */
  173. while (--child >= 0)
  174. /* Unlock what we have already uselessly locked */
  175. unlock_all_subtree(starpu_data_get_child(handle, child));
  176. return 0;
  177. }
  178. }
  179. return 1;
  180. }
  181. static unsigned may_free_subtree(starpu_data_handle_t handle, unsigned node)
  182. {
  183. /* we only free if no one refers to the leaf */
  184. uint32_t refcnt = _starpu_get_data_refcnt(handle, node);
  185. if (refcnt)
  186. return 0;
  187. if (handle->current_mode == STARPU_W)
  188. {
  189. unsigned n;
  190. for (n = 0; n < STARPU_MAXNODES; n++)
  191. if (_starpu_get_data_refcnt(handle, n))
  192. /* Some task is writing to the handle somewhere */
  193. return 0;
  194. }
  195. /* look into all sub-subtrees children */
  196. unsigned child;
  197. for (child = 0; child < handle->nchildren; child++)
  198. {
  199. unsigned res;
  200. starpu_data_handle_t child_handle = starpu_data_get_child(handle, child);
  201. res = may_free_subtree(child_handle, node);
  202. if (!res) return 0;
  203. }
  204. /* no problem was found */
  205. return 1;
  206. }
  207. /* Warn: this releases the header lock of the handle during the transfer
  208. * The handle may thus unexpectedly disappear. This returns 1 in that case.
  209. */
  210. static int STARPU_ATTRIBUTE_WARN_UNUSED_RESULT transfer_subtree_to_node(starpu_data_handle_t handle, unsigned src_node,
  211. unsigned dst_node)
  212. {
  213. unsigned i;
  214. unsigned last = 0;
  215. unsigned cnt;
  216. STARPU_ASSERT(dst_node != src_node);
  217. if (handle->nchildren == 0)
  218. {
  219. struct _starpu_data_replicate *src_replicate = &handle->per_node[src_node];
  220. struct _starpu_data_replicate *dst_replicate = &handle->per_node[dst_node];
  221. /* this is a leaf */
  222. while (src_replicate->state == STARPU_OWNER)
  223. {
  224. /* This is the only copy, push it to destination */
  225. struct _starpu_data_request *r;
  226. r = _starpu_create_request_to_fetch_data(handle, dst_replicate, STARPU_R, 0, 0, NULL, NULL, 0, "transfer_subtree_to_node");
  227. /* There is no way we don't need a request, since
  228. * source is OWNER, destination can't be having it */
  229. STARPU_ASSERT(r);
  230. /* Keep the handle alive while we are working on it */
  231. handle->busy_count++;
  232. _starpu_spin_unlock(&handle->header_lock);
  233. _starpu_wait_data_request_completion(r, 1);
  234. _starpu_spin_lock(&handle->header_lock);
  235. handle->busy_count--;
  236. if (_starpu_data_check_not_busy(handle))
  237. /* Actually disappeared, abort completely */
  238. return -1;
  239. if (!may_free_subtree(handle, src_node))
  240. /* Oops, while we released the header lock, a
  241. * task got in, abort. */
  242. return 0;
  243. }
  244. STARPU_ASSERT(may_free_subtree(handle, src_node));
  245. if (src_replicate->state == STARPU_SHARED)
  246. {
  247. /* some other node may have the copy */
  248. _STARPU_TRACE_DATA_INVALIDATE(handle, src_node);
  249. src_replicate->state = STARPU_INVALID;
  250. /* count the number of copies */
  251. cnt = 0;
  252. for (i = 0; i < STARPU_MAXNODES; i++)
  253. {
  254. if (handle->per_node[i].state == STARPU_SHARED)
  255. {
  256. cnt++;
  257. last = i;
  258. }
  259. }
  260. STARPU_ASSERT(cnt > 0);
  261. if (cnt == 1)
  262. handle->per_node[last].state = STARPU_OWNER;
  263. }
  264. else
  265. STARPU_ASSERT(src_replicate->state == STARPU_INVALID);
  266. /* Already dropped by somebody, in which case there is nothing to be done */
  267. }
  268. else
  269. {
  270. /* transfer all sub-subtrees children */
  271. unsigned child;
  272. int res;
  273. for (child = 0; child < handle->nchildren; child++)
  274. {
  275. starpu_data_handle_t child_handle = starpu_data_get_child(handle, child);
  276. res = transfer_subtree_to_node(child_handle, src_node, dst_node);
  277. if (res == 0)
  278. return 0;
  279. /* There is no way children have disappeared since we
  280. * keep the parent lock held */
  281. STARPU_ASSERT(res != -1);
  282. }
  283. }
  284. /* Success! */
  285. return 1;
  286. }
  287. static void notify_handle_children(starpu_data_handle_t handle, struct _starpu_data_replicate *replicate, unsigned node)
  288. {
  289. unsigned child;
  290. replicate->allocated = 0;
  291. /* XXX why do we need that ? */
  292. replicate->automatically_allocated = 0;
  293. for (child = 0; child < handle->nchildren; child++)
  294. {
  295. /* Notify children that their buffer has been deallocated too */
  296. starpu_data_handle_t child_handle = starpu_data_get_child(handle, child);
  297. notify_handle_children(child_handle, &child_handle->per_node[node], node);
  298. }
  299. }
  300. static size_t free_memory_on_node(struct _starpu_mem_chunk *mc, unsigned node)
  301. {
  302. size_t freed = 0;
  303. STARPU_ASSERT(mc->ops);
  304. STARPU_ASSERT(mc->ops->free_data_on_node);
  305. starpu_data_handle_t handle = mc->data;
  306. struct _starpu_data_replicate *replicate = mc->replicate;
  307. if (handle)
  308. _starpu_spin_checklocked(&handle->header_lock);
  309. if (mc->automatically_allocated &&
  310. (!handle || replicate->refcnt == 0))
  311. {
  312. void *data_interface;
  313. if (handle)
  314. STARPU_ASSERT(replicate->allocated);
  315. #if defined(STARPU_USE_CUDA) && defined(HAVE_CUDA_MEMCPY_PEER) && !defined(STARPU_SIMGRID)
  316. if (starpu_node_get_kind(node) == STARPU_CUDA_RAM)
  317. {
  318. /* To facilitate the design of interface, we set the
  319. * proper CUDA device in case it is needed. This avoids
  320. * having to set it again in the free method of each
  321. * interface. */
  322. starpu_cuda_set_device(_starpu_memory_node_get_devid(node));
  323. }
  324. #endif
  325. if (handle)
  326. data_interface = replicate->data_interface;
  327. else
  328. data_interface = mc->chunk_interface;
  329. STARPU_ASSERT(data_interface);
  330. if (handle && node == STARPU_MAIN_RAM)
  331. _starpu_data_unregister_ram_pointer(handle);
  332. _STARPU_TRACE_START_FREE(node, mc->size);
  333. mc->ops->free_data_on_node(data_interface, node);
  334. _STARPU_TRACE_END_FREE(node);
  335. if (handle)
  336. notify_handle_children(handle, replicate, node);
  337. freed = mc->size;
  338. if (handle)
  339. STARPU_ASSERT(replicate->refcnt == 0);
  340. }
  341. return freed;
  342. }
  343. /* mc_lock is held */
  344. static size_t do_free_mem_chunk(struct _starpu_mem_chunk *mc, unsigned node)
  345. {
  346. size_t size;
  347. starpu_data_handle_t handle = mc->data;
  348. if (handle)
  349. {
  350. _starpu_spin_checklocked(&handle->header_lock);
  351. mc->size = _starpu_data_get_size(handle);
  352. }
  353. if (mc->replicate)
  354. mc->replicate->mc=NULL;
  355. /* free the actual buffer */
  356. size = free_memory_on_node(mc, node);
  357. /* remove the mem_chunk from the list */
  358. MC_LIST_ERASE(node, mc);
  359. _starpu_mem_chunk_delete(mc);
  360. return size;
  361. }
  362. /* This function is called for memory chunks that are possibly in used (ie. not
  363. * in the cache). They should therefore still be associated to a handle. */
  364. /* mc_lock is held and may be temporarily released! */
  365. static size_t try_to_free_mem_chunk(struct _starpu_mem_chunk *mc, unsigned node)
  366. {
  367. size_t freed = 0;
  368. starpu_data_handle_t handle;
  369. handle = mc->data;
  370. STARPU_ASSERT(handle);
  371. /* This data should be written through to this node, avoid dropping it! */
  372. if (handle->wt_mask & (1<<node))
  373. return 0;
  374. /* This data was registered from this node, we will not be able to drop it anyway */
  375. if ((int) node == handle->home_node)
  376. return 0;
  377. /* REDUX memchunk */
  378. if (mc->relaxed_coherency == 2)
  379. {
  380. /* TODO: reduce it back to e.g. main memory */
  381. }
  382. else
  383. /* Either it's a "relaxed coherency" memchunk (SCRATCH), or it's a
  384. * memchunk that could be used with filters. */
  385. if (mc->relaxed_coherency == 1)
  386. {
  387. STARPU_ASSERT(mc->replicate);
  388. if (_starpu_spin_trylock(&handle->header_lock))
  389. /* Handle is busy, abort */
  390. return 0;
  391. if (mc->replicate->refcnt == 0)
  392. {
  393. /* Note that there is no need to transfer any data or
  394. * to update the status in terms of MSI protocol
  395. * because this memchunk is associated to a replicate
  396. * in "relaxed coherency" mode. */
  397. freed = do_free_mem_chunk(mc, node);
  398. }
  399. _starpu_spin_unlock(&handle->header_lock);
  400. }
  401. /* try to lock all the subtree */
  402. else if (lock_all_subtree(handle))
  403. {
  404. /* check if they are all "free" */
  405. if (may_free_subtree(handle, node))
  406. {
  407. int target = -1;
  408. /* XXX Considering only owner to invalidate */
  409. STARPU_ASSERT(handle->per_node[node].refcnt == 0);
  410. /* in case there was nobody using that buffer, throw it
  411. * away after writing it back to main memory */
  412. /* choose the best target */
  413. target = choose_target(handle, node);
  414. if (target != -1)
  415. {
  416. int res;
  417. /* Should have been avoided in our caller */
  418. STARPU_ASSERT(!mc->remove_notify);
  419. mc->remove_notify = &mc;
  420. _starpu_spin_unlock(&mc_lock[node]);
  421. #ifdef STARPU_MEMORY_STATS
  422. if (handle->per_node[node].state == STARPU_OWNER)
  423. _starpu_memory_handle_stats_invalidated(handle, node);
  424. #endif
  425. _STARPU_TRACE_START_WRITEBACK(node);
  426. /* Note: this may need to allocate data etc.
  427. * and thus release the header lock, take
  428. * mc_lock, etc. */
  429. res = transfer_subtree_to_node(handle, node, target);
  430. _STARPU_TRACE_END_WRITEBACK(node);
  431. #ifdef STARPU_MEMORY_STATS
  432. _starpu_memory_handle_stats_loaded_owner(handle, target);
  433. #endif
  434. _starpu_spin_lock(&mc_lock[node]);
  435. if (!mc)
  436. {
  437. if (res == -1)
  438. {
  439. /* handle disappeared, abort without unlocking it */
  440. return 0;
  441. }
  442. }
  443. else
  444. {
  445. STARPU_ASSERT(mc->remove_notify == &mc);
  446. mc->remove_notify = NULL;
  447. if (res == -1)
  448. {
  449. /* handle disappeared, abort without unlocking it */
  450. return 0;
  451. }
  452. if (res == 1)
  453. {
  454. /* mc is still associated with the old
  455. * handle, now free it.
  456. */
  457. if (handle->per_node[node].refcnt == 0)
  458. /* And still nobody on it, now the actual buffer may be freed */
  459. freed = do_free_mem_chunk(mc, node);
  460. }
  461. }
  462. }
  463. }
  464. /* unlock the tree */
  465. unlock_all_subtree(handle);
  466. }
  467. return freed;
  468. }
  469. #ifdef STARPU_USE_ALLOCATION_CACHE
  470. /* We assume that mc_lock[node] is taken. is_already_in_mc_list indicates
  471. * that the mc is already in the list of buffers that are possibly used, and
  472. * therefore not in the cache. */
  473. static void reuse_mem_chunk(unsigned node, struct _starpu_data_replicate *new_replicate, struct _starpu_mem_chunk *mc, unsigned is_already_in_mc_list)
  474. {
  475. void *data_interface;
  476. /* we found an appropriate mem chunk: so we get it out
  477. * of the "to free" list, and reassign it to the new
  478. * piece of data */
  479. struct _starpu_data_replicate *old_replicate = mc->replicate;
  480. if (old_replicate)
  481. {
  482. old_replicate->allocated = 0;
  483. old_replicate->automatically_allocated = 0;
  484. old_replicate->initialized = 0;
  485. data_interface = old_replicate->data_interface;
  486. }
  487. else
  488. data_interface = mc->chunk_interface;
  489. STARPU_ASSERT(new_replicate->data_interface);
  490. STARPU_ASSERT(data_interface);
  491. memcpy(new_replicate->data_interface, data_interface, mc->size_interface);
  492. if (!old_replicate)
  493. {
  494. /* Free the copy that we made */
  495. free(mc->chunk_interface);
  496. mc->chunk_interface = NULL;
  497. }
  498. /* XXX: We do not actually reuse the mc at the moment, only the interface */
  499. /* mc->data = new_replicate->handle; */
  500. /* mc->footprint, mc->ops, mc->size_interface, mc->automatically_allocated should be
  501. * unchanged ! */
  502. /* remove the mem chunk from the list of active memory chunks, register_mem_chunk will put it back later */
  503. if (is_already_in_mc_list)
  504. MC_LIST_ERASE(node, mc);
  505. free(mc);
  506. }
  507. /* mc_lock is held and may be temporarily released! */
  508. 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)
  509. {
  510. unsigned success = 0;
  511. starpu_data_handle_t old_data;
  512. old_data = mc->data;
  513. STARPU_ASSERT(old_data);
  514. /* try to lock all the subtree */
  515. /* and check if they are all "free" */
  516. if (lock_all_subtree(old_data))
  517. {
  518. if (may_free_subtree(old_data, node))
  519. {
  520. int res;
  521. /* Should have been avoided in our caller */
  522. STARPU_ASSERT(!mc->remove_notify);
  523. mc->remove_notify = &mc;
  524. /* in case there was nobody using that buffer, throw it
  525. * away after writing it back to main memory */
  526. _starpu_spin_unlock(&mc_lock[node]);
  527. _STARPU_TRACE_START_WRITEBACK(node);
  528. res = transfer_subtree_to_node(old_data, node, STARPU_MAIN_RAM);
  529. _STARPU_TRACE_END_WRITEBACK(node);
  530. _starpu_spin_lock(&mc_lock[node]);
  531. if (!mc)
  532. {
  533. if (res == -1)
  534. {
  535. /* handle disappeared, abort without unlocking it */
  536. return 0;
  537. }
  538. }
  539. else
  540. {
  541. STARPU_ASSERT(mc->remove_notify == &mc);
  542. mc->remove_notify = NULL;
  543. if (res == -1)
  544. {
  545. /* handle disappeared, abort without unlocking it */
  546. return 0;
  547. }
  548. if (res == 1)
  549. {
  550. /* mc is still associated with the old
  551. * handle, now replace the previous data
  552. */
  553. reuse_mem_chunk(node, replicate, mc, is_already_in_mc_list);
  554. success = 1;
  555. }
  556. }
  557. }
  558. /* unlock the tree */
  559. unlock_all_subtree(old_data);
  560. }
  561. return success;
  562. }
  563. static int _starpu_data_interface_compare(void *data_interface_a, struct starpu_data_interface_ops *ops_a,
  564. void *data_interface_b, struct starpu_data_interface_ops *ops_b)
  565. {
  566. if (ops_a->interfaceid != ops_b->interfaceid)
  567. return -1;
  568. int ret = ops_a->compare(data_interface_a, data_interface_b);
  569. return ret;
  570. }
  571. /* This function must be called with mc_lock[node] taken */
  572. static struct _starpu_mem_chunk *_starpu_memchunk_cache_lookup_locked(unsigned node, starpu_data_handle_t handle, uint32_t footprint)
  573. {
  574. /* go through all buffers in the cache */
  575. struct mc_cache_entry *entry;
  576. HASH_FIND(hh, mc_cache[node], &footprint, sizeof(footprint), entry);
  577. if (!entry)
  578. /* No data with that footprint */
  579. return NULL;
  580. struct _starpu_mem_chunk *mc;
  581. for (mc = _starpu_mem_chunk_list_begin(&entry->list);
  582. mc != _starpu_mem_chunk_list_end(&entry->list);
  583. mc = _starpu_mem_chunk_list_next(mc))
  584. {
  585. /* Is that a false hit ? (this is _very_ unlikely) */
  586. if (_starpu_data_interface_compare(handle->per_node[node].data_interface, handle->ops, mc->chunk_interface, mc->ops) != 1)
  587. continue;
  588. /* Cache hit */
  589. /* Remove from the cache */
  590. _starpu_mem_chunk_list_erase(&entry->list, mc);
  591. mc_cache_nb[node]--;
  592. STARPU_ASSERT(mc_cache_nb[node] >= 0);
  593. mc_cache_size[node] -= mc->size;
  594. STARPU_ASSERT(mc_cache_size[node] >= 0);
  595. return mc;
  596. }
  597. /* This is a cache miss */
  598. return NULL;
  599. }
  600. /* this function looks for a memory chunk that matches a given footprint in the
  601. * list of mem chunk that need to be freed. */
  602. static unsigned try_to_find_reusable_mem_chunk(unsigned node, starpu_data_handle_t data, struct _starpu_data_replicate *replicate, uint32_t footprint)
  603. {
  604. struct _starpu_mem_chunk *mc, *orig_next_mc, *next_mc;
  605. int success = 0;
  606. _starpu_spin_lock(&mc_lock[node]);
  607. /* go through all buffers in the cache */
  608. mc = _starpu_memchunk_cache_lookup_locked(node, data, footprint);
  609. if (mc)
  610. {
  611. /* We found an entry in the cache so we can reuse it */
  612. reuse_mem_chunk(node, replicate, mc, 0);
  613. _starpu_spin_unlock(&mc_lock[node]);
  614. return 1;
  615. }
  616. if (!_starpu_has_not_important_data)
  617. {
  618. _starpu_spin_unlock(&mc_lock[node]);
  619. return 0;
  620. }
  621. restart:
  622. /* now look for some non essential data in the active list */
  623. for (mc = _starpu_mem_chunk_list_begin(&mc_list[node]);
  624. mc != _starpu_mem_chunk_list_end(&mc_list[node]) && !success;
  625. mc = next_mc)
  626. {
  627. /* there is a risk that the memory chunk is freed before next
  628. * iteration starts: so we compute the next element of the list
  629. * now */
  630. orig_next_mc = next_mc = _starpu_mem_chunk_list_next(mc);
  631. if (mc->remove_notify)
  632. /* Somebody already working here, skip */
  633. continue;
  634. if (next_mc)
  635. {
  636. if (next_mc->remove_notify)
  637. /* Somebody already working here, skip */
  638. continue;
  639. next_mc->remove_notify = &next_mc;
  640. }
  641. if (mc->data->is_not_important && (mc->footprint == footprint))
  642. {
  643. // fprintf(stderr, "found a candidate ...\n");
  644. /* Note: this may unlock mc_list! */
  645. success = try_to_reuse_mem_chunk(mc, node, replicate, 1);
  646. }
  647. if (orig_next_mc)
  648. {
  649. if (!next_mc)
  650. /* Oops, somebody dropped the next item while we were
  651. * not keeping the mc_lock. Restart from the beginning
  652. * of the list */
  653. goto restart;
  654. else
  655. {
  656. STARPU_ASSERT(next_mc->remove_notify == &next_mc);
  657. next_mc->remove_notify = NULL;
  658. }
  659. }
  660. }
  661. _starpu_spin_unlock(&mc_lock[node]);
  662. return success;
  663. }
  664. #endif
  665. /*
  666. * Free the memory chuncks that are explicitely tagged to be freed.
  667. */
  668. static size_t flush_memchunk_cache(unsigned node, size_t reclaim)
  669. {
  670. struct _starpu_mem_chunk *mc;
  671. struct mc_cache_entry *entry, *tmp;
  672. size_t freed = 0;
  673. _starpu_spin_lock(&mc_lock[node]);
  674. HASH_ITER(hh, mc_cache[node], entry, tmp)
  675. {
  676. while (!_starpu_mem_chunk_list_empty(&entry->list))
  677. {
  678. mc = _starpu_mem_chunk_list_pop_front(&entry->list);
  679. STARPU_ASSERT(!mc->data);
  680. STARPU_ASSERT(!mc->replicate);
  681. mc_cache_nb[node]--;
  682. STARPU_ASSERT(mc_cache_nb[node] >= 0);
  683. mc_cache_size[node] -= mc->size;
  684. STARPU_ASSERT(mc_cache_size[node] >= 0);
  685. freed += free_memory_on_node(mc, node);
  686. free(mc->chunk_interface);
  687. _starpu_mem_chunk_delete(mc);
  688. if (reclaim && freed >= reclaim)
  689. break;
  690. }
  691. if (reclaim && freed >= reclaim)
  692. break;
  693. }
  694. _starpu_spin_unlock(&mc_lock[node]);
  695. return freed;
  696. }
  697. /*
  698. * Try to free the buffers currently in use on the memory node. If the force
  699. * flag is set, the memory is freed regardless of coherency concerns (this
  700. * should only be used at the termination of StarPU for instance). The
  701. * mc_lock[node] should be taken prior to calling this function.
  702. */
  703. static size_t free_potentially_in_use_mc(unsigned node, unsigned force, size_t reclaim)
  704. {
  705. size_t freed = 0;
  706. struct _starpu_mem_chunk *mc, *next_mc;
  707. /*
  708. * We have to unlock mc_lock before locking header_lock, so we have
  709. * to be careful with the list. We try to do just one pass, by
  710. * remembering the next mc to be tried. If it gets dropped, we restart
  711. * from zero. So we continue until we go through the whole list without
  712. * finding anything to free.
  713. */
  714. restart:
  715. _starpu_spin_lock(&mc_lock[node]);
  716. restart2:
  717. for (mc = _starpu_mem_chunk_list_begin(&mc_list[node]);
  718. mc != _starpu_mem_chunk_list_end(&mc_list[node]) && (!reclaim || freed < reclaim);
  719. mc = next_mc)
  720. {
  721. /* mc hopefully gets out of the list, we thus need to prefetch
  722. * the next element */
  723. next_mc = _starpu_mem_chunk_list_next(mc);
  724. if (!force)
  725. {
  726. struct _starpu_mem_chunk *orig_next_mc = next_mc;
  727. if (mc->remove_notify)
  728. /* Somebody already working here, skip */
  729. continue;
  730. if (next_mc)
  731. {
  732. if (next_mc->remove_notify)
  733. /* Somebody already working here, skip */
  734. continue;
  735. next_mc->remove_notify = &next_mc;
  736. }
  737. /* Note: this may unlock mc_list! */
  738. freed += try_to_free_mem_chunk(mc, node);
  739. if (orig_next_mc)
  740. {
  741. if (!next_mc)
  742. /* Oops, somebody dropped the next item while we were
  743. * not keeping the mc_lock. Restart from the beginning
  744. * of the list */
  745. goto restart2;
  746. else
  747. {
  748. STARPU_ASSERT(next_mc->remove_notify == &next_mc);
  749. next_mc->remove_notify = NULL;
  750. }
  751. }
  752. }
  753. else
  754. {
  755. /* Shutting down, really free */
  756. starpu_data_handle_t handle = mc->data;
  757. if (_starpu_spin_trylock(&handle->header_lock))
  758. {
  759. /* Ergl. We are shutting down, but somebody is
  760. * still locking the handle. That's not
  761. * supposed to happen, but better be safe by
  762. * letting it go through. */
  763. _starpu_spin_unlock(&mc_lock[node]);
  764. goto restart;
  765. }
  766. /* We must free the memory now, because we are
  767. * terminating the drivers: note that data coherency is
  768. * not maintained in that case ! */
  769. freed += do_free_mem_chunk(mc, node);
  770. _starpu_spin_unlock(&handle->header_lock);
  771. }
  772. }
  773. _starpu_spin_unlock(&mc_lock[node]);
  774. return freed;
  775. }
  776. size_t _starpu_memory_reclaim_generic(unsigned node, unsigned force, size_t reclaim)
  777. {
  778. size_t freed = 0;
  779. if (reclaim && !force)
  780. {
  781. static unsigned warned;
  782. if (!warned)
  783. {
  784. if (STARPU_ATOMIC_ADD(&warned, 1) == 1)
  785. {
  786. char name[32];
  787. _starpu_memory_node_get_name(node, name, sizeof(name));
  788. _STARPU_DISP("Not enough memory left on node %s. Your application data set seems too huge to fit on the device, StarPU will cope by trying to purge %lu MiB out. This message will not be printed again for further purges\n", name, (unsigned long) (reclaim / 1048576));
  789. }
  790. }
  791. }
  792. /* remove all buffers for which there was a removal request */
  793. freed += flush_memchunk_cache(node, reclaim);
  794. /* try to free all allocated data potentially in use */
  795. if (force || (reclaim && freed<reclaim))
  796. freed += free_potentially_in_use_mc(node, force, reclaim);
  797. return freed;
  798. }
  799. /*
  800. * This function frees all the memory that was implicitely allocated by StarPU
  801. * (for the data replicates). This is not ensuring data coherency, and should
  802. * only be called while StarPU is getting shut down.
  803. */
  804. size_t _starpu_free_all_automatically_allocated_buffers(unsigned node)
  805. {
  806. return _starpu_memory_reclaim_generic(node, 1, 0);
  807. }
  808. /* Periodic tidy of available memory */
  809. void starpu_memchunk_tidy(unsigned node)
  810. {
  811. starpu_ssize_t total = starpu_memory_get_total(node);
  812. starpu_ssize_t available = starpu_memory_get_available(node);
  813. size_t target, amount;
  814. /* Count cached allocation as being available */
  815. available += mc_cache_size[node];
  816. if (total > 0 && available >= (total * minimum_p) / 100)
  817. /* Enough available space, do not trigger reclaiming */
  818. return;
  819. if (mc_clean_nb[node] < (mc_nb[node] * minimum_clean_p) / 100)
  820. {
  821. struct _starpu_mem_chunk *mc, *orig_next_mc, *next_mc;
  822. int skipped = 0; /* Whether we skipped a dirty MC, and we should thus stop updating mc_dirty_head. */
  823. /* _STARPU_DEBUG("%d not clean: %d %d\n", node, mc_clean_nb[node], mc_nb[node]); */
  824. _STARPU_TRACE_START_WRITEBACK_ASYNC(node);
  825. _starpu_spin_lock(&mc_lock[node]);
  826. for (mc = mc_dirty_head[node];
  827. mc && mc_clean_nb[node] < (mc_nb[node] * target_clean_p) / 100;
  828. mc = next_mc, mc && skipped ? 0 : (mc_dirty_head[node] = mc))
  829. {
  830. starpu_data_handle_t handle;
  831. /* mc may get out of the list, we thus need to prefetch
  832. * the next element */
  833. next_mc = _starpu_mem_chunk_list_next(mc);
  834. if (mc->home)
  835. /* Home node, it's always clean */
  836. continue;
  837. if (mc->clean)
  838. /* already clean */
  839. continue;
  840. if (next_mc && next_mc->remove_notify)
  841. {
  842. /* Somebody already working here, skip */
  843. skipped = 1;
  844. continue;
  845. }
  846. handle = mc->data;
  847. STARPU_ASSERT(handle);
  848. STARPU_ASSERT(handle->home_node != -1);
  849. if (_starpu_spin_trylock(&handle->header_lock))
  850. {
  851. /* the handle is busy, abort */
  852. skipped = 1;
  853. continue;
  854. }
  855. if (handle->current_mode == STARPU_W)
  856. {
  857. unsigned n;
  858. for (n = 0; n < STARPU_MAXNODES; n++)
  859. if (_starpu_get_data_refcnt(handle, n))
  860. break;
  861. if (n < STARPU_MAXNODES)
  862. {
  863. /* Some task is writing to the handle somewhere */
  864. _starpu_spin_unlock(&handle->header_lock);
  865. skipped = 1;
  866. continue;
  867. }
  868. }
  869. if (
  870. /* This data should be written through to this node, avoid
  871. * dropping it! */
  872. handle->wt_mask & (1<<node)
  873. /* This is partitioned, don't care about the
  874. * whole data, we'll work on the subdatas. */
  875. || handle->nchildren
  876. /* REDUX, can't do anything with it, skip it */
  877. || mc->relaxed_coherency == 2
  878. )
  879. {
  880. _starpu_spin_unlock(&handle->header_lock);
  881. continue;
  882. }
  883. /* This should have been marked as clean already */
  884. if (handle->per_node[handle->home_node].state != STARPU_INVALID || mc->relaxed_coherency == 1)
  885. {
  886. /* it's actually clean */
  887. mc->clean = 1;
  888. mc_clean_nb[node]++;
  889. }
  890. else
  891. {
  892. /* MC is dirty and nobody working on it, submit writeback */
  893. /* MC will be clean, consider it as such */
  894. mc->clean = 1;
  895. mc_clean_nb[node]++;
  896. orig_next_mc = next_mc;
  897. if (next_mc)
  898. {
  899. STARPU_ASSERT(!next_mc->remove_notify);
  900. next_mc->remove_notify = &next_mc;
  901. }
  902. _starpu_spin_unlock(&mc_lock[node]);
  903. if (!_starpu_create_request_to_fetch_data(handle, &handle->per_node[handle->home_node], STARPU_R, 2, 1, NULL, NULL, 0, "starpu_memchunk_tidy"))
  904. {
  905. /* No request was actually needed??
  906. * Odd, but cope with it. */
  907. handle = NULL;
  908. }
  909. _starpu_spin_lock(&mc_lock[node]);
  910. if (orig_next_mc)
  911. {
  912. if (!next_mc)
  913. /* Oops, somebody dropped the next item while we were
  914. * not keeping the mc_lock. Give up for now, and we'll
  915. * see the rest later */
  916. ;
  917. else
  918. {
  919. STARPU_ASSERT(next_mc->remove_notify == &next_mc);
  920. next_mc->remove_notify = NULL;
  921. }
  922. }
  923. }
  924. if (handle)
  925. _starpu_spin_unlock(&handle->header_lock);
  926. }
  927. _starpu_spin_unlock(&mc_lock[node]);
  928. _STARPU_TRACE_END_WRITEBACK_ASYNC(node);
  929. }
  930. if (total <= 0)
  931. return;
  932. /* Not enough available space, reclaim until we reach the target. */
  933. target = (total * target_p) / 100;
  934. amount = target - available;
  935. if (!STARPU_RUNNING_ON_VALGRIND && tidying[node])
  936. /* Some thread is already tidying this node, let it do it */
  937. return;
  938. if (STARPU_ATOMIC_ADD(&tidying[node], 1) > 1)
  939. /* Some thread got it before us, let it do it */
  940. goto out;
  941. static unsigned warned;
  942. if (!warned)
  943. {
  944. if (STARPU_ATOMIC_ADD(&warned, 1) == 1)
  945. {
  946. char name[32];
  947. _starpu_memory_node_get_name(node, name, sizeof(name));
  948. _STARPU_DISP("Low memory left on node %s (%luMiB over %luMiB). Your application data set seems too huge to fit on the device, StarPU will cope by trying to purge %lu MiB out. This message will not be printed again for further purges. The thresholds can be tuned using the STARPU_MINIMUM_AVAILABLE_MEM and STARPU_TARGET_AVAILABLE_MEM environment variables.\n", name, (unsigned long) (available / 1048576), (unsigned long) (total / 1048576), (unsigned long) (amount / 1048576));
  949. }
  950. }
  951. _STARPU_TRACE_START_MEMRECLAIM(node,2);
  952. _STARPU_TRACE_START_MEMRECLAIM(node,2);
  953. free_potentially_in_use_mc(node, 0, amount);
  954. _STARPU_TRACE_END_MEMRECLAIM(node,2);
  955. out:
  956. (void) STARPU_ATOMIC_ADD(&tidying[node], -1);
  957. }
  958. static struct _starpu_mem_chunk *_starpu_memchunk_init(struct _starpu_data_replicate *replicate, size_t interface_size, unsigned home, unsigned automatically_allocated)
  959. {
  960. struct _starpu_mem_chunk *mc = _starpu_mem_chunk_new();
  961. starpu_data_handle_t handle = replicate->handle;
  962. STARPU_ASSERT(handle);
  963. STARPU_ASSERT(handle->ops);
  964. mc->data = handle;
  965. mc->footprint = _starpu_compute_data_footprint(handle);
  966. mc->ops = handle->ops;
  967. mc->automatically_allocated = automatically_allocated;
  968. mc->relaxed_coherency = replicate->relaxed_coherency;
  969. mc->home = home;
  970. mc->clean = 0;
  971. if (replicate->relaxed_coherency == 1)
  972. /* SCRATCH is always easy to drop, thus clean */
  973. mc->clean = 1;
  974. else if (replicate->relaxed_coherency == 0 && handle->home_node != -1 && handle->per_node[(int) replicate->memory_node].state != STARPU_INVALID)
  975. /* This is a normal data and the home node has the value */
  976. mc->clean = 1;
  977. mc->replicate = replicate;
  978. mc->replicate->mc = mc;
  979. mc->chunk_interface = NULL;
  980. mc->size_interface = interface_size;
  981. mc->remove_notify = NULL;
  982. return mc;
  983. }
  984. static void register_mem_chunk(starpu_data_handle_t handle, struct _starpu_data_replicate *replicate, unsigned automatically_allocated)
  985. {
  986. unsigned dst_node = replicate->memory_node;
  987. struct _starpu_mem_chunk *mc;
  988. /* the interface was already filled by ops->allocate_data_on_node */
  989. size_t interface_size = replicate->handle->ops->interface_size;
  990. /* Put this memchunk in the list of memchunk in use */
  991. mc = _starpu_memchunk_init(replicate, interface_size, handle->home_node == -1 || (int) dst_node == handle->home_node, automatically_allocated);
  992. _starpu_spin_lock(&mc_lock[dst_node]);
  993. MC_LIST_PUSH_BACK(dst_node, mc);
  994. _starpu_spin_unlock(&mc_lock[dst_node]);
  995. }
  996. /* This function is called when the handle is destroyed (eg. when calling
  997. * unregister or unpartition). It puts all the memchunks that refer to the
  998. * specified handle into the cache.
  999. */
  1000. void _starpu_request_mem_chunk_removal(starpu_data_handle_t handle, struct _starpu_data_replicate *replicate, unsigned node, size_t size)
  1001. {
  1002. struct _starpu_mem_chunk *mc = replicate->mc;
  1003. STARPU_ASSERT(mc->data == handle);
  1004. _starpu_spin_checklocked(&handle->header_lock);
  1005. /* Record the allocated size, so that later in memory
  1006. * reclaiming we can estimate how much memory we free
  1007. * by freeing this. */
  1008. mc->size = size;
  1009. /* Also keep the interface parameters and pointers, for later reuse
  1010. * while detached, or freed */
  1011. mc->chunk_interface = malloc(mc->size_interface);
  1012. memcpy(mc->chunk_interface, replicate->data_interface, mc->size_interface);
  1013. /* This memchunk doesn't have to do with the data any more. */
  1014. replicate->mc = NULL;
  1015. mc->replicate = NULL;
  1016. replicate->allocated = 0;
  1017. replicate->automatically_allocated = 0;
  1018. replicate->initialized = 0;
  1019. _starpu_spin_lock(&mc_lock[node]);
  1020. mc->data = NULL;
  1021. /* remove it from the main list */
  1022. MC_LIST_ERASE(node, mc);
  1023. _starpu_spin_unlock(&mc_lock[node]);
  1024. /*
  1025. * Unless the user has provided a main RAM limitation, we would fill
  1026. * memory with cached data and then eventually swap.
  1027. */
  1028. /*
  1029. * This is particularly important when
  1030. * STARPU_USE_ALLOCATION_CACHE is not enabled, as we
  1031. * wouldn't even re-use these allocations!
  1032. */
  1033. if (handle->ops->dontcache || (starpu_node_get_kind(node) == STARPU_CPU_RAM
  1034. #ifdef STARPU_USE_ALLOCATION_CACHE
  1035. && limit_cpu_mem < 0
  1036. #endif
  1037. ))
  1038. {
  1039. /* Free data immediately */
  1040. free_memory_on_node(mc, node);
  1041. free(mc->chunk_interface);
  1042. _starpu_mem_chunk_delete(mc);
  1043. }
  1044. else
  1045. {
  1046. /* put it in the list of buffers to be removed */
  1047. uint32_t footprint = mc->footprint;
  1048. struct mc_cache_entry *entry;
  1049. _starpu_spin_lock(&mc_lock[node]);
  1050. HASH_FIND(hh, mc_cache[node], &footprint, sizeof(footprint), entry);
  1051. if (!entry)
  1052. {
  1053. entry = malloc(sizeof(*entry));
  1054. _starpu_mem_chunk_list_init(&entry->list);
  1055. entry->footprint = footprint;
  1056. HASH_ADD(hh, mc_cache[node], footprint, sizeof(entry->footprint), entry);
  1057. }
  1058. mc_cache_nb[node]++;
  1059. mc_cache_size[node] += mc->size;
  1060. _starpu_mem_chunk_list_push_front(&entry->list, mc);
  1061. _starpu_spin_unlock(&mc_lock[node]);
  1062. }
  1063. }
  1064. /*
  1065. * In order to allocate a piece of data, we try to reuse existing buffers if
  1066. * its possible.
  1067. * 1 - we try to reuse a memchunk that is explicitely unused.
  1068. * 2 - we go through the list of memory chunks and find one that is not
  1069. * referenced and that has the same footprint to reuse it.
  1070. * 3 - we call the usual driver's alloc method
  1071. * 4 - we go through the list of memory chunks and release those that are
  1072. * not referenced (or part of those).
  1073. *
  1074. */
  1075. static starpu_ssize_t _starpu_allocate_interface(starpu_data_handle_t handle, struct _starpu_data_replicate *replicate, unsigned dst_node, unsigned is_prefetch)
  1076. {
  1077. unsigned attempts = 0;
  1078. starpu_ssize_t allocated_memory;
  1079. int ret;
  1080. starpu_ssize_t data_size = _starpu_data_get_size(handle);
  1081. int told_reclaiming = 0;
  1082. _starpu_spin_checklocked(&handle->header_lock);
  1083. _starpu_data_allocation_inc_stats(dst_node);
  1084. #ifdef STARPU_USE_ALLOCATION_CACHE
  1085. /* perhaps we can directly reuse a buffer in the free-list */
  1086. uint32_t footprint = _starpu_compute_data_footprint(handle);
  1087. _STARPU_TRACE_START_ALLOC_REUSE(dst_node, data_size);
  1088. if (try_to_find_reusable_mem_chunk(dst_node, handle, replicate, footprint))
  1089. {
  1090. _starpu_allocation_cache_hit(dst_node);
  1091. return data_size;
  1092. }
  1093. _STARPU_TRACE_END_ALLOC_REUSE(dst_node);
  1094. #endif
  1095. STARPU_ASSERT(handle->ops);
  1096. STARPU_ASSERT(handle->ops->allocate_data_on_node);
  1097. STARPU_ASSERT(replicate->data_interface);
  1098. size_t size = handle->ops->interface_size;
  1099. if (!size)
  1100. /* nul-size VLA is undefined... */
  1101. size = 1;
  1102. char data_interface[size];
  1103. memcpy(data_interface, replicate->data_interface, handle->ops->interface_size);
  1104. /* Take temporary reference on the replicate */
  1105. replicate->refcnt++;
  1106. handle->busy_count++;
  1107. _starpu_spin_unlock(&handle->header_lock);
  1108. do
  1109. {
  1110. _STARPU_TRACE_START_ALLOC(dst_node, data_size);
  1111. #if defined(STARPU_USE_CUDA) && defined(HAVE_CUDA_MEMCPY_PEER) && !defined(STARPU_SIMGRID)
  1112. if (starpu_node_get_kind(dst_node) == STARPU_CUDA_RAM)
  1113. {
  1114. /* To facilitate the design of interface, we set the
  1115. * proper CUDA device in case it is needed. This avoids
  1116. * having to set it again in the malloc method of each
  1117. * interface. */
  1118. starpu_cuda_set_device(_starpu_memory_node_get_devid(dst_node));
  1119. }
  1120. #endif
  1121. allocated_memory = handle->ops->allocate_data_on_node(data_interface, dst_node);
  1122. _STARPU_TRACE_END_ALLOC(dst_node);
  1123. if (allocated_memory == -ENOMEM)
  1124. {
  1125. if (!told_reclaiming)
  1126. {
  1127. /* Prevent prefetches and such from happening */
  1128. (void) STARPU_ATOMIC_ADD(&reclaiming[dst_node], 1);
  1129. told_reclaiming = 1;
  1130. }
  1131. size_t reclaim = 0.25*_starpu_memory_manager_get_global_memory_size(dst_node);
  1132. size_t handle_size = handle->ops->get_size(handle);
  1133. if (starpu_memstrategy_data_size_coefficient*handle_size > reclaim)
  1134. reclaim = starpu_memstrategy_data_size_coefficient*handle_size;
  1135. _STARPU_TRACE_START_MEMRECLAIM(dst_node,is_prefetch);
  1136. if (is_prefetch)
  1137. {
  1138. flush_memchunk_cache(dst_node, reclaim);
  1139. }
  1140. else
  1141. _starpu_memory_reclaim_generic(dst_node, 0, reclaim);
  1142. _STARPU_TRACE_END_MEMRECLAIM(dst_node,is_prefetch);
  1143. }
  1144. }
  1145. while((allocated_memory == -ENOMEM) && attempts++ < 2);
  1146. int cpt = 0;
  1147. while (cpt < STARPU_SPIN_MAXTRY && _starpu_spin_trylock(&handle->header_lock))
  1148. {
  1149. cpt++;
  1150. _starpu_datawizard_progress(_starpu_memory_node_get_local_key(), 0);
  1151. }
  1152. if (cpt == STARPU_SPIN_MAXTRY)
  1153. _starpu_spin_lock(&handle->header_lock);
  1154. replicate->refcnt--;
  1155. STARPU_ASSERT(replicate->refcnt >= 0);
  1156. STARPU_ASSERT(handle->busy_count > 0);
  1157. handle->busy_count--;
  1158. ret = _starpu_data_check_not_busy(handle);
  1159. STARPU_ASSERT(ret == 0);
  1160. if (told_reclaiming)
  1161. /* We've finished with reclaiming memory, let prefetches start again */
  1162. (void) STARPU_ATOMIC_ADD(&reclaiming[dst_node], -1);
  1163. if (allocated_memory == -ENOMEM)
  1164. {
  1165. if (replicate->allocated)
  1166. /* Didn't manage to allocate, but somebody else did */
  1167. allocated_memory = 0;
  1168. goto out;
  1169. }
  1170. if (replicate->allocated)
  1171. {
  1172. /* Argl, somebody allocated it in between already, drop this one */
  1173. _STARPU_TRACE_START_FREE(dst_node, data_size);
  1174. handle->ops->free_data_on_node(data_interface, dst_node);
  1175. _STARPU_TRACE_END_FREE(dst_node);
  1176. allocated_memory = 0;
  1177. }
  1178. else
  1179. /* Install allocated interface */
  1180. memcpy(replicate->data_interface, data_interface, handle->ops->interface_size);
  1181. out:
  1182. return allocated_memory;
  1183. }
  1184. int _starpu_allocate_memory_on_node(starpu_data_handle_t handle, struct _starpu_data_replicate *replicate, unsigned is_prefetch)
  1185. {
  1186. starpu_ssize_t allocated_memory;
  1187. unsigned dst_node = replicate->memory_node;
  1188. STARPU_ASSERT(handle);
  1189. _starpu_spin_checklocked(&handle->header_lock);
  1190. /* A buffer is already allocated on the node */
  1191. if (replicate->allocated)
  1192. return 0;
  1193. STARPU_ASSERT(replicate->data_interface);
  1194. allocated_memory = _starpu_allocate_interface(handle, replicate, dst_node, is_prefetch);
  1195. /* perhaps we could really not handle that capacity misses */
  1196. if (allocated_memory == -ENOMEM)
  1197. return -ENOMEM;
  1198. if (replicate->allocated)
  1199. /* Somebody allocated it in between already */
  1200. return 0;
  1201. register_mem_chunk(handle, replicate, 1);
  1202. replicate->allocated = 1;
  1203. replicate->automatically_allocated = 1;
  1204. if (replicate->relaxed_coherency == 0 && dst_node == STARPU_MAIN_RAM)
  1205. {
  1206. /* We are allocating the buffer in main memory, also register it
  1207. * for the gcc plugin. */
  1208. void *ptr = starpu_data_handle_to_pointer(handle, STARPU_MAIN_RAM);
  1209. if (ptr != NULL)
  1210. {
  1211. _starpu_data_register_ram_pointer(handle, ptr);
  1212. }
  1213. }
  1214. return 0;
  1215. }
  1216. unsigned starpu_data_test_if_allocated_on_node(starpu_data_handle_t handle, unsigned memory_node)
  1217. {
  1218. return handle->per_node[memory_node].allocated;
  1219. }
  1220. /* This memchunk has been recently used, put it last on the mc_list, so we will
  1221. * try to evict it as late as possible */
  1222. void _starpu_memchunk_recently_used(struct _starpu_mem_chunk *mc, unsigned node)
  1223. {
  1224. if (!mc)
  1225. /* user-allocated memory */
  1226. return;
  1227. _starpu_spin_lock(&mc_lock[node]);
  1228. MC_LIST_ERASE(node, mc);
  1229. MC_LIST_PUSH_BACK(node, mc);
  1230. _starpu_spin_unlock(&mc_lock[node]);
  1231. }
  1232. /* This memchunk will not be used in the close future, put it on the clean
  1233. * list, so we will to evict it first */
  1234. void _starpu_memchunk_wont_use(struct _starpu_mem_chunk *mc, unsigned node)
  1235. {
  1236. if (!mc)
  1237. /* user-allocated memory */
  1238. return;
  1239. _starpu_spin_lock(&mc_lock[node]);
  1240. MC_LIST_ERASE(node, mc);
  1241. /* Caller will schedule a clean transfer */
  1242. mc->clean = 1;
  1243. MC_LIST_PUSH_CLEAN(node, mc);
  1244. _starpu_spin_unlock(&mc_lock[node]);
  1245. }
  1246. /* This memchunk is being written to, and thus becomes dirty */
  1247. void _starpu_memchunk_dirty(struct _starpu_mem_chunk *mc, unsigned node)
  1248. {
  1249. if (!mc)
  1250. /* user-allocated memory */
  1251. return;
  1252. if (mc->home)
  1253. /* Home is always clean */
  1254. return;
  1255. _starpu_spin_lock(&mc_lock[node]);
  1256. if (mc->relaxed_coherency == 1)
  1257. {
  1258. /* SCRATCH, make it clean if not already*/
  1259. if (!mc->clean)
  1260. {
  1261. mc_clean_nb[node]++;
  1262. mc->clean = 1;
  1263. }
  1264. }
  1265. else
  1266. {
  1267. if (mc->clean)
  1268. {
  1269. mc_clean_nb[node]--;
  1270. mc->clean = 0;
  1271. }
  1272. }
  1273. _starpu_spin_unlock(&mc_lock[node]);
  1274. }
  1275. #ifdef STARPU_MEMORY_STATS
  1276. void _starpu_memory_display_stats_by_node(int node)
  1277. {
  1278. _starpu_spin_lock(&mc_lock[node]);
  1279. if (!_starpu_mem_chunk_list_empty(&mc_list[node]))
  1280. {
  1281. struct _starpu_mem_chunk *mc;
  1282. fprintf(stderr, "#-------\n");
  1283. fprintf(stderr, "Data on Node #%d\n",node);
  1284. for (mc = _starpu_mem_chunk_list_begin(&mc_list[node]);
  1285. mc != _starpu_mem_chunk_list_end(&mc_list[node]);
  1286. mc = _starpu_mem_chunk_list_next(mc))
  1287. {
  1288. if (mc->automatically_allocated == 0)
  1289. _starpu_memory_display_handle_stats(mc->data);
  1290. }
  1291. }
  1292. _starpu_spin_unlock(&mc_lock[node]);
  1293. }
  1294. #endif
  1295. void starpu_data_display_memory_stats(void)
  1296. {
  1297. #ifdef STARPU_MEMORY_STATS
  1298. unsigned node;
  1299. fprintf(stderr, "\n#---------------------\n");
  1300. fprintf(stderr, "Memory stats :\n");
  1301. for (node = 0; node < STARPU_MAXNODES; node++)
  1302. {
  1303. _starpu_memory_display_stats_by_node(node);
  1304. }
  1305. fprintf(stderr, "\n#---------------------\n");
  1306. #endif
  1307. }
  1308. static int
  1309. get_better_disk_can_accept_size(starpu_data_handle_t handle, unsigned node)
  1310. {
  1311. int target = -1;
  1312. unsigned nnodes = starpu_memory_nodes_get_count();
  1313. unsigned int i;
  1314. double time_disk = 0;
  1315. for (i = 0; i < nnodes; i++)
  1316. {
  1317. if (starpu_node_get_kind(i) == STARPU_DISK_RAM && i != node &&
  1318. (_starpu_memory_manager_test_allocate_size(i, _starpu_data_get_size(handle)) == 1 ||
  1319. handle->per_node[i].allocated))
  1320. {
  1321. /* if we can write on the disk */
  1322. if (_starpu_get_disk_flag(i) != STARPU_DISK_NO_RECLAIM)
  1323. {
  1324. /* only time can change between disk <-> main_ram
  1325. * and not between main_ram <-> worker if we compare diks*/
  1326. double time_tmp = starpu_transfer_predict(i, STARPU_MAIN_RAM, _starpu_data_get_size(handle));
  1327. if (target == -1 || time_disk > time_tmp)
  1328. {
  1329. target = i;
  1330. time_disk = time_tmp;
  1331. }
  1332. }
  1333. }
  1334. }
  1335. return target;
  1336. }
  1337. static unsigned
  1338. choose_target(starpu_data_handle_t handle, unsigned node)
  1339. {
  1340. int target = -1;
  1341. size_t size_handle = _starpu_data_get_size(handle);
  1342. if (handle->home_node != -1)
  1343. /* try to push on RAM if we can before to push on disk */
  1344. if(starpu_node_get_kind(handle->home_node) == STARPU_DISK_RAM && node != STARPU_MAIN_RAM)
  1345. {
  1346. if (handle->per_node[STARPU_MAIN_RAM].allocated ||
  1347. _starpu_memory_manager_test_allocate_size(STARPU_MAIN_RAM, size_handle) == 1)
  1348. {
  1349. target = STARPU_MAIN_RAM;
  1350. }
  1351. else
  1352. {
  1353. target = get_better_disk_can_accept_size(handle, node);
  1354. }
  1355. }
  1356. /* others memory nodes */
  1357. else
  1358. {
  1359. target = handle->home_node;
  1360. }
  1361. else
  1362. {
  1363. /* handle->home_node == -1 */
  1364. /* no place for datas in RAM, we push on disk */
  1365. if (node == STARPU_MAIN_RAM)
  1366. {
  1367. target = get_better_disk_can_accept_size(handle, node);
  1368. }
  1369. /* node != 0 */
  1370. /* try to push data to RAM if we can before to push on disk*/
  1371. else if (handle->per_node[STARPU_MAIN_RAM].allocated ||
  1372. _starpu_memory_manager_test_allocate_size(STARPU_MAIN_RAM, size_handle) == 1)
  1373. {
  1374. target = STARPU_MAIN_RAM;
  1375. }
  1376. /* no place in RAM */
  1377. else
  1378. {
  1379. target = get_better_disk_can_accept_size(handle, node);
  1380. }
  1381. }
  1382. /* we haven't the right to write on the disk */
  1383. if (target != -1 && starpu_node_get_kind(target) == STARPU_DISK_RAM && _starpu_get_disk_flag(target) == STARPU_DISK_NO_RECLAIM)
  1384. target = -1;
  1385. return target;
  1386. }