malloc.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009, 2010 Université de Bordeaux 1
  4. * Copyright (C) 2010, 2011, 2012 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 <starpu_cuda.h>
  22. #include <drivers/opencl/driver_opencl.h>
  23. #if defined(STARPU_USE_CUDA) || defined(STARPU_USE_OPENCL)
  24. struct malloc_pinned_codelet_struct
  25. {
  26. void **ptr;
  27. size_t dim;
  28. };
  29. #endif
  30. //#ifdef STARPU_USE_OPENCL
  31. //static void malloc_pinned_opencl_codelet(void *buffers[] STARPU_ATTRIBUTE_UNUSED, void *arg)
  32. //{
  33. // struct malloc_pinned_codelet_struct *s = arg;
  34. // // *(s->ptr) = malloc(s->dim);
  35. // _starpu_opencl_allocate_memory((void **)(s->ptr), s->dim, CL_MEM_READ_WRITE|CL_MEM_ALLOC_HOST_PTR);
  36. //}
  37. //#endif
  38. #ifdef STARPU_USE_CUDA
  39. static void malloc_pinned_cuda_codelet(void *buffers[] STARPU_ATTRIBUTE_UNUSED, void *arg)
  40. {
  41. struct malloc_pinned_codelet_struct *s = arg;
  42. cudaError_t cures;
  43. cures = cudaHostAlloc((void **)(s->ptr), s->dim, cudaHostAllocPortable);
  44. if (STARPU_UNLIKELY(cures))
  45. STARPU_CUDA_REPORT_ERROR(cures);
  46. }
  47. #endif
  48. #if defined(STARPU_USE_CUDA)// || defined(STARPU_USE_OPENCL)
  49. static struct starpu_perfmodel malloc_pinned_model =
  50. {
  51. .type = STARPU_HISTORY_BASED,
  52. .symbol = "malloc_pinned"
  53. };
  54. static struct starpu_codelet malloc_pinned_cl =
  55. {
  56. .cuda_funcs = {malloc_pinned_cuda_codelet, NULL},
  57. //#ifdef STARPU_USE_OPENCL
  58. // .opencl_funcs = {malloc_pinned_opencl_codelet, NULL},
  59. //#endif
  60. .nbuffers = 0,
  61. .model = &malloc_pinned_model
  62. };
  63. #endif
  64. int starpu_malloc(void **A, size_t dim)
  65. {
  66. if (STARPU_UNLIKELY(!_starpu_worker_may_perform_blocking_calls()))
  67. return -EDEADLK;
  68. STARPU_ASSERT(A);
  69. if (_starpu_can_submit_cuda_task())
  70. {
  71. #ifdef STARPU_USE_CUDA
  72. int push_res;
  73. struct malloc_pinned_codelet_struct s =
  74. {
  75. .ptr = A,
  76. .dim = dim
  77. };
  78. malloc_pinned_cl.where = STARPU_CUDA;
  79. struct starpu_task *task = starpu_task_create();
  80. task->callback_func = NULL;
  81. task->cl = &malloc_pinned_cl;
  82. task->cl_arg = &s;
  83. task->synchronous = 1;
  84. _starpu_exclude_task_from_dag(task);
  85. push_res = starpu_task_submit(task);
  86. STARPU_ASSERT(push_res != -ENODEV);
  87. #endif
  88. }
  89. // else if (_starpu_can_submit_opencl_task())
  90. // {
  91. //#ifdef STARPU_USE_OPENCL
  92. // int push_res;
  93. //
  94. // struct malloc_pinned_codelet_struct s =
  95. // {
  96. // .ptr = A,
  97. // .dim = dim
  98. // };
  99. //
  100. // malloc_pinned_cl.where = STARPU_OPENCL;
  101. // struct starpu_task *task = starpu_task_create();
  102. // task->callback_func = NULL;
  103. // task->cl = &malloc_pinned_cl;
  104. // task->cl_arg = &s;
  105. //
  106. // task->synchronous = 1;
  107. //
  108. // _starpu_exclude_task_from_dag(task);
  109. //
  110. // push_res = starpu_task_submit(task);
  111. // STARPU_ASSERT(push_res != -ENODEV);
  112. //#endif
  113. // }
  114. else
  115. {
  116. *A = malloc(dim);
  117. }
  118. STARPU_ASSERT(*A);
  119. return 0;
  120. }
  121. #ifdef STARPU_USE_CUDA
  122. static void free_pinned_cuda_codelet(void *buffers[] STARPU_ATTRIBUTE_UNUSED, void *arg)
  123. {
  124. cudaError_t cures;
  125. cures = cudaFreeHost(arg);
  126. if (STARPU_UNLIKELY(cures))
  127. STARPU_CUDA_REPORT_ERROR(cures);
  128. }
  129. #endif
  130. //#ifdef STARPU_USE_OPENCL
  131. //static void free_pinned_opencl_codelet(void *buffers[] STARPU_ATTRIBUTE_UNUSED, void *arg)
  132. //{
  133. // // free(arg);
  134. // int err = clReleaseMemObject(arg);
  135. // if (err != CL_SUCCESS) STARPU_OPENCL_REPORT_ERROR(err);
  136. //}
  137. //#endif
  138. #if defined(STARPU_USE_CUDA) // || defined(STARPU_USE_OPENCL)
  139. static struct starpu_perfmodel free_pinned_model =
  140. {
  141. .type = STARPU_HISTORY_BASED,
  142. .symbol = "free_pinned"
  143. };
  144. static struct starpu_codelet free_pinned_cl =
  145. {
  146. .cuda_funcs = {free_pinned_cuda_codelet, NULL},
  147. //#ifdef STARPU_USE_OPENCL
  148. // .opencl_funcs = {free_pinned_opencl_codelet, NULL},
  149. //#endif
  150. .nbuffers = 0,
  151. .model = &free_pinned_model
  152. };
  153. #endif
  154. int starpu_free(void *A)
  155. {
  156. if (STARPU_UNLIKELY(!_starpu_worker_may_perform_blocking_calls()))
  157. return -EDEADLK;
  158. if (_starpu_can_submit_cuda_task())
  159. {
  160. #ifdef STARPU_USE_CUDA
  161. int push_res;
  162. free_pinned_cl.where = STARPU_CUDA;
  163. struct starpu_task *task = starpu_task_create();
  164. task->callback_func = NULL;
  165. task->cl = &free_pinned_cl;
  166. task->cl_arg = A;
  167. task->synchronous = 1;
  168. _starpu_exclude_task_from_dag(task);
  169. push_res = starpu_task_submit(task);
  170. STARPU_ASSERT(push_res != -ENODEV);
  171. #endif
  172. }
  173. // else if (_starpu_can_submit_opencl_task())
  174. // {
  175. //#ifdef STARPU_USE_OPENCL
  176. // int push_res;
  177. //
  178. // free_pinned_cl.where = STARPU_OPENCL;
  179. // struct starpu_task *task = starpu_task_create();
  180. // task->callback_func = NULL;
  181. // task->cl = &free_pinned_cl;
  182. // task->cl_arg = A;
  183. //
  184. // task->synchronous = 1;
  185. //
  186. // _starpu_exclude_task_from_dag(task);
  187. //
  188. // push_res = starpu_task_submit(task);
  189. // STARPU_ASSERT(push_res != -ENODEV);
  190. //#endif
  191. // }
  192. else
  193. {
  194. free(A);
  195. }
  196. return 0;
  197. }