memalloc.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-2011 Université de Bordeaux 1
  4. * Copyright (C) 2010, 2011 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 pthread_rwlock_t mc_rwlock[STARPU_MAXNODES];
  23. /* This per-node RW-locks protect lru_list */
  24. static pthread_rwlock_t lru_rwlock[STARPU_MAXNODES];
  25. /* Last Recently used memory chunkgs */
  26. static starpu_mem_chunk_lru_list_t starpu_lru_list[STARPU_MAXNODES];
  27. /* Potentially in use memory chunks */
  28. static starpu_mem_chunk_list_t mc_list[STARPU_MAXNODES];
  29. /* Explicitly caches memory chunks that can be reused */
  30. static starpu_mem_chunk_list_t 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. PTHREAD_RWLOCK_INIT(&mc_rwlock[i], NULL);
  40. 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 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. /* lock all sub-subtrees children */
  69. unsigned child;
  70. for (child = 0; child < handle->nchildren; child++)
  71. {
  72. lock_all_subtree(&handle->children[child]);
  73. }
  74. }
  75. }
  76. static void unlock_all_subtree(starpu_data_handle handle)
  77. {
  78. if (handle->nchildren == 0)
  79. {
  80. /* this is a leaf */
  81. _starpu_spin_unlock(&handle->header_lock);
  82. }
  83. else {
  84. /* lock all sub-subtrees children
  85. * Note that this is done in the reverse order of the
  86. * lock_all_subtree so that we avoid deadlock */
  87. unsigned i;
  88. for (i =0; i < handle->nchildren; i++)
  89. {
  90. unsigned child = handle->nchildren - 1 - i;
  91. unlock_all_subtree(&handle->children[child]);
  92. }
  93. }
  94. }
  95. static unsigned may_free_subtree(starpu_data_handle handle, unsigned node)
  96. {
  97. /* we only free if no one refers to the leaf */
  98. uint32_t refcnt = _starpu_get_data_refcnt(handle, node);
  99. if (refcnt)
  100. return 0;
  101. if (!handle->nchildren)
  102. return 1;
  103. /* look into all sub-subtrees children */
  104. unsigned child;
  105. for (child = 0; child < handle->nchildren; child++)
  106. {
  107. unsigned res;
  108. res = may_free_subtree(&handle->children[child], node);
  109. if (!res) return 0;
  110. }
  111. /* no problem was found */
  112. return 1;
  113. }
  114. static void transfer_subtree_to_node(starpu_data_handle handle, unsigned src_node,
  115. unsigned dst_node)
  116. {
  117. unsigned i;
  118. unsigned last = 0;
  119. unsigned cnt;
  120. int ret;
  121. if (handle->nchildren == 0)
  122. {
  123. struct starpu_data_replicate_s *src_replicate = &handle->per_node[src_node];
  124. struct starpu_data_replicate_s *dst_replicate = &handle->per_node[dst_node];
  125. /* this is a leaf */
  126. switch(src_replicate->state) {
  127. case STARPU_OWNER:
  128. /* the local node has the only copy */
  129. /* the owner is now the destination_node */
  130. src_replicate->state = STARPU_INVALID;
  131. dst_replicate->state = STARPU_OWNER;
  132. #ifdef STARPU_DEVEL
  133. #warning we should use requests during memory reclaim
  134. #endif
  135. /* TODO use request !! */
  136. src_replicate->refcnt++;
  137. dst_replicate->refcnt++;
  138. ret = _starpu_driver_copy_data_1_to_1(handle, src_replicate, dst_replicate, 0, NULL, 1);
  139. STARPU_ASSERT(ret == 0);
  140. src_replicate->refcnt--;
  141. dst_replicate->refcnt--;
  142. break;
  143. case STARPU_SHARED:
  144. /* some other node may have the copy */
  145. src_replicate->state = STARPU_INVALID;
  146. /* count the number of copies */
  147. cnt = 0;
  148. for (i = 0; i < STARPU_MAXNODES; i++)
  149. {
  150. if (handle->per_node[i].state == STARPU_SHARED) {
  151. cnt++;
  152. last = i;
  153. }
  154. }
  155. if (cnt == 1)
  156. handle->per_node[last].state = STARPU_OWNER;
  157. break;
  158. case STARPU_INVALID:
  159. /* nothing to be done */
  160. break;
  161. default:
  162. STARPU_ABORT();
  163. break;
  164. }
  165. }
  166. else {
  167. /* lock all sub-subtrees children */
  168. unsigned child;
  169. for (child = 0; child < handle->nchildren; child++)
  170. {
  171. transfer_subtree_to_node(&handle->children[child],
  172. src_node, dst_node);
  173. }
  174. }
  175. }
  176. static size_t free_memory_on_node(starpu_mem_chunk_t mc, uint32_t node)
  177. {
  178. size_t freed = 0;
  179. STARPU_ASSERT(mc->ops);
  180. STARPU_ASSERT(mc->ops->free_data_on_node);
  181. starpu_data_handle handle = mc->data;
  182. /* Does this memory chunk refers to a handle that does not exist
  183. * anymore ? */
  184. unsigned data_was_deleted = mc->data_was_deleted;
  185. struct starpu_data_replicate_s *replicate = mc->replicate;
  186. // while (_starpu_spin_trylock(&handle->header_lock))
  187. // _starpu_datawizard_progress(_starpu_get_local_memory_node());
  188. #ifdef STARPU_DEVEL
  189. #warning can we block here ?
  190. #endif
  191. // _starpu_spin_lock(&handle->header_lock);
  192. if (mc->automatically_allocated &&
  193. (!handle || data_was_deleted || replicate->refcnt == 0))
  194. {
  195. if (handle && !data_was_deleted)
  196. STARPU_ASSERT(replicate->allocated);
  197. #if defined(STARPU_USE_CUDA) && defined(HAVE_CUDA_MEMCPY_PEER)
  198. if (_starpu_get_node_kind(node) == STARPU_CUDA_RAM)
  199. {
  200. /* To facilitate the design of interface, we set the
  201. * proper CUDA device in case it is needed. This avoids
  202. * having to set it again in the free method of each
  203. * interface. */
  204. cudaError_t err = cudaSetDevice(starpu_memory_node_to_devid(node));
  205. STARPU_ASSERT(err == cudaSuccess);
  206. }
  207. #endif
  208. mc->ops->free_data_on_node(mc->chunk_interface, node);
  209. if (handle && !data_was_deleted)
  210. {
  211. replicate->allocated = 0;
  212. /* XXX why do we need that ? */
  213. replicate->automatically_allocated = 0;
  214. }
  215. freed = mc->size;
  216. if (handle && !data_was_deleted)
  217. STARPU_ASSERT(replicate->refcnt == 0);
  218. }
  219. // _starpu_spin_unlock(&handle->header_lock);
  220. return freed;
  221. }
  222. static size_t do_free_mem_chunk(starpu_mem_chunk_t mc, unsigned node)
  223. {
  224. size_t size;
  225. mc->replicate->mc=NULL;
  226. /* free the actual buffer */
  227. size = free_memory_on_node(mc, node);
  228. /* remove the mem_chunk from the list */
  229. starpu_mem_chunk_list_erase(mc_list[node], mc);
  230. free(mc->chunk_interface);
  231. starpu_mem_chunk_delete(mc);
  232. return size;
  233. }
  234. /* This function is called for memory chunks that are possibly in used (ie. not
  235. * in the cache). They should therefore still be associated to a handle. */
  236. static size_t try_to_free_mem_chunk(starpu_mem_chunk_t mc, unsigned node)
  237. {
  238. size_t freed = 0;
  239. starpu_data_handle handle;
  240. handle = mc->data;
  241. STARPU_ASSERT(handle);
  242. /* Either it's a "relaxed coherency" memchunk, or it's a memchunk that
  243. * could be used with filters. */
  244. if (mc->relaxed_coherency)
  245. {
  246. STARPU_ASSERT(mc->replicate);
  247. while (_starpu_spin_trylock(&handle->header_lock))
  248. _starpu_datawizard_progress(_starpu_get_local_memory_node(), 0);
  249. if (mc->replicate->refcnt == 0)
  250. {
  251. /* Note taht there is no need to transfer any data or
  252. * to update the status in terms of MSI protocol
  253. * because this memchunk is associated to a replicate
  254. * in "relaxed coherency" mode. */
  255. freed = do_free_mem_chunk(mc, node);
  256. }
  257. _starpu_spin_unlock(&handle->header_lock);
  258. }
  259. else {
  260. /* try to lock all the leafs of the subtree */
  261. lock_all_subtree(handle);
  262. /* check if they are all "free" */
  263. if (may_free_subtree(handle, node))
  264. {
  265. STARPU_ASSERT(handle->per_node[node].refcnt == 0);
  266. #ifdef STARPU_MEMORY_STATUS
  267. if (handle->per_node[node].state == STARPU_OWNER)
  268. _starpu_handle_stats_invalidated(handle, node);
  269. /* else XXX Considering only owner to invalidate */
  270. #endif
  271. /* in case there was nobody using that buffer, throw it
  272. * away after writing it back to main memory */
  273. transfer_subtree_to_node(handle, node, 0);
  274. #ifdef STARPU_MEMORY_STATUS
  275. _starpu_handle_stats_loaded_owner(handle, 0);
  276. #endif
  277. STARPU_ASSERT(handle->per_node[node].refcnt == 0);
  278. /* now the actual buffer may be freed */
  279. freed = do_free_mem_chunk(mc, node);
  280. }
  281. /* unlock the leafs */
  282. unlock_all_subtree(handle);
  283. }
  284. return freed;
  285. }
  286. #ifdef STARPU_USE_ALLOCATION_CACHE
  287. /* We assume that mc_rwlock[node] is taken. is_already_in_mc_list indicates
  288. * that the mc is already in the list of buffers that are possibly used, and
  289. * therefore not in the cache. */
  290. static void reuse_mem_chunk(unsigned node, struct starpu_data_replicate_s *new_replicate, starpu_mem_chunk_t mc, unsigned is_already_in_mc_list)
  291. {
  292. starpu_data_handle old_data;
  293. old_data = mc->data;
  294. /* we found an appropriate mem chunk: so we get it out
  295. * of the "to free" list, and reassign it to the new
  296. * piece of data */
  297. if (!is_already_in_mc_list)
  298. {
  299. starpu_mem_chunk_list_erase(memchunk_cache[node], mc);
  300. }
  301. struct starpu_data_replicate_s *old_replicate = mc->replicate;
  302. old_replicate->allocated = 0;
  303. old_replicate->automatically_allocated = 0;
  304. old_replicate->initialized = 0;
  305. new_replicate->allocated = 1;
  306. new_replicate->automatically_allocated = 1;
  307. new_replicate->initialized = 0;
  308. STARPU_ASSERT(new_replicate->chunk_interface);
  309. STARPU_ASSERT(mc->chunk_interface);
  310. memcpy(new_replicate->chunk_interface, mc->chunk_interface, old_replicate->ops->interface_size);
  311. mc->data = new_replicate->handle;
  312. mc->data_was_deleted = 0;
  313. /* mc->ops, mc->size, mc->footprint and mc->interface should be
  314. * unchanged ! */
  315. /* reinsert the mem chunk in the list of active memory chunks */
  316. if (!is_already_in_mc_list)
  317. {
  318. starpu_mem_chunk_list_push_front(mc_list[node], mc);
  319. }
  320. }
  321. static unsigned try_to_reuse_mem_chunk(starpu_mem_chunk_t mc, unsigned node, starpu_data_handle new_data, unsigned is_already_in_mc_list)
  322. {
  323. unsigned success = 0;
  324. starpu_data_handle old_data;
  325. old_data = mc->data;
  326. STARPU_ASSERT(old_data);
  327. /* try to lock all the leafs of the subtree */
  328. lock_all_subtree(old_data);
  329. /* check if they are all "free" */
  330. if (may_free_subtree(old_data, node))
  331. {
  332. success = 1;
  333. /* in case there was nobody using that buffer, throw it
  334. * away after writing it back to main memory */
  335. transfer_subtree_to_node(old_data, node, 0);
  336. /* now replace the previous data */
  337. reuse_mem_chunk(node, new_data, mc, is_already_in_mc_list);
  338. }
  339. /* unlock the leafs */
  340. unlock_all_subtree(old_data);
  341. return success;
  342. }
  343. static int _starpu_data_interface_compare(void *data_interface_a, struct starpu_data_interface_ops_t *ops_a,
  344. void *data_interface_b, struct starpu_data_interface_ops_t *ops_b)
  345. {
  346. if (ops_a->interfaceid != ops_b->interfaceid)
  347. return -1;
  348. int ret = ops_a->compare(interface_a, interface_b);
  349. return ret;
  350. }
  351. /* This function must be called with mc_rwlock[node] taken in write mode */
  352. static starpu_mem_chunk_t _starpu_memchunk_cache_lookup_locked(uint32_t node, starpu_data_handle handle)
  353. {
  354. uint32_t footprint = _starpu_compute_data_footprint(handle);
  355. /* go through all buffers in the cache */
  356. starpu_mem_chunk_t mc;
  357. for (mc = starpu_mem_chunk_list_begin(memchunk_cache[node]);
  358. mc != starpu_mem_chunk_list_end(memchunk_cache[node]);
  359. mc = starpu_mem_chunk_list_next(mc))
  360. {
  361. if (mc->footprint == footprint)
  362. {
  363. /* Is that a false hit ? (this is _very_ unlikely) */
  364. if (_starpu_data_interface_compare(handle->per_node[node].interface, handle->ops, mc->interface, mc->ops))
  365. continue;
  366. /* Cache hit */
  367. /* Remove from the cache */
  368. starpu_mem_chunk_list_erase(memchunk_cache[node], mc);
  369. return mc;
  370. }
  371. }
  372. /* This is a cache miss */
  373. return NULL;
  374. }
  375. /* this function looks for a memory chunk that matches a given footprint in the
  376. * list of mem chunk that need to be freed. This function must be called with
  377. * mc_rwlock[node] taken in write mode. */
  378. static unsigned try_to_find_reusable_mem_chunk(unsigned node, starpu_data_handle data, uint32_t footprint)
  379. {
  380. starpu_mem_chunk_t mc, next_mc;
  381. /* go through all buffers in the cache */
  382. mc = _starpu_memchunk_cache_lookup_locked(node, handle);
  383. if (mc)
  384. {
  385. /* We found an entry in the cache so we can reuse it */
  386. reuse_mem_chunk(node, data, mc, 0);
  387. return 1;
  388. }
  389. /* now look for some non essential data in the active list */
  390. for (mc = starpu_mem_chunk_list_begin(mc_list[node]);
  391. mc != starpu_mem_chunk_list_end(mc_list[node]);
  392. mc = next_mc)
  393. {
  394. /* there is a risk that the memory chunk is freed before next
  395. * iteration starts: so we compute the next element of the list
  396. * now */
  397. next_mc = starpu_mem_chunk_list_next(mc);
  398. if (mc->data->is_not_important && (mc->footprint == footprint))
  399. {
  400. // fprintf(stderr, "found a candidate ...\n");
  401. if (try_to_reuse_mem_chunk(mc, node, data, 1))
  402. return 1;
  403. }
  404. }
  405. return 0;
  406. }
  407. #endif
  408. /*
  409. * Free the memory chuncks that are explicitely tagged to be freed. The
  410. * mc_rwlock[node] rw-lock should be taken prior to calling this function.
  411. */
  412. static size_t flush_memchunk_cache(uint32_t node, size_t reclaim)
  413. {
  414. starpu_mem_chunk_t mc, next_mc;
  415. size_t freed = 0;
  416. for (mc = starpu_mem_chunk_list_begin(memchunk_cache[node]);
  417. mc != starpu_mem_chunk_list_end(memchunk_cache[node]);
  418. mc = next_mc)
  419. {
  420. next_mc = starpu_mem_chunk_list_next(mc);
  421. freed += free_memory_on_node(mc, node);
  422. starpu_mem_chunk_list_erase(memchunk_cache[node], mc);
  423. free(mc->chunk_interface);
  424. starpu_mem_chunk_delete(mc);
  425. if (reclaim && freed>reclaim)
  426. break;
  427. }
  428. return freed;
  429. }
  430. /*
  431. * Try to free the buffers currently in use on the memory node. If the force
  432. * flag is set, the memory is freed regardless of coherency concerns (this
  433. * should only be used at the termination of StarPU for instance). The
  434. * mc_rwlock[node] rw-lock should be taken prior to calling this function.
  435. */
  436. static size_t free_potentially_in_use_mc(uint32_t node, unsigned force, size_t reclaim)
  437. {
  438. size_t freed = 0;
  439. starpu_mem_chunk_t mc, next_mc;
  440. for (mc = starpu_mem_chunk_list_begin(mc_list[node]);
  441. mc != starpu_mem_chunk_list_end(mc_list[node]);
  442. mc = next_mc)
  443. {
  444. /* there is a risk that the memory chunk is freed
  445. before next iteration starts: so we compute the next
  446. element of the list now */
  447. next_mc = starpu_mem_chunk_list_next(mc);
  448. if (!force)
  449. {
  450. freed += try_to_free_mem_chunk(mc, node);
  451. #if 1
  452. if (reclaim && freed > reclaim)
  453. break;
  454. #endif
  455. }
  456. else {
  457. /* We must free the memory now: note that data
  458. * coherency is not maintained in that case ! */
  459. freed += do_free_mem_chunk(mc, node);
  460. }
  461. }
  462. return freed;
  463. }
  464. static size_t reclaim_memory_generic(uint32_t node, unsigned force, size_t reclaim)
  465. {
  466. size_t freed = 0;
  467. PTHREAD_RWLOCK_WRLOCK(&mc_rwlock[node]);
  468. starpu_lru(node);
  469. /* remove all buffers for which there was a removal request */
  470. freed += flush_memchunk_cache(node, reclaim);
  471. /* try to free all allocated data potentially in use */
  472. if (reclaim && freed<reclaim)
  473. freed += free_potentially_in_use_mc(node, force, reclaim);
  474. PTHREAD_RWLOCK_UNLOCK(&mc_rwlock[node]);
  475. return freed;
  476. }
  477. /*
  478. * This function frees all the memory that was implicitely allocated by StarPU
  479. * (for the data replicates). This is not ensuring data coherency, and should
  480. * only be called while StarPU is getting shut down.
  481. */
  482. size_t _starpu_free_all_automatically_allocated_buffers(uint32_t node)
  483. {
  484. return reclaim_memory_generic(node, 1, 0);
  485. }
  486. static starpu_mem_chunk_t _starpu_memchunk_init(struct starpu_data_replicate_s *replicate, size_t size, size_t interface_size, unsigned automatically_allocated)
  487. {
  488. starpu_mem_chunk_t mc = starpu_mem_chunk_new();
  489. starpu_data_handle handle = replicate->handle;
  490. STARPU_ASSERT(handle);
  491. STARPU_ASSERT(handle->ops);
  492. mc->data = handle;
  493. mc->size = size;
  494. mc->footprint = _starpu_compute_data_footprint(handle);
  495. mc->ops = handle->ops;
  496. mc->data_was_deleted = 0;
  497. mc->automatically_allocated = automatically_allocated;
  498. mc->relaxed_coherency = replicate->relaxed_coherency;
  499. mc->replicate = replicate;
  500. mc->replicate->mc = mc;
  501. /* Save a copy of the interface */
  502. mc->chunk_interface = malloc(interface_size);
  503. STARPU_ASSERT(mc->chunk_interface);
  504. memcpy(mc->chunk_interface, replicate->data_interface, interface_size);
  505. return mc;
  506. }
  507. static void register_mem_chunk(struct starpu_data_replicate_s *replicate, size_t size, unsigned automatically_allocated)
  508. {
  509. unsigned dst_node = replicate->memory_node;
  510. starpu_mem_chunk_t mc;
  511. /* the interface was already filled by ops->allocate_data_on_node */
  512. size_t interface_size = replicate->handle->ops->interface_size;
  513. /* Put this memchunk in the list of memchunk in use */
  514. mc = _starpu_memchunk_init(replicate, size, interface_size, automatically_allocated);
  515. PTHREAD_RWLOCK_WRLOCK(&mc_rwlock[dst_node]);
  516. starpu_mem_chunk_list_push_back(mc_list[dst_node], mc);
  517. PTHREAD_RWLOCK_UNLOCK(&mc_rwlock[dst_node]);
  518. }
  519. /* This function is called when the handle is destroyed (eg. when calling
  520. * unregister or unpartition). It puts all the memchunks that refer to the
  521. * specified handle into the cache. */
  522. void _starpu_request_mem_chunk_removal(starpu_data_handle handle, unsigned node)
  523. {
  524. PTHREAD_RWLOCK_WRLOCK(&mc_rwlock[node]);
  525. /* iterate over the list of memory chunks and remove the entry */
  526. starpu_mem_chunk_t mc, next_mc;
  527. for (mc = starpu_mem_chunk_list_begin(mc_list[node]);
  528. mc != starpu_mem_chunk_list_end(mc_list[node]);
  529. mc = next_mc)
  530. {
  531. next_mc = starpu_mem_chunk_list_next(mc);
  532. if (mc->data == handle) {
  533. /* we found the data */
  534. mc->data_was_deleted = 1;
  535. /* remove it from the main list */
  536. starpu_mem_chunk_list_erase(mc_list[node], mc);
  537. /* put it in the list of buffers to be removed */
  538. starpu_mem_chunk_list_push_front(memchunk_cache[node], mc);
  539. /* Note that we do not stop here because there can be
  540. * multiple replicates associated to the same handle on
  541. * the same memory node. */
  542. }
  543. }
  544. /* there was no corresponding buffer ... */
  545. PTHREAD_RWLOCK_UNLOCK(&mc_rwlock[node]);
  546. }
  547. static size_t _starpu_get_global_mem_size(int dst_node)
  548. {
  549. starpu_node_kind kind = _starpu_get_node_kind(dst_node);
  550. size_t global_mem_size;
  551. switch(kind)
  552. {
  553. case STARPU_CPU_RAM:
  554. #ifdef STARPU_DEVEL
  555. #warning to be fixed
  556. #endif
  557. global_mem_size = 64*1024*1024;
  558. break;
  559. #ifdef STARPU_USE_CUDA
  560. case STARPU_CUDA_RAM:
  561. {
  562. int devid = starpu_memory_node_to_devid(dst_node);
  563. global_mem_size = starpu_cuda_get_global_mem_size(devid);
  564. break;
  565. }
  566. #endif
  567. #ifdef STARPU_USE_OPENCL
  568. case STARPU_OPENCL_RAM:
  569. {
  570. int devid = starpu_memory_node_to_devid(dst_node);
  571. global_mem_size = starpu_opencl_get_global_mem_size(devid);
  572. break;
  573. }
  574. #endif
  575. }
  576. return global_mem_size;
  577. }
  578. /*
  579. * In order to allocate a piece of data, we try to reuse existing buffers if
  580. * its possible.
  581. * 1 - we try to reuse a memchunk that is explicitely unused.
  582. * 2 - we go through the list of memory chunks and find one that is not
  583. * referenced and that has the same footprint to reuse it.
  584. * 3 - we call the usual driver's alloc method
  585. * 4 - we go through the list of memory chunks and release those that are
  586. * not referenced (or part of those).
  587. *
  588. */
  589. static ssize_t _starpu_allocate_interface(starpu_data_handle handle, struct starpu_data_replicate_s *replicate, uint32_t dst_node, unsigned is_prefetch)
  590. {
  591. unsigned attempts = 0;
  592. ssize_t allocated_memory;
  593. _starpu_data_allocation_inc_stats(dst_node);
  594. #ifdef STARPU_USE_ALLOCATION_CACHE
  595. /* perhaps we can directly reuse a buffer in the free-list */
  596. uint32_t footprint = _starpu_compute_data_footprint(handle);
  597. STARPU_TRACE_START_ALLOC_REUSE(dst_node);
  598. PTHREAD_RWLOCK_WRLOCK(&mc_rwlock[node]);
  599. if (try_to_find_reusable_mem_chunk(dst_node, handle, footprint))
  600. {
  601. PTHREAD_RWLOCK_UNLOCK(&mc_rwlock[node]);
  602. _starpu_allocation_cache_hit(dst_node);
  603. ssize_t data_size = _starpu_data_get_size(handle);
  604. return data_size;
  605. }
  606. PTHREAD_RWLOCK_UNLOCK(&mc_rwlock[node]);
  607. STARPU_TRACE_END_ALLOC_REUSE(dst_node);
  608. #endif
  609. do {
  610. STARPU_ASSERT(handle->ops);
  611. STARPU_ASSERT(handle->ops->allocate_data_on_node);
  612. STARPU_TRACE_START_ALLOC(dst_node);
  613. STARPU_ASSERT(replicate->data_interface);
  614. #if defined(STARPU_USE_CUDA) && defined(HAVE_CUDA_MEMCPY_PEER)
  615. if (_starpu_get_node_kind(dst_node) == STARPU_CUDA_RAM)
  616. {
  617. /* To facilitate the design of interface, we set the
  618. * proper CUDA device in case it is needed. This avoids
  619. * having to set it again in the malloc method of each
  620. * interface. */
  621. cudaError_t err = cudaSetDevice(starpu_memory_node_to_devid(dst_node));
  622. STARPU_ASSERT(err == cudaSuccess);
  623. }
  624. #endif
  625. allocated_memory = handle->ops->allocate_data_on_node(replicate->data_interface, dst_node);
  626. STARPU_TRACE_END_ALLOC(dst_node);
  627. if (allocated_memory == -ENOMEM)
  628. {
  629. _STARPU_DEBUG("needs to reclaim memory\n");
  630. size_t reclaim = 0.25*_starpu_get_global_mem_size(dst_node);
  631. if (starpu_memstrategy_data_size_coefficient*handle->data_size > reclaim)
  632. reclaim = starpu_memstrategy_data_size_coefficient*handle->data_size;
  633. replicate->refcnt++;
  634. _starpu_spin_unlock(&handle->header_lock);
  635. STARPU_TRACE_START_MEMRECLAIM(dst_node);
  636. if (is_prefetch)
  637. flush_memchunk_cache(dst_node, reclaim);
  638. else
  639. reclaim_memory_generic(dst_node, 0, reclaim);
  640. STARPU_TRACE_END_MEMRECLAIM(dst_node);
  641. while (_starpu_spin_trylock(&handle->header_lock))
  642. _starpu_datawizard_progress(_starpu_get_local_memory_node(), 0);
  643. replicate->refcnt--;
  644. }
  645. } while((allocated_memory == -ENOMEM) && attempts++ < 2);
  646. return allocated_memory;
  647. }
  648. int _starpu_allocate_memory_on_node(starpu_data_handle handle, struct starpu_data_replicate_s *replicate, unsigned is_prefetch)
  649. {
  650. ssize_t allocated_memory;
  651. unsigned dst_node = replicate->memory_node;
  652. STARPU_ASSERT(handle);
  653. /* A buffer is already allocated on the node */
  654. if (replicate->allocated)
  655. return 0;
  656. STARPU_ASSERT(replicate->data_interface);
  657. allocated_memory = _starpu_allocate_interface(handle, replicate, dst_node, is_prefetch);
  658. /* perhaps we could really not handle that capacity misses */
  659. if (allocated_memory == -ENOMEM)
  660. return -ENOMEM;
  661. register_mem_chunk(replicate, allocated_memory, 1);
  662. replicate->allocated = 1;
  663. replicate->automatically_allocated = 1;
  664. if (dst_node == 0)
  665. {
  666. void *ptr = starpu_handle_to_pointer(handle, 0);
  667. if (ptr != NULL)
  668. {
  669. _starpu_data_register_ram_pointer(handle, ptr);
  670. }
  671. }
  672. return 0;
  673. }
  674. unsigned starpu_data_test_if_allocated_on_node(starpu_data_handle handle, uint32_t memory_node)
  675. {
  676. return handle->per_node[memory_node].allocated;
  677. }
  678. void starpu_memchunk_recently_used(starpu_mem_chunk_t mc, unsigned node)
  679. {
  680. PTHREAD_RWLOCK_WRLOCK(&lru_rwlock[node]);
  681. starpu_mem_chunk_lru_t mc_lru=starpu_mem_chunk_lru_new();
  682. mc_lru->mc=mc;
  683. starpu_mem_chunk_lru_list_push_front(starpu_lru_list[node],mc_lru);
  684. PTHREAD_RWLOCK_UNLOCK(&lru_rwlock[node]);
  685. }
  686. /* The mc_rwlock[node] rw-lock should be taken prior to calling this function.*/
  687. static void starpu_memchunk_recently_used_move(starpu_mem_chunk_t mc, unsigned node)
  688. {
  689. /* XXX Sometimes the memchunk is not in the list... */
  690. starpu_mem_chunk_t mc_iter;
  691. for (mc_iter = starpu_mem_chunk_list_begin(mc_list[node]);
  692. mc_iter != starpu_mem_chunk_list_end(mc_list[node]);
  693. mc_iter = starpu_mem_chunk_list_next(mc_iter) )
  694. {
  695. if (mc_iter==mc)
  696. {
  697. starpu_mem_chunk_list_erase(mc_list[node], mc);
  698. starpu_mem_chunk_list_push_back(mc_list[node], mc);
  699. return;
  700. }
  701. }
  702. }
  703. static void starpu_lru(unsigned node)
  704. {
  705. PTHREAD_RWLOCK_WRLOCK(&lru_rwlock[node]);
  706. while (!starpu_mem_chunk_lru_list_empty(starpu_lru_list[node]))
  707. {
  708. starpu_mem_chunk_lru_t mc_lru=starpu_mem_chunk_lru_list_front(starpu_lru_list[node]);
  709. starpu_memchunk_recently_used_move(mc_lru->mc, node);
  710. starpu_mem_chunk_lru_list_erase(starpu_lru_list[node], mc_lru);
  711. starpu_mem_chunk_lru_delete(mc_lru);
  712. }
  713. PTHREAD_RWLOCK_UNLOCK(&lru_rwlock[node]);
  714. }
  715. #ifdef STARPU_MEMORY_STATUS
  716. void _starpu_display_data_stats_by_node(int node)
  717. {
  718. PTHREAD_RWLOCK_WRLOCK(&mc_rwlock[node]);
  719. if (!starpu_mem_chunk_list_empty(mc_list[node]))
  720. {
  721. fprintf(stderr, "#-------\n");
  722. fprintf(stderr, "Data on Node #%d\n",node);
  723. starpu_mem_chunk_t mc;
  724. for (mc = starpu_mem_chunk_list_begin(mc_list[node]);
  725. mc != starpu_mem_chunk_list_end(mc_list[node]);
  726. mc = starpu_mem_chunk_list_next(mc))
  727. {
  728. _starpu_display_data_handle_stats(mc->data);
  729. }
  730. }
  731. PTHREAD_RWLOCK_UNLOCK(&mc_rwlock[node]);
  732. }
  733. #endif