malloc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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. #ifdef STARPU_HAVE_POSIX_MEMALIGN
  159. if (_malloc_align != sizeof(void*))
  160. {
  161. if (posix_memalign(A, _malloc_align, dim))
  162. {
  163. ret = -ENOMEM;
  164. *A = NULL;
  165. }
  166. }
  167. else
  168. #elif defined(STARPU_HAVE_MEMALIGN)
  169. if (_malloc_align != sizeof(void*))
  170. {
  171. *A = memalign(_malloc_align, dim);
  172. }
  173. else
  174. #endif /* STARPU_HAVE_POSIX_MEMALIGN */
  175. {
  176. *A = malloc(dim);
  177. }
  178. end:
  179. if (ret == 0)
  180. {
  181. STARPU_ASSERT(*A);
  182. }
  183. return ret;
  184. }
  185. int starpu_malloc(void **A, size_t dim)
  186. {
  187. return starpu_malloc_flags(A, dim, STARPU_MALLOC_PINNED);
  188. }
  189. #if defined(STARPU_USE_CUDA) && !defined(HAVE_CUDA_MEMCPY_PEER) && !defined(STARPU_SIMGRID)
  190. static void free_pinned_cuda_codelet(void *buffers[] STARPU_ATTRIBUTE_UNUSED, void *arg)
  191. {
  192. cudaError_t cures;
  193. cures = cudaFreeHost(arg);
  194. if (STARPU_UNLIKELY(cures))
  195. STARPU_CUDA_REPORT_ERROR(cures);
  196. }
  197. #endif
  198. //#ifdef STARPU_USE_OPENCL
  199. //static void free_pinned_opencl_codelet(void *buffers[] STARPU_ATTRIBUTE_UNUSED, void *arg)
  200. //{
  201. // // free(arg);
  202. // int err = clReleaseMemObject(arg);
  203. // if (err != CL_SUCCESS) STARPU_OPENCL_REPORT_ERROR(err);
  204. //}
  205. //#endif
  206. #if defined(STARPU_USE_CUDA) && !defined(HAVE_CUDA_MEMCPY_PEER) && !defined(STARPU_SIMGRID) // || defined(STARPU_USE_OPENCL)
  207. static struct starpu_perfmodel free_pinned_model =
  208. {
  209. .type = STARPU_HISTORY_BASED,
  210. .symbol = "free_pinned"
  211. };
  212. static struct starpu_codelet free_pinned_cl =
  213. {
  214. .cuda_funcs = {free_pinned_cuda_codelet, NULL},
  215. //#ifdef STARPU_USE_OPENCL
  216. // .opencl_funcs = {free_pinned_opencl_codelet, NULL},
  217. //#endif
  218. .nbuffers = 0,
  219. .model = &free_pinned_model
  220. };
  221. #endif
  222. int starpu_free_flags(void *A, size_t dim, int flags)
  223. {
  224. #ifndef STARPU_SIMGRID
  225. if (flags & STARPU_MALLOC_PINNED)
  226. {
  227. if (_starpu_can_submit_cuda_task())
  228. {
  229. #ifdef STARPU_USE_CUDA
  230. #ifndef HAVE_CUDA_MEMCPY_PEER
  231. if (!_starpu_is_initialized())
  232. {
  233. #endif
  234. /* This is especially useful when starpu_free is called from
  235. * the GCC-plugin. starpu_shutdown will probably have already
  236. * been called, so we will not be able to submit a task. */
  237. cudaError_t err = cudaFreeHost(A);
  238. if (STARPU_UNLIKELY(err))
  239. STARPU_CUDA_REPORT_ERROR(err);
  240. goto out;
  241. #ifndef HAVE_CUDA_MEMCPY_PEER
  242. }
  243. else
  244. {
  245. int push_res;
  246. if (STARPU_UNLIKELY(!_starpu_worker_may_perform_blocking_calls()))
  247. return -EDEADLK;
  248. free_pinned_cl.where = STARPU_CUDA;
  249. struct starpu_task *task = starpu_task_create();
  250. task->callback_func = NULL;
  251. task->cl = &free_pinned_cl;
  252. task->cl_arg = A;
  253. task->synchronous = 1;
  254. _starpu_exclude_task_from_dag(task);
  255. push_res = _starpu_task_submit_internally(task);
  256. STARPU_ASSERT(push_res != -ENODEV);
  257. goto out;
  258. }
  259. #endif /* HAVE_CUDA_MEMCPY_PEER */
  260. #endif /* STARPU_USE_CUDA */
  261. }
  262. // else if (_starpu_can_submit_opencl_task())
  263. // {
  264. //#ifdef STARPU_USE_OPENCL
  265. // int push_res;
  266. //
  267. // if (STARPU_UNLIKELY(!_starpu_worker_may_perform_blocking_calls()))
  268. // return -EDEADLK;
  269. //
  270. // free_pinned_cl.where = STARPU_OPENCL;
  271. // struct starpu_task *task = starpu_task_create();
  272. // task->callback_func = NULL;
  273. // task->cl = &free_pinned_cl;
  274. // task->cl_arg = A;
  275. // task->synchronous = 1;
  276. //
  277. // _starpu_exclude_task_from_dag(task);
  278. //
  279. // push_res = starpu_task_submit(task);
  280. // STARPU_ASSERT(push_res != -ENODEV);
  281. // goto out;
  282. // }
  283. //#endif
  284. }
  285. #endif /* STARPU_SIMGRID */
  286. free(A);
  287. out:
  288. if (flags & STARPU_MALLOC_COUNT)
  289. {
  290. _starpu_memory_manager_deallocate_size(dim, 0);
  291. }
  292. return 0;
  293. }
  294. int starpu_free(void *A)
  295. {
  296. return starpu_free_flags(A, 0, STARPU_MALLOC_PINNED);
  297. }
  298. #ifdef STARPU_SIMGRID
  299. static starpu_pthread_mutex_t cuda_alloc_mutex = STARPU_PTHREAD_MUTEX_INITIALIZER;
  300. static starpu_pthread_mutex_t opencl_alloc_mutex = STARPU_PTHREAD_MUTEX_INITIALIZER;
  301. #endif
  302. uintptr_t
  303. starpu_malloc_on_node(unsigned dst_node, size_t size)
  304. {
  305. uintptr_t addr = 0;
  306. #ifdef STARPU_USE_CUDA
  307. cudaError_t status;
  308. #endif
  309. if (_starpu_memory_manager_can_allocate_size(size, dst_node) == 0)
  310. return 0;
  311. switch(starpu_node_get_kind(dst_node))
  312. {
  313. case STARPU_CPU_RAM:
  314. {
  315. addr = (uintptr_t)malloc(size);
  316. break;
  317. }
  318. #if defined(STARPU_USE_CUDA) || defined(STARPU_SIMGRID)
  319. case STARPU_CUDA_RAM:
  320. #ifdef STARPU_SIMGRID
  321. #ifdef STARPU_DEVEL
  322. #warning TODO: record used memory, using a simgrid property to know the available memory
  323. #endif
  324. /* Sleep 10µs for the allocation */
  325. _STARPU_PTHREAD_MUTEX_LOCK(&cuda_alloc_mutex);
  326. MSG_process_sleep(0.000010);
  327. addr = 1;
  328. _STARPU_PTHREAD_MUTEX_UNLOCK(&cuda_alloc_mutex);
  329. #else
  330. status = cudaMalloc((void **)&addr, size);
  331. if (!addr || (status != cudaSuccess))
  332. {
  333. if (STARPU_UNLIKELY(status != cudaErrorMemoryAllocation))
  334. STARPU_CUDA_REPORT_ERROR(status);
  335. addr = 0;
  336. }
  337. #endif
  338. break;
  339. #endif
  340. #if defined(STARPU_USE_OPENCL) || defined(STARPU_SIMGRID)
  341. case STARPU_OPENCL_RAM:
  342. {
  343. #ifdef STARPU_SIMGRID
  344. /* Sleep 10µs for the allocation */
  345. _STARPU_PTHREAD_MUTEX_LOCK(&opencl_alloc_mutex);
  346. MSG_process_sleep(0.000010);
  347. addr = 1;
  348. _STARPU_PTHREAD_MUTEX_UNLOCK(&opencl_alloc_mutex);
  349. #else
  350. int ret;
  351. cl_mem ptr;
  352. ret = starpu_opencl_allocate_memory(&ptr, size, CL_MEM_READ_WRITE);
  353. if (ret)
  354. {
  355. addr = 0;
  356. }
  357. else
  358. {
  359. addr = (uintptr_t)ptr;
  360. }
  361. break;
  362. #endif
  363. }
  364. #endif
  365. default:
  366. STARPU_ABORT();
  367. }
  368. if (addr == 0)
  369. {
  370. // Allocation failed, gives the memory back to the memory manager
  371. _starpu_memory_manager_deallocate_size(size, dst_node);
  372. }
  373. return addr;
  374. }
  375. void
  376. starpu_free_on_node(unsigned dst_node, uintptr_t addr, size_t size)
  377. {
  378. enum starpu_node_kind kind = starpu_node_get_kind(dst_node);
  379. switch(kind)
  380. {
  381. case STARPU_CPU_RAM:
  382. free((void*)addr);
  383. break;
  384. #if defined(STARPU_USE_CUDA) || defined(STARPU_SIMGRID)
  385. case STARPU_CUDA_RAM:
  386. {
  387. #ifdef STARPU_SIMGRID
  388. _STARPU_PTHREAD_MUTEX_LOCK(&cuda_alloc_mutex);
  389. /* Sleep 10µs for the free */
  390. MSG_process_sleep(0.000010);
  391. _STARPU_PTHREAD_MUTEX_UNLOCK(&cuda_alloc_mutex);
  392. #else
  393. cudaError_t err;
  394. err = cudaFree((void*)addr);
  395. if (STARPU_UNLIKELY(err != cudaSuccess))
  396. STARPU_CUDA_REPORT_ERROR(err);
  397. #endif
  398. break;
  399. }
  400. #endif
  401. #if defined(STARPU_USE_OPENCL) || defined(STARPU_SIMGRID)
  402. case STARPU_OPENCL_RAM:
  403. {
  404. #ifdef STARPU_SIMGRID
  405. _STARPU_PTHREAD_MUTEX_LOCK(&opencl_alloc_mutex);
  406. /* Sleep 10µs for the free */
  407. MSG_process_sleep(0.000010);
  408. _STARPU_PTHREAD_MUTEX_UNLOCK(&opencl_alloc_mutex);
  409. #else
  410. cl_int err;
  411. err = clReleaseMemObject((void*)addr);
  412. if (STARPU_UNLIKELY(err != CL_SUCCESS))
  413. STARPU_OPENCL_REPORT_ERROR(err);
  414. #endif
  415. break;
  416. }
  417. #endif
  418. default:
  419. STARPU_ABORT();
  420. }
  421. _starpu_memory_manager_deallocate_size(size, dst_node);
  422. }