command_queue.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. cl_event ev = cq->events;
  44. while (ev != NULL) {
  45. ndeps++;
  46. ev = ev->next;
  47. }
  48. }
  49. /*********************
  50. * Return dependencies
  51. *********************/
  52. cl_event * evs = malloc(ndeps * sizeof(cl_event));
  53. int n = 0;
  54. /* Add dependency to last barrier if applicable */
  55. if (cq->barrier != NULL)
  56. evs[n++] = cq->barrier;
  57. /* Add dependencies to out-of-order events (if any) */
  58. if (is_barrier) {
  59. cl_event ev = cq->events;
  60. while (ev != NULL) {
  61. evs[n++] = ev;
  62. ev = ev->next;
  63. }
  64. }
  65. *ret_num_events = ndeps;
  66. *ret_events = evs;
  67. }
  68. /**
  69. * Insert a task in the command queue
  70. * The command queue must be locked!
  71. */
  72. void command_queue_insert(
  73. cl_command_queue cq, /* Command queue */
  74. cl_event task_event, /* Event for the task */
  75. char is_barrier /* Is the task a barrier */
  76. ) {
  77. int in_order = !(cq->properties & CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE);
  78. /*********************
  79. * Insert event
  80. *********************/
  81. if (is_barrier)
  82. cq->events = NULL;
  83. /* Add event to the list of out-of-order events */
  84. if (!in_order) {
  85. task_event->next = cq->events;
  86. task_event->prev = NULL;
  87. if (cq->events != NULL)
  88. cq->events->prev = task_event;
  89. cq->events = task_event;
  90. }
  91. /* Register this event as last barrier */
  92. if (is_barrier || in_order)
  93. cq->barrier = task_event;
  94. /* Add reference to the command queue */
  95. gc_entity_store(&task_event->cq, cq);
  96. }
  97. /**
  98. * Return implicit and explicit dependencies for a task
  99. * The command queue must be locked!
  100. */
  101. void command_queue_dependencies(
  102. cl_command_queue cq, /* Command queue */
  103. char is_barrier, /* Is the task a barrier */
  104. cl_int num_events, /* Number of explicit dependencies */
  105. const cl_event events, /* Explicit dependencies */
  106. cl_int * ret_num_events, /* Returned number of dependencies */
  107. cl_event ** ret_events /* Returned dependencies */
  108. ) {
  109. cl_int implicit_num_events;
  110. cl_event * implicit_events;
  111. /* Implicit dependencies */
  112. command_queue_dependencies_implicit(cq, is_barrier, &implicit_num_events, &implicit_events);
  113. /* Explicit dependencies */
  114. cl_int ndeps = implicit_num_events + num_events;
  115. cl_event * evs = malloc(sizeof(cl_event) * ndeps);
  116. memcpy(evs, implicit_events, sizeof(cl_event) * implicit_num_events);
  117. memcpy(&evs[implicit_num_events], events, sizeof(cl_event) * num_events);
  118. *ret_num_events = ndeps;
  119. *ret_events = evs;
  120. }
  121. /**
  122. * Enqueue the given task and put ev into the command queue.
  123. */
  124. void command_queue_enqueue(
  125. cl_command_queue cq, /* Command queue */
  126. cl_event ev, /* Event triggered on task completion (can be NULL if task event should be used)*/
  127. cl_int is_barrier, /* True if the task acts as a barrier */
  128. cl_int num_events, /* Number of dependencies */
  129. const cl_event * events, /* Dependencies */
  130. cl_int * ret_num_events, /* Returned number of events */
  131. cl_event ** ret_events /* Returned events */
  132. ) {
  133. /* Lock command queue */
  134. pthread_spin_lock(&cq->spin);
  135. command_queue_dependencies(cq, is_barrier, num_events, events, ret_num_events, ret_events);
  136. command_queue_insert(cq, ev, is_barrier);
  137. /* Unlock command queue */
  138. pthread_spin_unlock(&cq->spin);
  139. }
  140. cl_event command_queue_barrier(cl_command_queue cq) {
  141. cl_int ndeps;
  142. cl_event *deps;
  143. //CL_COMMAND_MARKER has been chosen as CL_COMMAND_BARRIER doesn't exist
  144. starpu_task * task = task_create(CL_COMMAND_MARKER);
  145. DEBUG_MSG("Submitting barrier task (event %d)\n", task->tag_id);
  146. command_queue_enqueue(cq, task_event(task), 1, 0, NULL, &ndeps, &deps);
  147. task_submit(task, ndeps, deps);
  148. return task_event(task);
  149. }