command.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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. #include <string.h>
  18. /* Forward extern declaration */
  19. extern void soclEnqueueNDRangeKernel_task(void *descr[], void *args);
  20. cl_event command_event_get_ex(cl_command cmd) {
  21. cl_event ev = cmd->event;
  22. gc_entity_retain(ev);
  23. return ev;
  24. }
  25. static void command_release_callback(void *a) {
  26. cl_command cmd = (cl_command)a;
  27. // Call command specific release callback
  28. if (cmd->release_callback != NULL)
  29. cmd->release_callback(cmd);
  30. // Generic command destructor
  31. cl_uint i;
  32. for (i=0; i<cmd->num_events; i++) {
  33. gc_entity_unstore(&cmd->events[i]);
  34. }
  35. cmd->num_events = 0;
  36. free(cmd->events);
  37. /* Remove from command queue */
  38. cl_command_queue cq = cmd->event->cq;
  39. if (cq != NULL) {
  40. /* Lock command queue */
  41. starpu_pthread_mutex_lock(&cq->mutex);
  42. /* Remove barrier if applicable */
  43. if (cq->barrier == cmd)
  44. cq->barrier = NULL;
  45. /* Remove from the list of out-of-order commands */
  46. cq->commands = command_list_remove(cq->commands, cmd);
  47. /* Unlock command queue */
  48. starpu_pthread_mutex_unlock(&cq->mutex);
  49. }
  50. // Events may survive to commands that created them
  51. cmd->event->command = NULL;
  52. gc_entity_unstore(&cmd->event);
  53. }
  54. void command_init_ex(cl_command cmd, cl_command_type typ, void (*cb)(void*)) {
  55. gc_entity_init(&cmd->_entity, command_release_callback, "command");
  56. cmd->release_callback = cb;
  57. cmd->typ = typ;
  58. cmd->num_events = 0;
  59. cmd->events = NULL;
  60. cmd->event = event_create(); // we do not use gc_entity_store here because if nobody requires the event, it should be destroyed with the command
  61. cmd->event->command = cmd;
  62. cmd->task = NULL;
  63. cmd->submitted = 0;
  64. }
  65. void command_submit_ex(cl_command cmd) {
  66. #define SUBMIT(typ,name) case typ:\
  67. name##_submit((name)cmd);\
  68. break;
  69. assert(cmd->submitted == 0);
  70. switch(cmd->typ) {
  71. SUBMIT(CL_COMMAND_NDRANGE_KERNEL, command_ndrange_kernel)
  72. SUBMIT(CL_COMMAND_TASK, command_ndrange_kernel)
  73. SUBMIT(CL_COMMAND_READ_BUFFER, command_read_buffer)
  74. SUBMIT(CL_COMMAND_WRITE_BUFFER, command_write_buffer)
  75. SUBMIT(CL_COMMAND_COPY_BUFFER, command_copy_buffer)
  76. SUBMIT(CL_COMMAND_MAP_BUFFER, command_map_buffer)
  77. SUBMIT(CL_COMMAND_UNMAP_MEM_OBJECT, command_unmap_mem_object)
  78. SUBMIT(CL_COMMAND_MARKER, command_marker)
  79. SUBMIT(CL_COMMAND_BARRIER, command_barrier)
  80. default:
  81. ERROR_STOP("Trying to submit unknown command (type %x)", cmd->typ);
  82. }
  83. cmd->submitted = 1;
  84. #undef SUBMIT
  85. }
  86. cl_int command_submit_deep_ex(cl_command cmd) {
  87. if (cmd->submitted == 1)
  88. return CL_SUCCESS;
  89. /* We set this in order to avoid cyclic dependencies */
  90. cmd->submitted = 1;
  91. unsigned int i;
  92. for (i=0; i<cmd->num_events; i++)
  93. command_submit_deep(cmd->events[i]->command);
  94. cmd->submitted = 0;
  95. command_submit_ex(cmd);
  96. return CL_SUCCESS;
  97. }
  98. void command_graph_dump_ex(cl_command cmd) {
  99. unsigned int i;
  100. for (i=0; i<cmd->num_events; i++)
  101. command_graph_dump_ex(cmd->events[i]->command);
  102. const char * typ_str = (cmd->typ == CL_COMMAND_NDRANGE_KERNEL ? "ndrange_kernel" :
  103. cmd->typ == CL_COMMAND_TASK ? "task" :
  104. cmd->typ == CL_COMMAND_READ_BUFFER ? "read_buffer" :
  105. cmd->typ == CL_COMMAND_WRITE_BUFFER ? "write_buffer" :
  106. cmd->typ == CL_COMMAND_COPY_BUFFER ? "copy_buffer" :
  107. cmd->typ == CL_COMMAND_MAP_BUFFER ? "map_buffer" :
  108. cmd->typ == CL_COMMAND_UNMAP_MEM_OBJECT ? "unmap_mem_object" :
  109. cmd->typ == CL_COMMAND_MARKER ? "marker" :
  110. cmd->typ == CL_COMMAND_BARRIER ? "barrier" : "unknown");
  111. printf("CMD %p TYPE %s DEPS", cmd, typ_str);
  112. for (i=0; i<cmd->num_events; i++)
  113. printf(" %p", cmd->events[i]->command);
  114. printf("\n");
  115. }
  116. #define nullOrDup(name,size) cmd->name = memdup_safe(name,size)
  117. #define nullOrFree(name) if (cmd->name != NULL) free((void*)cmd->name)
  118. #define dup(name) cmd->name = name
  119. void command_ndrange_kernel_release(void * arg) {
  120. command_ndrange_kernel cmd = (command_ndrange_kernel)arg;
  121. gc_entity_unstore(&cmd->kernel);
  122. nullOrFree(global_work_offset);
  123. nullOrFree(global_work_size);
  124. nullOrFree(local_work_size);
  125. free(cmd->arg_sizes);
  126. free(cmd->arg_types);
  127. unsigned int i;
  128. for (i=0; i<cmd->num_args; i++) {
  129. free(cmd->args[i]);
  130. cmd->args[i] = NULL;
  131. }
  132. free(cmd->args);
  133. for (i=0; i<cmd->num_buffers; i++)
  134. gc_entity_unstore(&cmd->buffers[i]);
  135. free(cmd->buffers);
  136. }
  137. command_ndrange_kernel command_ndrange_kernel_create (
  138. cl_kernel kernel,
  139. cl_uint work_dim,
  140. const size_t * global_work_offset,
  141. const size_t * global_work_size,
  142. const size_t * local_work_size)
  143. {
  144. command_ndrange_kernel cmd = calloc(1, sizeof(struct command_ndrange_kernel_t));
  145. command_init(cmd, CL_COMMAND_NDRANGE_KERNEL, command_ndrange_kernel_release);
  146. gc_entity_store(&cmd->kernel, kernel);
  147. dup(work_dim);
  148. nullOrDup(global_work_offset, work_dim*sizeof(size_t));
  149. nullOrDup(global_work_size, work_dim*sizeof(size_t));
  150. nullOrDup(local_work_size, work_dim*sizeof(size_t));
  151. starpu_codelet_init(&cmd->codelet);
  152. cmd->codelet.where = STARPU_OPENCL;
  153. cmd->codelet.power_model = NULL;
  154. cmd->codelet.opencl_funcs[0] = &soclEnqueueNDRangeKernel_task;
  155. cmd->codelet.opencl_funcs[1] = NULL;
  156. /* Kernel is mutable, so we duplicate its parameters... */
  157. cmd->num_args = kernel->num_args;
  158. cmd->arg_sizes = memdup(kernel->arg_size, sizeof(size_t) * kernel->num_args);
  159. cmd->arg_types = memdup(kernel->arg_type, sizeof(enum kernel_arg_type) * kernel->num_args);
  160. cmd->args = memdup_deep_varsize_safe(kernel->arg_value, kernel->num_args, kernel->arg_size);
  161. return cmd;
  162. }
  163. command_ndrange_kernel command_task_create (cl_kernel kernel) {
  164. static cl_uint task_work_dim = 3;
  165. static const size_t task_global_work_offset[3] = {0,0,0};
  166. static const size_t task_global_work_size[3] = {1,1,1};
  167. static const size_t * task_local_work_size = NULL;
  168. command_ndrange_kernel cmd = command_ndrange_kernel_create(
  169. kernel, task_work_dim, task_global_work_offset,
  170. task_global_work_size, task_local_work_size);
  171. /* This is the only difference with command_ndrange_kernel_create */
  172. cmd->_command.typ = CL_COMMAND_TASK;
  173. return cmd;
  174. }
  175. command_barrier command_barrier_create () {
  176. command_barrier cmd = malloc(sizeof(struct command_barrier_t));
  177. command_init(cmd, CL_COMMAND_BARRIER, NULL);
  178. return cmd;
  179. }
  180. command_marker command_marker_create () {
  181. command_marker cmd = malloc(sizeof(struct command_marker_t));
  182. command_init(cmd, CL_COMMAND_MARKER, NULL);
  183. return cmd;
  184. }
  185. void command_map_buffer_release(void * UNUSED(arg)) {
  186. /* We DO NOT unstore (release) the buffer as unmap will do it
  187. gc_entity_unstore(&cmd->buffer); */
  188. }
  189. command_map_buffer command_map_buffer_create(
  190. cl_mem buffer,
  191. cl_map_flags map_flags,
  192. size_t offset,
  193. size_t cb
  194. ) {
  195. command_map_buffer cmd = malloc(sizeof(struct command_map_buffer_t));
  196. command_init(cmd, CL_COMMAND_MAP_BUFFER, command_map_buffer_release);
  197. gc_entity_store(&cmd->buffer, buffer);
  198. dup(map_flags);
  199. dup(offset);
  200. dup(cb);
  201. return cmd;
  202. }
  203. void command_unmap_mem_object_release(void * arg) {
  204. command_unmap_mem_object cmd = (command_unmap_mem_object)arg;
  205. /* We release the buffer twice because map buffer command did not */
  206. gc_entity_release(cmd->buffer);
  207. gc_entity_unstore(&cmd->buffer);
  208. }
  209. command_unmap_mem_object command_unmap_mem_object_create(cl_mem buffer, void * ptr) {
  210. command_unmap_mem_object cmd = malloc(sizeof(struct command_unmap_mem_object_t));
  211. command_init(cmd, CL_COMMAND_UNMAP_MEM_OBJECT, command_unmap_mem_object_release);
  212. gc_entity_store(&cmd->buffer, buffer);
  213. dup(ptr);
  214. return cmd;
  215. }
  216. void command_read_buffer_release(void *arg) {
  217. command_read_buffer cmd = (command_read_buffer)arg;
  218. gc_entity_unstore(&cmd->buffer);
  219. }
  220. command_read_buffer command_read_buffer_create(cl_mem buffer, size_t offset, size_t cb, void * ptr) {
  221. command_read_buffer cmd = malloc(sizeof(struct command_read_buffer_t));
  222. command_init(cmd, CL_COMMAND_READ_BUFFER, command_read_buffer_release);
  223. gc_entity_store(&cmd->buffer, buffer);
  224. dup(offset);
  225. dup(cb);
  226. dup(ptr);
  227. return cmd;
  228. }
  229. void command_write_buffer_release(void *arg) {
  230. command_write_buffer cmd = (command_write_buffer)arg;
  231. gc_entity_unstore(&cmd->buffer);
  232. }
  233. command_write_buffer command_write_buffer_create(cl_mem buffer, size_t offset, size_t cb, const void * ptr) {
  234. command_write_buffer cmd = malloc(sizeof(struct command_write_buffer_t));
  235. command_init(cmd, CL_COMMAND_WRITE_BUFFER, command_write_buffer_release);
  236. gc_entity_store(&cmd->buffer, buffer);
  237. dup(offset);
  238. dup(cb);
  239. dup(ptr);
  240. return cmd;
  241. }
  242. void command_copy_buffer_release(void *arg) {
  243. command_copy_buffer cmd = (command_copy_buffer)arg;
  244. gc_entity_unstore(&cmd->src_buffer);
  245. gc_entity_unstore(&cmd->dst_buffer);
  246. }
  247. command_copy_buffer command_copy_buffer_create( cl_mem src_buffer, cl_mem dst_buffer,
  248. size_t src_offset, size_t dst_offset, size_t cb)
  249. {
  250. command_copy_buffer cmd = malloc(sizeof(struct command_copy_buffer_t));
  251. command_init(cmd, CL_COMMAND_COPY_BUFFER, command_copy_buffer_release);
  252. gc_entity_store(&cmd->src_buffer, src_buffer);
  253. gc_entity_store(&cmd->dst_buffer, dst_buffer);
  254. dup(src_offset);
  255. dup(dst_offset);
  256. dup(cb);
  257. return cmd;
  258. }
  259. #undef nullOrDup
  260. #undef nodeNullOrDup
  261. #undef dup
  262. #undef nodeDup
  263. #undef memdup