cl_enqueuewritebuffer.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. struct arg_writebuffer {
  18. size_t offset;
  19. size_t cb;
  20. const void * ptr;
  21. cl_mem buffer;
  22. };
  23. static void soclEnqueueWriteBuffer_cpu_task(void *descr[], void *args) {
  24. struct arg_writebuffer *arg;
  25. arg = (struct arg_writebuffer*)args;
  26. void * ptr = (void*)STARPU_VARIABLE_GET_PTR(descr[0]);
  27. DEBUG_MSG("[Buffer %d] Writing %ld bytes from %p to %p\n", arg->buffer->id, arg->cb, arg->ptr, ptr+arg->offset);
  28. //FIXME: Fix for people who use USE_HOST_PTR, modify data at host_ptr and use WriteBuffer to commit the change.
  29. // StarPU may have erased host mem at host_ptr (for instance by retrieving current buffer data at host_ptr)
  30. // Buffer mapping facilities should be used instead
  31. // Maybe we should report the bug here... for now, we just avoid memcpy crash due to overlapping regions...
  32. if (ptr+arg->offset != arg->ptr)
  33. memcpy(ptr+arg->offset, arg->ptr, arg->cb);
  34. gc_entity_unstore(&arg->buffer);
  35. free(args);
  36. }
  37. static void soclEnqueueWriteBuffer_opencl_task(void *descr[], void *args) {
  38. struct arg_writebuffer *arg;
  39. arg = (struct arg_writebuffer*)args;
  40. cl_mem mem = (cl_mem)STARPU_VARIABLE_GET_PTR(descr[0]);
  41. DEBUG_MSG("[Buffer %d] Writing %ld bytes to offset %ld from %p\n", arg->buffer->id, arg->cb, arg->offset, arg->ptr);
  42. int wid = starpu_worker_get_id();
  43. cl_command_queue cq;
  44. starpu_opencl_get_queue(wid, &cq);
  45. cl_int err = clEnqueueWriteBuffer(cq, mem, CL_TRUE, arg->offset, arg->cb, arg->ptr, 0, NULL, NULL);
  46. if (err != CL_SUCCESS)
  47. DEBUG_CL("clEnqueueWriteBuffer", err);
  48. gc_entity_unstore(&arg->buffer);
  49. free(args);
  50. }
  51. static starpu_codelet codelet_writebuffer = {
  52. .where = STARPU_OPENCL,
  53. .model = NULL,
  54. .cpu_func = &soclEnqueueWriteBuffer_cpu_task,
  55. .opencl_func = &soclEnqueueWriteBuffer_opencl_task,
  56. .nbuffers = 1
  57. };
  58. cl_int command_write_buffer_submit(command_write_buffer cmd) {
  59. /* Aliases */
  60. cl_mem buffer = cmd->buffer;
  61. size_t offset = cmd->offset;
  62. size_t cb = cmd->cb;
  63. const void * ptr = cmd->ptr;
  64. struct starpu_task *task;
  65. struct arg_writebuffer *arg;
  66. task = task_create(CL_COMMAND_WRITE_BUFFER);
  67. task->buffers[0].handle = buffer->handle;
  68. //If only a subpart of the buffer is written, RW access mode is required
  69. if (cb != buffer->size)
  70. task->buffers[0].mode = STARPU_RW;
  71. else
  72. task->buffers[0].mode = STARPU_W;
  73. task->cl = &codelet_writebuffer;
  74. arg = (struct arg_writebuffer*)malloc(sizeof(struct arg_writebuffer));
  75. arg->offset = offset;
  76. arg->cb = cb;
  77. arg->ptr = ptr;
  78. task->cl_arg = arg;
  79. task->cl_arg_size = sizeof(struct arg_writebuffer);
  80. gc_entity_store(&arg->buffer, buffer);
  81. //The buffer now contains meaningful data
  82. arg->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. }