command_queue.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2020 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
  4. * Copyright (C) 2012 Vincent Danjean
  5. *
  6. * StarPU is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation; either version 2.1 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * StarPU is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. *
  15. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  16. */
  17. #include "socl.h"
  18. #include "task.h"
  19. #include "gc.h"
  20. /**
  21. * WARNING: command queues do NOT hold references on events. Only events hold references
  22. * on command queues. This way, event release will automatically remove the event from
  23. * its command queue.
  24. */
  25. void command_queue_enqueue_ex(cl_command_queue cq, cl_command cmd, cl_uint num_events, const cl_event * events)
  26. {
  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. STARPU_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. {
  44. command_list cl = cq->commands;
  45. while (cl != NULL)
  46. {
  47. ndeps++;
  48. cl = cl->next;
  49. }
  50. }
  51. /*** Dependencies ***/
  52. cl_event * deps = malloc(ndeps * sizeof(cl_event));
  53. int n = 0;
  54. /* Add dependency to last barrier if applicable */
  55. if (cq->barrier != NULL)
  56. gc_entity_store(&deps[n++], cq->barrier->event);
  57. /* Add dependencies to out-of-order events (if any) */
  58. if (is_barrier)
  59. {
  60. command_list cl = cq->commands;
  61. while (cl != NULL)
  62. {
  63. gc_entity_store(&deps[n++], cl->cmd->event);
  64. cl = cl->next;
  65. }
  66. }
  67. /* Add explicit dependencies */
  68. unsigned i;
  69. for (i=0; i<num_events; i++)
  70. {
  71. gc_entity_store(&deps[n++], events[i]);
  72. }
  73. /* Make all dependencies explicit for the command */
  74. cmd->num_events = ndeps;
  75. cmd->events = deps;
  76. /* Insert command in the queue */
  77. if (is_barrier)
  78. {
  79. /* Remove out-of-order commands */
  80. cq->commands = NULL;
  81. /* Register the command as the last barrier */
  82. cq->barrier = cmd;
  83. }
  84. else
  85. {
  86. /* Add command to the list of out-of-order commands */
  87. cq->commands = command_list_cons(cmd, cq->commands);
  88. }
  89. /* Submit command
  90. * We need to do it before unlocking because we don't want events to get
  91. * released while we use them to set dependencies
  92. */
  93. command_submit_ex(cmd);
  94. /* Unlock command queue */
  95. STARPU_PTHREAD_MUTEX_UNLOCK(&cq->mutex);
  96. gc_entity_release(cmd);
  97. }