memalloc.c 45 KB

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