malloc.c 5.2 KB

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