memalloc.c 45 KB

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