memalloc.c 45 KB

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