malloc.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011-2014,2016-2017 Inria
  4. * Copyright (C) 2009-2018 Université de Bordeaux
  5. * Copyright (C) 2010-2017 CNRS
  6. *
  7. * StarPU is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation; either version 2.1 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * StarPU is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. *
  16. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  17. */
  18. #include <errno.h>
  19. #include <core/workers.h>
  20. #include <core/disk.h>
  21. #include <common/config.h>
  22. #include <common/fxt.h>
  23. #include <starpu.h>
  24. #include <drivers/opencl/driver_opencl.h>
  25. #include <datawizard/memory_manager.h>
  26. #include <datawizard/memory_nodes.h>
  27. #include <datawizard/malloc.h>
  28. #include <core/simgrid.h>
  29. #include <core/task.h>
  30. #ifdef STARPU_SIMGRID
  31. #include <sys/mman.h>
  32. #include <fcntl.h>
  33. #include <smpi/smpi.h>
  34. #endif
  35. #ifdef STARPU_HAVE_HWLOC
  36. #include <hwloc.h>
  37. #ifndef HWLOC_API_VERSION
  38. #define HWLOC_OBJ_PU HWLOC_OBJ_PROC
  39. #endif
  40. #if HWLOC_API_VERSION < 0x00010b00
  41. #define HWLOC_OBJ_NUMANODE HWLOC_OBJ_NODE
  42. #endif
  43. #endif
  44. #ifndef O_BINARY
  45. #define O_BINARY 0
  46. #endif
  47. #ifndef MAP_POPULATE
  48. #define MAP_POPULATE 0
  49. #endif
  50. static size_t _malloc_align = sizeof(void*);
  51. static int disable_pinning;
  52. static int malloc_on_node_default_flags[STARPU_MAXNODES];
  53. /* This file is used for implementing "folded" allocation */
  54. #ifdef STARPU_SIMGRID
  55. #if SIMGRID_VERSION < 31500 || SIMGRID_VERSION == 31559
  56. /* TODO: drop when simgrid 3.15 is reasonably largely used by people who need the feature */
  57. static int bogusfile = -1;
  58. static unsigned long _starpu_malloc_simulation_fold;
  59. #endif
  60. #endif
  61. void starpu_malloc_set_align(size_t align)
  62. {
  63. STARPU_ASSERT_MSG(!(align & (align - 1)), "Alignment given to starpu_malloc_set_align (%lu) must be a power of two", (unsigned long) align);
  64. if (_malloc_align < align)
  65. _malloc_align = align;
  66. }
  67. #if (defined(STARPU_USE_CUDA) && !defined(STARPU_HAVE_CUDA_MEMCPY_PEER))// || defined(STARPU_USE_OPENCL)
  68. struct malloc_pinned_codelet_struct
  69. {
  70. void **ptr;
  71. size_t dim;
  72. };
  73. #endif
  74. /* Would be difficult to do it this way, we need to remember the cl_mem to be able to free it later... */
  75. //#ifdef STARPU_USE_OPENCL
  76. //static void malloc_pinned_opencl_codelet(void *buffers[] STARPU_ATTRIBUTE_UNUSED, void *arg)
  77. //{
  78. // struct malloc_pinned_codelet_struct *s = arg;
  79. // // _STARPU_MALLOC(*(s->ptr), s->dim);
  80. // starpu_opencl_allocate_memory(devid, (void **)(s->ptr), s->dim, CL_MEM_READ_WRITE|CL_MEM_ALLOC_HOST_PTR);
  81. //}
  82. //#endif
  83. #if defined(STARPU_USE_CUDA) && !defined(STARPU_HAVE_CUDA_MEMCPY_PEER) && !defined(STARPU_SIMGRID)
  84. static void malloc_pinned_cuda_codelet(void *buffers[] STARPU_ATTRIBUTE_UNUSED, void *arg)
  85. {
  86. struct malloc_pinned_codelet_struct *s = arg;
  87. cudaError_t cures;
  88. cures = cudaHostAlloc((void **)(s->ptr), s->dim, cudaHostAllocPortable);
  89. if (STARPU_UNLIKELY(cures))
  90. STARPU_CUDA_REPORT_ERROR(cures);
  91. }
  92. #endif
  93. #if (defined(STARPU_USE_CUDA) && !defined(STARPU_HAVE_CUDA_MEMCPY_PEER)) && !defined(STARPU_SIMGRID)// || defined(STARPU_USE_OPENCL)
  94. static struct starpu_perfmodel malloc_pinned_model =
  95. {
  96. .type = STARPU_HISTORY_BASED,
  97. .symbol = "malloc_pinned"
  98. };
  99. static struct starpu_codelet malloc_pinned_cl =
  100. {
  101. .cuda_funcs = {malloc_pinned_cuda_codelet},
  102. //#ifdef STARPU_USE_OPENCL
  103. // .opencl_funcs = {malloc_pinned_opencl_codelet},
  104. //#endif
  105. .nbuffers = 0,
  106. .model = &malloc_pinned_model
  107. };
  108. #endif
  109. /* Allocation in CPU RAM */
  110. int starpu_malloc_flags(void **A, size_t dim, int flags)
  111. {
  112. return _starpu_malloc_flags_on_node(STARPU_MAIN_RAM, A, dim, flags);
  113. }
  114. int _starpu_malloc_flags_on_node(unsigned dst_node, void **A, size_t dim, int flags)
  115. {
  116. int ret=0;
  117. STARPU_ASSERT(A);
  118. if (flags & STARPU_MALLOC_COUNT)
  119. {
  120. if (!(flags & STARPU_MALLOC_NORECLAIM))
  121. while (starpu_memory_allocate(dst_node, dim, flags) != 0)
  122. {
  123. size_t freed;
  124. size_t reclaim = 2 * dim;
  125. _STARPU_DEBUG("There is not enough memory left, we are going to reclaim %ld\n", (long)reclaim);
  126. _STARPU_TRACE_START_MEMRECLAIM(dst_node,0);
  127. freed = _starpu_memory_reclaim_generic(dst_node, 0, reclaim);
  128. _STARPU_TRACE_END_MEMRECLAIM(dst_node,0);
  129. if (freed < dim && !(flags & STARPU_MEMORY_WAIT))
  130. {
  131. // We could not reclaim enough memory
  132. *A = NULL;
  133. return -ENOMEM;
  134. }
  135. }
  136. else if (flags & STARPU_MEMORY_WAIT)
  137. starpu_memory_allocate(dst_node, dim, flags);
  138. else
  139. starpu_memory_allocate(dst_node, dim, flags | STARPU_MEMORY_OVERFLOW);
  140. }
  141. if (flags & STARPU_MALLOC_PINNED && disable_pinning <= 0 && STARPU_RUNNING_ON_VALGRIND == 0)
  142. {
  143. if (_starpu_can_submit_cuda_task())
  144. {
  145. #ifdef STARPU_SIMGRID
  146. /* FIXME: CUDA seems to be taking 650µs every 1MiB.
  147. * Ideally we would simulate this batching in 1MiB requests
  148. * instead of computing an average value.
  149. */
  150. if (_starpu_simgrid_cuda_malloc_cost())
  151. MSG_process_sleep((float) dim * 0.000650 / 1048576.);
  152. #else /* STARPU_SIMGRID */
  153. #ifdef STARPU_USE_CUDA
  154. #ifdef STARPU_HAVE_CUDA_MEMCPY_PEER
  155. cudaError_t cures;
  156. cures = cudaHostAlloc(A, dim, cudaHostAllocPortable);
  157. if (STARPU_UNLIKELY(cures))
  158. {
  159. STARPU_CUDA_REPORT_ERROR(cures);
  160. ret = -ENOMEM;
  161. }
  162. goto end;
  163. #else
  164. int push_res;
  165. /* Old versions of CUDA are not thread-safe, we have to
  166. * run cudaHostAlloc from CUDA workers */
  167. STARPU_ASSERT_MSG(_starpu_worker_may_perform_blocking_calls(), "without CUDA peer allocation support, pinned allocation must not be done from task or callback");
  168. struct malloc_pinned_codelet_struct s =
  169. {
  170. .ptr = A,
  171. .dim = dim
  172. };
  173. malloc_pinned_cl.where = STARPU_CUDA;
  174. struct starpu_task *task = starpu_task_create();
  175. task->name = "cuda_malloc_pinned";
  176. task->callback_func = NULL;
  177. task->cl = &malloc_pinned_cl;
  178. task->cl_arg = &s;
  179. task->type = STARPU_TASK_TYPE_INTERNAL;
  180. task->synchronous = 1;
  181. _starpu_exclude_task_from_dag(task);
  182. push_res = _starpu_task_submit_internally(task);
  183. STARPU_ASSERT(push_res != -ENODEV);
  184. goto end;
  185. #endif /* STARPU_HAVE_CUDA_MEMCPY_PEER */
  186. #endif /* STARPU_USE_CUDA */
  187. // }
  188. // else if (_starpu_can_submit_opencl_task())
  189. // {
  190. //#ifdef STARPU_USE_OPENCL
  191. // int push_res;
  192. //
  193. // STARPU_ASSERT_MSG(_starpu_worker_may_perform_blocking_calls(), "pinned OpenCL allocation must not be done from task or callback");
  194. //
  195. // struct malloc_pinned_codelet_struct s =
  196. // {
  197. // .ptr = A,
  198. // .dim = dim
  199. // };
  200. //
  201. // malloc_pinned_cl.where = STARPU_OPENCL;
  202. // struct starpu_task *task = starpu_task_create();
  203. // task->name = "opencl_malloc_pinned";
  204. // task->callback_func = NULL;
  205. // task->cl = &malloc_pinned_cl;
  206. // task->cl_arg = &s;
  207. // task->synchronous = 1;
  208. // task->type = STARPU_TASK_TYPE_INTERNAL;
  209. //
  210. // _starpu_exclude_task_from_dag(task);
  211. //
  212. // push_res = _starpu_task_submit_internally(task);
  213. // STARPU_ASSERT(push_res != -ENODEV);
  214. // goto end;
  215. //#endif /* STARPU_USE_OPENCL */
  216. #endif /* STARPU_SIMGRID */
  217. }
  218. }
  219. #ifdef STARPU_SIMGRID
  220. if (flags & STARPU_MALLOC_SIMULATION_FOLDED)
  221. {
  222. #if SIMGRID_VERSION >= 31500 && SIMGRID_VERSION != 31559
  223. *A = SMPI_SHARED_MALLOC(dim);
  224. #else
  225. /* TODO: drop when simgrid 3.15 is reasonably largely used by people who need the feature */
  226. /* Use "folded" allocation: the same file is mapped several
  227. * times contiguously, to get a memory area one can read/write,
  228. * without consuming memory */
  229. /* First reserve memory area */
  230. void *buf = mmap (NULL, dim, PROT_READ|PROT_WRITE, MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
  231. unsigned i;
  232. if (buf == MAP_FAILED)
  233. {
  234. _STARPU_DISP("Warning: could not allocate %luMiB of memory, you need to run \"sysctl vm.overcommit_memory=1\" as root to allow so big allocations\n", (unsigned long) (dim >> 20));
  235. ret = -ENOMEM;
  236. *A = NULL;
  237. }
  238. else
  239. {
  240. if (bogusfile == -1)
  241. {
  242. char *path = starpu_getenv("TMPDIR");
  243. if (!path)
  244. path = starpu_getenv("TEMP");
  245. if (!path)
  246. path = starpu_getenv("TMP");
  247. if (!path)
  248. path = "/tmp";
  249. /* Create bogus file if not done already */
  250. char *name = _starpu_mktemp(path, O_RDWR | O_BINARY, &bogusfile);
  251. char *dumb;
  252. if (!name)
  253. {
  254. ret = errno;
  255. munmap(buf, dim);
  256. *A = NULL;
  257. goto end;
  258. }
  259. unlink(name);
  260. free(name);
  261. _STARPU_CALLOC(dumb, 1,_starpu_malloc_simulation_fold);
  262. write(bogusfile, dumb, _starpu_malloc_simulation_fold);
  263. free(dumb);
  264. }
  265. /* Map the bogus file in place of the anonymous memory */
  266. for (i = 0; i < dim / _starpu_malloc_simulation_fold; i++)
  267. {
  268. void *pos = (void*) ((unsigned long) buf + i * _starpu_malloc_simulation_fold);
  269. void *res = mmap(pos, _starpu_malloc_simulation_fold, PROT_READ|PROT_WRITE, MAP_FIXED|MAP_SHARED|MAP_POPULATE, bogusfile, 0);
  270. STARPU_ASSERT_MSG(res == pos, "Could not map folded virtual memory (%s). Do you perhaps need to increase the STARPU_MALLOC_SIMULATION_FOLD environment variable or the sysctl vm.max_map_count?", strerror(errno));
  271. }
  272. if (dim % _starpu_malloc_simulation_fold)
  273. {
  274. void *pos = (void*) ((unsigned long) buf + i * _starpu_malloc_simulation_fold);
  275. void *res = mmap(pos, dim % _starpu_malloc_simulation_fold, PROT_READ|PROT_WRITE, MAP_FIXED|MAP_SHARED|MAP_POPULATE, bogusfile, 0);
  276. STARPU_ASSERT_MSG(res == pos, "Could not map folded virtual memory (%s). Do you perhaps need to increase the STARPU_MALLOC_SIMULATION_FOLD environment variable or the sysctl vm.max_map_count?", strerror(errno));
  277. }
  278. *A = buf;
  279. }
  280. #endif
  281. }
  282. else
  283. #endif
  284. if (_starpu_can_submit_scc_task())
  285. {
  286. #ifdef STARPU_USE_SCC
  287. _starpu_scc_allocate_shared_memory(A, dim);
  288. #endif
  289. }
  290. #ifdef STARPU_HAVE_HWLOC
  291. if (starpu_memory_nodes_get_numa_count() > 1)
  292. {
  293. struct _starpu_machine_config *config = _starpu_get_machine_config();
  294. hwloc_topology_t hwtopology = config->topology.hwtopology;
  295. hwloc_obj_t numa_node_obj = hwloc_get_obj_by_type(hwtopology, HWLOC_OBJ_NUMANODE, starpu_memory_nodes_numa_id_to_hwloclogid(dst_node));
  296. hwloc_bitmap_t nodeset = numa_node_obj->nodeset;
  297. #if HWLOC_API_VERSION >= 0x00020000
  298. *A = hwloc_alloc_membind(hwtopology, dim, nodeset, HWLOC_MEMBIND_BIND, HWLOC_MEMBIND_BYNODESET | HWLOC_MEMBIND_NOCPUBIND);
  299. #else
  300. *A = hwloc_alloc_membind_nodeset(hwtopology, dim, nodeset, HWLOC_MEMBIND_BIND, HWLOC_MEMBIND_NOCPUBIND);
  301. #endif
  302. //fprintf(stderr, "Allocation %lu bytes on NUMA node %d [%p]\n", (unsigned long) dim, starpu_memnode_get_numaphysid(dst_node), *A);
  303. if (!*A)
  304. ret = -ENOMEM;
  305. }
  306. #endif /* STARPU_HAVE_HWLOC */
  307. else
  308. #ifdef STARPU_HAVE_POSIX_MEMALIGN
  309. if (_malloc_align != sizeof(void*))
  310. {
  311. if (posix_memalign(A, _malloc_align, dim))
  312. {
  313. ret = -ENOMEM;
  314. *A = NULL;
  315. }
  316. }
  317. else
  318. #elif defined(STARPU_HAVE_MEMALIGN)
  319. if (_malloc_align != sizeof(void*))
  320. {
  321. *A = memalign(_malloc_align, dim);
  322. if (!*A)
  323. ret = -ENOMEM;
  324. }
  325. else
  326. #endif /* STARPU_HAVE_POSIX_MEMALIGN */
  327. {
  328. *A = malloc(dim);
  329. if (!*A)
  330. ret = -ENOMEM;
  331. }
  332. #if defined(STARPU_SIMGRID) || defined(STARPU_USE_CUDA)
  333. end:
  334. #endif
  335. if (ret == 0)
  336. {
  337. STARPU_ASSERT_MSG(*A, "Failed to allocated memory of size %lu b\n", (unsigned long)dim);
  338. }
  339. else if (flags & STARPU_MALLOC_COUNT)
  340. {
  341. starpu_memory_deallocate(dst_node, dim);
  342. }
  343. return ret;
  344. }
  345. int starpu_malloc(void **A, size_t dim)
  346. {
  347. return starpu_malloc_flags(A, dim, STARPU_MALLOC_PINNED);
  348. }
  349. #if defined(STARPU_USE_CUDA) && !defined(STARPU_HAVE_CUDA_MEMCPY_PEER) && !defined(STARPU_SIMGRID)
  350. static void free_pinned_cuda_codelet(void *buffers[] STARPU_ATTRIBUTE_UNUSED, void *arg)
  351. {
  352. cudaError_t cures;
  353. cures = cudaFreeHost(arg);
  354. if (STARPU_UNLIKELY(cures))
  355. STARPU_CUDA_REPORT_ERROR(cures);
  356. }
  357. #endif
  358. //#ifdef STARPU_USE_OPENCL
  359. //static void free_pinned_opencl_codelet(void *buffers[] STARPU_ATTRIBUTE_UNUSED, void *arg)
  360. //{
  361. // // free(arg);
  362. // int err = clReleaseMemObject(arg);
  363. // if (err != CL_SUCCESS) STARPU_OPENCL_REPORT_ERROR(err);
  364. //}
  365. //#endif
  366. #if defined(STARPU_USE_CUDA) && !defined(STARPU_HAVE_CUDA_MEMCPY_PEER) && !defined(STARPU_SIMGRID) // || defined(STARPU_USE_OPENCL)
  367. static struct starpu_perfmodel free_pinned_model =
  368. {
  369. .type = STARPU_HISTORY_BASED,
  370. .symbol = "free_pinned"
  371. };
  372. static struct starpu_codelet free_pinned_cl =
  373. {
  374. .cuda_funcs = {free_pinned_cuda_codelet},
  375. //#ifdef STARPU_USE_OPENCL
  376. // .opencl_funcs = {free_pinned_opencl_codelet},
  377. //#endif
  378. .nbuffers = 0,
  379. .model = &free_pinned_model
  380. };
  381. #endif
  382. int starpu_free_flags(void *A, size_t dim, int flags)
  383. {
  384. return _starpu_free_flags_on_node(STARPU_MAIN_RAM, A, dim, flags);
  385. }
  386. int _starpu_free_flags_on_node(unsigned dst_node, void *A, size_t dim, int flags)
  387. {
  388. if (flags & STARPU_MALLOC_PINNED && disable_pinning <= 0 && STARPU_RUNNING_ON_VALGRIND == 0)
  389. {
  390. if (_starpu_can_submit_cuda_task())
  391. {
  392. #ifdef STARPU_SIMGRID
  393. /* TODO: simulate CUDA barrier */
  394. #else /* !STARPU_SIMGRID */
  395. #ifdef STARPU_USE_CUDA
  396. #ifndef STARPU_HAVE_CUDA_MEMCPY_PEER
  397. if (!starpu_is_initialized())
  398. {
  399. #endif
  400. /* This is especially useful when starpu_free is called from
  401. * the GCC-plugin. starpu_shutdown will probably have already
  402. * been called, so we will not be able to submit a task. */
  403. cudaError_t err = cudaFreeHost(A);
  404. if (STARPU_UNLIKELY(err))
  405. STARPU_CUDA_REPORT_ERROR(err);
  406. goto out;
  407. #ifndef STARPU_HAVE_CUDA_MEMCPY_PEER
  408. }
  409. else
  410. {
  411. int push_res;
  412. STARPU_ASSERT_MSG(_starpu_worker_may_perform_blocking_calls(), "without CUDA peer allocation support, pinned deallocation must not be done from task or callback");
  413. free_pinned_cl.where = STARPU_CUDA;
  414. struct starpu_task *task = starpu_task_create();
  415. task->name = "cuda_free_pinned";
  416. task->callback_func = NULL;
  417. task->cl = &free_pinned_cl;
  418. task->cl_arg = A;
  419. task->synchronous = 1;
  420. task->type = STARPU_TASK_TYPE_INTERNAL;
  421. _starpu_exclude_task_from_dag(task);
  422. push_res = _starpu_task_submit_internally(task);
  423. STARPU_ASSERT(push_res != -ENODEV);
  424. goto out;
  425. }
  426. #endif /* STARPU_HAVE_CUDA_MEMCPY_PEER */
  427. #endif /* STARPU_USE_CUDA */
  428. #endif /* STARPU_SIMGRID */
  429. }
  430. // else if (_starpu_can_submit_opencl_task())
  431. // {
  432. //#ifdef STARPU_USE_OPENCL
  433. // int push_res;
  434. //
  435. // STARPU_ASSERT_MSG(_starpu_worker_may_perform_blocking_calls(), "pinned OpenCL deallocation must not be done from task or callback");
  436. //
  437. // free_pinned_cl.where = STARPU_OPENCL;
  438. // struct starpu_task *task = starpu_task_create();
  439. // task->name = "opencl_free_pinned";
  440. // task->callback_func = NULL;
  441. // task->cl = &free_pinned_cl;
  442. // task->cl_arg = A;
  443. // task->synchronous = 1;
  444. // task->type = STARPU_TASK_TYPE_INTERNAL;
  445. //
  446. // _starpu_exclude_task_from_dag(task);
  447. //
  448. // push_res = starpu_task_submit(task);
  449. // STARPU_ASSERT(push_res != -ENODEV);
  450. // goto out;
  451. // }
  452. //#endif
  453. }
  454. #ifdef STARPU_SIMGRID
  455. if (flags & STARPU_MALLOC_SIMULATION_FOLDED)
  456. {
  457. #if SIMGRID_VERSION >= 31500 && SIMGRID_VERSION != 31559
  458. SMPI_SHARED_FREE(A);
  459. #else
  460. /* TODO: drop when simgrid 3.15 is reasonably largely used by people who need the feature */
  461. munmap(A, dim);
  462. #endif
  463. }
  464. else
  465. #endif
  466. if (_starpu_can_submit_scc_task())
  467. {
  468. #ifdef STARPU_USE_SCC
  469. _starpu_scc_free_shared_memory(A);
  470. #endif
  471. }
  472. #ifdef STARPU_HAVE_HWLOC
  473. else if (starpu_memory_nodes_get_numa_count() > 1)
  474. {
  475. struct _starpu_machine_config *config = _starpu_get_machine_config();
  476. hwloc_topology_t hwtopology = config->topology.hwtopology;
  477. hwloc_free(hwtopology, A, dim);
  478. }
  479. #endif /* STARPU_HAVE_HWLOC */
  480. else
  481. free(A);
  482. #if !defined(STARPU_SIMGRID) && defined(STARPU_USE_CUDA)
  483. out:
  484. #endif
  485. if (flags & STARPU_MALLOC_COUNT)
  486. {
  487. starpu_memory_deallocate(dst_node, dim);
  488. }
  489. return 0;
  490. }
  491. int starpu_free(void *A)
  492. {
  493. return starpu_free_flags(A, 0, STARPU_MALLOC_PINNED);
  494. }
  495. #ifdef STARPU_SIMGRID
  496. static starpu_pthread_mutex_t cuda_alloc_mutex = STARPU_PTHREAD_MUTEX_INITIALIZER;
  497. static starpu_pthread_mutex_t opencl_alloc_mutex = STARPU_PTHREAD_MUTEX_INITIALIZER;
  498. #endif
  499. static uintptr_t
  500. _starpu_malloc_on_node(unsigned dst_node, size_t size, int flags)
  501. {
  502. uintptr_t addr = 0;
  503. #if defined(STARPU_USE_CUDA) && !defined(STARPU_SIMGRID)
  504. cudaError_t status;
  505. #endif
  506. /* Handle count first */
  507. if (flags & STARPU_MALLOC_COUNT)
  508. {
  509. if (starpu_memory_allocate(dst_node, size, flags) != 0)
  510. return 0;
  511. /* And prevent double-count in starpu_malloc_flags */
  512. flags &= ~STARPU_MALLOC_COUNT;
  513. }
  514. switch(starpu_node_get_kind(dst_node))
  515. {
  516. case STARPU_CPU_RAM:
  517. {
  518. _starpu_malloc_flags_on_node(dst_node, (void**) &addr, size,
  519. #if defined(STARPU_USE_CUDA) && !defined(STARPU_HAVE_CUDA_MEMCPY_PEER) && !defined(STARPU_SIMGRID)
  520. /* without memcpy_peer, we can not
  521. * allocated pinned memory, since it
  522. * requires waiting for a task, and we
  523. * may be called with a spinlock held
  524. */
  525. flags & ~STARPU_MALLOC_PINNED
  526. #else
  527. flags
  528. #endif
  529. );
  530. break;
  531. }
  532. #if defined(STARPU_USE_CUDA) || defined(STARPU_SIMGRID)
  533. case STARPU_CUDA_RAM:
  534. {
  535. #ifdef STARPU_SIMGRID
  536. static uintptr_t last[STARPU_MAXNODES];
  537. #ifdef STARPU_DEVEL
  538. #warning TODO: record used memory, using a simgrid property to know the available memory
  539. #endif
  540. /* Sleep for the allocation */
  541. STARPU_PTHREAD_MUTEX_LOCK(&cuda_alloc_mutex);
  542. if (_starpu_simgrid_cuda_malloc_cost())
  543. MSG_process_sleep(0.000175);
  544. if (!last[dst_node])
  545. last[dst_node] = 1<<10;
  546. addr = last[dst_node];
  547. last[dst_node]+=size;
  548. STARPU_ASSERT(last[dst_node] >= addr);
  549. STARPU_PTHREAD_MUTEX_UNLOCK(&cuda_alloc_mutex);
  550. #else
  551. unsigned devid = _starpu_memory_node_get_devid(dst_node);
  552. #if defined(STARPU_HAVE_CUDA_MEMCPY_PEER)
  553. starpu_cuda_set_device(devid);
  554. #else
  555. struct _starpu_worker *worker = _starpu_get_local_worker_key();
  556. if (!worker || worker->arch != STARPU_CUDA_WORKER || worker->devid != devid)
  557. STARPU_ASSERT_MSG(0, "CUDA peer access is not available with this version of CUDA");
  558. #endif
  559. status = cudaMalloc((void **)&addr, size);
  560. if (!addr || (status != cudaSuccess))
  561. {
  562. if (STARPU_UNLIKELY(status != cudaErrorMemoryAllocation))
  563. STARPU_CUDA_REPORT_ERROR(status);
  564. addr = 0;
  565. }
  566. #endif
  567. break;
  568. }
  569. #endif
  570. #if defined(STARPU_USE_OPENCL) || defined(STARPU_SIMGRID)
  571. case STARPU_OPENCL_RAM:
  572. {
  573. #ifdef STARPU_SIMGRID
  574. static uintptr_t last[STARPU_MAXNODES];
  575. /* Sleep for the allocation */
  576. STARPU_PTHREAD_MUTEX_LOCK(&opencl_alloc_mutex);
  577. if (_starpu_simgrid_cuda_malloc_cost())
  578. MSG_process_sleep(0.000175);
  579. if (!last[dst_node])
  580. last[dst_node] = 1<<10;
  581. addr = last[dst_node];
  582. last[dst_node]+=size;
  583. STARPU_ASSERT(last[dst_node] >= addr);
  584. STARPU_PTHREAD_MUTEX_UNLOCK(&opencl_alloc_mutex);
  585. #else
  586. int ret;
  587. cl_mem ptr;
  588. ret = starpu_opencl_allocate_memory(_starpu_memory_node_get_devid(dst_node), &ptr, size, CL_MEM_READ_WRITE);
  589. if (ret)
  590. {
  591. addr = 0;
  592. }
  593. else
  594. {
  595. addr = (uintptr_t)ptr;
  596. }
  597. #endif
  598. break;
  599. }
  600. #endif
  601. case STARPU_DISK_RAM:
  602. {
  603. addr = (uintptr_t) _starpu_disk_alloc(dst_node, size);
  604. break;
  605. }
  606. #ifdef STARPU_USE_MIC
  607. case STARPU_MIC_RAM:
  608. if (_starpu_mic_allocate_memory((void **)(&addr), size, dst_node))
  609. addr = 0;
  610. break;
  611. #endif
  612. #ifdef STARPU_USE_MPI_MASTER_SLAVE
  613. case STARPU_MPI_MS_RAM:
  614. if (_starpu_mpi_src_allocate_memory((void **)(&addr), size, dst_node))
  615. addr = 0;
  616. break;
  617. #endif
  618. #ifdef STARPU_USE_SCC
  619. case STARPU_SCC_RAM:
  620. if (_starpu_scc_allocate_memory((void **)(&addr), size, dst_node))
  621. addr = 0;
  622. break;
  623. #endif
  624. default:
  625. STARPU_ABORT();
  626. }
  627. if (addr == 0)
  628. {
  629. // Allocation failed, gives the memory back to the memory manager
  630. _STARPU_TRACE_MEMORY_FULL(size);
  631. starpu_memory_deallocate(dst_node, size);
  632. }
  633. return addr;
  634. }
  635. void
  636. _starpu_free_on_node_flags(unsigned dst_node, uintptr_t addr, size_t size, int flags)
  637. {
  638. int count = flags & STARPU_MALLOC_COUNT;
  639. flags &= ~STARPU_MALLOC_COUNT;
  640. enum starpu_node_kind kind = starpu_node_get_kind(dst_node);
  641. switch(kind)
  642. {
  643. case STARPU_CPU_RAM:
  644. _starpu_free_flags_on_node(dst_node, (void*)addr, size,
  645. #if defined(STARPU_USE_CUDA) && !defined(STARPU_HAVE_CUDA_MEMCPY_PEER) && !defined(STARPU_SIMGRID)
  646. flags & ~STARPU_MALLOC_PINNED
  647. #else
  648. flags
  649. #endif
  650. );
  651. break;
  652. #if defined(STARPU_USE_CUDA) || defined(STARPU_SIMGRID)
  653. case STARPU_CUDA_RAM:
  654. {
  655. #ifdef STARPU_SIMGRID
  656. STARPU_PTHREAD_MUTEX_LOCK(&cuda_alloc_mutex);
  657. /* Sleep for the free */
  658. if (_starpu_simgrid_cuda_malloc_cost())
  659. MSG_process_sleep(0.000750);
  660. STARPU_PTHREAD_MUTEX_UNLOCK(&cuda_alloc_mutex);
  661. /* CUDA also synchronizes roughly everything on cudaFree */
  662. _starpu_simgrid_sync_gpus();
  663. #else
  664. cudaError_t err;
  665. unsigned devid = _starpu_memory_node_get_devid(dst_node);
  666. #if defined(STARPU_HAVE_CUDA_MEMCPY_PEER)
  667. starpu_cuda_set_device(devid);
  668. #else
  669. struct _starpu_worker *worker = _starpu_get_local_worker_key();
  670. if (!worker || worker->arch != STARPU_CUDA_WORKER || worker->devid != devid)
  671. STARPU_ASSERT_MSG(0, "CUDA peer access is not available with this version of CUDA");
  672. #endif
  673. err = cudaFree((void*)addr);
  674. if (STARPU_UNLIKELY(err != cudaSuccess
  675. #ifdef STARPU_OPENMP
  676. /* When StarPU is used as Open Runtime support,
  677. * starpu_omp_shutdown() will usually be called from a
  678. * destructor, in which case cudaThreadExit() reports a
  679. * cudaErrorCudartUnloading here. There should not
  680. * be any remaining tasks running at this point so
  681. * we can probably ignore it without much consequences. */
  682. && err != cudaErrorCudartUnloading
  683. #endif /* STARPU_OPENMP */
  684. ))
  685. STARPU_CUDA_REPORT_ERROR(err);
  686. #endif
  687. break;
  688. }
  689. #endif
  690. #if defined(STARPU_USE_OPENCL) || defined(STARPU_SIMGRID)
  691. case STARPU_OPENCL_RAM:
  692. {
  693. #ifdef STARPU_SIMGRID
  694. STARPU_PTHREAD_MUTEX_LOCK(&opencl_alloc_mutex);
  695. /* Sleep for the free */
  696. if (_starpu_simgrid_cuda_malloc_cost())
  697. MSG_process_sleep(0.000750);
  698. STARPU_PTHREAD_MUTEX_UNLOCK(&opencl_alloc_mutex);
  699. #else
  700. cl_int err;
  701. err = clReleaseMemObject((void*)addr);
  702. if (STARPU_UNLIKELY(err != CL_SUCCESS))
  703. STARPU_OPENCL_REPORT_ERROR(err);
  704. #endif
  705. break;
  706. }
  707. #endif
  708. case STARPU_DISK_RAM:
  709. {
  710. _starpu_disk_free (dst_node, (void *) addr , size);
  711. break;
  712. }
  713. #ifdef STARPU_USE_MIC
  714. case STARPU_MIC_RAM:
  715. _starpu_mic_free_memory((void*) addr, size, dst_node);
  716. break;
  717. #endif
  718. #ifdef STARPU_USE_MPI_MASTER_SLAVE
  719. case STARPU_MPI_MS_RAM:
  720. _starpu_mpi_source_free_memory((void*) addr, dst_node);
  721. break;
  722. #endif
  723. #ifdef STARPU_USE_SCC
  724. case STARPU_SCC_RAM:
  725. _starpu_scc_free_memory((void *) addr, dst_node);
  726. break;
  727. #endif
  728. default:
  729. STARPU_ABORT();
  730. }
  731. if (count)
  732. starpu_memory_deallocate(dst_node, size);
  733. }
  734. int
  735. starpu_memory_pin(void *addr STARPU_ATTRIBUTE_UNUSED, size_t size STARPU_ATTRIBUTE_UNUSED)
  736. {
  737. if (STARPU_MALLOC_PINNED && disable_pinning <= 0 && STARPU_RUNNING_ON_VALGRIND == 0)
  738. {
  739. #if defined(STARPU_USE_CUDA) && defined(STARPU_HAVE_CUDA_MEMCPY_PEER)
  740. if (cudaHostRegister(addr, size, cudaHostRegisterPortable) != cudaSuccess)
  741. return -1;
  742. #endif
  743. }
  744. return 0;
  745. }
  746. int
  747. starpu_memory_unpin(void *addr STARPU_ATTRIBUTE_UNUSED, size_t size STARPU_ATTRIBUTE_UNUSED)
  748. {
  749. if (STARPU_MALLOC_PINNED && disable_pinning <= 0 && STARPU_RUNNING_ON_VALGRIND == 0)
  750. {
  751. #if defined(STARPU_USE_CUDA) && defined(STARPU_HAVE_CUDA_MEMCPY_PEER)
  752. if (cudaHostUnregister(addr) != cudaSuccess)
  753. return -1;
  754. #endif
  755. }
  756. return 0;
  757. }
  758. /*
  759. * On CUDA which has very expensive malloc, for small sizes, allocate big
  760. * chunks divided in blocks, and we actually allocate segments of consecutive
  761. * blocks.
  762. *
  763. * We try to keep the list of chunks with increasing occupancy, so we can
  764. * quickly find free segments to allocate.
  765. */
  766. /* Size of each chunk, 32MiB granularity brings 128 chunks to be allocated in
  767. * order to fill a 4GiB GPU. */
  768. #define CHUNK_SIZE (32*1024*1024)
  769. /* Maximum segment size we will allocate in chunks */
  770. #define CHUNK_ALLOC_MAX (CHUNK_SIZE / 8)
  771. /* Granularity of allocation, i.e. block size, StarPU will never allocate less
  772. * than this.
  773. * 16KiB (i.e. 64x64 float) granularity eats 2MiB RAM for managing a 4GiB GPU.
  774. */
  775. #define CHUNK_ALLOC_MIN (16*1024)
  776. /* Don't really deallocate chunks unless we have more than this many chunks
  777. * which are completely free. */
  778. #define CHUNKS_NFREE 4
  779. /* Number of blocks */
  780. #define CHUNK_NBLOCKS (CHUNK_SIZE/CHUNK_ALLOC_MIN)
  781. /* Linked list for available segments */
  782. struct block
  783. {
  784. int length; /* Number of consecutive free blocks */
  785. int next; /* next free segment */
  786. };
  787. /* One chunk */
  788. LIST_TYPE(_starpu_chunk,
  789. uintptr_t base;
  790. /* Available number of blocks, for debugging */
  791. int available;
  792. /* Overestimation of the maximum size of available segments in this chunk */
  793. int available_max;
  794. /* Bitmap describing availability of the block */
  795. /* Block 0 is always empty, and is just the head of the free segments list */
  796. struct block bitmap[CHUNK_NBLOCKS+1];
  797. )
  798. /* One list of chunks per node */
  799. static struct _starpu_chunk_list chunks[STARPU_MAXNODES];
  800. /* Number of completely free chunks */
  801. static int nfreechunks[STARPU_MAXNODES];
  802. /* This protects chunks and nfreechunks */
  803. static starpu_pthread_mutex_t chunk_mutex[STARPU_MAXNODES];
  804. void
  805. _starpu_malloc_init(unsigned dst_node)
  806. {
  807. _starpu_chunk_list_init(&chunks[dst_node]);
  808. nfreechunks[dst_node] = 0;
  809. STARPU_PTHREAD_MUTEX_INIT(&chunk_mutex[dst_node], NULL);
  810. disable_pinning = starpu_get_env_number("STARPU_DISABLE_PINNING");
  811. malloc_on_node_default_flags[dst_node] = STARPU_MALLOC_PINNED | STARPU_MALLOC_COUNT;
  812. #ifdef STARPU_SIMGRID
  813. #if SIMGRID_VERSION < 31500 || SIMGRID_VERSION == 31559
  814. /* Reasonably "costless" */
  815. _starpu_malloc_simulation_fold = starpu_get_env_number_default("STARPU_MALLOC_SIMULATION_FOLD", 1) << 20;
  816. #endif
  817. #endif
  818. }
  819. void
  820. _starpu_malloc_shutdown(unsigned dst_node)
  821. {
  822. struct _starpu_chunk *chunk, *next_chunk;
  823. STARPU_PTHREAD_MUTEX_LOCK(&chunk_mutex[dst_node]);
  824. for (chunk = _starpu_chunk_list_begin(&chunks[dst_node]);
  825. chunk != _starpu_chunk_list_end(&chunks[dst_node]);
  826. chunk = next_chunk)
  827. {
  828. next_chunk = _starpu_chunk_list_next(chunk);
  829. _starpu_free_on_node_flags(dst_node, chunk->base, CHUNK_SIZE, malloc_on_node_default_flags[dst_node]);
  830. _starpu_chunk_list_erase(&chunks[dst_node], chunk);
  831. free(chunk);
  832. }
  833. STARPU_PTHREAD_MUTEX_UNLOCK(&chunk_mutex[dst_node]);
  834. STARPU_PTHREAD_MUTEX_DESTROY(&chunk_mutex[dst_node]);
  835. }
  836. /* Create a new chunk */
  837. static struct _starpu_chunk *_starpu_new_chunk(unsigned dst_node, int flags)
  838. {
  839. struct _starpu_chunk *chunk;
  840. uintptr_t base = _starpu_malloc_on_node(dst_node, CHUNK_SIZE, flags);
  841. if (!base)
  842. return NULL;
  843. /* Create a new chunk */
  844. chunk = _starpu_chunk_new();
  845. chunk->base = base;
  846. /* First block is just a fake block pointing to the free segments list */
  847. chunk->bitmap[0].length = 0;
  848. chunk->bitmap[0].next = 1;
  849. /* At first we have only one big segment for the whole chunk */
  850. chunk->bitmap[1].length = CHUNK_NBLOCKS;
  851. chunk->bitmap[1].next = -1;
  852. chunk->available_max = CHUNK_NBLOCKS;
  853. chunk->available = CHUNK_NBLOCKS;
  854. return chunk;
  855. }
  856. uintptr_t
  857. starpu_malloc_on_node_flags(unsigned dst_node, size_t size, int flags)
  858. {
  859. /* Big allocation, allocate normally */
  860. if (size > CHUNK_ALLOC_MAX || starpu_node_get_kind(dst_node) != STARPU_CUDA_RAM)
  861. return _starpu_malloc_on_node(dst_node, size, flags);
  862. /* Round up allocation to block size */
  863. int nblocks = (size + CHUNK_ALLOC_MIN - 1) / CHUNK_ALLOC_MIN;
  864. struct _starpu_chunk *chunk;
  865. int prevblock, block;
  866. int available_max;
  867. struct block *bitmap;
  868. STARPU_PTHREAD_MUTEX_LOCK(&chunk_mutex[dst_node]);
  869. /* Try to find a big enough segment among the chunks */
  870. for (chunk = _starpu_chunk_list_begin(&chunks[dst_node]);
  871. chunk != _starpu_chunk_list_end(&chunks[dst_node]);
  872. chunk = _starpu_chunk_list_next(chunk))
  873. {
  874. if (chunk->available_max < nblocks)
  875. continue;
  876. bitmap = chunk->bitmap;
  877. available_max = 0;
  878. for (prevblock = block = 0;
  879. block != -1;
  880. prevblock = block, block = bitmap[prevblock].next)
  881. {
  882. STARPU_ASSERT(block >= 0 && block <= CHUNK_NBLOCKS);
  883. int length = bitmap[block].length;
  884. if (length >= nblocks)
  885. {
  886. if (length >= 2*nblocks)
  887. {
  888. /* This one this has quite some room,
  889. * put it front, to make finding it
  890. * easier next time. */
  891. _starpu_chunk_list_erase(&chunks[dst_node], chunk);
  892. _starpu_chunk_list_push_front(&chunks[dst_node], chunk);
  893. }
  894. if (chunk->available == CHUNK_NBLOCKS)
  895. /* This one was empty, it's not empty any more */
  896. nfreechunks[dst_node]--;
  897. goto found;
  898. }
  899. if (length > available_max)
  900. available_max = length;
  901. }
  902. /* Didn't find a big enough segment in this chunk, its
  903. * available_max is out of date */
  904. chunk->available_max = available_max;
  905. }
  906. /* Didn't find a big enough segment, create another chunk. */
  907. chunk = _starpu_new_chunk(dst_node, flags);
  908. if (!chunk)
  909. {
  910. /* Really no memory any more, fail */
  911. STARPU_PTHREAD_MUTEX_UNLOCK(&chunk_mutex[dst_node]);
  912. errno = ENOMEM;
  913. return 0;
  914. }
  915. /* And make it easy to find. */
  916. _starpu_chunk_list_push_front(&chunks[dst_node], chunk);
  917. bitmap = chunk->bitmap;
  918. prevblock = 0;
  919. block = 1;
  920. found:
  921. chunk->available -= nblocks;
  922. STARPU_ASSERT(bitmap[block].length >= nblocks);
  923. STARPU_ASSERT(block <= CHUNK_NBLOCKS);
  924. if (bitmap[block].length == nblocks)
  925. {
  926. /* Fits exactly, drop this segment from the skip list */
  927. bitmap[prevblock].next = bitmap[block].next;
  928. }
  929. else
  930. {
  931. /* Still some room */
  932. STARPU_ASSERT(block + nblocks <= CHUNK_NBLOCKS);
  933. bitmap[prevblock].next = block + nblocks;
  934. bitmap[block + nblocks].length = bitmap[block].length - nblocks;
  935. bitmap[block + nblocks].next = bitmap[block].next;
  936. }
  937. STARPU_PTHREAD_MUTEX_UNLOCK(&chunk_mutex[dst_node]);
  938. return chunk->base + (block-1) * CHUNK_ALLOC_MIN;
  939. }
  940. void
  941. starpu_free_on_node_flags(unsigned dst_node, uintptr_t addr, size_t size, int flags)
  942. {
  943. /* Big allocation, deallocate normally */
  944. if (size > CHUNK_ALLOC_MAX || starpu_node_get_kind(dst_node) != STARPU_CUDA_RAM)
  945. {
  946. _starpu_free_on_node_flags(dst_node, addr, size, flags);
  947. return;
  948. }
  949. struct _starpu_chunk *chunk;
  950. /* Round up allocation to block size */
  951. int nblocks = (size + CHUNK_ALLOC_MIN - 1) / CHUNK_ALLOC_MIN;
  952. STARPU_PTHREAD_MUTEX_LOCK(&chunk_mutex[dst_node]);
  953. for (chunk = _starpu_chunk_list_begin(&chunks[dst_node]);
  954. chunk != _starpu_chunk_list_end(&chunks[dst_node]);
  955. chunk = _starpu_chunk_list_next(chunk))
  956. if (addr >= chunk->base && addr < chunk->base + CHUNK_SIZE)
  957. break;
  958. STARPU_ASSERT(chunk != _starpu_chunk_list_end(&chunks[dst_node]));
  959. struct block *bitmap = chunk->bitmap;
  960. int block = ((addr - chunk->base) / CHUNK_ALLOC_MIN) + 1, prevblock, nextblock;
  961. /* Look for free segment just before this one */
  962. for (prevblock = 0;
  963. prevblock != -1;
  964. prevblock = nextblock)
  965. {
  966. STARPU_ASSERT(prevblock >= 0 && prevblock <= CHUNK_NBLOCKS);
  967. nextblock = bitmap[prevblock].next;
  968. 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);
  969. if (nextblock > block || nextblock == -1)
  970. break;
  971. }
  972. STARPU_ASSERT(prevblock != -1);
  973. chunk->available += nblocks;
  974. /* Insert in free segments list */
  975. bitmap[block].next = nextblock;
  976. bitmap[prevblock].next = block;
  977. bitmap[block].length = nblocks;
  978. STARPU_ASSERT(nextblock >= -1 && nextblock <= CHUNK_NBLOCKS);
  979. if (nextblock == block + nblocks)
  980. {
  981. /* This freed segment is just before a free segment, merge them */
  982. bitmap[block].next = bitmap[nextblock].next;
  983. bitmap[block].length += bitmap[nextblock].length;
  984. if (bitmap[block].length > chunk->available_max)
  985. chunk->available_max = bitmap[block].length;
  986. }
  987. if (prevblock > 0 && prevblock + bitmap[prevblock].length == block)
  988. {
  989. /* This free segment is just after a free segment, merge them */
  990. bitmap[prevblock].next = bitmap[block].next;
  991. bitmap[prevblock].length += bitmap[block].length;
  992. if (bitmap[prevblock].length > chunk->available_max)
  993. chunk->available_max = bitmap[prevblock].length;
  994. block = prevblock;
  995. }
  996. if (chunk->available == CHUNK_NBLOCKS)
  997. {
  998. /* This chunk is now empty, but avoid chunk free/alloc
  999. * ping-pong by keeping some of these. */
  1000. if (nfreechunks[dst_node] >= CHUNKS_NFREE)
  1001. {
  1002. /* We already have free chunks, release this one */
  1003. _starpu_free_on_node_flags(dst_node, chunk->base, CHUNK_SIZE, flags);
  1004. _starpu_chunk_list_erase(&chunks[dst_node], chunk);
  1005. free(chunk);
  1006. }
  1007. else
  1008. nfreechunks[dst_node]++;
  1009. }
  1010. else
  1011. {
  1012. /* Freed some room, put this first in chunks list */
  1013. _starpu_chunk_list_erase(&chunks[dst_node], chunk);
  1014. _starpu_chunk_list_push_front(&chunks[dst_node], chunk);
  1015. }
  1016. STARPU_PTHREAD_MUTEX_UNLOCK(&chunk_mutex[dst_node]);
  1017. }
  1018. void starpu_malloc_on_node_set_default_flags(unsigned node, int flags)
  1019. {
  1020. STARPU_ASSERT_MSG(node < STARPU_MAXNODES, "bogus node value %u given to starpu_malloc_on_node_set_default_flags\n", node);
  1021. malloc_on_node_default_flags[node] = flags;
  1022. }
  1023. uintptr_t
  1024. starpu_malloc_on_node(unsigned dst_node, size_t size)
  1025. {
  1026. return starpu_malloc_on_node_flags(dst_node, size, malloc_on_node_default_flags[dst_node]);
  1027. }
  1028. void
  1029. starpu_free_on_node(unsigned dst_node, uintptr_t addr, size_t size)
  1030. {
  1031. starpu_free_on_node_flags(dst_node, addr, size, malloc_on_node_default_flags[dst_node]);
  1032. }