command_queue.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. 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->event;
  57. /* Add dependencies to out-of-order events (if any) */
  58. if (is_barrier) {
  59. command_list cl = cq->commands;
  60. while (cl != NULL) {
  61. evs[n++] = cl->cmd->event;
  62. cl = cl->next;
  63. }
  64. }
  65. *ret_num_events = ndeps;
  66. *ret_events = evs;
  67. }
  68. /**
  69. * Insert a command 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_command cmd, /* Command */
  75. int is_barrier /* Is the task a barrier */
  76. ) {
  77. int in_order = !(cq->properties & CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE);
  78. if (is_barrier)
  79. cq->commands = NULL;
  80. /* Add command to the list of out-of-order commands */
  81. if (!in_order)
  82. cq->commands = command_list_cons(cmd, cq->commands);
  83. /* Register this event as last barrier */
  84. if (is_barrier || in_order)
  85. cq->barrier = cmd;
  86. /* Add reference to the command queue */
  87. gc_entity_store(&cmd->event->cq, cq);
  88. }
  89. /**
  90. * Return implicit and explicit dependencies for a task
  91. * The command queue must be locked!
  92. */
  93. void command_queue_dependencies(
  94. cl_command_queue cq, /* Command queue */
  95. int is_barrier, /* Is the task a barrier */
  96. cl_int num_events, /* Number of explicit dependencies */
  97. const cl_event * events, /* Explicit dependencies */
  98. cl_int * ret_num_events, /* Returned number of dependencies */
  99. cl_event ** ret_events /* Returned dependencies */
  100. ) {
  101. cl_int implicit_num_events;
  102. cl_event * implicit_events;
  103. /* Implicit dependencies */
  104. command_queue_dependencies_implicit(cq, is_barrier, &implicit_num_events, &implicit_events);
  105. /* Explicit dependencies */
  106. cl_int ndeps = implicit_num_events + num_events;
  107. cl_event * evs = malloc(sizeof(cl_event) * ndeps);
  108. memcpy(evs, implicit_events, sizeof(cl_event) * implicit_num_events);
  109. memcpy(&evs[implicit_num_events], events, sizeof(cl_event) * num_events);
  110. *ret_num_events = ndeps;
  111. *ret_events = evs;
  112. }
  113. void command_queue_enqueue_ex(cl_command_queue cq, cl_command cmd, cl_uint num_events, const cl_event * events) {
  114. /* Check if the command is a barrier */
  115. int is_barrier = 0;
  116. if (cmd->typ == CL_COMMAND_BARRIER) {
  117. is_barrier = 1;
  118. /* OpenCL has no CL_COMMAND_BARRIER type, so we fall back on CL_COMMAND_MARKER */
  119. cmd->typ = CL_COMMAND_MARKER;
  120. }
  121. /* Set command queue field */
  122. cmd->cq = cq;
  123. /* Lock command queue */
  124. pthread_mutex_lock(&cq->mutex);
  125. //FIXME: crappy separation (command_queue_dependencies + command_queue_insert)
  126. /* Get all (explicit + implicit) dependencies */
  127. cl_int all_num_events;
  128. cl_event * all_events;
  129. command_queue_dependencies(cq, is_barrier, num_events, events, &all_num_events, &all_events);
  130. /* Make all dependencies explicit for the command */
  131. cmd->num_events = all_num_events;
  132. cmd->events = all_events;
  133. /* Insert command in the queue */
  134. command_queue_insert(cq, cmd, is_barrier);
  135. /* Unlock command queue */
  136. pthread_mutex_unlock(&cq->mutex);
  137. }