command.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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 command_init_ex(cl_command cmd, cl_command_type typ) {
  18. cmd->typ = typ;
  19. cmd->num_events = 0;
  20. cmd->events = NULL;
  21. cmd->event = event_create();
  22. cmd->event->command = cmd;
  23. cmd->cq = NULL;
  24. cmd->task = NULL;
  25. cmd->submitted = 0;
  26. }
  27. void command_submit_ex(cl_command cmd) {
  28. #define SUBMIT(typ,name) case typ:\
  29. name##_submit((name)cmd);\
  30. break;
  31. assert(cmd->submitted == 0);
  32. switch(cmd->typ) {
  33. SUBMIT(CL_COMMAND_NDRANGE_KERNEL, command_ndrange_kernel)
  34. SUBMIT(CL_COMMAND_TASK, command_ndrange_kernel)
  35. SUBMIT(CL_COMMAND_READ_BUFFER, command_read_buffer)
  36. SUBMIT(CL_COMMAND_WRITE_BUFFER, command_write_buffer)
  37. SUBMIT(CL_COMMAND_COPY_BUFFER, command_copy_buffer)
  38. SUBMIT(CL_COMMAND_MAP_BUFFER, command_map_buffer)
  39. SUBMIT(CL_COMMAND_UNMAP_MEM_OBJECT, command_unmap_mem_object)
  40. SUBMIT(CL_COMMAND_MARKER, command_marker)
  41. default:
  42. ERROR_STOP("Trying to submit unknown command (type %x)", cmd->typ);
  43. }
  44. cmd->submitted = 1;
  45. #undef SUBMIT
  46. }
  47. cl_int command_submit_deep_ex(cl_command cmd) {
  48. if (cmd->submitted == 1)
  49. return CL_SUCCESS;
  50. /* We set this in order to avoid cyclic dependencies */
  51. cmd->submitted = 1;
  52. unsigned int i;
  53. for (i=0; i<cmd->num_events; i++)
  54. command_submit_deep(cmd->events[i]->command);
  55. cmd->submitted = 0;
  56. command_submit_ex(cmd);
  57. return CL_SUCCESS;
  58. }
  59. void command_graph_dump_ex(cl_command cmd) {
  60. unsigned int i;
  61. for (i=0; i<cmd->num_events; i++)
  62. command_graph_dump_ex(cmd->events[i]->command);
  63. printf("CMD %lx TYPE %d DEPS", cmd, cmd->typ);
  64. for (i=0; i<cmd->num_events; i++)
  65. printf(" %lx", cmd->events[i]->command);
  66. printf("\n");
  67. }
  68. #define nullOrDup(name,size) cmd->name = memdup_safe(name,size)
  69. #define dup(name) cmd->name = name
  70. #define dupEntity(name) do { cmd->name = name; gc_entity_retain(name); } while (0);
  71. void soclEnqueueNDRangeKernel_task(void *descr[], void *args);
  72. command_ndrange_kernel command_ndrange_kernel_create (
  73. cl_kernel kernel,
  74. cl_uint work_dim,
  75. const size_t * global_work_offset,
  76. const size_t * global_work_size,
  77. const size_t * local_work_size)
  78. {
  79. command_ndrange_kernel cmd = malloc(sizeof(struct command_ndrange_kernel_t));
  80. command_init(cmd, CL_COMMAND_NDRANGE_KERNEL);
  81. dupEntity(kernel);
  82. dup(work_dim);
  83. nullOrDup(global_work_offset, work_dim*sizeof(size_t));
  84. nullOrDup(global_work_size, work_dim*sizeof(size_t));
  85. nullOrDup(local_work_size, work_dim*sizeof(size_t));
  86. /* Codelet */
  87. cmd->codelet = (starpu_codelet*)malloc(sizeof(starpu_codelet));
  88. starpu_codelet * codelet = cmd->codelet;
  89. codelet->where = STARPU_OPENCL;
  90. codelet->power_model = NULL;
  91. codelet->opencl_func = &soclEnqueueNDRangeKernel_task;
  92. codelet->model = NULL;
  93. /* Kernel is mutable, so we duplicate its parameters... */
  94. cmd->num_args = kernel->num_args;
  95. cmd->arg_sizes = memdup(kernel->arg_size, sizeof(size_t) * kernel->num_args);
  96. cmd->arg_types = memdup(kernel->arg_type, sizeof(enum kernel_arg_type) * kernel->num_args);
  97. cmd->args = memdup_deep_varsize_safe(kernel->arg_value, kernel->num_args, kernel->arg_size);
  98. return cmd;
  99. }
  100. command_ndrange_kernel command_task_create (cl_kernel kernel) {
  101. static cl_uint task_work_dim = 3;
  102. static const size_t task_global_work_offset[3] = {0,0,0};
  103. static const size_t task_global_work_size[3] = {1,1,1};
  104. static const size_t * task_local_work_size = NULL;
  105. command_ndrange_kernel cmd = command_ndrange_kernel_create(
  106. kernel, task_work_dim, task_global_work_offset,
  107. task_global_work_size, task_local_work_size);
  108. /* This is the only difference with command_ndrange_kernel_create */
  109. cmd->_command.typ = CL_COMMAND_TASK;
  110. return cmd;
  111. }
  112. command_marker command_barrier_create () {
  113. command_marker cmd = malloc(sizeof(struct command_marker_t));
  114. command_init(cmd, CL_COMMAND_BARRIER);
  115. return cmd;
  116. }
  117. command_marker command_marker_create () {
  118. command_marker cmd = malloc(sizeof(struct command_marker_t));
  119. command_init(cmd, CL_COMMAND_MARKER);
  120. return cmd;
  121. }
  122. command_map_buffer command_map_buffer_create(
  123. cl_mem buffer,
  124. cl_map_flags map_flags,
  125. size_t offset,
  126. size_t cb,
  127. cl_event event
  128. ) {
  129. command_map_buffer cmd = malloc(sizeof(struct command_map_buffer_t));
  130. command_init(cmd, CL_COMMAND_MAP_BUFFER);
  131. dupEntity(buffer);
  132. dup(map_flags);
  133. dup(offset);
  134. dup(cb);
  135. dupEntity(event);
  136. return cmd;
  137. }
  138. command_unmap_mem_object command_unmap_mem_object_create(cl_mem buffer, void * ptr) {
  139. command_unmap_mem_object cmd = malloc(sizeof(struct command_unmap_mem_object_t));
  140. command_init(cmd, CL_COMMAND_UNMAP_MEM_OBJECT);
  141. dupEntity(buffer);
  142. dup(ptr);
  143. return cmd;
  144. }
  145. command_read_buffer command_read_buffer_create(cl_mem buffer, size_t offset, size_t cb, void * ptr) {
  146. command_read_buffer cmd = malloc(sizeof(struct command_read_buffer_t));
  147. command_init(cmd, CL_COMMAND_READ_BUFFER);
  148. dupEntity(buffer);
  149. dup(offset);
  150. dup(cb);
  151. dup(ptr);
  152. return cmd;
  153. }
  154. command_write_buffer command_write_buffer_create(cl_mem buffer, size_t offset, size_t cb, const void * ptr) {
  155. command_write_buffer cmd = malloc(sizeof(struct command_write_buffer_t));
  156. command_init(cmd, CL_COMMAND_WRITE_BUFFER);
  157. dupEntity(buffer);
  158. dup(offset);
  159. dup(cb);
  160. dup(ptr);
  161. return cmd;
  162. }
  163. command_copy_buffer command_copy_buffer_create( cl_mem src_buffer, cl_mem dst_buffer,
  164. size_t src_offset, size_t dst_offset, size_t cb)
  165. {
  166. command_copy_buffer cmd = malloc(sizeof(struct command_copy_buffer_t));
  167. command_init(cmd, CL_COMMAND_COPY_BUFFER);
  168. dupEntity(src_buffer);
  169. dupEntity(dst_buffer);
  170. dup(src_offset);
  171. dup(dst_offset);
  172. dup(cb);
  173. return cmd;
  174. }
  175. #undef nullOrDup
  176. #undef nodeNullOrDup
  177. #undef dup
  178. #undef dupEntity
  179. #undef nodeDup
  180. #undef memdup