starpu_data_cpy.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2020 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
  4. * Copyright (C) 2013 Thibaut Lambert
  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 <starpu.h>
  18. #include <common/config.h>
  19. #include <core/task.h>
  20. #include <core/workers.h>
  21. #include <datawizard/datawizard.h>
  22. #include <util/starpu_data_cpy.h>
  23. #include <starpu_mic.h>
  24. #include <datawizard/memory_nodes.h>
  25. static void common_data_cpy_func(void *descr[], void *cl_arg)
  26. {
  27. unsigned interface_id = *(unsigned *)cl_arg;
  28. const struct starpu_data_interface_ops *interface_ops = _starpu_data_interface_get_ops(interface_id);
  29. const struct starpu_data_copy_methods *copy_methods = interface_ops->copy_methods;
  30. int workerid = starpu_worker_get_id_check();
  31. enum starpu_worker_archtype type = starpu_worker_get_type(workerid);
  32. unsigned memory_node = starpu_worker_get_memory_node(workerid);
  33. void *dst_interface = descr[0];
  34. void *src_interface = descr[1];
  35. switch (type)
  36. {
  37. case STARPU_CPU_WORKER:
  38. if (copy_methods->ram_to_ram)
  39. {
  40. copy_methods->ram_to_ram(src_interface, memory_node, dst_interface, memory_node);
  41. return;
  42. }
  43. break;
  44. #ifdef STARPU_USE_CUDA
  45. case STARPU_CUDA_WORKER:
  46. {
  47. cudaStream_t stream = starpu_cuda_get_local_stream();
  48. if (copy_methods->cuda_to_cuda_async)
  49. {
  50. copy_methods->cuda_to_cuda_async(src_interface, memory_node, dst_interface, memory_node, stream);
  51. return;
  52. }
  53. else if (copy_methods->cuda_to_cuda)
  54. {
  55. copy_methods->cuda_to_cuda(src_interface, memory_node, dst_interface, memory_node);
  56. return;
  57. }
  58. break;
  59. }
  60. #endif
  61. case STARPU_OPENCL_WORKER:
  62. if (copy_methods->opencl_to_opencl)
  63. {
  64. copy_methods->opencl_to_opencl(src_interface, memory_node, dst_interface, memory_node);
  65. return;
  66. }
  67. break;
  68. default:
  69. /* unknown architecture */
  70. STARPU_ABORT();
  71. }
  72. STARPU_ASSERT(copy_methods->any_to_any);
  73. copy_methods->any_to_any(src_interface, memory_node, dst_interface, memory_node, NULL);
  74. }
  75. void mp_cpy_kernel(void *descr[], void *cl_arg)
  76. {
  77. unsigned interface_id = *(unsigned *)cl_arg;
  78. const struct starpu_data_interface_ops *interface_ops = _starpu_data_interface_get_ops(interface_id);
  79. const struct starpu_data_copy_methods *copy_methods = interface_ops->copy_methods;
  80. void *dst_interface = descr[0];
  81. void *src_interface = descr[1];
  82. if(copy_methods->ram_to_ram)
  83. copy_methods->ram_to_ram(src_interface, STARPU_MAIN_RAM, dst_interface, STARPU_MAIN_RAM);
  84. else if(copy_methods->any_to_any)
  85. copy_methods->any_to_any(src_interface, STARPU_MAIN_RAM, dst_interface, STARPU_MAIN_RAM, NULL);
  86. else
  87. STARPU_ABORT();
  88. }
  89. static starpu_mic_kernel_t mic_cpy_func()
  90. {
  91. #ifdef STARPU_USE_MIC
  92. starpu_mic_func_symbol_t mic_symbol = NULL;
  93. starpu_mic_register_kernel(&mic_symbol, "mp_cpy_kernel");
  94. return starpu_mic_get_kernel(mic_symbol);
  95. #else
  96. STARPU_ABORT();
  97. return NULL;
  98. #endif
  99. }
  100. struct starpu_perfmodel copy_model =
  101. {
  102. .type = STARPU_HISTORY_BASED,
  103. .symbol = "starpu_data_cpy"
  104. };
  105. static struct starpu_codelet copy_cl =
  106. {
  107. .where = STARPU_CPU|STARPU_CUDA|STARPU_OPENCL|STARPU_MIC,
  108. .cpu_funcs = {common_data_cpy_func},
  109. .cuda_funcs = {common_data_cpy_func},
  110. .opencl_funcs = {common_data_cpy_func},
  111. .mic_funcs = {mic_cpy_func},
  112. .nbuffers = 2,
  113. .modes = {STARPU_W, STARPU_R},
  114. .model = &copy_model
  115. };
  116. int _starpu_data_cpy(starpu_data_handle_t dst_handle, starpu_data_handle_t src_handle,
  117. int asynchronous, void (*callback_func)(void*), void *callback_arg,
  118. int reduction, struct starpu_task *reduction_dep_task)
  119. {
  120. struct starpu_task *task = starpu_task_create();
  121. STARPU_ASSERT(task);
  122. task->name = "data_cpy";
  123. struct _starpu_job *j = _starpu_get_job_associated_to_task(task);
  124. if (reduction)
  125. {
  126. j->reduction_task = reduction;
  127. if (reduction_dep_task)
  128. starpu_task_declare_deps_array(task, 1, &reduction_dep_task);
  129. }
  130. task->cl = &copy_cl;
  131. unsigned *interface_id;
  132. _STARPU_MALLOC(interface_id, sizeof(*interface_id));
  133. *interface_id = dst_handle->ops->interfaceid;
  134. task->cl_arg = interface_id;
  135. task->cl_arg_size = sizeof(*interface_id);
  136. task->cl_arg_free = 1;
  137. task->priority = STARPU_MAX_PRIO; //TODO: make it as a parameter
  138. task->callback_func = callback_func;
  139. task->callback_arg = callback_arg;
  140. STARPU_TASK_SET_HANDLE(task, dst_handle, 0);
  141. STARPU_TASK_SET_HANDLE(task, src_handle, 1);
  142. task->synchronous = !asynchronous;
  143. int ret = _starpu_task_submit_internally(task);
  144. STARPU_ASSERT(!ret);
  145. return 0;
  146. }
  147. int starpu_data_cpy(starpu_data_handle_t dst_handle, starpu_data_handle_t src_handle,
  148. int asynchronous, void (*callback_func)(void*), void *callback_arg)
  149. {
  150. return _starpu_data_cpy(dst_handle, src_handle, asynchronous, callback_func, callback_arg, 0, NULL);
  151. }
  152. /* TODO: implement copy on write, and introduce starpu_data_dup as well */
  153. int starpu_data_dup_ro(starpu_data_handle_t *dst_handle, starpu_data_handle_t src_handle, int asynchronous)
  154. {
  155. _starpu_spin_lock(&src_handle->header_lock);
  156. if (src_handle->readonly_dup) {
  157. /* Already a ro duplicate, just return it with one more ref */
  158. *dst_handle = src_handle->readonly_dup;
  159. _starpu_spin_unlock(&src_handle->header_lock);
  160. _starpu_spin_lock(&(*dst_handle)->header_lock);
  161. (*dst_handle)->aliases++;
  162. _starpu_spin_unlock(&(*dst_handle)->header_lock);
  163. return 0;
  164. }
  165. if (src_handle->readonly) {
  166. src_handle->aliases++;
  167. _starpu_spin_unlock(&src_handle->header_lock);
  168. *dst_handle = src_handle;
  169. return 0;
  170. }
  171. _starpu_spin_unlock(&src_handle->header_lock);
  172. starpu_data_register_same(dst_handle, src_handle);
  173. _starpu_data_cpy(*dst_handle, src_handle, asynchronous, NULL, NULL, 0, NULL);
  174. (*dst_handle)->readonly = 1;
  175. _starpu_spin_lock(&src_handle->header_lock);
  176. src_handle->readonly_dup = (*dst_handle);
  177. (*dst_handle)->readonly_dup_of = src_handle;
  178. _starpu_spin_unlock(&src_handle->header_lock);
  179. return 0;
  180. }