starpu_data_cpy.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011 Inria
  4. * Copyright (C) 2010-2018 Université de Bordeaux
  5. * Copyright (C) 2011-2013,2016-2017 CNRS
  6. * Copyright (C) 2013 Thibaut Lambert
  7. *
  8. * StarPU is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU Lesser General Public License as published by
  10. * the Free Software Foundation; either version 2.1 of the License, or (at
  11. * your option) any later version.
  12. *
  13. * StarPU is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  16. *
  17. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  18. */
  19. #include <starpu.h>
  20. #include <common/config.h>
  21. #include <core/task.h>
  22. #include <core/workers.h>
  23. #include <datawizard/datawizard.h>
  24. #include <util/starpu_data_cpy.h>
  25. #include <starpu_mic.h>
  26. #include <starpu_scc.h>
  27. static void common_data_cpy_func(void *descr[], void *cl_arg)
  28. {
  29. unsigned interface_id = *(unsigned *)cl_arg;
  30. const struct starpu_data_interface_ops *interface_ops = _starpu_data_interface_get_ops(interface_id);
  31. const struct starpu_data_copy_methods *copy_methods = interface_ops->copy_methods;
  32. int workerid = starpu_worker_get_id_check();
  33. enum starpu_worker_archtype type = starpu_worker_get_type(workerid);
  34. unsigned memory_node = starpu_worker_get_memory_node(workerid);
  35. void *dst_interface = descr[0];
  36. void *src_interface = descr[1];
  37. switch (type)
  38. {
  39. case STARPU_CPU_WORKER:
  40. if (copy_methods->ram_to_ram)
  41. {
  42. copy_methods->ram_to_ram(src_interface, memory_node, dst_interface, memory_node);
  43. return;
  44. }
  45. break;
  46. #ifdef STARPU_USE_CUDA
  47. case STARPU_CUDA_WORKER:
  48. {
  49. cudaStream_t stream = starpu_cuda_get_local_stream();
  50. if (copy_methods->cuda_to_cuda_async)
  51. {
  52. copy_methods->cuda_to_cuda_async(src_interface, memory_node, dst_interface, memory_node, stream);
  53. return;
  54. }
  55. else if (copy_methods->cuda_to_cuda)
  56. {
  57. copy_methods->cuda_to_cuda(src_interface, memory_node, dst_interface, memory_node);
  58. return;
  59. }
  60. break;
  61. }
  62. #endif
  63. case STARPU_OPENCL_WORKER:
  64. if (copy_methods->opencl_to_opencl)
  65. {
  66. copy_methods->opencl_to_opencl(src_interface, memory_node, dst_interface, memory_node);
  67. return;
  68. }
  69. break;
  70. default:
  71. /* unknown architecture */
  72. STARPU_ABORT();
  73. }
  74. STARPU_ASSERT(copy_methods->any_to_any);
  75. copy_methods->any_to_any(src_interface, memory_node, dst_interface, memory_node, NULL);
  76. }
  77. void mp_cpy_kernel(void *descr[], void *cl_arg)
  78. {
  79. unsigned interface_id = *(unsigned *)cl_arg;
  80. const struct starpu_data_interface_ops *interface_ops = _starpu_data_interface_get_ops(interface_id);
  81. const struct starpu_data_copy_methods *copy_methods = interface_ops->copy_methods;
  82. void *dst_interface = descr[0];
  83. void *src_interface = descr[1];
  84. if(copy_methods->ram_to_ram)
  85. copy_methods->ram_to_ram(src_interface, STARPU_MAIN_RAM, dst_interface, STARPU_MAIN_RAM);
  86. else if(copy_methods->any_to_any)
  87. copy_methods->any_to_any(src_interface, STARPU_MAIN_RAM, dst_interface, STARPU_MAIN_RAM, NULL);
  88. else
  89. STARPU_ABORT();
  90. }
  91. static starpu_mic_kernel_t mic_cpy_func()
  92. {
  93. #ifdef STARPU_USE_MIC
  94. starpu_mic_func_symbol_t mic_symbol = NULL;
  95. starpu_mic_register_kernel(&mic_symbol, "mp_cpy_kernel");
  96. return starpu_mic_get_kernel(mic_symbol);
  97. #else
  98. STARPU_ABORT();
  99. return NULL;
  100. #endif
  101. }
  102. static starpu_scc_kernel_t scc_cpy_func()
  103. {
  104. #ifdef STARPU_USE_SCC
  105. static starpu_scc_func_symbol_t scc_symbol = NULL;
  106. if (scc_symbol == NULL)
  107. starpu_scc_register_kernel(&scc_symbol, "mp_cpy_kernel");
  108. return starpu_scc_get_kernel(scc_symbol);
  109. #else
  110. STARPU_ABORT();
  111. return NULL;
  112. #endif
  113. }
  114. struct starpu_perfmodel copy_model =
  115. {
  116. .type = STARPU_HISTORY_BASED,
  117. .symbol = "starpu_data_cpy"
  118. };
  119. static struct starpu_codelet copy_cl =
  120. {
  121. .where = STARPU_CPU|STARPU_CUDA|STARPU_OPENCL|STARPU_MIC|STARPU_SCC,
  122. .cpu_funcs = {common_data_cpy_func},
  123. .cuda_funcs = {common_data_cpy_func},
  124. .opencl_funcs = {common_data_cpy_func},
  125. .mic_funcs = {mic_cpy_func},
  126. .scc_funcs = {scc_cpy_func},
  127. .nbuffers = 2,
  128. .modes = {STARPU_W, STARPU_R},
  129. .model = &copy_model
  130. };
  131. int _starpu_data_cpy(starpu_data_handle_t dst_handle, starpu_data_handle_t src_handle,
  132. int asynchronous, void (*callback_func)(void*), void *callback_arg,
  133. int reduction, struct starpu_task *reduction_dep_task)
  134. {
  135. struct starpu_task *task = starpu_task_create();
  136. STARPU_ASSERT(task);
  137. task->name = "data_cpy";
  138. struct _starpu_job *j = _starpu_get_job_associated_to_task(task);
  139. if (reduction)
  140. {
  141. j->reduction_task = reduction;
  142. if (reduction_dep_task)
  143. starpu_task_declare_deps_array(task, 1, &reduction_dep_task);
  144. }
  145. task->cl = &copy_cl;
  146. unsigned *interface_id;
  147. _STARPU_MALLOC(interface_id, sizeof(*interface_id));
  148. *interface_id = dst_handle->ops->interfaceid;
  149. task->cl_arg = interface_id;
  150. task->cl_arg_size = sizeof(*interface_id);
  151. task->cl_arg_free = 1;
  152. task->callback_func = callback_func;
  153. task->callback_arg = callback_arg;
  154. STARPU_TASK_SET_HANDLE(task, dst_handle, 0);
  155. STARPU_TASK_SET_HANDLE(task, src_handle, 1);
  156. task->synchronous = !asynchronous;
  157. int ret = _starpu_task_submit_internally(task);
  158. STARPU_ASSERT(!ret);
  159. return 0;
  160. }
  161. int starpu_data_cpy(starpu_data_handle_t dst_handle, starpu_data_handle_t src_handle,
  162. int asynchronous, void (*callback_func)(void*), void *callback_arg)
  163. {
  164. return _starpu_data_cpy(dst_handle, src_handle, asynchronous, callback_func, callback_arg, 0, NULL);
  165. }