command_queue.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2012 University of Bordeaux
  4. * Copyright (C) 2012 CNRS
  5. * Copyright (C) 2012 Vincent Danjean <Vincent.Danjean@ens-lyon.org>
  6. *
  7. * StarPU is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation; either version 2.1 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * StarPU is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. *
  16. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  17. */
  18. #include "socl.h"
  19. #include "task.h"
  20. #include "gc.h"
  21. /**
  22. * WARNING: command queues do NOT hold references on events. Only events hold references
  23. * on command queues. This way, event release will automatically remove the event from
  24. * its command queue.
  25. */
  26. /**
  27. * Returned implicit dependencies for a task
  28. * Command queue must be locked!
  29. */
  30. void command_queue_dependencies_implicit(
  31. cl_command_queue cq, /* Command queue */
  32. char is_barrier, /* Is the task a barrier */
  33. cl_int * ret_num_events, /* Returned number of dependencies */
  34. cl_event ** ret_events /* Returned dependencies */
  35. ) {
  36. /*********************
  37. * Count dependencies
  38. *********************/
  39. int ndeps = 0;
  40. /* Add dependency to last barrier if applicable */
  41. if (cq->barrier != NULL)
  42. ndeps++;
  43. /* Add dependencies to out-of-order events (if any) */
  44. if (is_barrier) {
  45. command_list cl = cq->commands;
  46. while (cl != NULL) {
  47. ndeps++;
  48. cl = cl->next;
  49. }
  50. }
  51. /*********************
  52. * Return dependencies
  53. *********************/
  54. int n = 0;
  55. cl_event * evs = NULL;
  56. if (ndeps > 0)
  57. evs = malloc(ndeps * sizeof(cl_event));
  58. /* Add dependency to last barrier if applicable */
  59. if (cq->barrier != NULL)
  60. evs[n++] = cq->barrier->event;
  61. /* Add dependencies to out-of-order events (if any) */
  62. if (is_barrier) {
  63. command_list cl = cq->commands;
  64. while (cl != NULL) {
  65. evs[n++] = cl->cmd->event;
  66. cl = cl->next;
  67. }
  68. }
  69. *ret_num_events = ndeps;
  70. *ret_events = evs;
  71. }
  72. /**
  73. * Insert a command in the command queue
  74. * The command queue must be locked!
  75. */
  76. void command_queue_insert(
  77. cl_command_queue cq, /* Command queue */
  78. cl_command cmd, /* Command */
  79. int is_barrier /* Is the task a barrier */
  80. ) {
  81. int in_order = !(cq->properties & CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE);
  82. if (is_barrier)
  83. cq->commands = NULL;
  84. /* Add command to the list of out-of-order commands */
  85. if (!in_order)
  86. cq->commands = command_list_cons(cmd, cq->commands);
  87. /* Register this event as last barrier */
  88. if (is_barrier || in_order)
  89. cq->barrier = cmd;
  90. /* Add reference to the command queue */
  91. gc_entity_store(&cmd->event->cq, cq);
  92. }
  93. /**
  94. * Return implicit and explicit dependencies for a task
  95. * The command queue must be locked!
  96. */
  97. void command_queue_dependencies(
  98. cl_command_queue cq, /* Command queue */
  99. int is_barrier, /* Is the task a barrier */
  100. cl_int num_events, /* Number of explicit dependencies */
  101. const cl_event * events, /* Explicit dependencies */
  102. cl_int * ret_num_events, /* Returned number of dependencies */
  103. cl_event ** ret_events /* Returned dependencies */
  104. ) {
  105. cl_int implicit_num_events;
  106. cl_event * implicit_events;
  107. /* Implicit dependencies */
  108. command_queue_dependencies_implicit(cq, is_barrier, &implicit_num_events, &implicit_events);
  109. /* Explicit dependencies */
  110. cl_int ndeps = implicit_num_events + num_events;
  111. cl_event * evs = malloc(sizeof(cl_event) * ndeps);
  112. memcpy(evs, implicit_events, sizeof(cl_event) * implicit_num_events);
  113. memcpy(&evs[implicit_num_events], events, sizeof(cl_event) * num_events);
  114. free(implicit_events);
  115. *ret_num_events = ndeps;
  116. *ret_events = evs;
  117. }
  118. void command_queue_enqueue_ex(cl_command_queue cq, cl_command cmd, cl_uint num_events, const cl_event * events) {
  119. /* Check if the command is a barrier */
  120. int is_barrier = 0;
  121. if (cmd->typ == CL_COMMAND_BARRIER) {
  122. is_barrier = 1;
  123. /* OpenCL has no CL_COMMAND_BARRIER type, so we fall back on CL_COMMAND_MARKER
  124. WARNING OpenCL has CL_COMMAND_BARRIER in 1.2*/
  125. cmd->typ = CL_COMMAND_MARKER;
  126. }
  127. /* Set command queue field */
  128. cmd->cq = cq;
  129. /* Lock command queue */
  130. pthread_mutex_lock(&cq->mutex);
  131. //FIXME: crappy separation (command_queue_dependencies + command_queue_insert)
  132. /* Get all (explicit + implicit) dependencies */
  133. cl_int all_num_events;
  134. cl_event * all_events;
  135. command_queue_dependencies(cq, is_barrier, num_events, events, &all_num_events, &all_events);
  136. /* Make all dependencies explicit for the command */
  137. cmd->num_events = all_num_events;
  138. cmd->events = all_events;
  139. /* Increment event ref count */
  140. gc_entity_retain(cmd->event);
  141. /* Insert command in the queue */
  142. command_queue_insert(cq, cmd, is_barrier);
  143. /* Unlock command queue */
  144. pthread_mutex_unlock(&cq->mutex);
  145. /* Submit command */
  146. command_submit_ex(cmd);
  147. }