cl_enqueuendrangekernel.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. void soclEnqueueNDRangeKernel_task(void *descr[], void *args) {
  18. command_ndrange_kernel cmd = (command_ndrange_kernel)args;
  19. cl_command_queue cq;
  20. int wid;
  21. cl_int err;
  22. wid = starpu_worker_get_id();
  23. starpu_opencl_get_queue(wid, &cq);
  24. DEBUG_MSG("[worker %d] [kernel %d] Executing kernel...\n", wid, cmd->kernel->id);
  25. int range = starpu_worker_get_range();
  26. /* Set arguments */
  27. {
  28. unsigned int i;
  29. int buf = 0;
  30. for (i=0; i<cmd->num_args; i++) {
  31. switch (cmd->arg_types[i]) {
  32. case Null:
  33. err = clSetKernelArg(cmd->kernel->cl_kernels[range], i, cmd->arg_sizes[i], NULL);
  34. break;
  35. case Buffer: {
  36. cl_mem mem;
  37. mem = (cl_mem)STARPU_VARIABLE_GET_PTR(descr[buf]);
  38. err = clSetKernelArg(cmd->kernel->cl_kernels[range], i, cmd->arg_sizes[i], &mem);
  39. buf++;
  40. }
  41. break;
  42. case Immediate:
  43. err = clSetKernelArg(cmd->kernel->cl_kernels[range], i, cmd->arg_sizes[i], cmd->args[i]);
  44. break;
  45. }
  46. if (err != CL_SUCCESS) {
  47. DEBUG_CL("clSetKernelArg", err);
  48. DEBUG_ERROR("Aborting\n");
  49. }
  50. }
  51. }
  52. /* Calling Kernel */
  53. cl_event event;
  54. err = clEnqueueNDRangeKernel(cq, cmd->kernel->cl_kernels[range], cmd->work_dim, cmd->global_work_offset, cmd->global_work_size, cmd->local_work_size, 0, NULL, &event);
  55. if (err != CL_SUCCESS) {
  56. ERROR_MSG("Worker[%d] Unable to Enqueue kernel (error %d)\n", wid, err);
  57. DEBUG_CL("clEnqueueNDRangeKernel", err);
  58. DEBUG_MSG("Workdim %d, global_work_offset %p, global_work_size %p, local_work_size %p\n",
  59. cmd->work_dim, cmd->global_work_offset, cmd->global_work_size, cmd->local_work_size);
  60. DEBUG_MSG("Global work size: %ld %ld %ld\n", cmd->global_work_size[0],
  61. (cmd->work_dim > 1 ? cmd->global_work_size[1] : 1), (cmd->work_dim > 2 ? cmd->global_work_size[2] : 1));
  62. if (cmd->local_work_size != NULL)
  63. DEBUG_MSG("Local work size: %ld %ld %ld\n", cmd->local_work_size[0],
  64. (cmd->work_dim > 1 ? cmd->local_work_size[1] : 1), (cmd->work_dim > 2 ? cmd->local_work_size[2] : 1));
  65. ERROR_MSG("Aborting.\n");
  66. exit(1);
  67. }
  68. /* Waiting for kernel to terminate */
  69. clWaitForEvents(1, &event);
  70. }
  71. static void cleaning_task_callback(void *args) {
  72. command_ndrange_kernel cmd = (command_ndrange_kernel)args;
  73. free(cmd->arg_sizes);
  74. free(cmd->arg_types);
  75. unsigned int i;
  76. for (i=0; i<cmd->num_args; i++) {
  77. free(cmd->args[i]);
  78. }
  79. free(cmd->args);
  80. for (i=0; i<cmd->num_buffers; i++)
  81. gc_entity_unstore(&cmd->buffers[i]);
  82. free(cmd->buffers);
  83. free(cmd->codelet);
  84. cmd->codelet = NULL;
  85. if (cmd->global_work_offset != NULL) {
  86. free((void*)cmd->global_work_offset);
  87. cmd->global_work_offset = NULL;
  88. }
  89. if (cmd->global_work_size != NULL) {
  90. free((void*)cmd->global_work_size);
  91. cmd->global_work_size = NULL;
  92. }
  93. if (cmd->local_work_size != NULL) {
  94. free((void*)cmd->local_work_size);
  95. cmd->local_work_size = NULL;
  96. }
  97. }
  98. /**
  99. * Real kernel enqueuing command
  100. */
  101. cl_int command_ndrange_kernel_submit(command_ndrange_kernel cmd) {
  102. starpu_task task = task_create();
  103. task->cl = cmd->codelet;
  104. task->cl_arg = cmd;
  105. task->cl_arg_size = sizeof(cmd);
  106. struct starpu_codelet * codelet = cmd->codelet;
  107. /* We need to detect which parameters are OpenCL's memory objects and
  108. * we retrieve their corresponding StarPU buffers */
  109. cmd->num_buffers = 0;
  110. cmd->buffers = malloc(sizeof(cl_mem) * cmd->num_args);
  111. unsigned int i;
  112. for (i=0; i<cmd->num_args; i++) {
  113. if (cmd->arg_types[i] == Buffer) {
  114. cl_mem buf = *(cl_mem*)cmd->args[i];
  115. gc_entity_store(&cmd->buffers[cmd->num_buffers], buf);
  116. task->handles[cmd->num_buffers] = buf->handle;
  117. /* Determine best StarPU buffer access mode */
  118. int mode;
  119. if (buf->mode == CL_MEM_READ_ONLY)
  120. mode = STARPU_R;
  121. else if (buf->mode == CL_MEM_WRITE_ONLY) {
  122. mode = STARPU_W;
  123. buf->scratch = 0;
  124. }
  125. else if (buf->scratch) { //RW but never accessed in RW or W mode
  126. mode = STARPU_W;
  127. buf->scratch = 0;
  128. }
  129. else {
  130. mode = STARPU_RW;
  131. buf->scratch = 0;
  132. }
  133. codelet->modes[cmd->num_buffers] = mode;
  134. cmd->num_buffers += 1;
  135. }
  136. }
  137. codelet->nbuffers = cmd->num_buffers;
  138. task_submit(task, cmd);
  139. /* Enqueue a cleaning task */
  140. //FIXME: execute this in the callback?
  141. starpu_task cleaning_task = task_create_cpu(cleaning_task_callback, cmd,0);
  142. cl_event ev = command_event_get(cmd);
  143. task_depends_on(cleaning_task, 1, &ev);
  144. task_submit(cleaning_task, cmd);
  145. return CL_SUCCESS;
  146. }
  147. CL_API_ENTRY cl_int CL_API_CALL
  148. soclEnqueueNDRangeKernel(cl_command_queue cq,
  149. cl_kernel kernel,
  150. cl_uint work_dim,
  151. const size_t * global_work_offset,
  152. const size_t * global_work_size,
  153. const size_t * local_work_size,
  154. cl_uint num_events,
  155. const cl_event * events,
  156. cl_event * event) CL_API_SUFFIX__VERSION_1_1
  157. {
  158. command_ndrange_kernel cmd = command_ndrange_kernel_create(kernel, work_dim,
  159. global_work_offset, global_work_size, local_work_size);
  160. command_queue_enqueue(cq, cmd, num_events, events);
  161. RETURN_EVENT(cmd, event);
  162. return CL_SUCCESS;
  163. }