memalloc.c 25 KB

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