command_queue.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. void command_queue_enqueue_ex(cl_command_queue cq, cl_command cmd, cl_uint num_events, const cl_event * events) {
  27. cl_event ev = command_event_get_ex(cmd);
  28. ev->prof_queued = _socl_nanotime();
  29. gc_entity_release(ev);
  30. /* Check if the command is a barrier */
  31. int is_barrier = (cmd->typ == CL_COMMAND_BARRIER || !(cq->properties & CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE));
  32. /* Add references to the command queue */
  33. gc_entity_store(&cmd->event->cq, cq);
  34. /* Lock command queue */
  35. pthread_mutex_lock(&cq->mutex);
  36. /*** Number of dependencies ***/
  37. int ndeps = num_events;
  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. /*** Dependencies ***/
  50. cl_event * deps = malloc(ndeps * sizeof(cl_event));
  51. int n = 0;
  52. /* Add dependency to last barrier if applicable */
  53. if (cq->barrier != NULL)
  54. gc_entity_store(&deps[n++], cq->barrier->event);
  55. /* Add dependencies to out-of-order events (if any) */
  56. if (is_barrier) {
  57. command_list cl = cq->commands;
  58. while (cl != NULL) {
  59. gc_entity_store(&deps[n++], cl->cmd->event);
  60. cl = cl->next;
  61. }
  62. }
  63. /* Add explicit dependencies */
  64. unsigned i;
  65. for (i=0; i<num_events; i++) {
  66. gc_entity_store(&deps[n++], events[i]);
  67. }
  68. /* Make all dependencies explicit for the command */
  69. cmd->num_events = ndeps;
  70. cmd->events = deps;
  71. /* Insert command in the queue */
  72. if (is_barrier) {
  73. /* Remove out-of-order commands */
  74. cq->commands = NULL;
  75. /* Register the command as the last barrier */
  76. cq->barrier = cmd;
  77. }
  78. else {
  79. /* Add command to the list of out-of-order commands */
  80. cq->commands = command_list_cons(cmd, cq->commands);
  81. }
  82. /* Submit command
  83. * We need to do it before unlocking because we don't want events to get
  84. * released while we use them to set dependencies
  85. */
  86. command_submit_ex(cmd);
  87. /* Unlock command queue */
  88. pthread_mutex_unlock(&cq->mutex);
  89. gc_entity_release(cmd);
  90. }