malloc.c 30 KB

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