command_queue.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 "task.h"
  18. #include "gc.h"
  19. /**
  20. * WARNING: command queues do NOT hold references on events. Only events hold references
  21. * on command queues. This way, event release will automatically remove the event from
  22. * its command queue.
  23. */
  24. /**
  25. * Returned implicit dependencies for a task
  26. * Command queue must be locked!
  27. */
  28. void command_queue_dependencies_implicit(
  29. cl_command_queue cq, /* Command queue */
  30. char is_barrier, /* Is the task a barrier */
  31. cl_int * ret_num_events, /* Returned number of dependencies */
  32. cl_event ** ret_events /* Returned dependencies */
  33. ) {
  34. /*********************
  35. * Count dependencies
  36. *********************/
  37. int ndeps = 0;
  38. /* Add dependency to last barrier if applicable */
  39. if (cq->barrier != NULL)
  40. ndeps++;
  41. /* Add dependencies to out-of-order events (if any) */
  42. if (is_barrier) {
  43. command_list cl = cq->commands;
  44. while (cl != NULL) {
  45. ndeps++;
  46. cl = cl->next;
  47. }
  48. }
  49. /*********************
  50. * Return dependencies
  51. *********************/
  52. int n = 0;
  53. cl_event * evs = NULL;
  54. if (ndeps > 0)
  55. evs = malloc(ndeps * sizeof(cl_event));
  56. /* Add dependency to last barrier if applicable */
  57. if (cq->barrier != NULL)
  58. evs[n++] = cq->barrier->event;
  59. /* Add dependencies to out-of-order events (if any) */
  60. if (is_barrier) {
  61. command_list cl = cq->commands;
  62. while (cl != NULL) {
  63. evs[n++] = cl->cmd->event;
  64. cl = cl->next;
  65. }
  66. }
  67. *ret_num_events = ndeps;
  68. *ret_events = evs;
  69. }
  70. /**
  71. * Insert a command in the command queue
  72. * The command queue must be locked!
  73. */
  74. void command_queue_insert(
  75. cl_command_queue cq, /* Command queue */
  76. cl_command cmd, /* Command */
  77. int is_barrier /* Is the task a barrier */
  78. ) {
  79. int in_order = !(cq->properties & CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE);
  80. if (is_barrier)
  81. cq->commands = NULL;
  82. /* Add command to the list of out-of-order commands */
  83. if (!in_order)
  84. cq->commands = command_list_cons(cmd, cq->commands);
  85. /* Register this event as last barrier */
  86. if (is_barrier || in_order)
  87. cq->barrier = cmd;
  88. /* Add reference to the command queue */
  89. gc_entity_store(&cmd->event->cq, cq);
  90. }
  91. /**
  92. * Return implicit and explicit dependencies for a task
  93. * The command queue must be locked!
  94. */
  95. void command_queue_dependencies(
  96. cl_command_queue cq, /* Command queue */
  97. int is_barrier, /* Is the task a barrier */
  98. cl_int num_events, /* Number of explicit dependencies */
  99. const cl_event * events, /* Explicit dependencies */
  100. cl_int * ret_num_events, /* Returned number of dependencies */
  101. cl_event ** ret_events /* Returned dependencies */
  102. ) {
  103. cl_int implicit_num_events;
  104. cl_event * implicit_events;
  105. /* Implicit dependencies */
  106. command_queue_dependencies_implicit(cq, is_barrier, &implicit_num_events, &implicit_events);
  107. /* Explicit dependencies */
  108. cl_int ndeps = implicit_num_events + num_events;
  109. cl_event * evs = malloc(sizeof(cl_event) * ndeps);
  110. memcpy(evs, implicit_events, sizeof(cl_event) * implicit_num_events);
  111. memcpy(&evs[implicit_num_events], events, sizeof(cl_event) * num_events);
  112. free(implicit_events);
  113. *ret_num_events = ndeps;
  114. *ret_events = evs;
  115. }
  116. void command_queue_enqueue_ex(cl_command_queue cq, cl_command cmd, cl_uint num_events, const cl_event * events) {
  117. /* Check if the command is a barrier */
  118. int is_barrier = 0;
  119. if (cmd->typ == CL_COMMAND_BARRIER) {
  120. is_barrier = 1;
  121. /* OpenCL has no CL_COMMAND_BARRIER type, so we fall back on CL_COMMAND_MARKER */
  122. cmd->typ = CL_COMMAND_MARKER;
  123. }
  124. /* Set command queue field */
  125. cmd->cq = cq;
  126. /* Lock command queue */
  127. pthread_mutex_lock(&cq->mutex);
  128. //FIXME: crappy separation (command_queue_dependencies + command_queue_insert)
  129. /* Get all (explicit + implicit) dependencies */
  130. cl_int all_num_events;
  131. cl_event * all_events;
  132. command_queue_dependencies(cq, is_barrier, num_events, events, &all_num_events, &all_events);
  133. /* Make all dependencies explicit for the command */
  134. cmd->num_events = all_num_events;
  135. cmd->events = all_events;
  136. /* Increment event ref count */
  137. gc_entity_retain(cmd->event);
  138. /* Insert command in the queue */
  139. command_queue_insert(cq, cmd, is_barrier);
  140. /* Unlock command queue */
  141. pthread_mutex_unlock(&cq->mutex);
  142. /* Submit command */
  143. command_submit_ex(cmd);
  144. }