cl_enqueuereadbuffer.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011-2012 Inria
  4. * Copyright (C) 2012,2016-2017 CNRS
  5. * Copyright (C) 2010-2011,2013-2014, 2018 Université de Bordeaux
  6. *
  7. * StarPU is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation; either version 2.1 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * StarPU is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. *
  16. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  17. */
  18. #include "socl.h"
  19. static void soclEnqueueReadBuffer_cpu_task(void *descr[], void *args)
  20. {
  21. command_read_buffer cmd = (command_read_buffer)args;
  22. cl_event ev = command_event_get(cmd);
  23. ev->prof_start = _socl_nanotime();
  24. gc_entity_release(ev);
  25. char * ptr = (void*)STARPU_VARIABLE_GET_PTR(descr[0]);
  26. DEBUG_MSG("[Buffer %d] Reading %ld bytes from %p to %p\n", cmd->buffer->id, (long)cmd->cb, ptr+cmd->offset, cmd->ptr);
  27. //This fix is for people who use USE_HOST_PTR and still use ReadBuffer to sync the buffer in host mem at host_ptr.
  28. //They should use buffer mapping facilities instead.
  29. if (ptr+cmd->offset != cmd->ptr)
  30. memcpy(cmd->ptr, ptr+cmd->offset, cmd->cb);
  31. gc_entity_release_cmd(cmd);
  32. }
  33. static void soclEnqueueReadBuffer_opencl_task(void *descr[], void *args)
  34. {
  35. command_read_buffer cmd = (command_read_buffer)args;
  36. cl_event event = command_event_get(cmd);
  37. event->prof_start = _socl_nanotime();
  38. gc_entity_release(event);
  39. cl_mem mem = (cl_mem)STARPU_VARIABLE_GET_PTR(descr[0]);
  40. DEBUG_MSG("[Buffer %d] Reading %ld bytes from offset %ld into %p\n", cmd->buffer->id, (long)cmd->cb, (long)cmd->offset, cmd->ptr);
  41. int wid = starpu_worker_get_id_check();
  42. cl_command_queue cq;
  43. starpu_opencl_get_queue(wid, &cq);
  44. cl_event ev;
  45. cl_int ret = clEnqueueReadBuffer(cq, mem, CL_TRUE, cmd->offset, cmd->cb, cmd->ptr, 0, NULL, &ev);
  46. if (ret != CL_SUCCESS)
  47. ERROR_CL("clEnqueueReadBuffer", ret);
  48. clWaitForEvents(1, &ev);
  49. clReleaseEvent(ev);
  50. gc_entity_release_cmd(cmd);
  51. }
  52. static struct starpu_perfmodel read_buffer_perfmodel =
  53. {
  54. .type = STARPU_HISTORY_BASED,
  55. .symbol = "SOCL_READ_BUFFER"
  56. };
  57. static struct starpu_codelet codelet_readbuffer =
  58. {
  59. .where = STARPU_OPENCL,
  60. .model = &read_buffer_perfmodel,
  61. .cpu_funcs = { &soclEnqueueReadBuffer_cpu_task },
  62. .opencl_funcs = { &soclEnqueueReadBuffer_opencl_task },
  63. .modes = {STARPU_R},
  64. .nbuffers = 1
  65. };
  66. cl_int command_read_buffer_submit(command_read_buffer cmd)
  67. {
  68. struct starpu_task * task = task_create(CL_COMMAND_READ_BUFFER);
  69. task->handles[0] = cmd->buffer->handle;
  70. task->cl = &codelet_readbuffer;
  71. /* Execute the task on a specific worker? */
  72. if (cmd->_command.event->cq->device != NULL)
  73. {
  74. task->execute_on_a_specific_worker = 1;
  75. task->workerid = cmd->_command.event->cq->device->worker_id;
  76. }
  77. gc_entity_store_cmd(&task->cl_arg, cmd);
  78. task->cl_arg_size = sizeof(*cmd);
  79. task_submit(task, cmd);
  80. return CL_SUCCESS;
  81. }
  82. CL_API_SUFFIX__VERSION_1_0
  83. CL_API_ENTRY cl_int CL_API_CALL
  84. soclEnqueueReadBuffer(cl_command_queue cq,
  85. cl_mem buffer,
  86. cl_bool blocking,
  87. size_t offset,
  88. size_t cb,
  89. void * ptr,
  90. cl_uint num_events,
  91. const cl_event * events,
  92. cl_event * event)
  93. {
  94. command_read_buffer cmd = command_read_buffer_create(buffer, offset, cb, ptr);
  95. cl_event ev = command_event_get(cmd);
  96. command_queue_enqueue(cq, cmd, num_events, events);
  97. MAY_BLOCK_THEN_RETURN_EVENT(ev, blocking, event);
  98. return CL_SUCCESS;
  99. }