cl_enqueuecopybuffer.c 3.5 KB

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