malloc.c 4.9 KB

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