cl_enqueuewritebuffer.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 soclEnqueueWriteBuffer_cpu_task(void *descr[], void *args) {
  18. command_write_buffer cmd = (command_write_buffer)args;
  19. void * ptr = (void*)STARPU_VARIABLE_GET_PTR(descr[0]);
  20. DEBUG_MSG("[Buffer %d] Writing %ld bytes from %p to %p\n", cmd->buffer->id, cmd->cb, cmd->ptr, ptr+cmd->offset);
  21. //FIXME: Fix for people who use USE_HOST_PTR, modify data at host_ptr and use WriteBuffer to commit the change.
  22. // StarPU may have erased host mem at host_ptr (for instance by retrieving current buffer data at host_ptr)
  23. // Buffer mapping facilities should be used instead
  24. // Maybe we should report the bug here... for now, we just avoid memcpy crash due to overlapping regions...
  25. if (ptr+cmd->offset != cmd->ptr)
  26. memcpy(ptr+cmd->offset, cmd->ptr, cmd->cb);
  27. }
  28. static void soclEnqueueWriteBuffer_opencl_task(void *descr[], void *args) {
  29. command_write_buffer cmd = (command_write_buffer)args;
  30. cl_mem mem = (cl_mem)STARPU_VARIABLE_GET_PTR(descr[0]);
  31. DEBUG_MSG("[Buffer %d] Writing %ld bytes to offset %ld from %p\n", cmd->buffer->id, cmd->cb, cmd->offset, cmd->ptr);
  32. int wid = starpu_worker_get_id();
  33. cl_command_queue cq;
  34. starpu_opencl_get_queue(wid, &cq);
  35. cl_event ev;
  36. cl_int err = clEnqueueWriteBuffer(cq, mem, CL_TRUE, cmd->offset, cmd->cb, cmd->ptr, 0, NULL, &ev);
  37. if (err != CL_SUCCESS)
  38. ERROR_CL("clEnqueueWriteBuffer", err);
  39. clWaitForEvents(1, &ev);
  40. clReleaseEvent(ev);
  41. }
  42. static struct starpu_perfmodel write_buffer_perfmodel = {
  43. .type = STARPU_HISTORY_BASED,
  44. .symbol = "SOCL_WRITE_BUFFER"
  45. };
  46. static struct starpu_codelet codelet_writebuffer = {
  47. .where = STARPU_OPENCL,
  48. .model = &write_buffer_perfmodel,
  49. .cpu_funcs = { &soclEnqueueWriteBuffer_cpu_task, NULL },
  50. .opencl_funcs = { &soclEnqueueWriteBuffer_opencl_task, NULL },
  51. .modes = {STARPU_W},
  52. .nbuffers = 1
  53. };
  54. static struct starpu_codelet codelet_writebuffer_partial = {
  55. .where = STARPU_OPENCL,
  56. .model = &write_buffer_perfmodel,
  57. .cpu_funcs = { &soclEnqueueWriteBuffer_cpu_task, NULL },
  58. .opencl_funcs = { &soclEnqueueWriteBuffer_opencl_task, NULL },
  59. .modes = {STARPU_RW},
  60. .nbuffers = 1
  61. };
  62. cl_int command_write_buffer_submit(command_write_buffer cmd) {
  63. /* Aliases */
  64. cl_mem buffer = cmd->buffer;
  65. size_t cb = cmd->cb;
  66. struct starpu_task *task;
  67. task = task_create(CL_COMMAND_WRITE_BUFFER);
  68. task->handles[0] = buffer->handle;
  69. //If only a subpart of the buffer is written, RW access mode is required
  70. if (cb != buffer->size)
  71. task->cl = &codelet_writebuffer_partial;
  72. else
  73. task->cl = &codelet_writebuffer;
  74. task->cl_arg = cmd;
  75. task->cl_arg_size = sizeof(*cmd);
  76. /* Execute the task on a specific worker? */
  77. if (cmd->_command.cq->device != NULL) {
  78. task->execute_on_a_specific_worker = 1;
  79. task->workerid = cmd->_command.cq->device->worker_id;
  80. }
  81. //The buffer now contains meaningful data
  82. cmd->buffer->scratch = 0;
  83. task_submit(task, cmd);
  84. return CL_SUCCESS;
  85. }
  86. CL_API_ENTRY cl_int CL_API_CALL
  87. soclEnqueueWriteBuffer(cl_command_queue cq,
  88. cl_mem buffer,
  89. cl_bool blocking,
  90. size_t offset,
  91. size_t cb,
  92. const void * ptr,
  93. cl_uint num_events,
  94. const cl_event * events,
  95. cl_event * event) CL_API_SUFFIX__VERSION_1_0
  96. {
  97. command_write_buffer cmd = command_write_buffer_create(buffer, offset, cb, ptr);
  98. command_queue_enqueue(cq, cmd, num_events, events);
  99. RETURN_EVENT(cmd, event);
  100. MAY_BLOCK(blocking);
  101. return CL_SUCCESS;
  102. }