cl_enqueuewritebuffer.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 struct starpu_codelet codelet_writebuffer = {
  52. .where = STARPU_OPENCL,
  53. .model = NULL,
  54. .cpu_funcs = { &soclEnqueueWriteBuffer_cpu_task, NULL },
  55. .opencl_funcs = { &soclEnqueueWriteBuffer_opencl_task, NULL },
  56. .modes = {STARPU_W},
  57. .nbuffers = 1
  58. };
  59. static struct starpu_codelet codelet_writebuffer_partial = {
  60. .where = STARPU_OPENCL,
  61. .model = NULL,
  62. .cpu_funcs = { &soclEnqueueWriteBuffer_cpu_task, NULL },
  63. .opencl_funcs = { &soclEnqueueWriteBuffer_opencl_task, NULL },
  64. .modes = {STARPU_RW},
  65. .nbuffers = 1
  66. };
  67. cl_int command_write_buffer_submit(command_write_buffer cmd) {
  68. /* Aliases */
  69. cl_mem buffer = cmd->buffer;
  70. size_t offset = cmd->offset;
  71. size_t cb = cmd->cb;
  72. const void * ptr = cmd->ptr;
  73. struct starpu_task *task;
  74. struct arg_writebuffer *arg;
  75. task = task_create(CL_COMMAND_WRITE_BUFFER);
  76. task->handles[0] = buffer->handle;
  77. //If only a subpart of the buffer is written, RW access mode is required
  78. if (cb != buffer->size)
  79. task->cl = &codelet_writebuffer_partial;
  80. else
  81. task->cl = &codelet_writebuffer;
  82. arg = (struct arg_writebuffer*)malloc(sizeof(struct arg_writebuffer));
  83. arg->offset = offset;
  84. arg->cb = cb;
  85. arg->ptr = ptr;
  86. task->cl_arg = arg;
  87. task->cl_arg_size = sizeof(struct arg_writebuffer);
  88. gc_entity_store(&arg->buffer, buffer);
  89. //The buffer now contains meaningful data
  90. arg->buffer->scratch = 0;
  91. task_submit(task, cmd);
  92. return CL_SUCCESS;
  93. }
  94. CL_API_ENTRY cl_int CL_API_CALL
  95. soclEnqueueWriteBuffer(cl_command_queue cq,
  96. cl_mem buffer,
  97. cl_bool blocking,
  98. size_t offset,
  99. size_t cb,
  100. const void * ptr,
  101. cl_uint num_events,
  102. const cl_event * events,
  103. cl_event * event) CL_API_SUFFIX__VERSION_1_0
  104. {
  105. command_write_buffer cmd = command_write_buffer_create(buffer, offset, cb, ptr);
  106. command_queue_enqueue(cq, cmd, num_events, events);
  107. RETURN_EVENT(cmd, event);
  108. MAY_BLOCK(blocking);
  109. return CL_SUCCESS;
  110. }