memalloc.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-2012 Université de Bordeaux 1
  4. * Copyright (C) 2010, 2011, 2012 Centre National de la Recherche Scientifique
  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/memalloc.h>
  18. #include <datawizard/footprint.h>
  19. #include <starpu_cuda.h>
  20. #include <starpu_opencl.h>
  21. /* This per-node RW-locks protect mc_list and memchunk_cache entries */
  22. static _starpu_pthread_rwlock_t mc_rwlock[STARPU_MAXNODES];
  23. /* This per-node RW-locks protect lru_list */
  24. static _starpu_pthread_rwlock_t lru_rwlock[STARPU_MAXNODES];
  25. /* Last Recently used memory chunkgs */
  26. static struct _starpu_mem_chunk_lru_list *starpu_lru_list[STARPU_MAXNODES];
  27. /* Potentially in use memory chunks */
  28. static struct _starpu_mem_chunk_list *mc_list[STARPU_MAXNODES];
  29. /* Explicitly caches memory chunks that can be reused */
  30. static struct _starpu_mem_chunk_list *memchunk_cache[STARPU_MAXNODES];
  31. /* When reclaiming memory to allocate, we reclaim MAX(what_is_to_reclaim_on_device, data_size_coefficient*data_size) */
  32. const unsigned starpu_memstrategy_data_size_coefficient=2;
  33. static void starpu_lru(unsigned node);
  34. void _starpu_init_mem_chunk_lists(void)
  35. {
  36. unsigned i;
  37. for (i = 0; i < STARPU_MAXNODES; i++)
  38. {
  39. _STARPU_PTHREAD_RWLOCK_INIT(&mc_rwlock[i], NULL);
  40. _STARPU_PTHREAD_RWLOCK_INIT(&lru_rwlock[i], NULL);
  41. mc_list[i] = _starpu_mem_chunk_list_new();
  42. starpu_lru_list[i] = _starpu_mem_chunk_lru_list_new();
  43. memchunk_cache[i] = _starpu_mem_chunk_list_new();
  44. }
  45. }
  46. void _starpu_deinit_mem_chunk_lists(void)
  47. {
  48. unsigned i;
  49. for (i = 0; i < STARPU_MAXNODES; i++)
  50. {
  51. _starpu_mem_chunk_list_delete(mc_list[i]);
  52. _starpu_mem_chunk_list_delete(memchunk_cache[i]);
  53. _starpu_mem_chunk_lru_list_delete(starpu_lru_list[i]);
  54. }
  55. }
  56. /*
  57. * Manipulate subtrees
  58. */
  59. static void lock_all_subtree(starpu_data_handle_t handle)
  60. {
  61. if (handle->nchildren == 0)
  62. {
  63. /* this is a leaf */
  64. while (_starpu_spin_trylock(&handle->header_lock))
  65. _starpu_datawizard_progress(_starpu_get_local_memory_node(), 0);
  66. }
  67. else
  68. {
  69. /* lock all sub-subtrees children */
  70. unsigned child;
  71. for (child = 0; child < handle->nchildren; child++)
  72. {
  73. lock_all_subtree(&handle->children[child]);
  74. }
  75. }
  76. }
  77. static void unlock_all_subtree(starpu_data_handle_t handle)
  78. {
  79. if (handle->nchildren == 0)
  80. {
  81. /* this is a leaf */
  82. _starpu_spin_unlock(&handle->header_lock);
  83. }
  84. else
  85. {
  86. /* lock all sub-subtrees children
  87. * Note that this is done in the reverse order of the
  88. * lock_all_subtree so that we avoid deadlock */
  89. unsigned i;
  90. for (i =0; i < handle->nchildren; i++)
  91. {
  92. unsigned child = handle->nchildren - 1 - i;
  93. unlock_all_subtree(&handle->children[child]);
  94. }
  95. }
  96. }
  97. static unsigned may_free_subtree(starpu_data_handle_t handle, unsigned node)
  98. {
  99. /* we only free if no one refers to the leaf */
  100. uint32_t refcnt = _starpu_get_data_refcnt(handle, node);
  101. if (refcnt)
  102. return 0;
  103. if (!handle->nchildren)
  104. return 1;
  105. /* look into all sub-subtrees children */
  106. unsigned child;
  107. for (child = 0; child < handle->nchildren; child++)
  108. {
  109. unsigned res;
  110. res = may_free_subtree(&handle->children[child], node);
  111. if (!res) return 0;
  112. }
  113. /* no problem was found */
  114. return 1;
  115. }
  116. static void transfer_subtree_to_node(starpu_data_handle_t handle, unsigned src_node,
  117. unsigned dst_node)
  118. {
  119. unsigned i;
  120. unsigned last = 0;
  121. unsigned cnt;
  122. int ret;
  123. if (handle->nchildren == 0)
  124. {
  125. struct _starpu_data_replicate *src_replicate = &handle->per_node[src_node];
  126. struct _starpu_data_replicate *dst_replicate = &handle->per_node[dst_node];
  127. /* this is a leaf */
  128. switch(src_replicate->state)
  129. {
  130. case STARPU_OWNER:
  131. /* the local node has the only copy */
  132. /* the owner is now the destination_node */
  133. src_replicate->state = STARPU_INVALID;
  134. dst_replicate->state = STARPU_OWNER;
  135. #ifdef STARPU_DEVEL
  136. #warning we should use requests during memory reclaim
  137. #endif
  138. /* TODO use request !! */
  139. /* Take temporary references on the replicates */
  140. _starpu_spin_checklocked(&handle->header_lock);
  141. src_replicate->refcnt++;
  142. dst_replicate->refcnt++;
  143. handle->busy_count+=2;
  144. ret = _starpu_driver_copy_data_1_to_1(handle, src_replicate, dst_replicate, 0, NULL, 1);
  145. STARPU_ASSERT(ret == 0);
  146. src_replicate->refcnt--;
  147. dst_replicate->refcnt--;
  148. STARPU_ASSERT(handle->busy_count >= 2);
  149. handle->busy_count -= 2;
  150. _starpu_data_check_not_busy(handle);
  151. break;
  152. case STARPU_SHARED:
  153. /* some other node may have the copy */
  154. src_replicate->state = STARPU_INVALID;
  155. /* count the number of copies */
  156. cnt = 0;
  157. for (i = 0; i < STARPU_MAXNODES; i++)
  158. {
  159. if (handle->per_node[i].state == STARPU_SHARED)
  160. {
  161. cnt++;
  162. last = i;
  163. }
  164. }
  165. STARPU_ASSERT(cnt > 0);
  166. if (cnt == 1)
  167. handle->per_node[last].state = STARPU_OWNER;
  168. break;
  169. case STARPU_INVALID:
  170. /* nothing to be done */
  171. break;
  172. default:
  173. STARPU_ABORT();
  174. break;
  175. }
  176. }
  177. else
  178. {
  179. /* lock all sub-subtrees children */
  180. unsigned child;
  181. for (child = 0; child < handle->nchildren; child++)
  182. {
  183. transfer_subtree_to_node(&handle->children[child],
  184. src_node, dst_node);
  185. }
  186. }
  187. }
  188. static size_t free_memory_on_node(struct _starpu_mem_chunk *mc, uint32_t node)
  189. {
  190. size_t freed = 0;
  191. STARPU_ASSERT(mc->ops);
  192. STARPU_ASSERT(mc->ops->free_data_on_node);
  193. starpu_data_handle_t handle = mc->data;
  194. /* Does this memory chunk refers to a handle that does not exist
  195. * anymore ? */
  196. unsigned data_was_deleted = mc->data_was_deleted;
  197. struct _starpu_data_replicate *replicate = mc->replicate;
  198. // while (_starpu_spin_trylock(&handle->header_lock))
  199. // _starpu_datawizard_progress(_starpu_get_local_memory_node());
  200. #ifdef STARPU_DEVEL
  201. #warning can we block here ?
  202. #endif
  203. // _starpu_spin_lock(&handle->header_lock);
  204. if (mc->automatically_allocated &&
  205. (!handle || data_was_deleted || replicate->refcnt == 0))
  206. {
  207. if (handle && !data_was_deleted)
  208. STARPU_ASSERT(replicate->allocated);
  209. #if defined(STARPU_USE_CUDA) && defined(HAVE_CUDA_MEMCPY_PEER) && !defined(STARPU_SIMGRID)
  210. if (starpu_node_get_kind(node) == STARPU_CUDA_RAM)
  211. {
  212. /* To facilitate the design of interface, we set the
  213. * proper CUDA device in case it is needed. This avoids
  214. * having to set it again in the free method of each
  215. * interface. */
  216. starpu_cuda_set_device(_starpu_memory_node_to_devid(node));
  217. }
  218. #endif
  219. mc->ops->free_data_on_node(mc->chunk_interface, node);
  220. if (handle && !data_was_deleted)
  221. {
  222. replicate->allocated = 0;
  223. /* XXX why do we need that ? */
  224. replicate->automatically_allocated = 0;
  225. }
  226. freed = mc->size;
  227. if (handle && !data_was_deleted)
  228. STARPU_ASSERT(replicate->refcnt == 0);
  229. }
  230. // _starpu_spin_unlock(&handle->header_lock);
  231. return freed;
  232. }
  233. static size_t do_free_mem_chunk(struct _starpu_mem_chunk *mc, unsigned node)
  234. {
  235. size_t size;
  236. mc->replicate->mc=NULL;
  237. /* free the actual buffer */
  238. size = free_memory_on_node(mc, node);
  239. /* remove the mem_chunk from the list */
  240. _starpu_mem_chunk_list_erase(mc_list[node], mc);
  241. free(mc->chunk_interface);
  242. _starpu_mem_chunk_delete(mc);
  243. return size;
  244. }
  245. /* This function is called for memory chunks that are possibly in used (ie. not
  246. * in the cache). They should therefore still be associated to a handle. */
  247. static size_t try_to_free_mem_chunk(struct _starpu_mem_chunk *mc, unsigned node)
  248. {
  249. size_t freed = 0;
  250. starpu_data_handle_t handle;
  251. handle = mc->data;
  252. STARPU_ASSERT(handle);
  253. /* This data should be written through to this node, avoid dropping it! */
  254. if (handle->wt_mask & (1<<node))
  255. return 0;
  256. /* REDUX memchunk */
  257. if (mc->relaxed_coherency == 2)
  258. {
  259. /* TODO: reduce it back to e.g. main memory */
  260. }
  261. else
  262. /* Either it's a "relaxed coherency" memchunk (SCRATCH), or it's a
  263. * memchunk that could be used with filters. */
  264. if (mc->relaxed_coherency == 1)
  265. {
  266. STARPU_ASSERT(mc->replicate);
  267. while (_starpu_spin_trylock(&handle->header_lock))
  268. _starpu_datawizard_progress(_starpu_get_local_memory_node(), 0);
  269. if (mc->replicate->refcnt == 0)
  270. {
  271. /* Note taht there is no need to transfer any data or
  272. * to update the status in terms of MSI protocol
  273. * because this memchunk is associated to a replicate
  274. * in "relaxed coherency" mode. */
  275. freed = do_free_mem_chunk(mc, node);
  276. }
  277. _starpu_spin_unlock(&handle->header_lock);
  278. }
  279. else
  280. {
  281. /* try to lock all the leafs of the subtree */
  282. lock_all_subtree(handle);
  283. /* check if they are all "free" */
  284. if (may_free_subtree(handle, node))
  285. {
  286. STARPU_ASSERT(handle->per_node[node].refcnt == 0);
  287. #ifdef STARPU_MEMORY_STATS
  288. if (handle->per_node[node].state == STARPU_OWNER)
  289. _starpu_memory_handle_stats_invalidated(handle, node);
  290. /* else XXX Considering only owner to invalidate */
  291. #endif
  292. /* in case there was nobody using that buffer, throw it
  293. * away after writing it back to main memory */
  294. transfer_subtree_to_node(handle, node, 0);
  295. #ifdef STARPU_MEMORY_STATS
  296. _starpu_memory_handle_stats_loaded_owner(handle, 0);
  297. #endif
  298. STARPU_ASSERT(handle->per_node[node].refcnt == 0);
  299. /* now the actual buffer may be freed */
  300. freed = do_free_mem_chunk(mc, node);
  301. }
  302. /* unlock the leafs */
  303. unlock_all_subtree(handle);
  304. }
  305. return freed;
  306. }
  307. #ifdef STARPU_USE_ALLOCATION_CACHE
  308. /* We assume that mc_rwlock[node] is taken. is_already_in_mc_list indicates
  309. * that the mc is already in the list of buffers that are possibly used, and
  310. * therefore not in the cache. */
  311. static void reuse_mem_chunk(unsigned node, struct _starpu_data_replicate *new_replicate, struct _starpu_mem_chunk *mc, unsigned is_already_in_mc_list)
  312. {
  313. /* we found an appropriate mem chunk: so we get it out
  314. * of the "to free" list, and reassign it to the new
  315. * piece of data */
  316. if (!is_already_in_mc_list)
  317. {
  318. _starpu_mem_chunk_list_erase(memchunk_cache[node], mc);
  319. }
  320. struct _starpu_data_replicate *old_replicate = mc->replicate;
  321. old_replicate->allocated = 0;
  322. old_replicate->automatically_allocated = 0;
  323. old_replicate->initialized = 0;
  324. new_replicate->allocated = 1;
  325. new_replicate->automatically_allocated = 1;
  326. new_replicate->initialized = 0;
  327. STARPU_ASSERT(new_replicate->data_interface);
  328. STARPU_ASSERT(mc->chunk_interface);
  329. memcpy(new_replicate->data_interface, mc->chunk_interface, old_replicate->handle->ops->interface_size);
  330. mc->data = new_replicate->handle;
  331. mc->data_was_deleted = 0;
  332. /* mc->ops, mc->footprint and mc->interface should be
  333. * unchanged ! */
  334. /* reinsert the mem chunk in the list of active memory chunks */
  335. if (!is_already_in_mc_list)
  336. {
  337. _starpu_mem_chunk_list_push_front(mc_list[node], mc);
  338. }
  339. }
  340. 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)
  341. {
  342. unsigned success = 0;
  343. starpu_data_handle_t old_data;
  344. old_data = mc->data;
  345. STARPU_ASSERT(old_data);
  346. /* try to lock all the leafs of the subtree */
  347. lock_all_subtree(old_data);
  348. /* check if they are all "free" */
  349. if (may_free_subtree(old_data, node))
  350. {
  351. success = 1;
  352. /* in case there was nobody using that buffer, throw it
  353. * away after writing it back to main memory */
  354. transfer_subtree_to_node(old_data, node, 0);
  355. /* now replace the previous data */
  356. reuse_mem_chunk(node, replicate, mc, is_already_in_mc_list);
  357. }
  358. /* unlock the leafs */
  359. unlock_all_subtree(old_data);
  360. return success;
  361. }
  362. static int _starpu_data_interface_compare(void *data_interface_a, struct starpu_data_interface_ops *ops_a,
  363. void *data_interface_b, struct starpu_data_interface_ops *ops_b)
  364. {
  365. if (ops_a->interfaceid != ops_b->interfaceid)
  366. return -1;
  367. int ret = ops_a->compare(data_interface_a, data_interface_b);
  368. return ret;
  369. }
  370. /* This function must be called with mc_rwlock[node] taken in write mode */
  371. static struct _starpu_mem_chunk *_starpu_memchunk_cache_lookup_locked(uint32_t node, starpu_data_handle_t handle)
  372. {
  373. uint32_t footprint = _starpu_compute_data_footprint(handle);
  374. /* go through all buffers in the cache */
  375. struct _starpu_mem_chunk *mc;
  376. for (mc = _starpu_mem_chunk_list_begin(memchunk_cache[node]);
  377. mc != _starpu_mem_chunk_list_end(memchunk_cache[node]);
  378. mc = _starpu_mem_chunk_list_next(mc))
  379. {
  380. if (mc->footprint == footprint)
  381. {
  382. /* Is that a false hit ? (this is _very_ unlikely) */
  383. if (_starpu_data_interface_compare(handle->per_node[node].data_interface, handle->ops, mc->chunk_interface, mc->ops))
  384. continue;
  385. /* Cache hit */
  386. /* Remove from the cache */
  387. _starpu_mem_chunk_list_erase(memchunk_cache[node], mc);
  388. return mc;
  389. }
  390. }
  391. /* This is a cache miss */
  392. return NULL;
  393. }
  394. /* this function looks for a memory chunk that matches a given footprint in the
  395. * list of mem chunk that need to be freed. This function must be called with
  396. * mc_rwlock[node] taken in write mode. */
  397. static unsigned try_to_find_reusable_mem_chunk(unsigned node, starpu_data_handle_t data, struct _starpu_data_replicate *replicate, uint32_t footprint)
  398. {
  399. struct _starpu_mem_chunk *mc, *next_mc;
  400. /* go through all buffers in the cache */
  401. mc = _starpu_memchunk_cache_lookup_locked(node, data);
  402. if (mc)
  403. {
  404. /* We found an entry in the cache so we can reuse it */
  405. reuse_mem_chunk(node, replicate, mc, 0);
  406. return 1;
  407. }
  408. /* now look for some non essential data in the active list */
  409. for (mc = _starpu_mem_chunk_list_begin(mc_list[node]);
  410. mc != _starpu_mem_chunk_list_end(mc_list[node]);
  411. mc = next_mc)
  412. {
  413. /* there is a risk that the memory chunk is freed before next
  414. * iteration starts: so we compute the next element of the list
  415. * now */
  416. next_mc = _starpu_mem_chunk_list_next(mc);
  417. if (mc->data->is_not_important && (mc->footprint == footprint))
  418. {
  419. // fprintf(stderr, "found a candidate ...\n");
  420. if (try_to_reuse_mem_chunk(mc, node, replicate, 1))
  421. return 1;
  422. }
  423. }
  424. return 0;
  425. }
  426. #endif
  427. /*
  428. * Free the memory chuncks that are explicitely tagged to be freed. The
  429. * mc_rwlock[node] rw-lock should be taken prior to calling this function.
  430. */
  431. static size_t flush_memchunk_cache(uint32_t node, size_t reclaim)
  432. {
  433. struct _starpu_mem_chunk *mc, *next_mc;
  434. size_t freed = 0;
  435. for (mc = _starpu_mem_chunk_list_begin(memchunk_cache[node]);
  436. mc != _starpu_mem_chunk_list_end(memchunk_cache[node]);
  437. mc = next_mc)
  438. {
  439. next_mc = _starpu_mem_chunk_list_next(mc);
  440. freed += free_memory_on_node(mc, node);
  441. _starpu_mem_chunk_list_erase(memchunk_cache[node], mc);
  442. free(mc->chunk_interface);
  443. _starpu_mem_chunk_delete(mc);
  444. if (reclaim && freed>reclaim)
  445. break;
  446. }
  447. return freed;
  448. }
  449. /*
  450. * Try to free the buffers currently in use on the memory node. If the force
  451. * flag is set, the memory is freed regardless of coherency concerns (this
  452. * should only be used at the termination of StarPU for instance). The
  453. * mc_rwlock[node] rw-lock should be taken prior to calling this function.
  454. */
  455. static size_t free_potentially_in_use_mc(uint32_t node, unsigned force, size_t reclaim)
  456. {
  457. size_t freed = 0;
  458. struct _starpu_mem_chunk *mc, *next_mc;
  459. for (mc = _starpu_mem_chunk_list_begin(mc_list[node]);
  460. mc != _starpu_mem_chunk_list_end(mc_list[node]);
  461. mc = next_mc)
  462. {
  463. /* there is a risk that the memory chunk is freed
  464. before next iteration starts: so we compute the next
  465. element of the list now */
  466. next_mc = _starpu_mem_chunk_list_next(mc);
  467. if (!force)
  468. {
  469. freed += try_to_free_mem_chunk(mc, node);
  470. #if 1
  471. if (reclaim && freed > reclaim)
  472. break;
  473. #endif
  474. }
  475. else
  476. {
  477. /* We must free the memory now: note that data
  478. * coherency is not maintained in that case ! */
  479. freed += do_free_mem_chunk(mc, node);
  480. }
  481. }
  482. return freed;
  483. }
  484. static size_t reclaim_memory_generic(uint32_t node, unsigned force, size_t reclaim)
  485. {
  486. size_t freed = 0;
  487. _STARPU_PTHREAD_RWLOCK_WRLOCK(&mc_rwlock[node]);
  488. starpu_lru(node);
  489. /* remove all buffers for which there was a removal request */
  490. freed += flush_memchunk_cache(node, reclaim);
  491. /* try to free all allocated data potentially in use */
  492. if (reclaim && freed<reclaim)
  493. freed += free_potentially_in_use_mc(node, force, reclaim);
  494. _STARPU_PTHREAD_RWLOCK_UNLOCK(&mc_rwlock[node]);
  495. return freed;
  496. }
  497. /*
  498. * This function frees all the memory that was implicitely allocated by StarPU
  499. * (for the data replicates). This is not ensuring data coherency, and should
  500. * only be called while StarPU is getting shut down.
  501. */
  502. size_t _starpu_free_all_automatically_allocated_buffers(uint32_t node)
  503. {
  504. return reclaim_memory_generic(node, 1, 0);
  505. }
  506. static struct _starpu_mem_chunk *_starpu_memchunk_init(struct _starpu_data_replicate *replicate, size_t interface_size, unsigned automatically_allocated)
  507. {
  508. struct _starpu_mem_chunk *mc = _starpu_mem_chunk_new();
  509. starpu_data_handle_t handle = replicate->handle;
  510. STARPU_ASSERT(handle);
  511. STARPU_ASSERT(handle->ops);
  512. mc->data = handle;
  513. mc->footprint = _starpu_compute_data_footprint(handle);
  514. mc->ops = handle->ops;
  515. mc->data_was_deleted = 0;
  516. mc->automatically_allocated = automatically_allocated;
  517. mc->relaxed_coherency = replicate->relaxed_coherency;
  518. mc->replicate = replicate;
  519. mc->replicate->mc = mc;
  520. /* Save a copy of the interface */
  521. mc->chunk_interface = malloc(interface_size);
  522. STARPU_ASSERT(mc->chunk_interface);
  523. memcpy(mc->chunk_interface, replicate->data_interface, interface_size);
  524. return mc;
  525. }
  526. static void register_mem_chunk(struct _starpu_data_replicate *replicate, unsigned automatically_allocated)
  527. {
  528. unsigned dst_node = replicate->memory_node;
  529. struct _starpu_mem_chunk *mc;
  530. /* the interface was already filled by ops->allocate_data_on_node */
  531. size_t interface_size = replicate->handle->ops->interface_size;
  532. /* Put this memchunk in the list of memchunk in use */
  533. mc = _starpu_memchunk_init(replicate, interface_size, automatically_allocated);
  534. _STARPU_PTHREAD_RWLOCK_WRLOCK(&mc_rwlock[dst_node]);
  535. _starpu_mem_chunk_list_push_back(mc_list[dst_node], mc);
  536. _STARPU_PTHREAD_RWLOCK_UNLOCK(&mc_rwlock[dst_node]);
  537. }
  538. /* This function is called when the handle is destroyed (eg. when calling
  539. * unregister or unpartition). It puts all the memchunks that refer to the
  540. * specified handle into the cache.
  541. * handle_deleted specifies whether the handle is deleted or not (and thus we
  542. * need to update it)
  543. */
  544. void _starpu_request_mem_chunk_removal(starpu_data_handle_t handle, unsigned node, int handle_deleted)
  545. {
  546. _STARPU_PTHREAD_RWLOCK_WRLOCK(&mc_rwlock[node]);
  547. size_t size = _starpu_data_get_size(handle);
  548. /* iterate over the list of memory chunks and remove the entry */
  549. struct _starpu_mem_chunk *mc, *next_mc;
  550. for (mc = _starpu_mem_chunk_list_begin(mc_list[node]);
  551. mc != _starpu_mem_chunk_list_end(mc_list[node]);
  552. mc = next_mc)
  553. {
  554. next_mc = _starpu_mem_chunk_list_next(mc);
  555. if (mc->data == handle)
  556. {
  557. /* we found the data */
  558. mc->size = size;
  559. mc->data_was_deleted = handle_deleted;
  560. /* remove it from the main list */
  561. _starpu_mem_chunk_list_erase(mc_list[node], mc);
  562. /* We would never flush the node 0 cache, unless
  563. * malloc() returns NULL, which is very unlikely... */
  564. /* This is particularly important when
  565. * STARPU_USE_ALLOCATION_CACHE is not enabled, as we
  566. * wouldn't even re-use these allocations! */
  567. if (starpu_node_get_kind(node) == STARPU_CPU_RAM) {
  568. free_memory_on_node(mc, node);
  569. free(mc->chunk_interface);
  570. _starpu_mem_chunk_delete(mc);
  571. } else
  572. /* put it in the list of buffers to be removed */
  573. _starpu_mem_chunk_list_push_front(memchunk_cache[node], mc);
  574. /* Note that we do not stop here because there can be
  575. * multiple replicates associated to the same handle on
  576. * the same memory node. */
  577. }
  578. }
  579. /* there was no corresponding buffer ... */
  580. _STARPU_PTHREAD_RWLOCK_UNLOCK(&mc_rwlock[node]);
  581. }
  582. static size_t _starpu_get_global_mem_size(int dst_node)
  583. {
  584. enum starpu_node_kind kind = starpu_node_get_kind(dst_node);
  585. size_t global_mem_size;
  586. switch(kind)
  587. {
  588. case STARPU_CPU_RAM:
  589. {
  590. /* We should probably never get here : if there is no
  591. * space left in RAM, the operating system should swap
  592. * to disk for us. */
  593. STARPU_ABORT();
  594. }
  595. #ifdef STARPU_USE_CUDA
  596. case STARPU_CUDA_RAM:
  597. {
  598. int devid = _starpu_memory_node_to_devid(dst_node);
  599. global_mem_size = starpu_cuda_get_global_mem_size(devid);
  600. break;
  601. }
  602. #endif
  603. #ifdef STARPU_USE_OPENCL
  604. case STARPU_OPENCL_RAM:
  605. {
  606. int devid = _starpu_memory_node_to_devid(dst_node);
  607. global_mem_size = starpu_opencl_get_global_mem_size(devid);
  608. break;
  609. }
  610. #endif
  611. default:
  612. STARPU_ABORT();
  613. }
  614. return global_mem_size;
  615. }
  616. #ifdef STARPU_SIMGRID
  617. static _starpu_pthread_mutex_t cuda_alloc_mutex = _STARPU_PTHREAD_MUTEX_INITIALIZER;
  618. static _starpu_pthread_mutex_t opencl_alloc_mutex = _STARPU_PTHREAD_MUTEX_INITIALIZER;
  619. #endif
  620. uintptr_t
  621. starpu_allocate_buffer_on_node(uint32_t dst_node, size_t size)
  622. {
  623. uintptr_t addr = 0;
  624. #ifdef STARPU_USE_CUDA
  625. cudaError_t status;
  626. #endif
  627. #ifdef STARPU_DEVEL
  628. #warning TODO: we need to use starpu_malloc
  629. #endif
  630. switch(starpu_node_get_kind(dst_node))
  631. {
  632. case STARPU_CPU_RAM:
  633. addr = (uintptr_t)malloc(size);
  634. break;
  635. #ifdef STARPU_USE_CUDA
  636. case STARPU_CUDA_RAM:
  637. #ifdef STARPU_SIMGRID
  638. #ifdef STARPU_DEVEL
  639. #warning TODO: record used memory, using a simgrid property to know the available memory
  640. #endif
  641. /* Sleep 10µs for the allocation */
  642. _STARPU_PTHREAD_MUTEX_LOCK(&cuda_alloc_mutex);
  643. MSG_process_sleep(0.000010);
  644. addr = 1;
  645. status = cudaSuccess;
  646. _STARPU_PTHREAD_MUTEX_UNLOCK(&cuda_alloc_mutex);
  647. #else
  648. status = cudaMalloc((void **)&addr, size);
  649. #endif
  650. if (!addr || (status != cudaSuccess))
  651. {
  652. if (STARPU_UNLIKELY(status != cudaErrorMemoryAllocation))
  653. STARPU_CUDA_REPORT_ERROR(status);
  654. addr = 0;
  655. }
  656. break;
  657. #endif
  658. #ifdef STARPU_USE_OPENCL
  659. case STARPU_OPENCL_RAM:
  660. {
  661. int ret;
  662. cl_mem ptr;
  663. #ifdef STARPU_SIMGRID
  664. /* Sleep 10µs for the allocation */
  665. _STARPU_PTHREAD_MUTEX_LOCK(&opencl_alloc_mutex);
  666. MSG_process_sleep(0.000010);
  667. ptr = (cl_mem) 1;
  668. ret = CL_SUCCESS;
  669. _STARPU_PTHREAD_MUTEX_UNLOCK(&opencl_alloc_mutex);
  670. #else
  671. ret = starpu_opencl_allocate_memory(&ptr, size, CL_MEM_READ_WRITE);
  672. #endif
  673. if (ret)
  674. addr = 0;
  675. else
  676. addr = (uintptr_t)ptr;
  677. break;
  678. }
  679. #endif
  680. default:
  681. STARPU_ABORT();
  682. }
  683. return addr;
  684. }
  685. void
  686. starpu_free_buffer_on_node(uint32_t dst_node, uintptr_t addr, size_t size)
  687. {
  688. enum starpu_node_kind kind = starpu_node_get_kind(dst_node);
  689. switch(kind)
  690. {
  691. #ifdef STARPU_DEVEL
  692. #warning TODO we need to call starpu_free
  693. #endif
  694. case STARPU_CPU_RAM:
  695. free((void*)addr);
  696. break;
  697. #ifdef STARPU_USE_CUDA
  698. case STARPU_CUDA_RAM:
  699. {
  700. cudaError_t err;
  701. #ifdef STARPU_SIMGRID
  702. _STARPU_PTHREAD_MUTEX_LOCK(&cuda_alloc_mutex);
  703. /* Sleep 10µs for the free */
  704. MSG_process_sleep(0.000010);
  705. err = cudaSuccess;
  706. _STARPU_PTHREAD_MUTEX_UNLOCK(&cuda_alloc_mutex);
  707. #else
  708. err = cudaFree((void*)addr);
  709. #endif
  710. if (STARPU_UNLIKELY(err != cudaSuccess))
  711. STARPU_CUDA_REPORT_ERROR(err);
  712. break;
  713. }
  714. #endif
  715. #ifdef STARPU_USE_OPENCL
  716. case STARPU_OPENCL_RAM:
  717. {
  718. cl_int err;
  719. #ifdef STARPU_SIMGRID
  720. _STARPU_PTHREAD_MUTEX_LOCK(&opencl_alloc_mutex);
  721. /* Sleep 10µs for the free */
  722. MSG_process_sleep(0.000010);
  723. err = CL_SUCCESS;
  724. _STARPU_PTHREAD_MUTEX_UNLOCK(&opencl_alloc_mutex);
  725. #else
  726. err = clReleaseMemObject((void*)addr);
  727. #endif
  728. if (STARPU_UNLIKELY(err != CL_SUCCESS))
  729. STARPU_OPENCL_REPORT_ERROR(err);
  730. break;
  731. }
  732. #endif
  733. default:
  734. STARPU_ABORT();
  735. }
  736. }
  737. /*
  738. * In order to allocate a piece of data, we try to reuse existing buffers if
  739. * its possible.
  740. * 1 - we try to reuse a memchunk that is explicitely unused.
  741. * 2 - we go through the list of memory chunks and find one that is not
  742. * referenced and that has the same footprint to reuse it.
  743. * 3 - we call the usual driver's alloc method
  744. * 4 - we go through the list of memory chunks and release those that are
  745. * not referenced (or part of those).
  746. *
  747. */
  748. static ssize_t _starpu_allocate_interface(starpu_data_handle_t handle, struct _starpu_data_replicate *replicate, uint32_t dst_node, unsigned is_prefetch)
  749. {
  750. unsigned attempts = 0;
  751. ssize_t allocated_memory;
  752. _starpu_spin_checklocked(&handle->header_lock);
  753. _starpu_data_allocation_inc_stats(dst_node);
  754. #ifdef STARPU_USE_ALLOCATION_CACHE
  755. /* perhaps we can directly reuse a buffer in the free-list */
  756. uint32_t footprint = _starpu_compute_data_footprint(handle);
  757. _STARPU_TRACE_START_ALLOC_REUSE(dst_node);
  758. _STARPU_PTHREAD_RWLOCK_WRLOCK(&mc_rwlock[dst_node]);
  759. if (try_to_find_reusable_mem_chunk(dst_node, handle, replicate, footprint))
  760. {
  761. _STARPU_PTHREAD_RWLOCK_UNLOCK(&mc_rwlock[dst_node]);
  762. _starpu_allocation_cache_hit(dst_node);
  763. ssize_t data_size = _starpu_data_get_size(handle);
  764. return data_size;
  765. }
  766. _STARPU_PTHREAD_RWLOCK_UNLOCK(&mc_rwlock[dst_node]);
  767. _STARPU_TRACE_END_ALLOC_REUSE(dst_node);
  768. #endif
  769. do
  770. {
  771. STARPU_ASSERT(handle->ops);
  772. STARPU_ASSERT(handle->ops->allocate_data_on_node);
  773. _STARPU_TRACE_START_ALLOC(dst_node);
  774. STARPU_ASSERT(replicate->data_interface);
  775. #if defined(STARPU_USE_CUDA) && defined(HAVE_CUDA_MEMCPY_PEER) && !defined(STARPU_SIMGRID)
  776. if (starpu_node_get_kind(dst_node) == STARPU_CUDA_RAM)
  777. {
  778. /* To facilitate the design of interface, we set the
  779. * proper CUDA device in case it is needed. This avoids
  780. * having to set it again in the malloc method of each
  781. * interface. */
  782. starpu_cuda_set_device(_starpu_memory_node_to_devid(dst_node));
  783. }
  784. #endif
  785. allocated_memory = handle->ops->allocate_data_on_node(replicate->data_interface, dst_node);
  786. _STARPU_TRACE_END_ALLOC(dst_node);
  787. if (allocated_memory == -ENOMEM)
  788. {
  789. size_t reclaim = 0.25*_starpu_get_global_mem_size(dst_node);
  790. size_t handle_size = handle->ops->get_size(handle);
  791. if (starpu_memstrategy_data_size_coefficient*handle_size > reclaim)
  792. reclaim = starpu_memstrategy_data_size_coefficient*handle_size;
  793. /* Take temporary reference on the replicate */
  794. replicate->refcnt++;
  795. handle->busy_count++;
  796. _starpu_spin_unlock(&handle->header_lock);
  797. _STARPU_TRACE_START_MEMRECLAIM(dst_node);
  798. if (is_prefetch) {
  799. _STARPU_PTHREAD_RWLOCK_WRLOCK(&mc_rwlock[dst_node]);
  800. flush_memchunk_cache(dst_node, reclaim);
  801. _STARPU_PTHREAD_RWLOCK_UNLOCK(&mc_rwlock[dst_node]);
  802. } else
  803. reclaim_memory_generic(dst_node, 0, reclaim);
  804. _STARPU_TRACE_END_MEMRECLAIM(dst_node);
  805. while (_starpu_spin_trylock(&handle->header_lock))
  806. _starpu_datawizard_progress(_starpu_get_local_memory_node(), 0);
  807. replicate->refcnt--;
  808. STARPU_ASSERT(replicate->refcnt >= 0);
  809. STARPU_ASSERT(handle->busy_count > 0);
  810. handle->busy_count--;
  811. _starpu_data_check_not_busy(handle);
  812. }
  813. }
  814. while((allocated_memory == -ENOMEM) && attempts++ < 2);
  815. return allocated_memory;
  816. }
  817. int _starpu_allocate_memory_on_node(starpu_data_handle_t handle, struct _starpu_data_replicate *replicate, unsigned is_prefetch)
  818. {
  819. ssize_t allocated_memory;
  820. unsigned dst_node = replicate->memory_node;
  821. STARPU_ASSERT(handle);
  822. /* A buffer is already allocated on the node */
  823. if (replicate->allocated)
  824. return 0;
  825. STARPU_ASSERT(replicate->data_interface);
  826. allocated_memory = _starpu_allocate_interface(handle, replicate, dst_node, is_prefetch);
  827. /* perhaps we could really not handle that capacity misses */
  828. if (allocated_memory == -ENOMEM)
  829. return -ENOMEM;
  830. register_mem_chunk(replicate, 1);
  831. replicate->allocated = 1;
  832. replicate->automatically_allocated = 1;
  833. if (dst_node == 0)
  834. {
  835. void *ptr = starpu_handle_to_pointer(handle, 0);
  836. if (ptr != NULL)
  837. {
  838. _starpu_data_register_ram_pointer(handle, ptr);
  839. }
  840. }
  841. return 0;
  842. }
  843. unsigned starpu_data_test_if_allocated_on_node(starpu_data_handle_t handle, uint32_t memory_node)
  844. {
  845. return handle->per_node[memory_node].allocated;
  846. }
  847. void _starpu_memchunk_recently_used(struct _starpu_mem_chunk *mc, unsigned node)
  848. {
  849. _STARPU_PTHREAD_RWLOCK_WRLOCK(&lru_rwlock[node]);
  850. struct _starpu_mem_chunk_lru *mc_lru=_starpu_mem_chunk_lru_new();
  851. mc_lru->mc=mc;
  852. _starpu_mem_chunk_lru_list_push_front(starpu_lru_list[node],mc_lru);
  853. _STARPU_PTHREAD_RWLOCK_UNLOCK(&lru_rwlock[node]);
  854. }
  855. /* The mc_rwlock[node] rw-lock should be taken prior to calling this function.*/
  856. static void _starpu_memchunk_recently_used_move(struct _starpu_mem_chunk *mc, unsigned node)
  857. {
  858. /* XXX Sometimes the memchunk is not in the list... */
  859. struct _starpu_mem_chunk *mc_iter;
  860. for (mc_iter = _starpu_mem_chunk_list_begin(mc_list[node]);
  861. mc_iter != _starpu_mem_chunk_list_end(mc_list[node]);
  862. mc_iter = _starpu_mem_chunk_list_next(mc_iter) )
  863. {
  864. if (mc_iter==mc)
  865. {
  866. _starpu_mem_chunk_list_erase(mc_list[node], mc);
  867. _starpu_mem_chunk_list_push_back(mc_list[node], mc);
  868. return;
  869. }
  870. }
  871. }
  872. static void starpu_lru(unsigned node)
  873. {
  874. _STARPU_PTHREAD_RWLOCK_WRLOCK(&lru_rwlock[node]);
  875. while (!_starpu_mem_chunk_lru_list_empty(starpu_lru_list[node]))
  876. {
  877. struct _starpu_mem_chunk_lru *mc_lru=_starpu_mem_chunk_lru_list_front(starpu_lru_list[node]);
  878. _starpu_memchunk_recently_used_move(mc_lru->mc, node);
  879. _starpu_mem_chunk_lru_list_erase(starpu_lru_list[node], mc_lru);
  880. _starpu_mem_chunk_lru_delete(mc_lru);
  881. }
  882. _STARPU_PTHREAD_RWLOCK_UNLOCK(&lru_rwlock[node]);
  883. }
  884. #ifdef STARPU_MEMORY_STATS
  885. void _starpu_memory_display_stats_by_node(int node)
  886. {
  887. _STARPU_PTHREAD_RWLOCK_WRLOCK(&mc_rwlock[node]);
  888. if (!_starpu_mem_chunk_list_empty(mc_list[node]))
  889. {
  890. struct _starpu_mem_chunk *mc;
  891. fprintf(stderr, "#-------\n");
  892. fprintf(stderr, "Data on Node #%d\n",node);
  893. for (mc = _starpu_mem_chunk_list_begin(mc_list[node]);
  894. mc != _starpu_mem_chunk_list_end(mc_list[node]);
  895. mc = _starpu_mem_chunk_list_next(mc))
  896. {
  897. _starpu_memory_display_handle_stats(mc->data);
  898. }
  899. }
  900. _STARPU_PTHREAD_RWLOCK_UNLOCK(&mc_rwlock[node]);
  901. }
  902. #endif
  903. void starpu_memory_display_stats(void)
  904. {
  905. #ifdef STARPU_MEMORY_STATS
  906. unsigned node;
  907. fprintf(stderr, "\n#---------------------\n");
  908. fprintf(stderr, "Memory stats :\n");
  909. for (node = 0; node < STARPU_MAXNODES; node++)
  910. {
  911. _starpu_memory_display_stats_by_node(node);
  912. }
  913. fprintf(stderr, "\n#---------------------\n");
  914. #endif
  915. }