malloc.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. _starpu_exclude_task_from_dag(task);
  80. push_res = starpu_task_submit(task);
  81. STARPU_ASSERT(push_res != -ENODEV);
  82. #endif
  83. }
  84. // else if (_starpu_may_submit_opencl_task())
  85. // {
  86. //#ifdef STARPU_USE_OPENCL
  87. // int push_res;
  88. //
  89. // struct malloc_pinned_codelet_struct s = {
  90. // .ptr = A,
  91. // .dim = dim
  92. // };
  93. //
  94. // malloc_pinned_cl.where = STARPU_OPENCL;
  95. // struct starpu_task *task = starpu_task_create();
  96. // task->callback_func = NULL;
  97. // task->cl = &malloc_pinned_cl;
  98. // task->cl_arg = &s;
  99. //
  100. // task->synchronous = 1;
  101. //
  102. // _starpu_exclude_task_from_dag(task);
  103. //
  104. // push_res = starpu_task_submit(task);
  105. // STARPU_ASSERT(push_res != -ENODEV);
  106. //#endif
  107. // }
  108. else {
  109. *A = malloc(dim);
  110. }
  111. STARPU_ASSERT(*A);
  112. return 0;
  113. }
  114. #ifdef STARPU_USE_CUDA
  115. static void free_pinned_cuda_codelet(void *buffers[] __attribute__((unused)), void *arg)
  116. {
  117. cudaError_t cures;
  118. cures = cudaFreeHost(arg);
  119. if (STARPU_UNLIKELY(cures))
  120. STARPU_CUDA_REPORT_ERROR(cures);
  121. }
  122. #endif
  123. //#ifdef STARPU_USE_OPENCL
  124. //static void free_pinned_opencl_codelet(void *buffers[] __attribute__((unused)), void *arg)
  125. //{
  126. // // free(arg);
  127. // int err = clReleaseMemObject(arg);
  128. // if (err != CL_SUCCESS) STARPU_OPENCL_REPORT_ERROR(err);
  129. //}
  130. //#endif
  131. #if defined(STARPU_USE_CUDA) // || defined(STARPU_USE_OPENCL)
  132. static struct starpu_perfmodel_t free_pinned_model = {
  133. .type = STARPU_HISTORY_BASED,
  134. .symbol = "free_pinned"
  135. };
  136. static starpu_codelet free_pinned_cl = {
  137. .cuda_func = free_pinned_cuda_codelet,
  138. //#ifdef STARPU_USE_OPENCL
  139. // .opencl_func = free_pinned_opencl_codelet,
  140. //#endif
  141. .nbuffers = 0,
  142. .model = &free_pinned_model
  143. };
  144. #endif
  145. int starpu_data_free_pinned_if_possible(void *A)
  146. {
  147. if (STARPU_UNLIKELY(!_starpu_worker_may_perform_blocking_calls()))
  148. return -EDEADLK;
  149. if (_starpu_may_submit_cuda_task())
  150. {
  151. #ifdef STARPU_USE_CUDA
  152. int push_res;
  153. free_pinned_cl.where = STARPU_CUDA;
  154. struct starpu_task *task = starpu_task_create();
  155. task->callback_func = NULL;
  156. task->cl = &free_pinned_cl;
  157. task->cl_arg = A;
  158. task->synchronous = 1;
  159. _starpu_exclude_task_from_dag(task);
  160. push_res = starpu_task_submit(task);
  161. STARPU_ASSERT(push_res != -ENODEV);
  162. #endif
  163. }
  164. // else if (_starpu_may_submit_opencl_task())
  165. // {
  166. //#ifdef STARPU_USE_OPENCL
  167. // int push_res;
  168. //
  169. // free_pinned_cl.where = STARPU_OPENCL;
  170. // struct starpu_task *task = starpu_task_create();
  171. // task->callback_func = NULL;
  172. // task->cl = &free_pinned_cl;
  173. // task->cl_arg = A;
  174. //
  175. // task->synchronous = 1;
  176. //
  177. // _starpu_exclude_task_from_dag(task);
  178. //
  179. // push_res = starpu_task_submit(task);
  180. // STARPU_ASSERT(push_res != -ENODEV);
  181. //#endif
  182. // }
  183. else {
  184. free(A);
  185. }
  186. return 0;
  187. }