malloc.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-2010, 2012-2013 Université de Bordeaux 1
  4. * Copyright (C) 2010, 2011, 2012, 2013 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 <common/config.h>
  20. #include <starpu.h>
  21. #include <drivers/opencl/driver_opencl.h>
  22. #include <datawizard/memory_manager.h>
  23. static size_t _malloc_align = sizeof(void*);
  24. void starpu_malloc_set_align(size_t align)
  25. {
  26. STARPU_ASSERT_MSG(!(align & (align - 1)), "Alignment given to starpu_malloc_set_align must be a power of two");
  27. if (_malloc_align < align)
  28. _malloc_align = align;
  29. }
  30. #if (defined(STARPU_USE_CUDA) && !defined(HAVE_CUDA_MEMCPY_PEER))// || defined(STARPU_USE_OPENCL)
  31. struct malloc_pinned_codelet_struct
  32. {
  33. void **ptr;
  34. size_t dim;
  35. };
  36. #endif
  37. /* Would be difficult to do it this way, we need to remember the cl_mem to be able to free it later... */
  38. //#ifdef STARPU_USE_OPENCL
  39. //static void malloc_pinned_opencl_codelet(void *buffers[] STARPU_ATTRIBUTE_UNUSED, void *arg)
  40. //{
  41. // struct malloc_pinned_codelet_struct *s = arg;
  42. // // *(s->ptr) = malloc(s->dim);
  43. // starpu_opencl_allocate_memory((void **)(s->ptr), s->dim, CL_MEM_READ_WRITE|CL_MEM_ALLOC_HOST_PTR);
  44. //}
  45. //#endif
  46. #if defined(STARPU_USE_CUDA) && !defined(HAVE_CUDA_MEMCPY_PEER) && !defined(STARPU_SIMGRID)
  47. static void malloc_pinned_cuda_codelet(void *buffers[] STARPU_ATTRIBUTE_UNUSED, void *arg)
  48. {
  49. struct malloc_pinned_codelet_struct *s = arg;
  50. cudaError_t cures;
  51. cures = cudaHostAlloc((void **)(s->ptr), s->dim, cudaHostAllocPortable);
  52. if (STARPU_UNLIKELY(cures))
  53. STARPU_CUDA_REPORT_ERROR(cures);
  54. }
  55. #endif
  56. #if (defined(STARPU_USE_CUDA) && !defined(HAVE_CUDA_MEMCPY_PEER)) && !defined(STARPU_SIMGRID)// || defined(STARPU_USE_OPENCL)
  57. static struct starpu_perfmodel malloc_pinned_model =
  58. {
  59. .type = STARPU_HISTORY_BASED,
  60. .symbol = "malloc_pinned"
  61. };
  62. static struct starpu_codelet malloc_pinned_cl =
  63. {
  64. .cuda_funcs = {malloc_pinned_cuda_codelet, NULL},
  65. //#ifdef STARPU_USE_OPENCL
  66. // .opencl_funcs = {malloc_pinned_opencl_codelet, NULL},
  67. //#endif
  68. .nbuffers = 0,
  69. .model = &malloc_pinned_model
  70. };
  71. #endif
  72. int starpu_malloc_flags(void **A, size_t dim, int flags)
  73. {
  74. int ret=0;
  75. STARPU_ASSERT(A);
  76. if (flags & STARPU_MALLOC_COUNT)
  77. {
  78. if (_starpu_memory_manager_can_allocate_size(dim, 0) == 0)
  79. {
  80. size_t freed;
  81. size_t reclaim = 2 * dim;
  82. _STARPU_DEBUG("There is not enough memory left, we are going to reclaim %ld\n", reclaim);
  83. _STARPU_TRACE_START_MEMRECLAIM(0);
  84. freed = _starpu_memory_reclaim_generic(0, 0, reclaim);
  85. _STARPU_TRACE_END_MEMRECLAIM(0);
  86. if (freed < dim)
  87. {
  88. // We could not reclaim enough memory
  89. *A = NULL;
  90. return -ENOMEM;
  91. }
  92. }
  93. }
  94. #ifndef STARPU_SIMGRID
  95. if (flags & STARPU_MALLOC_PINNED)
  96. {
  97. if (_starpu_can_submit_cuda_task())
  98. {
  99. #ifdef STARPU_USE_CUDA
  100. #ifdef HAVE_CUDA_MEMCPY_PEER
  101. cudaError_t cures;
  102. cures = cudaHostAlloc(A, dim, cudaHostAllocPortable);
  103. if (STARPU_UNLIKELY(cures))
  104. STARPU_CUDA_REPORT_ERROR(cures);
  105. goto end;
  106. #else
  107. int push_res;
  108. if (STARPU_UNLIKELY(!_starpu_worker_may_perform_blocking_calls()))
  109. return -EDEADLK;
  110. struct malloc_pinned_codelet_struct s =
  111. {
  112. .ptr = A,
  113. .dim = dim
  114. };
  115. malloc_pinned_cl.where = STARPU_CUDA;
  116. struct starpu_task *task = starpu_task_create();
  117. task->callback_func = NULL;
  118. task->cl = &malloc_pinned_cl;
  119. task->cl_arg = &s;
  120. task->synchronous = 1;
  121. _starpu_exclude_task_from_dag(task);
  122. push_res = _starpu_task_submit_internally(task);
  123. STARPU_ASSERT(push_res != -ENODEV);
  124. goto end;
  125. #endif /* HAVE_CUDA_MEMCPY_PEER */
  126. #endif /* STARPU_USE_CUDA */
  127. }
  128. // else if (_starpu_can_submit_opencl_task())
  129. // {
  130. //#ifdef STARPU_USE_OPENCL
  131. // int push_res;
  132. //
  133. // if (STARPU_UNLIKELY(!_starpu_worker_may_perform_blocking_calls()))
  134. // return -EDEADLK;
  135. //
  136. // struct malloc_pinned_codelet_struct s =
  137. // {
  138. // .ptr = A,
  139. // .dim = dim
  140. // };
  141. //
  142. // malloc_pinned_cl.where = STARPU_OPENCL;
  143. // struct starpu_task *task = starpu_task_create();
  144. // task->callback_func = NULL;
  145. // task->cl = &malloc_pinned_cl;
  146. // task->cl_arg = &s;
  147. // task->synchronous = 1;
  148. //
  149. // _starpu_exclude_task_from_dag(task);
  150. //
  151. // push_res = _starpu_task_submit_internally(task);
  152. // STARPU_ASSERT(push_res != -ENODEV);
  153. // goto end;
  154. //#endif /* STARPU_USE_OPENCL */
  155. // }
  156. }
  157. #endif /* STARPU_SIMGRID */
  158. if (_starpu_can_submit_scc_task())
  159. {
  160. #ifdef STARPU_USE_SCC
  161. _starpu_scc_allocate_shared_memory(A, dim);
  162. #endif
  163. }
  164. else
  165. #ifdef STARPU_HAVE_POSIX_MEMALIGN
  166. if (_malloc_align != sizeof(void*))
  167. {
  168. if (posix_memalign(A, _malloc_align, dim))
  169. {
  170. ret = -ENOMEM;
  171. *A = NULL;
  172. }
  173. }
  174. else
  175. #elif defined(STARPU_HAVE_MEMALIGN)
  176. if (_malloc_align != sizeof(void*))
  177. {
  178. *A = memalign(_malloc_align, dim);
  179. }
  180. else
  181. #endif /* STARPU_HAVE_POSIX_MEMALIGN */
  182. {
  183. *A = malloc(dim);
  184. }
  185. end:
  186. if (ret == 0)
  187. {
  188. STARPU_ASSERT(*A);
  189. }
  190. return ret;
  191. }
  192. int starpu_malloc(void **A, size_t dim)
  193. {
  194. return starpu_malloc_flags(A, dim, STARPU_MALLOC_PINNED);
  195. }
  196. #if defined(STARPU_USE_CUDA) && !defined(HAVE_CUDA_MEMCPY_PEER) && !defined(STARPU_SIMGRID)
  197. static void free_pinned_cuda_codelet(void *buffers[] STARPU_ATTRIBUTE_UNUSED, void *arg)
  198. {
  199. cudaError_t cures;
  200. cures = cudaFreeHost(arg);
  201. if (STARPU_UNLIKELY(cures))
  202. STARPU_CUDA_REPORT_ERROR(cures);
  203. }
  204. #endif
  205. //#ifdef STARPU_USE_OPENCL
  206. //static void free_pinned_opencl_codelet(void *buffers[] STARPU_ATTRIBUTE_UNUSED, void *arg)
  207. //{
  208. // // free(arg);
  209. // int err = clReleaseMemObject(arg);
  210. // if (err != CL_SUCCESS) STARPU_OPENCL_REPORT_ERROR(err);
  211. //}
  212. //#endif
  213. #if defined(STARPU_USE_CUDA) && !defined(HAVE_CUDA_MEMCPY_PEER) && !defined(STARPU_SIMGRID) // || defined(STARPU_USE_OPENCL)
  214. static struct starpu_perfmodel free_pinned_model =
  215. {
  216. .type = STARPU_HISTORY_BASED,
  217. .symbol = "free_pinned"
  218. };
  219. static struct starpu_codelet free_pinned_cl =
  220. {
  221. .cuda_funcs = {free_pinned_cuda_codelet, NULL},
  222. //#ifdef STARPU_USE_OPENCL
  223. // .opencl_funcs = {free_pinned_opencl_codelet, NULL},
  224. //#endif
  225. .nbuffers = 0,
  226. .model = &free_pinned_model
  227. };
  228. #endif
  229. int starpu_free_flags(void *A, size_t dim, int flags)
  230. {
  231. #ifndef STARPU_SIMGRID
  232. if (flags & STARPU_MALLOC_PINNED)
  233. {
  234. if (_starpu_can_submit_cuda_task())
  235. {
  236. #ifdef STARPU_USE_CUDA
  237. #ifndef HAVE_CUDA_MEMCPY_PEER
  238. if (!_starpu_is_initialized())
  239. {
  240. #endif
  241. /* This is especially useful when starpu_free is called from
  242. * the GCC-plugin. starpu_shutdown will probably have already
  243. * been called, so we will not be able to submit a task. */
  244. cudaError_t err = cudaFreeHost(A);
  245. if (STARPU_UNLIKELY(err))
  246. STARPU_CUDA_REPORT_ERROR(err);
  247. goto out;
  248. #ifndef HAVE_CUDA_MEMCPY_PEER
  249. }
  250. else
  251. {
  252. int push_res;
  253. if (STARPU_UNLIKELY(!_starpu_worker_may_perform_blocking_calls()))
  254. return -EDEADLK;
  255. free_pinned_cl.where = STARPU_CUDA;
  256. struct starpu_task *task = starpu_task_create();
  257. task->callback_func = NULL;
  258. task->cl = &free_pinned_cl;
  259. task->cl_arg = A;
  260. task->synchronous = 1;
  261. _starpu_exclude_task_from_dag(task);
  262. push_res = _starpu_task_submit_internally(task);
  263. STARPU_ASSERT(push_res != -ENODEV);
  264. goto out;
  265. }
  266. #endif /* HAVE_CUDA_MEMCPY_PEER */
  267. #endif /* STARPU_USE_CUDA */
  268. }
  269. // else if (_starpu_can_submit_opencl_task())
  270. // {
  271. //#ifdef STARPU_USE_OPENCL
  272. // int push_res;
  273. //
  274. // if (STARPU_UNLIKELY(!_starpu_worker_may_perform_blocking_calls()))
  275. // return -EDEADLK;
  276. //
  277. // free_pinned_cl.where = STARPU_OPENCL;
  278. // struct starpu_task *task = starpu_task_create();
  279. // task->callback_func = NULL;
  280. // task->cl = &free_pinned_cl;
  281. // task->cl_arg = A;
  282. // task->synchronous = 1;
  283. //
  284. // _starpu_exclude_task_from_dag(task);
  285. //
  286. // push_res = starpu_task_submit(task);
  287. // STARPU_ASSERT(push_res != -ENODEV);
  288. // goto out;
  289. // }
  290. //#endif
  291. }
  292. #endif /* STARPU_SIMGRID */
  293. if (_starpu_can_submit_scc_task())
  294. {
  295. #ifdef STARPU_USE_SCC
  296. _starpu_scc_free_shared_memory(A);
  297. #endif
  298. } else
  299. free(A);
  300. out:
  301. if (flags & STARPU_MALLOC_COUNT)
  302. {
  303. _starpu_memory_manager_deallocate_size(dim, 0);
  304. }
  305. return 0;
  306. }
  307. int starpu_free(void *A)
  308. {
  309. return starpu_free_flags(A, 0, STARPU_MALLOC_PINNED);
  310. }
  311. #ifdef STARPU_SIMGRID
  312. static starpu_pthread_mutex_t cuda_alloc_mutex = STARPU_PTHREAD_MUTEX_INITIALIZER;
  313. static starpu_pthread_mutex_t opencl_alloc_mutex = STARPU_PTHREAD_MUTEX_INITIALIZER;
  314. #endif
  315. uintptr_t
  316. starpu_malloc_on_node(unsigned dst_node, size_t size)
  317. {
  318. uintptr_t addr = 0;
  319. #ifdef STARPU_USE_CUDA
  320. cudaError_t status;
  321. #endif
  322. if (_starpu_memory_manager_can_allocate_size(size, dst_node) == 0)
  323. return 0;
  324. switch(starpu_node_get_kind(dst_node))
  325. {
  326. case STARPU_CPU_RAM:
  327. {
  328. addr = (uintptr_t)malloc(size);
  329. break;
  330. }
  331. #if defined(STARPU_USE_CUDA) || defined(STARPU_SIMGRID)
  332. case STARPU_CUDA_RAM:
  333. #ifdef STARPU_SIMGRID
  334. #ifdef STARPU_DEVEL
  335. #warning TODO: record used memory, using a simgrid property to know the available memory
  336. #endif
  337. /* Sleep 10µs for the allocation */
  338. STARPU_PTHREAD_MUTEX_LOCK(&cuda_alloc_mutex);
  339. MSG_process_sleep(0.000010);
  340. addr = 1;
  341. STARPU_PTHREAD_MUTEX_UNLOCK(&cuda_alloc_mutex);
  342. #else
  343. status = cudaMalloc((void **)&addr, size);
  344. if (!addr || (status != cudaSuccess))
  345. {
  346. if (STARPU_UNLIKELY(status != cudaErrorMemoryAllocation))
  347. STARPU_CUDA_REPORT_ERROR(status);
  348. addr = 0;
  349. }
  350. #endif
  351. break;
  352. #endif
  353. #if defined(STARPU_USE_OPENCL) || defined(STARPU_SIMGRID)
  354. case STARPU_OPENCL_RAM:
  355. {
  356. #ifdef STARPU_SIMGRID
  357. /* Sleep 10µs for the allocation */
  358. STARPU_PTHREAD_MUTEX_LOCK(&opencl_alloc_mutex);
  359. MSG_process_sleep(0.000010);
  360. addr = 1;
  361. STARPU_PTHREAD_MUTEX_UNLOCK(&opencl_alloc_mutex);
  362. #else
  363. int ret;
  364. cl_mem ptr;
  365. ret = starpu_opencl_allocate_memory(&ptr, size, CL_MEM_READ_WRITE);
  366. if (ret)
  367. {
  368. addr = 0;
  369. }
  370. else
  371. {
  372. addr = (uintptr_t)ptr;
  373. }
  374. break;
  375. #endif
  376. }
  377. #endif
  378. #ifdef STARPU_USE_MIC
  379. case STARPU_MIC_RAM:
  380. if (_starpu_mic_allocate_memory((void **)(&addr), size, dst_node))
  381. addr = 0;
  382. break;
  383. #endif
  384. #ifdef STARPU_USE_SCC
  385. case STARPU_SCC_RAM:
  386. if (_starpu_scc_allocate_memory((void **)(&addr), size, dst_node))
  387. addr = 0;
  388. break;
  389. #endif
  390. default:
  391. STARPU_ABORT();
  392. }
  393. if (addr == 0)
  394. {
  395. // Allocation failed, gives the memory back to the memory manager
  396. _starpu_memory_manager_deallocate_size(size, dst_node);
  397. }
  398. return addr;
  399. }
  400. void
  401. starpu_free_on_node(unsigned dst_node, uintptr_t addr, size_t size)
  402. {
  403. enum starpu_node_kind kind = starpu_node_get_kind(dst_node);
  404. switch(kind)
  405. {
  406. case STARPU_CPU_RAM:
  407. free((void*)addr);
  408. break;
  409. #if defined(STARPU_USE_CUDA) || defined(STARPU_SIMGRID)
  410. case STARPU_CUDA_RAM:
  411. {
  412. #ifdef STARPU_SIMGRID
  413. STARPU_PTHREAD_MUTEX_LOCK(&cuda_alloc_mutex);
  414. /* Sleep 10µs for the free */
  415. MSG_process_sleep(0.000010);
  416. STARPU_PTHREAD_MUTEX_UNLOCK(&cuda_alloc_mutex);
  417. #else
  418. cudaError_t err;
  419. err = cudaFree((void*)addr);
  420. if (STARPU_UNLIKELY(err != cudaSuccess))
  421. STARPU_CUDA_REPORT_ERROR(err);
  422. #endif
  423. break;
  424. }
  425. #endif
  426. #if defined(STARPU_USE_OPENCL) || defined(STARPU_SIMGRID)
  427. case STARPU_OPENCL_RAM:
  428. {
  429. #ifdef STARPU_SIMGRID
  430. STARPU_PTHREAD_MUTEX_LOCK(&opencl_alloc_mutex);
  431. /* Sleep 10µs for the free */
  432. MSG_process_sleep(0.000010);
  433. STARPU_PTHREAD_MUTEX_UNLOCK(&opencl_alloc_mutex);
  434. #else
  435. cl_int err;
  436. err = clReleaseMemObject((void*)addr);
  437. if (STARPU_UNLIKELY(err != CL_SUCCESS))
  438. STARPU_OPENCL_REPORT_ERROR(err);
  439. #endif
  440. break;
  441. }
  442. #endif
  443. #ifdef STARPU_USE_MIC
  444. case STARPU_MIC_RAM:
  445. _starpu_mic_free_memory((void*) addr, size, dst_node);
  446. break;
  447. #endif
  448. #ifdef STARPU_USE_SCC
  449. case STARPU_SCC_RAM:
  450. _starpu_scc_free_memory((void *) addr, dst_node);
  451. break;
  452. #endif
  453. default:
  454. STARPU_ABORT();
  455. }
  456. _starpu_memory_manager_deallocate_size(size, dst_node);
  457. }