malloc.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. * StarPU
  3. * Copyright (C) Université Bordeaux 1, CNRS 2008-2010 (see AUTHORS file)
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation; either version 2.1 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. *
  14. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  15. */
  16. #include <errno.h>
  17. #include <core/workers.h>
  18. #include <common/config.h>
  19. #include <starpu.h>
  20. #include <starpu_cuda.h>
  21. #include <drivers/opencl/driver_opencl.h>
  22. #if defined(STARPU_USE_CUDA) || defined(STARPU_USE_OPENCL)
  23. struct malloc_pinned_codelet_struct {
  24. void **ptr;
  25. size_t dim;
  26. };
  27. #endif
  28. //#ifdef STARPU_USE_OPENCL
  29. //static void malloc_pinned_opencl_codelet(void *buffers[] __attribute__((unused)), void *arg)
  30. //{
  31. // struct malloc_pinned_codelet_struct *s = arg;
  32. // // *(s->ptr) = malloc(s->dim);
  33. // _starpu_opencl_allocate_memory((void **)(s->ptr), s->dim, CL_MEM_READ_WRITE|CL_MEM_ALLOC_HOST_PTR);
  34. //}
  35. //#endif
  36. #ifdef STARPU_USE_CUDA
  37. static void malloc_pinned_cuda_codelet(void *buffers[] __attribute__((unused)), void *arg)
  38. {
  39. struct malloc_pinned_codelet_struct *s = arg;
  40. cudaError_t cures;
  41. cures = cudaHostAlloc((void **)(s->ptr), s->dim, cudaHostAllocPortable);
  42. if (STARPU_UNLIKELY(cures))
  43. STARPU_CUDA_REPORT_ERROR(cures);
  44. }
  45. #endif
  46. #if defined(STARPU_USE_CUDA)// || defined(STARPU_USE_OPENCL)
  47. static struct starpu_perfmodel_t malloc_pinned_model = {
  48. .type = STARPU_HISTORY_BASED,
  49. .symbol = "malloc_pinned"
  50. };
  51. static starpu_codelet malloc_pinned_cl = {
  52. .cuda_func = malloc_pinned_cuda_codelet,
  53. //#ifdef STARPU_USE_OPENCL
  54. // .opencl_func = malloc_pinned_opencl_codelet,
  55. //#endif
  56. .nbuffers = 0,
  57. .model = &malloc_pinned_model
  58. };
  59. #endif
  60. int starpu_data_malloc_pinned_if_possible(void **A, size_t dim)
  61. {
  62. if (STARPU_UNLIKELY(!_starpu_worker_may_perform_blocking_calls()))
  63. return -EDEADLK;
  64. STARPU_ASSERT(A);
  65. if (_starpu_may_submit_cuda_task())
  66. {
  67. #ifdef STARPU_USE_CUDA
  68. int push_res;
  69. struct malloc_pinned_codelet_struct s = {
  70. .ptr = A,
  71. .dim = dim
  72. };
  73. malloc_pinned_cl.where = STARPU_CUDA;
  74. struct starpu_task *task = starpu_task_create();
  75. task->callback_func = NULL;
  76. task->cl = &malloc_pinned_cl;
  77. task->cl_arg = &s;
  78. task->synchronous = 1;
  79. #ifdef STARPU_USE_FXT
  80. _starpu_exclude_task_from_dag(task);
  81. #endif
  82. push_res = starpu_task_submit(task);
  83. STARPU_ASSERT(push_res != -ENODEV);
  84. #endif
  85. }
  86. // else if (_starpu_may_submit_opencl_task())
  87. // {
  88. //#ifdef STARPU_USE_OPENCL
  89. // int push_res;
  90. //
  91. // struct malloc_pinned_codelet_struct s = {
  92. // .ptr = A,
  93. // .dim = dim
  94. // };
  95. //
  96. // malloc_pinned_cl.where = STARPU_OPENCL;
  97. // struct starpu_task *task = starpu_task_create();
  98. // task->callback_func = NULL;
  99. // task->cl = &malloc_pinned_cl;
  100. // task->cl_arg = &s;
  101. //
  102. // task->synchronous = 1;
  103. //
  104. //#ifdef STARPU_USE_FXT
  105. // _starpu_exclude_task_from_dag(task);
  106. //#endif
  107. //
  108. // push_res = starpu_task_submit(task);
  109. // STARPU_ASSERT(push_res != -ENODEV);
  110. //#endif
  111. // }
  112. else {
  113. *A = malloc(dim);
  114. }
  115. STARPU_ASSERT(*A);
  116. return 0;
  117. }
  118. #ifdef STARPU_USE_CUDA
  119. static void free_pinned_cuda_codelet(void *buffers[] __attribute__((unused)), void *arg)
  120. {
  121. cudaError_t cures;
  122. cures = cudaFreeHost(arg);
  123. if (STARPU_UNLIKELY(cures))
  124. STARPU_CUDA_REPORT_ERROR(cures);
  125. }
  126. #endif
  127. //#ifdef STARPU_USE_OPENCL
  128. //static void free_pinned_opencl_codelet(void *buffers[] __attribute__((unused)), void *arg)
  129. //{
  130. // // free(arg);
  131. // int err = clReleaseMemObject(arg);
  132. // if (err != CL_SUCCESS) STARPU_OPENCL_REPORT_ERROR(err);
  133. //}
  134. //#endif
  135. #if defined(STARPU_USE_CUDA) // || defined(STARPU_USE_OPENCL)
  136. static struct starpu_perfmodel_t free_pinned_model = {
  137. .type = STARPU_HISTORY_BASED,
  138. .symbol = "free_pinned"
  139. };
  140. static starpu_codelet free_pinned_cl = {
  141. .cuda_func = free_pinned_cuda_codelet,
  142. //#ifdef STARPU_USE_OPENCL
  143. // .opencl_func = free_pinned_opencl_codelet,
  144. //#endif
  145. .nbuffers = 0,
  146. .model = &free_pinned_model
  147. };
  148. #endif
  149. int starpu_data_free_pinned_if_possible(void *A)
  150. {
  151. if (STARPU_UNLIKELY(!_starpu_worker_may_perform_blocking_calls()))
  152. return -EDEADLK;
  153. if (_starpu_may_submit_cuda_task())
  154. {
  155. #ifdef STARPU_USE_CUDA
  156. int push_res;
  157. free_pinned_cl.where = STARPU_CUDA;
  158. struct starpu_task *task = starpu_task_create();
  159. task->callback_func = NULL;
  160. task->cl = &free_pinned_cl;
  161. task->cl_arg = A;
  162. task->synchronous = 1;
  163. #ifdef STARPU_USE_FXT
  164. _starpu_exclude_task_from_dag(task);
  165. #endif
  166. push_res = starpu_task_submit(task);
  167. STARPU_ASSERT(push_res != -ENODEV);
  168. #endif
  169. }
  170. // else if (_starpu_may_submit_opencl_task())
  171. // {
  172. //#ifdef STARPU_USE_OPENCL
  173. // int push_res;
  174. //
  175. // free_pinned_cl.where = STARPU_OPENCL;
  176. // struct starpu_task *task = starpu_task_create();
  177. // task->callback_func = NULL;
  178. // task->cl = &free_pinned_cl;
  179. // task->cl_arg = A;
  180. //
  181. // task->synchronous = 1;
  182. //
  183. //#ifdef STARPU_USE_FXT
  184. // _starpu_exclude_task_from_dag(task);
  185. //#endif
  186. //
  187. // push_res = starpu_task_submit(task);
  188. // STARPU_ASSERT(push_res != -ENODEV);
  189. //#endif
  190. // }
  191. else {
  192. free(A);
  193. }
  194. return 0;
  195. }