memalloc.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669
  1. /*
  2. * StarPU
  3. * Copyright (C) INRIA 2008-2009 (see AUTHORS file)
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation; either version 2.1 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. *
  14. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  15. */
  16. #include "memalloc.h"
  17. #include <datawizard/footprint.h>
  18. static pthread_rwlock_t mc_rwlock[STARPU_MAXNODES];
  19. static starpu_mem_chunk_list_t mc_list[STARPU_MAXNODES];
  20. static starpu_mem_chunk_list_t mc_list_to_free[STARPU_MAXNODES];
  21. static size_t liberate_memory_on_node(starpu_mem_chunk_t mc, uint32_t node);
  22. void _starpu_init_mem_chunk_lists(void)
  23. {
  24. unsigned i;
  25. for (i = 0; i < STARPU_MAXNODES; i++)
  26. {
  27. pthread_rwlock_init(&mc_rwlock[i], NULL);
  28. mc_list[i] = starpu_mem_chunk_list_new();
  29. mc_list_to_free[i] = starpu_mem_chunk_list_new();
  30. }
  31. }
  32. void _starpu_deinit_mem_chunk_lists(void)
  33. {
  34. unsigned i;
  35. for (i = 0; i < STARPU_MAXNODES; i++)
  36. {
  37. starpu_mem_chunk_list_delete(mc_list[i]);
  38. starpu_mem_chunk_list_delete(mc_list_to_free[i]);
  39. }
  40. }
  41. static void lock_all_subtree(starpu_data_handle handle)
  42. {
  43. if (handle->nchildren == 0)
  44. {
  45. /* this is a leaf */
  46. while (_starpu_spin_trylock(&handle->header_lock))
  47. _starpu_datawizard_progress(_starpu_get_local_memory_node(), 0);
  48. }
  49. else {
  50. /* lock all sub-subtrees children */
  51. unsigned child;
  52. for (child = 0; child < handle->nchildren; child++)
  53. {
  54. lock_all_subtree(&handle->children[child]);
  55. }
  56. }
  57. }
  58. static void unlock_all_subtree(starpu_data_handle handle)
  59. {
  60. if (handle->nchildren == 0)
  61. {
  62. /* this is a leaf */
  63. _starpu_spin_unlock(&handle->header_lock);
  64. }
  65. else {
  66. /* lock all sub-subtrees children
  67. * Note that this is done in the reverse order of the
  68. * lock_all_subtree so that we avoid deadlock */
  69. unsigned i;
  70. for (i =0; i < handle->nchildren; i++)
  71. {
  72. unsigned child = handle->nchildren - 1 - i;
  73. unlock_all_subtree(&handle->children[child]);
  74. }
  75. }
  76. }
  77. static unsigned may_free_subtree(starpu_data_handle handle, unsigned node)
  78. {
  79. /* we only free if no one refers to the leaf */
  80. uint32_t refcnt = _starpu_get_data_refcnt(handle, node);
  81. if (refcnt)
  82. return 0;
  83. if (!handle->nchildren)
  84. return 1;
  85. /* look into all sub-subtrees children */
  86. unsigned child;
  87. for (child = 0; child < handle->nchildren; child++)
  88. {
  89. unsigned res;
  90. res = may_free_subtree(&handle->children[child], node);
  91. if (!res) return 0;
  92. }
  93. /* no problem was found */
  94. return 1;
  95. }
  96. static size_t do_free_mem_chunk(starpu_mem_chunk_t mc, unsigned node)
  97. {
  98. size_t size;
  99. /* free the actual buffer */
  100. size = liberate_memory_on_node(mc, node);
  101. /* remove the mem_chunk from the list */
  102. starpu_mem_chunk_list_erase(mc_list[node], mc);
  103. free(mc->interface);
  104. starpu_mem_chunk_delete(mc);
  105. return size;
  106. }
  107. static void transfer_subtree_to_node(starpu_data_handle handle, unsigned src_node,
  108. unsigned dst_node)
  109. {
  110. unsigned i;
  111. unsigned last = 0;
  112. unsigned cnt;
  113. int ret;
  114. if (handle->nchildren == 0)
  115. {
  116. /* this is a leaf */
  117. switch(handle->per_node[src_node].state) {
  118. case STARPU_OWNER:
  119. /* the local node has the only copy */
  120. /* the owner is now the destination_node */
  121. handle->per_node[src_node].state = STARPU_INVALID;
  122. handle->per_node[dst_node].state = STARPU_OWNER;
  123. #warning we should use requests during memory reclaim
  124. /* TODO use request !! */
  125. handle->per_node[src_node].refcnt++;
  126. handle->per_node[dst_node].refcnt++;
  127. ret = _starpu_driver_copy_data_1_to_1(handle, src_node, dst_node, 0, NULL, 1);
  128. STARPU_ASSERT(ret == 0);
  129. handle->per_node[src_node].refcnt--;
  130. handle->per_node[dst_node].refcnt--;
  131. break;
  132. case STARPU_SHARED:
  133. /* some other node may have the copy */
  134. handle->per_node[src_node].state = STARPU_INVALID;
  135. /* count the number of copies */
  136. cnt = 0;
  137. for (i = 0; i < STARPU_MAXNODES; i++)
  138. {
  139. if (handle->per_node[i].state == STARPU_SHARED) {
  140. cnt++;
  141. last = i;
  142. }
  143. }
  144. if (cnt == 1)
  145. handle->per_node[last].state = STARPU_OWNER;
  146. break;
  147. case STARPU_INVALID:
  148. /* nothing to be done */
  149. break;
  150. default:
  151. STARPU_ABORT();
  152. break;
  153. }
  154. }
  155. else {
  156. /* lock all sub-subtrees children */
  157. unsigned child;
  158. for (child = 0; child < handle->nchildren; child++)
  159. {
  160. transfer_subtree_to_node(&handle->children[child],
  161. src_node, dst_node);
  162. }
  163. }
  164. }
  165. static size_t try_to_free_mem_chunk(starpu_mem_chunk_t mc, unsigned node)
  166. {
  167. size_t liberated = 0;
  168. starpu_data_handle handle;
  169. handle = mc->data;
  170. STARPU_ASSERT(handle);
  171. /* try to lock all the leafs of the subtree */
  172. lock_all_subtree(handle);
  173. /* check if they are all "free" */
  174. if (may_free_subtree(handle, node))
  175. {
  176. STARPU_ASSERT(handle->per_node[node].refcnt == 0);
  177. /* in case there was nobody using that buffer, throw it
  178. * away after writing it back to main memory */
  179. transfer_subtree_to_node(handle, node, 0);
  180. STARPU_ASSERT(handle->per_node[node].refcnt == 0);
  181. /* now the actual buffer may be liberated */
  182. liberated = do_free_mem_chunk(mc, node);
  183. }
  184. /* unlock the leafs */
  185. unlock_all_subtree(handle);
  186. return liberated;
  187. }
  188. #ifdef STARPU_USE_ALLOCATION_CACHE
  189. /* we assume that mc_rwlock[node] is taken */
  190. static void reuse_mem_chunk(unsigned node, starpu_data_handle new_data, starpu_mem_chunk_t mc, unsigned is_already_in_mc_list)
  191. {
  192. starpu_data_handle old_data;
  193. old_data = mc->data;
  194. /* we found an appropriate mem chunk: so we get it out
  195. * of the "to free" list, and reassign it to the new
  196. * piece of data */
  197. if (!is_already_in_mc_list)
  198. {
  199. starpu_mem_chunk_list_erase(mc_list_to_free[node], mc);
  200. }
  201. if (!mc->data_was_deleted)
  202. {
  203. old_data->per_node[node].allocated = 0;
  204. old_data->per_node[node].automatically_allocated = 0;
  205. }
  206. new_data->per_node[node].allocated = 1;
  207. new_data->per_node[node].automatically_allocated = 1;
  208. memcpy(&new_data->interface[node], mc->interface, old_data->interface_size);
  209. mc->data = new_data;
  210. mc->data_was_deleted = 0;
  211. /* mc->ops, mc->size, mc->footprint and mc->interface should be
  212. * unchanged ! */
  213. /* reinsert the mem chunk in the list of active memory chunks */
  214. if (!is_already_in_mc_list)
  215. {
  216. starpu_mem_chunk_list_push_front(mc_list[node], mc);
  217. }
  218. }
  219. 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)
  220. {
  221. unsigned success = 0;
  222. starpu_data_handle old_data;
  223. old_data = mc->data;
  224. STARPU_ASSERT(old_data);
  225. /* try to lock all the leafs of the subtree */
  226. lock_all_subtree(old_data);
  227. /* check if they are all "free" */
  228. if (may_free_subtree(old_data, node))
  229. {
  230. success = 1;
  231. /* in case there was nobody using that buffer, throw it
  232. * away after writing it back to main memory */
  233. transfer_subtree_to_node(old_data, node, 0);
  234. /* now replace the previous data */
  235. reuse_mem_chunk(node, new_data, mc, is_already_in_mc_list);
  236. }
  237. /* unlock the leafs */
  238. unlock_all_subtree(old_data);
  239. return success;
  240. }
  241. /* this function looks for a memory chunk that matches a given footprint in the
  242. * list of mem chunk that need to be liberated */
  243. static unsigned try_to_find_reusable_mem_chunk(unsigned node, starpu_data_handle data, uint32_t footprint)
  244. {
  245. pthread_rwlock_wrlock(&mc_rwlock[node]);
  246. /* go through all buffers for which there was a removal request */
  247. starpu_mem_chunk_t mc, next_mc;
  248. for (mc = starpu_mem_chunk_list_begin(mc_list_to_free[node]);
  249. mc != starpu_mem_chunk_list_end(mc_list_to_free[node]);
  250. mc = next_mc)
  251. {
  252. next_mc = starpu_mem_chunk_list_next(mc);
  253. if (mc->footprint == footprint)
  254. {
  255. starpu_data_handle old_data;
  256. old_data = mc->data;
  257. if (old_data->per_node[node].allocated &&
  258. old_data->per_node[node].automatically_allocated)
  259. {
  260. reuse_mem_chunk(node, data, mc, 0);
  261. pthread_rwlock_unlock(&mc_rwlock[node]);
  262. return 1;
  263. }
  264. }
  265. }
  266. /* now look for some non essential data in the active list */
  267. for (mc = starpu_mem_chunk_list_begin(mc_list[node]);
  268. mc != starpu_mem_chunk_list_end(mc_list[node]);
  269. mc = next_mc)
  270. {
  271. /* there is a risk that the memory chunk is liberated
  272. before next iteration starts: so we compute the next
  273. element of the list now */
  274. next_mc = starpu_mem_chunk_list_next(mc);
  275. if (mc->data->is_not_important && (mc->footprint == footprint))
  276. {
  277. // fprintf(stderr, "found a candidate ...\n");
  278. if (try_to_reuse_mem_chunk(mc, node, data, 1))
  279. {
  280. pthread_rwlock_unlock(&mc_rwlock[node]);
  281. return 1;
  282. }
  283. }
  284. }
  285. pthread_rwlock_unlock(&mc_rwlock[node]);
  286. return 0;
  287. }
  288. #endif
  289. /*
  290. * Liberate the memory chuncks that are explicitely tagged to be liberated. The
  291. * mc_rwlock[node] rw-lock should be taken prior to calling this function.
  292. */
  293. static size_t perform_mc_removal_requests(uint32_t node)
  294. {
  295. starpu_mem_chunk_t mc, next_mc;
  296. size_t liberated = 0;
  297. for (mc = starpu_mem_chunk_list_begin(mc_list_to_free[node]);
  298. mc != starpu_mem_chunk_list_end(mc_list_to_free[node]);
  299. mc = next_mc)
  300. {
  301. next_mc = starpu_mem_chunk_list_next(mc);
  302. liberated += liberate_memory_on_node(mc, node);
  303. starpu_mem_chunk_list_erase(mc_list_to_free[node], mc);
  304. free(mc->interface);
  305. starpu_mem_chunk_delete(mc);
  306. }
  307. return liberated;
  308. }
  309. /*
  310. * Try to liberate the buffers currently in use on the memory node. If the
  311. * force flag is set, the memory is liberated regardless of coherency concerns
  312. * (this should only be used at the termination of StarPU for instance). The
  313. * mc_rwlock[node] rw-lock should be taken prior to calling this function.
  314. */
  315. static size_t liberate_potentially_in_use_mc(uint32_t node, unsigned force)
  316. {
  317. size_t liberated = 0;
  318. starpu_mem_chunk_t mc, next_mc;
  319. for (mc = starpu_mem_chunk_list_begin(mc_list[node]);
  320. mc != starpu_mem_chunk_list_end(mc_list[node]);
  321. mc = next_mc)
  322. {
  323. /* there is a risk that the memory chunk is liberated
  324. before next iteration starts: so we compute the next
  325. element of the list now */
  326. next_mc = starpu_mem_chunk_list_next(mc);
  327. if (!force)
  328. {
  329. liberated += try_to_free_mem_chunk(mc, node);
  330. #if 0
  331. if (liberated > toreclaim)
  332. break;
  333. #endif
  334. }
  335. else {
  336. /* We must liberate the memory now: note that data
  337. * coherency is not maintained in that case ! */
  338. liberated += do_free_mem_chunk(mc, node);
  339. }
  340. }
  341. return liberated;
  342. }
  343. /*
  344. * Try to free some memory on the specified node
  345. * returns 0 if no memory was released, 1 else
  346. */
  347. static size_t reclaim_memory(uint32_t node, size_t toreclaim __attribute__ ((unused)))
  348. {
  349. int res;
  350. size_t liberated = 0;
  351. res = pthread_rwlock_wrlock(&mc_rwlock[node]);
  352. STARPU_ASSERT(!res);
  353. /* remove all buffers for which there was a removal request */
  354. liberated += perform_mc_removal_requests(node);
  355. /* try to free all allocated data potentially in use */
  356. liberated += liberate_potentially_in_use_mc(node, 0);
  357. res = pthread_rwlock_unlock(&mc_rwlock[node]);
  358. STARPU_ASSERT(!res);
  359. return liberated;
  360. }
  361. /*
  362. * This function liberates all the memory that was implicitely allocated by
  363. * StarPU (for the data replicates). This is not ensuring data coherency, and
  364. * should only be called while StarPU is getting shut down.
  365. */
  366. size_t _starpu_liberate_all_automatically_allocated_buffers(uint32_t node)
  367. {
  368. int res;
  369. size_t liberated = 0;
  370. res = pthread_rwlock_wrlock(&mc_rwlock[node]);
  371. STARPU_ASSERT(!res);
  372. liberated += perform_mc_removal_requests(node);
  373. liberated += liberate_potentially_in_use_mc(node, 1);
  374. res = pthread_rwlock_unlock(&mc_rwlock[node]);
  375. STARPU_ASSERT(!res);
  376. return liberated;
  377. }
  378. static void register_mem_chunk(starpu_data_handle handle, uint32_t dst_node, size_t size, unsigned automatically_allocated)
  379. {
  380. int res;
  381. starpu_mem_chunk_t mc = starpu_mem_chunk_new();
  382. STARPU_ASSERT(handle);
  383. STARPU_ASSERT(handle->ops);
  384. mc->data = handle;
  385. mc->size = size;
  386. mc->footprint = _starpu_compute_data_footprint(handle);
  387. mc->ops = handle->ops;
  388. mc->data_was_deleted = 0;
  389. mc->automatically_allocated = automatically_allocated;
  390. /* the interface was already filled by ops->allocate_data_on_node */
  391. void *src_interface = starpu_data_get_interface_on_node(handle, dst_node);
  392. mc->interface = malloc(handle->ops->interface_size);
  393. STARPU_ASSERT(mc->interface);
  394. memcpy(mc->interface, src_interface, handle->ops->interface_size);
  395. res = pthread_rwlock_wrlock(&mc_rwlock[dst_node]);
  396. STARPU_ASSERT(!res);
  397. starpu_mem_chunk_list_push_front(mc_list[dst_node], mc);
  398. res = pthread_rwlock_unlock(&mc_rwlock[dst_node]);
  399. STARPU_ASSERT(!res);
  400. }
  401. void _starpu_request_mem_chunk_removal(starpu_data_handle handle, unsigned node)
  402. {
  403. int res;
  404. res = pthread_rwlock_wrlock(&mc_rwlock[node]);
  405. STARPU_ASSERT(!res);
  406. /* iterate over the list of memory chunks and remove the entry */
  407. starpu_mem_chunk_t mc, next_mc;
  408. for (mc = starpu_mem_chunk_list_begin(mc_list[node]);
  409. mc != starpu_mem_chunk_list_end(mc_list[node]);
  410. mc = next_mc)
  411. {
  412. next_mc = starpu_mem_chunk_list_next(mc);
  413. if (mc->data == handle) {
  414. /* we found the data */
  415. mc->data_was_deleted = 1;
  416. /* remove it from the main list */
  417. starpu_mem_chunk_list_erase(mc_list[node], mc);
  418. /* put it in the list of buffers to be removed */
  419. starpu_mem_chunk_list_push_front(mc_list_to_free[node], mc);
  420. res = pthread_rwlock_unlock(&mc_rwlock[node]);
  421. STARPU_ASSERT(!res);
  422. return;
  423. }
  424. }
  425. /* there was no corresponding buffer ... */
  426. res = pthread_rwlock_unlock(&mc_rwlock[node]);
  427. STARPU_ASSERT(!res);
  428. }
  429. static size_t liberate_memory_on_node(starpu_mem_chunk_t mc, uint32_t node)
  430. {
  431. size_t liberated = 0;
  432. STARPU_ASSERT(mc->ops);
  433. STARPU_ASSERT(mc->ops->liberate_data_on_node);
  434. starpu_data_handle handle = mc->data;
  435. // while (_starpu_spin_trylock(&handle->header_lock))
  436. // _starpu_datawizard_progress(_starpu_get_local_memory_node());
  437. #warning can we block here ?
  438. // _starpu_spin_lock(&handle->header_lock);
  439. if (mc->automatically_allocated && (handle->per_node[node].refcnt == 0))
  440. {
  441. STARPU_ASSERT(handle->per_node[node].allocated);
  442. mc->ops->liberate_data_on_node(mc->interface, node);
  443. if (!mc->data_was_deleted)
  444. {
  445. handle->per_node[node].allocated = 0;
  446. /* XXX why do we need that ? */
  447. handle->per_node[node].automatically_allocated = 0;
  448. }
  449. liberated = mc->size;
  450. STARPU_ASSERT(handle->per_node[node].refcnt == 0);
  451. }
  452. // _starpu_spin_unlock(&handle->header_lock);
  453. return liberated;
  454. }
  455. /*
  456. * In order to allocate a piece of data, we try to reuse existing buffers if
  457. * its possible.
  458. * 1 - we try to reuse a memchunk that is explicitely unused.
  459. * 2 - we go through the list of memory chunks and find one that is not
  460. * referenced and that has the same footprint to reuse it.
  461. * 3 - we call the usual driver's alloc method
  462. * 4 - we go through the list of memory chunks and release those that are
  463. * not referenced (or part of those).
  464. *
  465. */
  466. size_t _starpu_allocate_interface(starpu_data_handle handle, void *interface, uint32_t dst_node)
  467. {
  468. unsigned attempts = 0;
  469. size_t allocated_memory;
  470. _starpu_data_allocation_inc_stats(dst_node);
  471. #ifdef STARPU_USE_ALLOCATION_CACHE
  472. /* perhaps we can directly reuse a buffer in the free-list */
  473. uint32_t footprint = _starpu_compute_data_footprint(handle);
  474. STARPU_TRACE_START_ALLOC_REUSE(dst_node);
  475. if (try_to_find_reusable_mem_chunk(dst_node, handle, footprint))
  476. {
  477. _starpu_allocation_cache_hit(dst_node);
  478. return 0;
  479. }
  480. STARPU_TRACE_END_ALLOC_REUSE(dst_node);
  481. #endif
  482. do {
  483. STARPU_ASSERT(handle->ops);
  484. STARPU_ASSERT(handle->ops->allocate_data_on_node);
  485. STARPU_TRACE_START_ALLOC(dst_node);
  486. allocated_memory = handle->ops->allocate_data_on_node(interface, dst_node);
  487. STARPU_TRACE_END_ALLOC(dst_node);
  488. if (!allocated_memory) {
  489. /* XXX perhaps we should find the proper granularity
  490. * not to waste our cache all the time */
  491. STARPU_ASSERT(handle->ops->get_size);
  492. size_t data_size = handle->ops->get_size(handle);
  493. STARPU_TRACE_START_MEMRECLAIM(dst_node);
  494. reclaim_memory(dst_node, 2*data_size);
  495. STARPU_TRACE_END_MEMRECLAIM(dst_node);
  496. }
  497. } while(!allocated_memory && attempts++ < 2);
  498. return allocated_memory;
  499. }
  500. int _starpu_allocate_memory_on_node(starpu_data_handle handle, uint32_t dst_node, unsigned may_alloc)
  501. {
  502. size_t allocated_memory;
  503. STARPU_ASSERT(handle);
  504. /* A buffer is already allocated on the node */
  505. if (handle->per_node[dst_node].allocated)
  506. return 0;
  507. if (!may_alloc)
  508. return ENOMEM;
  509. void *interface = starpu_data_get_interface_on_node(handle, dst_node);
  510. allocated_memory = _starpu_allocate_interface(handle, interface, dst_node);
  511. /* perhaps we could really not handle that capacity misses */
  512. if (!allocated_memory)
  513. return ENOMEM;
  514. /* perhaps we could really not handle that capacity misses */
  515. if (allocated_memory)
  516. register_mem_chunk(handle, dst_node, allocated_memory, 1);
  517. handle->per_node[dst_node].allocated = 1;
  518. handle->per_node[dst_node].automatically_allocated = 1;
  519. return 0;
  520. }
  521. unsigned starpu_data_test_if_allocated_on_node(starpu_data_handle handle, uint32_t memory_node)
  522. {
  523. return handle->per_node[memory_node].allocated;
  524. }