malloc.c 28 KB

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