command.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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 %p TYPE %d DEPS", cmd, cmd->typ);
  64. for (i=0; i<cmd->num_events; i++)
  65. printf(" %p", 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 = calloc(1, 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 = (struct starpu_codelet*)malloc(sizeof(struct starpu_codelet));
  88. starpu_codelet_init(cmd->codelet);
  89. struct starpu_codelet * codelet = cmd->codelet;
  90. codelet->where = STARPU_OPENCL;
  91. codelet->power_model = NULL;
  92. codelet->opencl_funcs[0] = &soclEnqueueNDRangeKernel_task;
  93. codelet->opencl_funcs[1] = NULL;
  94. codelet->model = NULL;
  95. /* Kernel is mutable, so we duplicate its parameters... */
  96. cmd->num_args = kernel->num_args;
  97. cmd->arg_sizes = memdup(kernel->arg_size, sizeof(size_t) * kernel->num_args);
  98. cmd->arg_types = memdup(kernel->arg_type, sizeof(enum kernel_arg_type) * kernel->num_args);
  99. cmd->args = memdup_deep_varsize_safe(kernel->arg_value, kernel->num_args, kernel->arg_size);
  100. return cmd;
  101. }
  102. command_ndrange_kernel command_task_create (cl_kernel kernel) {
  103. static cl_uint task_work_dim = 3;
  104. static const size_t task_global_work_offset[3] = {0,0,0};
  105. static const size_t task_global_work_size[3] = {1,1,1};
  106. static const size_t * task_local_work_size = NULL;
  107. command_ndrange_kernel cmd = command_ndrange_kernel_create(
  108. kernel, task_work_dim, task_global_work_offset,
  109. task_global_work_size, task_local_work_size);
  110. /* This is the only difference with command_ndrange_kernel_create */
  111. cmd->_command.typ = CL_COMMAND_TASK;
  112. return cmd;
  113. }
  114. command_marker command_barrier_create () {
  115. command_marker cmd = malloc(sizeof(struct command_marker_t));
  116. command_init(cmd, CL_COMMAND_BARRIER);
  117. return cmd;
  118. }
  119. command_marker command_marker_create () {
  120. command_marker cmd = malloc(sizeof(struct command_marker_t));
  121. command_init(cmd, CL_COMMAND_MARKER);
  122. return cmd;
  123. }
  124. command_map_buffer command_map_buffer_create(
  125. cl_mem buffer,
  126. cl_map_flags map_flags,
  127. size_t offset,
  128. size_t cb,
  129. cl_event event
  130. ) {
  131. command_map_buffer cmd = malloc(sizeof(struct command_map_buffer_t));
  132. command_init(cmd, CL_COMMAND_MAP_BUFFER);
  133. dupEntity(buffer);
  134. dup(map_flags);
  135. dup(offset);
  136. dup(cb);
  137. dupEntity(event);
  138. return cmd;
  139. }
  140. command_unmap_mem_object command_unmap_mem_object_create(cl_mem buffer, void * ptr) {
  141. command_unmap_mem_object cmd = malloc(sizeof(struct command_unmap_mem_object_t));
  142. command_init(cmd, CL_COMMAND_UNMAP_MEM_OBJECT);
  143. dupEntity(buffer);
  144. dup(ptr);
  145. return cmd;
  146. }
  147. command_read_buffer command_read_buffer_create(cl_mem buffer, size_t offset, size_t cb, void * ptr) {
  148. command_read_buffer cmd = malloc(sizeof(struct command_read_buffer_t));
  149. command_init(cmd, CL_COMMAND_READ_BUFFER);
  150. dupEntity(buffer);
  151. dup(offset);
  152. dup(cb);
  153. dup(ptr);
  154. return cmd;
  155. }
  156. command_write_buffer command_write_buffer_create(cl_mem buffer, size_t offset, size_t cb, const void * ptr) {
  157. command_write_buffer cmd = malloc(sizeof(struct command_write_buffer_t));
  158. command_init(cmd, CL_COMMAND_WRITE_BUFFER);
  159. dupEntity(buffer);
  160. dup(offset);
  161. dup(cb);
  162. dup(ptr);
  163. return cmd;
  164. }
  165. command_copy_buffer command_copy_buffer_create( cl_mem src_buffer, cl_mem dst_buffer,
  166. size_t src_offset, size_t dst_offset, size_t cb)
  167. {
  168. command_copy_buffer cmd = malloc(sizeof(struct command_copy_buffer_t));
  169. command_init(cmd, CL_COMMAND_COPY_BUFFER);
  170. dupEntity(src_buffer);
  171. dupEntity(dst_buffer);
  172. dup(src_offset);
  173. dup(dst_offset);
  174. dup(cb);
  175. return cmd;
  176. }
  177. #undef nullOrDup
  178. #undef nodeNullOrDup
  179. #undef dup
  180. #undef dupEntity
  181. #undef nodeDup
  182. #undef memdup