memalloc.c 26 KB

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