malloc.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-2010, 2012-2013 Université de Bordeaux 1
  4. * Copyright (C) 2010, 2011, 2012, 2013 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 <errno.h>
  18. #include <core/workers.h>
  19. #include <core/disk.h>
  20. #include <common/config.h>
  21. #include <common/fxt.h>
  22. #include <starpu.h>
  23. #include <drivers/opencl/driver_opencl.h>
  24. #include <datawizard/memory_manager.h>
  25. #include <datawizard/malloc.h>
  26. static size_t _malloc_align = sizeof(void*);
  27. void starpu_malloc_set_align(size_t align)
  28. {
  29. STARPU_ASSERT_MSG(!(align & (align - 1)), "Alignment given to starpu_malloc_set_align (%lu) must be a power of two", (unsigned long) align);
  30. if (_malloc_align < align)
  31. _malloc_align = align;
  32. }
  33. #if (defined(STARPU_USE_CUDA) && !defined(HAVE_CUDA_MEMCPY_PEER))// || defined(STARPU_USE_OPENCL)
  34. struct malloc_pinned_codelet_struct
  35. {
  36. void **ptr;
  37. size_t dim;
  38. };
  39. #endif
  40. /* Would be difficult to do it this way, we need to remember the cl_mem to be able to free it later... */
  41. //#ifdef STARPU_USE_OPENCL
  42. //static void malloc_pinned_opencl_codelet(void *buffers[] STARPU_ATTRIBUTE_UNUSED, void *arg)
  43. //{
  44. // struct malloc_pinned_codelet_struct *s = arg;
  45. // // *(s->ptr) = malloc(s->dim);
  46. // starpu_opencl_allocate_memory((void **)(s->ptr), s->dim, CL_MEM_READ_WRITE|CL_MEM_ALLOC_HOST_PTR);
  47. //}
  48. //#endif
  49. #if defined(STARPU_USE_CUDA) && !defined(HAVE_CUDA_MEMCPY_PEER) && !defined(STARPU_SIMGRID)
  50. static void malloc_pinned_cuda_codelet(void *buffers[] STARPU_ATTRIBUTE_UNUSED, void *arg)
  51. {
  52. struct malloc_pinned_codelet_struct *s = arg;
  53. cudaError_t cures;
  54. cures = cudaHostAlloc((void **)(s->ptr), s->dim, cudaHostAllocPortable);
  55. if (STARPU_UNLIKELY(cures))
  56. STARPU_CUDA_REPORT_ERROR(cures);
  57. }
  58. #endif
  59. #if (defined(STARPU_USE_CUDA) && !defined(HAVE_CUDA_MEMCPY_PEER)) && !defined(STARPU_SIMGRID)// || defined(STARPU_USE_OPENCL)
  60. static struct starpu_perfmodel malloc_pinned_model =
  61. {
  62. .type = STARPU_HISTORY_BASED,
  63. .symbol = "malloc_pinned"
  64. };
  65. static struct starpu_codelet malloc_pinned_cl =
  66. {
  67. .cuda_funcs = {malloc_pinned_cuda_codelet, NULL},
  68. //#ifdef STARPU_USE_OPENCL
  69. // .opencl_funcs = {malloc_pinned_opencl_codelet, NULL},
  70. //#endif
  71. .nbuffers = 0,
  72. .model = &malloc_pinned_model
  73. };
  74. #endif
  75. int starpu_malloc_flags(void **A, size_t dim, int flags)
  76. {
  77. int ret=0;
  78. STARPU_ASSERT(A);
  79. if (flags & STARPU_MALLOC_COUNT)
  80. {
  81. if (_starpu_memory_manager_can_allocate_size(dim, STARPU_MAIN_RAM) == 0)
  82. {
  83. size_t freed;
  84. size_t reclaim = 2 * dim;
  85. _STARPU_DEBUG("There is not enough memory left, we are going to reclaim %ld\n", reclaim);
  86. _STARPU_TRACE_START_MEMRECLAIM(0,0);
  87. freed = _starpu_memory_reclaim_generic(0, 0, reclaim);
  88. _STARPU_TRACE_END_MEMRECLAIM(0,0);
  89. if (freed < dim)
  90. {
  91. // We could not reclaim enough memory
  92. *A = NULL;
  93. return -ENOMEM;
  94. }
  95. }
  96. }
  97. if (flags & STARPU_MALLOC_PINNED)
  98. {
  99. #ifdef STARPU_SIMGRID
  100. /* FIXME: CUDA seems to be taking 650µs every 1MiB.
  101. * Ideally we would simulate this batching in 1MiB requests
  102. * instead of computing an average value.
  103. */
  104. MSG_process_sleep((float) dim * 0.000650 / 1048576.);
  105. #else /* STARPU_SIMGRID */
  106. if (_starpu_can_submit_cuda_task())
  107. {
  108. #ifdef STARPU_USE_CUDA
  109. #ifdef HAVE_CUDA_MEMCPY_PEER
  110. cudaError_t cures;
  111. cures = cudaHostAlloc(A, dim, cudaHostAllocPortable);
  112. if (STARPU_UNLIKELY(cures))
  113. STARPU_CUDA_REPORT_ERROR(cures);
  114. goto end;
  115. #else
  116. int push_res;
  117. if (STARPU_UNLIKELY(!_starpu_worker_may_perform_blocking_calls()))
  118. return -EDEADLK;
  119. struct malloc_pinned_codelet_struct s =
  120. {
  121. .ptr = A,
  122. .dim = dim
  123. };
  124. malloc_pinned_cl.where = STARPU_CUDA;
  125. struct starpu_task *task = starpu_task_create();
  126. task->name = "cuda_malloc_pinned";
  127. task->callback_func = NULL;
  128. task->cl = &malloc_pinned_cl;
  129. task->cl_arg = &s;
  130. task->synchronous = 1;
  131. _starpu_exclude_task_from_dag(task);
  132. push_res = _starpu_task_submit_internally(task);
  133. STARPU_ASSERT(push_res != -ENODEV);
  134. goto end;
  135. #endif /* HAVE_CUDA_MEMCPY_PEER */
  136. #endif /* STARPU_USE_CUDA */
  137. }
  138. // else if (_starpu_can_submit_opencl_task())
  139. // {
  140. //#ifdef STARPU_USE_OPENCL
  141. // int push_res;
  142. //
  143. // if (STARPU_UNLIKELY(!_starpu_worker_may_perform_blocking_calls()))
  144. // return -EDEADLK;
  145. //
  146. // struct malloc_pinned_codelet_struct s =
  147. // {
  148. // .ptr = A,
  149. // .dim = dim
  150. // };
  151. //
  152. // malloc_pinned_cl.where = STARPU_OPENCL;
  153. // struct starpu_task *task = starpu_task_create();
  154. // task->name = "opencl_malloc_pinned";
  155. // task->callback_func = NULL;
  156. // task->cl = &malloc_pinned_cl;
  157. // task->cl_arg = &s;
  158. // task->synchronous = 1;
  159. //
  160. // _starpu_exclude_task_from_dag(task);
  161. //
  162. // push_res = _starpu_task_submit_internally(task);
  163. // STARPU_ASSERT(push_res != -ENODEV);
  164. // goto end;
  165. //#endif /* STARPU_USE_OPENCL */
  166. // }
  167. #endif /* STARPU_SIMGRID */
  168. }
  169. if (_starpu_can_submit_scc_task())
  170. {
  171. #ifdef STARPU_USE_SCC
  172. _starpu_scc_allocate_shared_memory(A, dim);
  173. #endif
  174. }
  175. else
  176. #ifdef STARPU_HAVE_POSIX_MEMALIGN
  177. if (_malloc_align != sizeof(void*))
  178. {
  179. if (posix_memalign(A, _malloc_align, dim))
  180. {
  181. ret = -ENOMEM;
  182. *A = NULL;
  183. }
  184. }
  185. else
  186. #elif defined(STARPU_HAVE_MEMALIGN)
  187. if (_malloc_align != sizeof(void*))
  188. {
  189. *A = memalign(_malloc_align, dim);
  190. }
  191. else
  192. #endif /* STARPU_HAVE_POSIX_MEMALIGN */
  193. {
  194. *A = malloc(dim);
  195. }
  196. end:
  197. if (ret == 0)
  198. {
  199. STARPU_ASSERT(*A);
  200. }
  201. return ret;
  202. }
  203. int starpu_malloc(void **A, size_t dim)
  204. {
  205. return starpu_malloc_flags(A, dim, STARPU_MALLOC_PINNED);
  206. }
  207. #if defined(STARPU_USE_CUDA) && !defined(HAVE_CUDA_MEMCPY_PEER) && !defined(STARPU_SIMGRID)
  208. static void free_pinned_cuda_codelet(void *buffers[] STARPU_ATTRIBUTE_UNUSED, void *arg)
  209. {
  210. cudaError_t cures;
  211. cures = cudaFreeHost(arg);
  212. if (STARPU_UNLIKELY(cures))
  213. STARPU_CUDA_REPORT_ERROR(cures);
  214. }
  215. #endif
  216. //#ifdef STARPU_USE_OPENCL
  217. //static void free_pinned_opencl_codelet(void *buffers[] STARPU_ATTRIBUTE_UNUSED, void *arg)
  218. //{
  219. // // free(arg);
  220. // int err = clReleaseMemObject(arg);
  221. // if (err != CL_SUCCESS) STARPU_OPENCL_REPORT_ERROR(err);
  222. //}
  223. //#endif
  224. #if defined(STARPU_USE_CUDA) && !defined(HAVE_CUDA_MEMCPY_PEER) && !defined(STARPU_SIMGRID) // || defined(STARPU_USE_OPENCL)
  225. static struct starpu_perfmodel free_pinned_model =
  226. {
  227. .type = STARPU_HISTORY_BASED,
  228. .symbol = "free_pinned"
  229. };
  230. static struct starpu_codelet free_pinned_cl =
  231. {
  232. .cuda_funcs = {free_pinned_cuda_codelet, NULL},
  233. //#ifdef STARPU_USE_OPENCL
  234. // .opencl_funcs = {free_pinned_opencl_codelet, NULL},
  235. //#endif
  236. .nbuffers = 0,
  237. .model = &free_pinned_model
  238. };
  239. #endif
  240. int starpu_free_flags(void *A, size_t dim, int flags)
  241. {
  242. #ifndef STARPU_SIMGRID
  243. if (flags & STARPU_MALLOC_PINNED)
  244. {
  245. if (_starpu_can_submit_cuda_task())
  246. {
  247. #ifdef STARPU_USE_CUDA
  248. #ifndef HAVE_CUDA_MEMCPY_PEER
  249. if (!_starpu_is_initialized())
  250. {
  251. #endif
  252. /* This is especially useful when starpu_free is called from
  253. * the GCC-plugin. starpu_shutdown will probably have already
  254. * been called, so we will not be able to submit a task. */
  255. cudaError_t err = cudaFreeHost(A);
  256. if (STARPU_UNLIKELY(err))
  257. STARPU_CUDA_REPORT_ERROR(err);
  258. goto out;
  259. #ifndef HAVE_CUDA_MEMCPY_PEER
  260. }
  261. else
  262. {
  263. int push_res;
  264. if (STARPU_UNLIKELY(!_starpu_worker_may_perform_blocking_calls()))
  265. return -EDEADLK;
  266. free_pinned_cl.where = STARPU_CUDA;
  267. struct starpu_task *task = starpu_task_create();
  268. task->name = "cuda_free_pinned";
  269. task->callback_func = NULL;
  270. task->cl = &free_pinned_cl;
  271. task->cl_arg = A;
  272. task->synchronous = 1;
  273. _starpu_exclude_task_from_dag(task);
  274. push_res = _starpu_task_submit_internally(task);
  275. STARPU_ASSERT(push_res != -ENODEV);
  276. goto out;
  277. }
  278. #endif /* HAVE_CUDA_MEMCPY_PEER */
  279. #endif /* STARPU_USE_CUDA */
  280. }
  281. // else if (_starpu_can_submit_opencl_task())
  282. // {
  283. //#ifdef STARPU_USE_OPENCL
  284. // int push_res;
  285. //
  286. // if (STARPU_UNLIKELY(!_starpu_worker_may_perform_blocking_calls()))
  287. // return -EDEADLK;
  288. //
  289. // free_pinned_cl.where = STARPU_OPENCL;
  290. // struct starpu_task *task = starpu_task_create();
  291. // task->name = "opencl_free_pinned";
  292. // task->callback_func = NULL;
  293. // task->cl = &free_pinned_cl;
  294. // task->cl_arg = A;
  295. // task->synchronous = 1;
  296. //
  297. // _starpu_exclude_task_from_dag(task);
  298. //
  299. // push_res = starpu_task_submit(task);
  300. // STARPU_ASSERT(push_res != -ENODEV);
  301. // goto out;
  302. // }
  303. //#endif
  304. }
  305. #endif /* STARPU_SIMGRID */
  306. if (_starpu_can_submit_scc_task())
  307. {
  308. #ifdef STARPU_USE_SCC
  309. _starpu_scc_free_shared_memory(A);
  310. #endif
  311. } else
  312. free(A);
  313. out:
  314. if (flags & STARPU_MALLOC_COUNT)
  315. {
  316. _starpu_memory_manager_deallocate_size(dim, STARPU_MAIN_RAM);
  317. }
  318. return 0;
  319. }
  320. int starpu_free(void *A)
  321. {
  322. return starpu_free_flags(A, 0, STARPU_MALLOC_PINNED);
  323. }
  324. #ifdef STARPU_SIMGRID
  325. static starpu_pthread_mutex_t cuda_alloc_mutex = STARPU_PTHREAD_MUTEX_INITIALIZER;
  326. static starpu_pthread_mutex_t opencl_alloc_mutex = STARPU_PTHREAD_MUTEX_INITIALIZER;
  327. #endif
  328. static uintptr_t
  329. _starpu_malloc_on_node(unsigned dst_node, size_t size)
  330. {
  331. uintptr_t addr = 0;
  332. #ifdef STARPU_USE_CUDA
  333. cudaError_t status;
  334. #endif
  335. if (_starpu_memory_manager_can_allocate_size(size, dst_node) == 0)
  336. return 0;
  337. switch(starpu_node_get_kind(dst_node))
  338. {
  339. case STARPU_CPU_RAM:
  340. {
  341. starpu_malloc_flags((void**) &addr, size,
  342. #if defined(STARPU_USE_CUDA) && !defined(HAVE_CUDA_MEMCPY_PEER) && !defined(STARPU_SIMGRID)
  343. 0
  344. #else
  345. STARPU_MALLOC_PINNED
  346. #endif
  347. );
  348. break;
  349. }
  350. #if defined(STARPU_USE_CUDA) || defined(STARPU_SIMGRID)
  351. case STARPU_CUDA_RAM:
  352. {
  353. #ifdef STARPU_SIMGRID
  354. static uintptr_t last[STARPU_MAXNODES];
  355. #ifdef STARPU_DEVEL
  356. #warning TODO: record used memory, using a simgrid property to know the available memory
  357. #endif
  358. /* Sleep for the allocation */
  359. STARPU_PTHREAD_MUTEX_LOCK(&cuda_alloc_mutex);
  360. MSG_process_sleep(0.000175);
  361. if (!last[dst_node])
  362. last[dst_node] = 1<<10;
  363. addr = last[dst_node];
  364. last[dst_node]+=size;
  365. STARPU_ASSERT(last[dst_node] >= addr);
  366. STARPU_PTHREAD_MUTEX_UNLOCK(&cuda_alloc_mutex);
  367. #else
  368. status = cudaMalloc((void **)&addr, size);
  369. if (!addr || (status != cudaSuccess))
  370. {
  371. if (STARPU_UNLIKELY(status != cudaErrorMemoryAllocation))
  372. STARPU_CUDA_REPORT_ERROR(status);
  373. addr = 0;
  374. }
  375. #endif
  376. break;
  377. }
  378. #endif
  379. #if defined(STARPU_USE_OPENCL) || defined(STARPU_SIMGRID)
  380. case STARPU_OPENCL_RAM:
  381. {
  382. #ifdef STARPU_SIMGRID
  383. static uintptr_t last[STARPU_MAXNODES];
  384. /* Sleep for the allocation */
  385. STARPU_PTHREAD_MUTEX_LOCK(&opencl_alloc_mutex);
  386. MSG_process_sleep(0.000175);
  387. if (!last[dst_node])
  388. last[dst_node] = 1<<10;
  389. addr = last[dst_node];
  390. last[dst_node]+=size;
  391. STARPU_ASSERT(last[dst_node] >= addr);
  392. STARPU_PTHREAD_MUTEX_UNLOCK(&opencl_alloc_mutex);
  393. #else
  394. int ret;
  395. cl_mem ptr;
  396. ret = starpu_opencl_allocate_memory(&ptr, size, CL_MEM_READ_WRITE);
  397. if (ret)
  398. {
  399. addr = 0;
  400. }
  401. else
  402. {
  403. addr = (uintptr_t)ptr;
  404. }
  405. break;
  406. #endif
  407. }
  408. #endif
  409. case STARPU_DISK_RAM:
  410. {
  411. addr = (uintptr_t) _starpu_disk_alloc(dst_node, size);
  412. break;
  413. }
  414. #ifdef STARPU_USE_MIC
  415. case STARPU_MIC_RAM:
  416. if (_starpu_mic_allocate_memory((void **)(&addr), size, dst_node))
  417. addr = 0;
  418. break;
  419. #endif
  420. #ifdef STARPU_USE_SCC
  421. case STARPU_SCC_RAM:
  422. if (_starpu_scc_allocate_memory((void **)(&addr), size, dst_node))
  423. addr = 0;
  424. break;
  425. #endif
  426. default:
  427. STARPU_ABORT();
  428. }
  429. if (addr == 0)
  430. {
  431. // Allocation failed, gives the memory back to the memory manager
  432. const char* file;
  433. file = strrchr(__FILE__,'/');
  434. file += sizeof(char);
  435. _STARPU_TRACE_MEMORY_FULL(size);
  436. _starpu_memory_manager_deallocate_size(size, dst_node);
  437. }
  438. return addr;
  439. }
  440. void
  441. _starpu_free_on_node(unsigned dst_node, uintptr_t addr, size_t size)
  442. {
  443. enum starpu_node_kind kind = starpu_node_get_kind(dst_node);
  444. switch(kind)
  445. {
  446. case STARPU_CPU_RAM:
  447. starpu_free_flags((void*)addr, size,
  448. #if defined(STARPU_USE_CUDA) && !defined(HAVE_CUDA_MEMCPY_PEER) && !defined(STARPU_SIMGRID)
  449. 0
  450. #else
  451. STARPU_MALLOC_PINNED
  452. #endif
  453. );
  454. break;
  455. #if defined(STARPU_USE_CUDA) || defined(STARPU_SIMGRID)
  456. case STARPU_CUDA_RAM:
  457. {
  458. #ifdef STARPU_SIMGRID
  459. STARPU_PTHREAD_MUTEX_LOCK(&cuda_alloc_mutex);
  460. /* Sleep for the free */
  461. MSG_process_sleep(0.000750);
  462. STARPU_PTHREAD_MUTEX_UNLOCK(&cuda_alloc_mutex);
  463. #else
  464. cudaError_t err;
  465. err = cudaFree((void*)addr);
  466. if (STARPU_UNLIKELY(err != cudaSuccess))
  467. STARPU_CUDA_REPORT_ERROR(err);
  468. #endif
  469. break;
  470. }
  471. #endif
  472. #if defined(STARPU_USE_OPENCL) || defined(STARPU_SIMGRID)
  473. case STARPU_OPENCL_RAM:
  474. {
  475. #ifdef STARPU_SIMGRID
  476. STARPU_PTHREAD_MUTEX_LOCK(&opencl_alloc_mutex);
  477. /* Sleep for the free */
  478. MSG_process_sleep(0.000750);
  479. STARPU_PTHREAD_MUTEX_UNLOCK(&opencl_alloc_mutex);
  480. #else
  481. cl_int err;
  482. err = clReleaseMemObject((void*)addr);
  483. if (STARPU_UNLIKELY(err != CL_SUCCESS))
  484. STARPU_OPENCL_REPORT_ERROR(err);
  485. #endif
  486. break;
  487. }
  488. #endif
  489. case STARPU_DISK_RAM:
  490. {
  491. _starpu_disk_free (dst_node, (void *) addr , size);
  492. break;
  493. }
  494. #ifdef STARPU_USE_MIC
  495. case STARPU_MIC_RAM:
  496. _starpu_mic_free_memory((void*) addr, size, dst_node);
  497. break;
  498. #endif
  499. #ifdef STARPU_USE_SCC
  500. case STARPU_SCC_RAM:
  501. _starpu_scc_free_memory((void *) addr, dst_node);
  502. break;
  503. #endif
  504. default:
  505. STARPU_ABORT();
  506. }
  507. _starpu_memory_manager_deallocate_size(size, dst_node);
  508. }
  509. /*
  510. * On CUDA which has very expensive malloc, for small sizes, allocate big
  511. * chunks divided in blocks, and we actually allocate segments of consecutive
  512. * blocks.
  513. *
  514. * We try to keep the list of chunks with increasing occupancy, so we can
  515. * quickly find free segments to allocate.
  516. */
  517. /* Size of each chunk, 32MiB granularity brings 128 chunks to be allocated in
  518. * order to fill a 4GiB GPU. */
  519. #define CHUNK_SIZE (32*1024*1024)
  520. /* Maximum segment size we will allocate in chunks */
  521. #define CHUNK_ALLOC_MAX (CHUNK_SIZE / 8)
  522. /* Granularity of allocation, i.e. block size, StarPU will never allocate less
  523. * than this.
  524. * 16KiB (i.e. 64x64 float) granularity eats 2MiB RAM for managing a 4GiB GPU.
  525. */
  526. #define CHUNK_ALLOC_MIN (16*1024)
  527. /* Number of blocks */
  528. #define CHUNK_NBLOCKS (CHUNK_SIZE/CHUNK_ALLOC_MIN)
  529. /* Linked list for available segments */
  530. struct block {
  531. int length; /* Number of consecutive free blocks */
  532. int next; /* next free segment */
  533. };
  534. /* One chunk */
  535. LIST_TYPE(_starpu_chunk,
  536. uintptr_t base;
  537. /* Available number of blocks, for debugging */
  538. int available;
  539. /* Overestimation of the maximum size of available segments in this chunk */
  540. int available_max;
  541. /* Bitmap describing availability of the block */
  542. /* Block 0 is always empty, and is just the head of the free segments list */
  543. struct block bitmap[CHUNK_NBLOCKS+1];
  544. )
  545. /* One list of chunks per node */
  546. static struct _starpu_chunk_list *chunks[STARPU_MAXNODES];
  547. /* Number of completely free chunks */
  548. static int nfreechunks[STARPU_MAXNODES];
  549. /* This protects chunks and nfreechunks */
  550. static starpu_pthread_mutex_t chunk_mutex[STARPU_MAXNODES];
  551. void
  552. _starpu_malloc_init(unsigned dst_node)
  553. {
  554. chunks[dst_node] = _starpu_chunk_list_new();
  555. nfreechunks[dst_node] = 0;
  556. STARPU_PTHREAD_MUTEX_INIT(&chunk_mutex[dst_node], NULL);
  557. }
  558. void
  559. _starpu_malloc_shutdown(unsigned dst_node)
  560. {
  561. struct _starpu_chunk *chunk, *next_chunk;
  562. if (!chunks[dst_node])
  563. return;
  564. STARPU_PTHREAD_MUTEX_LOCK(&chunk_mutex[dst_node]);
  565. for (chunk = _starpu_chunk_list_begin(chunks[dst_node]);
  566. chunk != _starpu_chunk_list_end(chunks[dst_node]);
  567. chunk = next_chunk)
  568. {
  569. next_chunk = _starpu_chunk_list_next(chunk);
  570. _starpu_free_on_node(dst_node, chunk->base, CHUNK_SIZE);
  571. _starpu_chunk_list_erase(chunks[dst_node], chunk);
  572. free(chunk);
  573. }
  574. _starpu_chunk_list_delete(chunks[dst_node]);
  575. chunks[dst_node] = NULL;
  576. STARPU_PTHREAD_MUTEX_UNLOCK(&chunk_mutex[dst_node]);
  577. STARPU_PTHREAD_MUTEX_DESTROY(&chunk_mutex[dst_node]);
  578. }
  579. /* Create a new chunk */
  580. static struct _starpu_chunk *_starpu_new_chunk(unsigned dst_node)
  581. {
  582. struct _starpu_chunk *chunk;
  583. uintptr_t base = _starpu_malloc_on_node(dst_node, CHUNK_SIZE);
  584. if (!base)
  585. return NULL;
  586. /* Create a new chunk */
  587. chunk = _starpu_chunk_new();
  588. chunk->base = base;
  589. /* First block is just a fake block pointing to the free segments list */
  590. chunk->bitmap[0].length = 0;
  591. chunk->bitmap[0].next = 1;
  592. /* At first we have only one big segment for the whole chunk */
  593. chunk->bitmap[1].length = CHUNK_NBLOCKS;
  594. chunk->bitmap[1].next = -1;
  595. chunk->available_max = CHUNK_NBLOCKS;
  596. chunk->available = CHUNK_NBLOCKS;
  597. return chunk;
  598. }
  599. uintptr_t
  600. starpu_malloc_on_node(unsigned dst_node, size_t size)
  601. {
  602. /* Big allocation, allocate normally */
  603. if (size > CHUNK_ALLOC_MAX || starpu_node_get_kind(dst_node) != STARPU_CUDA_RAM)
  604. return _starpu_malloc_on_node(dst_node, size);
  605. /* Round up allocation to block size */
  606. int nblocks = (size + CHUNK_ALLOC_MIN - 1) / CHUNK_ALLOC_MIN;
  607. struct _starpu_chunk *chunk;
  608. int prevblock, block;
  609. int available_max;
  610. struct block *bitmap;
  611. STARPU_PTHREAD_MUTEX_LOCK(&chunk_mutex[dst_node]);
  612. /* Try to find a big enough segment among the chunks */
  613. for (chunk = _starpu_chunk_list_begin(chunks[dst_node]);
  614. chunk != _starpu_chunk_list_end(chunks[dst_node]);
  615. chunk = _starpu_chunk_list_next(chunk))
  616. {
  617. if (chunk->available_max < nblocks)
  618. continue;
  619. bitmap = chunk->bitmap;
  620. available_max = 0;
  621. for (prevblock = block = 0;
  622. block != -1;
  623. prevblock = block, block = bitmap[prevblock].next)
  624. {
  625. STARPU_ASSERT(block >= 0 && block <= CHUNK_NBLOCKS);
  626. int length = bitmap[block].length;
  627. if (length >= nblocks) {
  628. if (length >= 2*nblocks)
  629. {
  630. /* This one this has quite some room,
  631. * put it front, to make finding it
  632. * easier next time. */
  633. _starpu_chunk_list_erase(chunks[dst_node], chunk);
  634. _starpu_chunk_list_push_front(chunks[dst_node], chunk);
  635. }
  636. if (chunk->available == CHUNK_NBLOCKS)
  637. /* This one was empty, it's not empty any more */
  638. nfreechunks[dst_node]--;
  639. goto found;
  640. }
  641. if (length > available_max)
  642. available_max = length;
  643. }
  644. /* Didn't find a big enough segment in this chunk, its
  645. * available_max is out of date */
  646. chunk->available_max = available_max;
  647. }
  648. /* Didn't find a big enough segment, create another chunk. */
  649. chunk = _starpu_new_chunk(dst_node);
  650. if (!chunk)
  651. {
  652. /* Really no memory any more, fail */
  653. STARPU_PTHREAD_MUTEX_UNLOCK(&chunk_mutex[dst_node]);
  654. errno = ENOMEM;
  655. return 0;
  656. }
  657. /* And make it easy to find. */
  658. _starpu_chunk_list_push_front(chunks[dst_node], chunk);
  659. bitmap = chunk->bitmap;
  660. prevblock = 0;
  661. block = 1;
  662. found:
  663. chunk->available -= nblocks;
  664. STARPU_ASSERT(bitmap[block].length >= nblocks);
  665. STARPU_ASSERT(block <= CHUNK_NBLOCKS);
  666. if (bitmap[block].length == nblocks)
  667. {
  668. /* Fits exactly, drop this segment from the skip list */
  669. bitmap[prevblock].next = bitmap[block].next;
  670. }
  671. else
  672. {
  673. /* Still some room */
  674. STARPU_ASSERT(block + nblocks <= CHUNK_NBLOCKS);
  675. bitmap[prevblock].next = block + nblocks;
  676. bitmap[block + nblocks].length = bitmap[block].length - nblocks;
  677. bitmap[block + nblocks].next = bitmap[block].next;
  678. }
  679. STARPU_PTHREAD_MUTEX_UNLOCK(&chunk_mutex[dst_node]);
  680. return chunk->base + (block-1) * CHUNK_ALLOC_MIN;
  681. }
  682. void
  683. starpu_free_on_node(unsigned dst_node, uintptr_t addr, size_t size)
  684. {
  685. /* Big allocation, deallocate normally */
  686. if (size > CHUNK_ALLOC_MAX || starpu_node_get_kind(dst_node) != STARPU_CUDA_RAM)
  687. {
  688. _starpu_free_on_node(dst_node, addr, size);
  689. return;
  690. }
  691. struct _starpu_chunk *chunk;
  692. /* Round up allocation to block size */
  693. int nblocks = (size + CHUNK_ALLOC_MIN - 1) / CHUNK_ALLOC_MIN;
  694. STARPU_PTHREAD_MUTEX_LOCK(&chunk_mutex[dst_node]);
  695. for (chunk = _starpu_chunk_list_begin(chunks[dst_node]);
  696. chunk != _starpu_chunk_list_end(chunks[dst_node]);
  697. chunk = _starpu_chunk_list_next(chunk))
  698. if (addr >= chunk->base && addr < chunk->base + CHUNK_SIZE)
  699. break;
  700. STARPU_ASSERT(chunk != _starpu_chunk_list_end(chunks[dst_node]));
  701. struct block *bitmap = chunk->bitmap;
  702. int block = ((addr - chunk->base) / CHUNK_ALLOC_MIN) + 1, prevblock, nextblock;
  703. /* Look for free segment just before this one */
  704. for (prevblock = 0;
  705. prevblock != -1;
  706. prevblock = nextblock)
  707. {
  708. STARPU_ASSERT(prevblock >= 0 && prevblock <= CHUNK_NBLOCKS);
  709. nextblock = bitmap[prevblock].next;
  710. STARPU_ASSERT_MSG(nextblock != block, "It seems data 0x%lx (size %u) on node %u is being freed a second time\n", (unsigned long) addr, (unsigned) size, dst_node);
  711. if (nextblock > block || nextblock == -1)
  712. break;
  713. }
  714. STARPU_ASSERT(prevblock != -1);
  715. chunk->available += nblocks;
  716. /* Insert in free segments list */
  717. bitmap[block].next = nextblock;
  718. bitmap[prevblock].next = block;
  719. bitmap[block].length = nblocks;
  720. STARPU_ASSERT(nextblock >= -1 && nextblock <= CHUNK_NBLOCKS);
  721. if (nextblock == block + nblocks)
  722. {
  723. /* This freed segment is just before a free segment, merge them */
  724. bitmap[block].next = bitmap[nextblock].next;
  725. bitmap[block].length += bitmap[nextblock].length;
  726. if (bitmap[block].length > chunk->available_max)
  727. chunk->available_max = bitmap[block].length;
  728. }
  729. if (prevblock > 0 && prevblock + bitmap[prevblock].length == block)
  730. {
  731. /* This free segment is just after a free segment, merge them */
  732. bitmap[prevblock].next = bitmap[block].next;
  733. bitmap[prevblock].length += bitmap[block].length;
  734. if (bitmap[prevblock].length > chunk->available_max)
  735. chunk->available_max = bitmap[prevblock].length;
  736. block = prevblock;
  737. }
  738. if (chunk->available == CHUNK_NBLOCKS)
  739. {
  740. /* This chunk is now empty, but avoid chunk free/alloc
  741. * ping-pong by keeping some of these. */
  742. if (nfreechunks[dst_node] >= 1) {
  743. /* We already have free chunks, release this one */
  744. _starpu_free_on_node(dst_node, chunk->base, CHUNK_SIZE);
  745. _starpu_chunk_list_erase(chunks[dst_node], chunk);
  746. free(chunk);
  747. } else
  748. nfreechunks[dst_node]++;
  749. }
  750. else
  751. {
  752. /* Freed some room, put this first in chunks list */
  753. _starpu_chunk_list_erase(chunks[dst_node], chunk);
  754. _starpu_chunk_list_push_front(chunks[dst_node], chunk);
  755. }
  756. STARPU_PTHREAD_MUTEX_UNLOCK(&chunk_mutex[dst_node]);
  757. }