malloc.c 32 KB

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