starpu_data_cpy.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010, 2012-2013 Université de Bordeaux 1
  4. *
  5. * StarPU 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. * StarPU 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 <starpu.h>
  17. #include <common/config.h>
  18. #include <core/task.h>
  19. #include <datawizard/datawizard.h>
  20. #include <util/starpu_data_cpy.h>
  21. #include <starpu_mic.h>
  22. #include <starpu_scc.h>
  23. static void common_data_cpy_func(void *descr[], void *cl_arg)
  24. {
  25. unsigned interface_id = *(unsigned *)cl_arg;
  26. const struct starpu_data_interface_ops *interface_ops = _starpu_data_interface_get_ops(interface_id);
  27. const struct starpu_data_copy_methods *copy_methods = interface_ops->copy_methods;
  28. int workerid = starpu_worker_get_id();
  29. enum starpu_worker_archtype type = starpu_worker_get_type(workerid);
  30. unsigned memory_node = starpu_worker_get_memory_node(workerid);
  31. void *dst_interface = descr[0];
  32. void *src_interface = descr[1];
  33. switch (type)
  34. {
  35. case STARPU_CPU_WORKER:
  36. if (copy_methods->ram_to_ram)
  37. {
  38. copy_methods->ram_to_ram(src_interface, memory_node, dst_interface, memory_node);
  39. return;
  40. }
  41. break;
  42. case STARPU_CUDA_WORKER:
  43. if (copy_methods->cuda_to_cuda)
  44. {
  45. copy_methods->cuda_to_cuda(src_interface, memory_node, dst_interface, memory_node);
  46. return;
  47. }
  48. break;
  49. case STARPU_OPENCL_WORKER:
  50. if (copy_methods->opencl_to_opencl)
  51. {
  52. copy_methods->opencl_to_opencl(src_interface, memory_node, dst_interface, memory_node);
  53. return;
  54. }
  55. break;
  56. default:
  57. /* unknown architecture */
  58. STARPU_ABORT();
  59. }
  60. STARPU_ASSERT(copy_methods->any_to_any);
  61. copy_methods->any_to_any(src_interface, memory_node, dst_interface, memory_node, NULL);
  62. }
  63. void mp_cpy_kernel(void *descr[], void *cl_arg)
  64. {
  65. unsigned interface_id = *(unsigned *)cl_arg;
  66. const struct starpu_data_interface_ops *interface_ops = _starpu_data_interface_get_ops(interface_id);
  67. const struct starpu_data_copy_methods *copy_methods = interface_ops->copy_methods;
  68. void *dst_interface = descr[0];
  69. void *src_interface = descr[1];
  70. STARPU_ASSERT(copy_methods->ram_to_ram);
  71. copy_methods->ram_to_ram(src_interface, 0, dst_interface, 0);
  72. }
  73. static starpu_mic_kernel_t mic_cpy_func()
  74. {
  75. #ifdef STARPU_USE_MIC
  76. static starpu_mic_func_symbol_t mic_symbol = NULL;
  77. if (mic_symbol == NULL)
  78. starpu_mic_register_kernel(&mic_symbol, "mp_cpy_kernel");
  79. return starpu_mic_get_kernel(mic_symbol);
  80. #else
  81. STARPU_ABORT();
  82. return NULL;
  83. #endif
  84. }
  85. static starpu_scc_kernel_t scc_cpy_func()
  86. {
  87. #ifdef STARPU_USE_SCC
  88. static starpu_scc_func_symbol_t scc_symbol = NULL;
  89. if (scc_symbol == NULL)
  90. starpu_scc_register_kernel(&scc_symbol, "mp_cpy_kernel");
  91. return starpu_scc_get_kernel(scc_symbol);
  92. #else
  93. STARPU_ABORT();
  94. return NULL;
  95. #endif
  96. }
  97. struct starpu_perfmodel copy_model =
  98. {
  99. .type = STARPU_HISTORY_BASED,
  100. .symbol = "starpu_data_cpy"
  101. };
  102. static struct starpu_codelet copy_cl =
  103. {
  104. .where = STARPU_CPU|STARPU_CUDA|STARPU_OPENCL|STARPU_MIC|STARPU_SCC,
  105. .cpu_funcs = {common_data_cpy_func, NULL},
  106. .cuda_funcs = {common_data_cpy_func, NULL},
  107. .opencl_funcs = {common_data_cpy_func, NULL},
  108. .mic_funcs = {mic_cpy_func, NULL},
  109. .scc_funcs = {scc_cpy_func, NULL},
  110. .nbuffers = 2,
  111. .modes = {STARPU_W, STARPU_R},
  112. .model = &copy_model
  113. };
  114. int _starpu_data_cpy(starpu_data_handle_t dst_handle, starpu_data_handle_t src_handle,
  115. int asynchronous, void (*callback_func)(void*), void *callback_arg,
  116. int reduction, struct starpu_task *reduction_dep_task)
  117. {
  118. struct starpu_task *task = starpu_task_create();
  119. STARPU_ASSERT(task);
  120. struct _starpu_job *j = _starpu_get_job_associated_to_task(task);
  121. if (reduction)
  122. {
  123. j->reduction_task = reduction;
  124. if (reduction_dep_task)
  125. starpu_task_declare_deps_array(task, 1, &reduction_dep_task);
  126. }
  127. task->cl = &copy_cl;
  128. unsigned *interface_id = malloc(sizeof(*interface_id));
  129. *interface_id = dst_handle->ops->interfaceid;
  130. task->cl_arg = interface_id;
  131. task->cl_arg_size = sizeof(*interface_id);
  132. task->cl_arg_free = 1;
  133. task->callback_func = callback_func;
  134. task->callback_arg = callback_arg;
  135. STARPU_TASK_SET_HANDLE(task, dst_handle, 0);
  136. STARPU_TASK_SET_HANDLE(task, src_handle, 1);
  137. task->synchronous = !asynchronous;
  138. int ret = _starpu_task_submit_internally(task);
  139. STARPU_ASSERT(!ret);
  140. return 0;
  141. }
  142. int starpu_data_cpy(starpu_data_handle_t dst_handle, starpu_data_handle_t src_handle,
  143. int asynchronous, void (*callback_func)(void*), void *callback_arg)
  144. {
  145. return _starpu_data_cpy(dst_handle, src_handle, asynchronous, callback_func, callback_arg, 0, NULL);
  146. }