command_queue.c 3.2 KB

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