malloc.c 22 KB

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