cl_enqueuereadbuffer.c 3.7 KB

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