memalloc.c 49 KB

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