cl_enqueuereadbuffer.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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_readbuffer {
  18. size_t offset;
  19. size_t cb;
  20. void * ptr;
  21. cl_mem buffer;
  22. };
  23. static void soclEnqueueReadBuffer_cpu_task(void *descr[], void *args) {
  24. struct arg_readbuffer *arg;
  25. arg = (struct arg_readbuffer*)args;
  26. void * ptr = (void*)STARPU_VARIABLE_GET_PTR(descr[0]);
  27. DEBUG_MSG("[Buffer %d] Reading %ld bytes from %p to %p\n", arg->buffer->id, arg->cb, ptr+arg->offset, arg->ptr);
  28. //This fix is for people who use USE_HOST_PTR and still use ReadBuffer to sync the buffer in host mem at host_ptr.
  29. //They should use buffer mapping facilities instead.
  30. if (ptr+arg->offset != arg->ptr)
  31. memcpy(arg->ptr, ptr+arg->offset, arg->cb);
  32. gc_entity_unstore(&arg->buffer);
  33. free(args);
  34. }
  35. static void soclEnqueueReadBuffer_opencl_task(void *descr[], void *args) {
  36. struct arg_readbuffer *arg;
  37. arg = (struct arg_readbuffer*)args;
  38. cl_mem mem = (cl_mem)STARPU_VARIABLE_GET_PTR(descr[0]);
  39. DEBUG_MSG("[Buffer %d] Reading %ld bytes from offset %ld into %p\n", arg->buffer->id, arg->cb, arg->offset, arg->ptr);
  40. int wid = starpu_worker_get_id();
  41. cl_command_queue cq;
  42. starpu_opencl_get_queue(wid, &cq);
  43. cl_int ret = clEnqueueReadBuffer(cq, mem, CL_TRUE, arg->offset, arg->cb, arg->ptr, 0, NULL, NULL);
  44. if (ret != CL_SUCCESS)
  45. DEBUG_CL("clEnqueueReadBuffer", ret);
  46. gc_entity_unstore(&arg->buffer);
  47. free(args);
  48. }
  49. static struct starpu_codelet codelet_readbuffer = {
  50. .where = STARPU_OPENCL,
  51. .model = NULL,
  52. .cpu_funcs = { &soclEnqueueReadBuffer_cpu_task, NULL },
  53. .opencl_funcs = { &soclEnqueueReadBuffer_opencl_task, NULL },
  54. .nbuffers = 1
  55. };
  56. cl_int command_read_buffer_submit(command_read_buffer cmd) {
  57. /* Aliases */
  58. cl_mem buffer = cmd->buffer;
  59. size_t offset = cmd->offset;
  60. size_t cb = cmd->cb;
  61. void * ptr = cmd->ptr;
  62. struct starpu_task *task;
  63. struct arg_readbuffer *arg;
  64. task = task_create(CL_COMMAND_READ_BUFFER);
  65. task->buffers[0].handle = buffer->handle;
  66. task->buffers[0].mode = STARPU_R;
  67. task->cl = &codelet_readbuffer;
  68. arg = (struct arg_readbuffer*)malloc(sizeof(struct arg_readbuffer));
  69. arg->offset = offset;
  70. arg->cb = cb;
  71. arg->ptr = ptr;
  72. task->cl_arg = arg;
  73. task->cl_arg_size = sizeof(struct arg_readbuffer);
  74. gc_entity_store(&arg->buffer, buffer);
  75. task_submit(task, cmd);
  76. return CL_SUCCESS;
  77. }
  78. CL_API_ENTRY cl_int CL_API_CALL
  79. soclEnqueueReadBuffer(cl_command_queue cq,
  80. cl_mem buffer,
  81. cl_bool blocking,
  82. size_t offset,
  83. size_t cb,
  84. void * ptr,
  85. cl_uint num_events,
  86. const cl_event * events,
  87. cl_event * event) CL_API_SUFFIX__VERSION_1_0
  88. {
  89. command_read_buffer cmd = command_read_buffer_create(buffer, offset, cb, ptr);
  90. command_queue_enqueue(cq, cmd, num_events, events);
  91. RETURN_EVENT(cmd, event);
  92. MAY_BLOCK(blocking);
  93. return CL_SUCCESS;
  94. }