cl_enqueuereadbuffer.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 soclEnqueueReadBuffer_cpu_task(void *descr[], void *args) {
  18. command_read_buffer cmd = (command_read_buffer)args;
  19. void * ptr = (void*)STARPU_VARIABLE_GET_PTR(descr[0]);
  20. DEBUG_MSG("[Buffer %d] Reading %ld bytes from %p to %p\n", cmd->buffer->id, cmd->cb, ptr+cmd->offset, cmd->ptr);
  21. //This fix is for people who use USE_HOST_PTR and still use ReadBuffer to sync the buffer in host mem at host_ptr.
  22. //They should use buffer mapping facilities instead.
  23. if (ptr+cmd->offset != cmd->ptr)
  24. memcpy(cmd->ptr, ptr+cmd->offset, cmd->cb);
  25. }
  26. static void soclEnqueueReadBuffer_opencl_task(void *descr[], void *args) {
  27. command_read_buffer cmd = (command_read_buffer)args;
  28. cl_mem mem = (cl_mem)STARPU_VARIABLE_GET_PTR(descr[0]);
  29. DEBUG_MSG("[Buffer %d] Reading %ld bytes from offset %ld into %p\n", cmd->buffer->id, cmd->cb, cmd->offset, cmd->ptr);
  30. int wid = starpu_worker_get_id();
  31. cl_command_queue cq;
  32. starpu_opencl_get_queue(wid, &cq);
  33. cl_event ev;
  34. cl_int ret = clEnqueueReadBuffer(cq, mem, CL_TRUE, cmd->offset, cmd->cb, cmd->ptr, 0, NULL, &ev);
  35. if (ret != CL_SUCCESS)
  36. ERROR_CL("clEnqueueReadBuffer", ret);
  37. clWaitForEvents(1, &ev);
  38. clReleaseEvent(ev);
  39. }
  40. static struct starpu_perfmodel read_buffer_perfmodel = {
  41. .type = STARPU_HISTORY_BASED,
  42. .symbol = "SOCL_READ_BUFFER"
  43. };
  44. static struct starpu_codelet codelet_readbuffer = {
  45. .where = STARPU_OPENCL,
  46. .model = &read_buffer_perfmodel,
  47. .cpu_funcs = { &soclEnqueueReadBuffer_cpu_task, NULL },
  48. .opencl_funcs = { &soclEnqueueReadBuffer_opencl_task, NULL },
  49. .modes = {STARPU_R},
  50. .nbuffers = 1
  51. };
  52. cl_int command_read_buffer_submit(command_read_buffer cmd) {
  53. struct starpu_task * task = task_create(CL_COMMAND_READ_BUFFER);
  54. task->handles[0] = cmd->buffer->handle;
  55. task->cl = &codelet_readbuffer;
  56. /* Execute the task on a specific worker? */
  57. if (cmd->_command.cq->device != NULL) {
  58. task->execute_on_a_specific_worker = 1;
  59. task->workerid = cmd->_command.cq->device->worker_id;
  60. }
  61. task->cl_arg = cmd;
  62. task->cl_arg_size = sizeof(*cmd);
  63. task_submit(task, cmd);
  64. return CL_SUCCESS;
  65. }
  66. CL_API_ENTRY cl_int CL_API_CALL
  67. soclEnqueueReadBuffer(cl_command_queue cq,
  68. cl_mem buffer,
  69. cl_bool blocking,
  70. size_t offset,
  71. size_t cb,
  72. void * ptr,
  73. cl_uint num_events,
  74. const cl_event * events,
  75. cl_event * event) CL_API_SUFFIX__VERSION_1_0
  76. {
  77. command_read_buffer cmd = command_read_buffer_create(buffer, offset, cb, ptr);
  78. command_queue_enqueue(cq, cmd, num_events, events);
  79. RETURN_EVENT(cmd, event);
  80. MAY_BLOCK(blocking);
  81. return CL_SUCCESS;
  82. }