malloc.c 32 KB

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