malloc.c 24 KB

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