cl_enqueuecopybuffer.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2020 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
  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 "socl.h"
  17. static void soclEnqueueCopyBuffer_opencl_task(void *descr[], void *args)
  18. {
  19. int wid;
  20. cl_command_queue cq;
  21. cl_event ev;
  22. command_copy_buffer cmd = (command_copy_buffer)args;
  23. cl_event event = command_event_get(cmd);
  24. event->prof_start = _socl_nanotime();
  25. gc_entity_release(event);
  26. wid = starpu_worker_get_id_check();
  27. starpu_opencl_get_queue(wid, &cq);
  28. cl_mem src = (cl_mem)STARPU_VARIABLE_GET_PTR(descr[0]);
  29. cl_mem dst = (cl_mem)STARPU_VARIABLE_GET_PTR(descr[1]);
  30. clEnqueueCopyBuffer(cq, src,dst, cmd->src_offset, cmd->dst_offset, cmd->cb, 0, NULL, &ev);
  31. clWaitForEvents(1, &ev);
  32. clReleaseEvent(ev);
  33. gc_entity_release_cmd(cmd);
  34. }
  35. static void soclEnqueueCopyBuffer_cpu_task(void *descr[], void *args)
  36. {
  37. command_copy_buffer cmd = (command_copy_buffer)args;
  38. cl_event ev = command_event_get(cmd);
  39. ev->prof_start = _socl_nanotime();
  40. gc_entity_release(ev);
  41. char * src = (void*)STARPU_VARIABLE_GET_PTR(descr[0]);
  42. char * dst = (void*)STARPU_VARIABLE_GET_PTR(descr[1]);
  43. memcpy(dst+cmd->dst_offset, src+cmd->src_offset, cmd->cb);
  44. gc_entity_release_cmd(cmd);
  45. }
  46. static struct starpu_perfmodel copy_buffer_perfmodel =
  47. {
  48. .type = STARPU_HISTORY_BASED,
  49. .symbol = "SOCL_COPY_BUFFER"
  50. };
  51. static struct starpu_codelet codelet_copybuffer =
  52. {
  53. .where = STARPU_CPU | STARPU_OPENCL,
  54. .model = &copy_buffer_perfmodel,
  55. .cpu_funcs = { &soclEnqueueCopyBuffer_cpu_task },
  56. .opencl_funcs = { &soclEnqueueCopyBuffer_opencl_task },
  57. .modes = {STARPU_R, STARPU_RW},
  58. .nbuffers = 2
  59. };
  60. cl_int command_copy_buffer_submit(command_copy_buffer cmd)
  61. {
  62. struct starpu_task * task = task_create(CL_COMMAND_COPY_BUFFER);
  63. task->handles[0] = cmd->src_buffer->handle;
  64. task->handles[1] = cmd->dst_buffer->handle;
  65. task->cl = &codelet_copybuffer;
  66. /* Execute the task on a specific worker? */
  67. if (cmd->_command.event->cq->device != NULL)
  68. {
  69. task->execute_on_a_specific_worker = 1;
  70. task->workerid = cmd->_command.event->cq->device->worker_id;
  71. }
  72. gc_entity_store_cmd(&task->cl_arg, cmd);
  73. task->cl_arg_size = sizeof(*cmd);
  74. cmd->dst_buffer->scratch = 0;
  75. task_submit(task, cmd);
  76. return CL_SUCCESS;
  77. }
  78. CL_API_SUFFIX__VERSION_1_0
  79. CL_API_ENTRY cl_int CL_API_CALL
  80. soclEnqueueCopyBuffer(cl_command_queue cq,
  81. cl_mem src_buffer,
  82. cl_mem dst_buffer,
  83. size_t src_offset,
  84. size_t dst_offset,
  85. size_t cb,
  86. cl_uint num_events,
  87. const cl_event * events,
  88. cl_event * event)
  89. {
  90. command_copy_buffer cmd = command_copy_buffer_create(src_buffer, dst_buffer, src_offset, dst_offset, cb);
  91. cl_event ev = command_event_get(cmd);
  92. command_queue_enqueue(cq, cmd, num_events, events);
  93. RETURN_EVENT(ev, event);
  94. return CL_SUCCESS;
  95. }