starpu_data_cpy.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. static void data_cpy_func(void *descr[], void *cl_arg)
  22. {
  23. const struct starpu_data_copy_methods *copy_methods = (const struct starpu_data_copy_methods *) cl_arg;
  24. int workerid = starpu_worker_get_id();
  25. enum starpu_worker_archtype type = starpu_worker_get_type(workerid);
  26. unsigned memory_node = starpu_worker_get_memory_node(workerid);
  27. void *dst_interface = descr[0];
  28. void *src_interface = descr[1];
  29. switch (type)
  30. {
  31. case STARPU_CPU_WORKER:
  32. if (copy_methods->ram_to_ram)
  33. {
  34. copy_methods->ram_to_ram(src_interface, memory_node, dst_interface, memory_node);
  35. return;
  36. }
  37. break;
  38. case STARPU_CUDA_WORKER:
  39. if (copy_methods->cuda_to_cuda)
  40. {
  41. copy_methods->cuda_to_cuda(src_interface, memory_node, dst_interface, memory_node);
  42. return;
  43. }
  44. break;
  45. case STARPU_OPENCL_WORKER:
  46. if (copy_methods->opencl_to_opencl)
  47. {
  48. copy_methods->opencl_to_opencl(src_interface, memory_node, dst_interface, memory_node);
  49. return;
  50. }
  51. break;
  52. default:
  53. /* unknown architecture */
  54. STARPU_ABORT();
  55. }
  56. STARPU_ASSERT(copy_methods->any_to_any);
  57. copy_methods->any_to_any(src_interface, memory_node, dst_interface, memory_node, NULL);
  58. }
  59. struct starpu_perfmodel copy_model =
  60. {
  61. .type = STARPU_HISTORY_BASED,
  62. .symbol = "starpu_data_cpy"
  63. };
  64. static struct starpu_codelet copy_cl =
  65. {
  66. .where = STARPU_CPU|STARPU_CUDA|STARPU_OPENCL,
  67. .cpu_funcs = {data_cpy_func, NULL},
  68. .cuda_funcs = {data_cpy_func, NULL},
  69. .opencl_funcs = {data_cpy_func, NULL},
  70. .nbuffers = 2,
  71. .modes = {STARPU_W, STARPU_R},
  72. .model = &copy_model
  73. };
  74. int _starpu_data_cpy(starpu_data_handle_t dst_handle, starpu_data_handle_t src_handle,
  75. int asynchronous, void (*callback_func)(void*), void *callback_arg,
  76. int reduction, struct starpu_task *reduction_dep_task)
  77. {
  78. const struct starpu_data_copy_methods *copy_methods = dst_handle->ops->copy_methods;
  79. struct starpu_task *task = starpu_task_create();
  80. STARPU_ASSERT(task);
  81. struct _starpu_job *j = _starpu_get_job_associated_to_task(task);
  82. if (reduction)
  83. {
  84. j->reduction_task = reduction;
  85. if (reduction_dep_task)
  86. starpu_task_declare_deps_array(task, 1, &reduction_dep_task);
  87. }
  88. task->cl = &copy_cl;
  89. task->cl_arg = (void *)copy_methods;
  90. task->callback_func = callback_func;
  91. task->callback_arg = callback_arg;
  92. STARPU_TASK_SET_HANDLE(task, dst_handle, 0);
  93. STARPU_TASK_SET_HANDLE(task, src_handle, 1);
  94. task->synchronous = !asynchronous;
  95. int ret = _starpu_task_submit_internally(task);
  96. STARPU_ASSERT(!ret);
  97. return 0;
  98. }
  99. int starpu_data_cpy(starpu_data_handle_t dst_handle, starpu_data_handle_t src_handle,
  100. int asynchronous, void (*callback_func)(void*), void *callback_arg)
  101. {
  102. return _starpu_data_cpy(dst_handle, src_handle, asynchronous, callback_func, callback_arg, 0, NULL);
  103. }