task.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2020 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
  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 "gc.h"
  18. #include "event.h"
  19. void command_completed(cl_command cmd)
  20. {
  21. starpu_task task = cmd->task;
  22. cl_event ev = command_event_get_ex(cmd);
  23. ev->status = CL_COMPLETE;
  24. ev->prof_end = _socl_nanotime();
  25. /* Commands without codelets (marker, barrier, unmap...) take no time */
  26. if (task->cl == NULL)
  27. ev->prof_start = ev->prof_end;
  28. /* Trigger the tag associated to the command event */
  29. DEBUG_MSG("Trigger event %d\n", ev->id);
  30. starpu_tag_notify_from_apps(ev->id);
  31. gc_entity_release(ev);
  32. }
  33. void command_completed_task_callback(void *arg)
  34. {
  35. cl_command cmd = (cl_command)arg;
  36. command_completed(cmd);
  37. /* Release the command stored task callback parameter */
  38. gc_entity_release(cmd);
  39. }
  40. /*
  41. * Create a StarPU task
  42. */
  43. starpu_task task_create()
  44. {
  45. struct starpu_task * task;
  46. /* Create StarPU task */
  47. task = starpu_task_create();
  48. /* Set task common settings */
  49. task->destroy = 0;
  50. task->detach = 0;
  51. task->use_tag = 1;
  52. task->tag_id = event_unique_id();
  53. return task;
  54. }
  55. void task_depends_on(starpu_task task, cl_uint num_events, cl_event *events)
  56. {
  57. if (num_events != 0)
  58. {
  59. cl_uint i;
  60. starpu_tag_t * tags = malloc(num_events * sizeof(starpu_tag_t));
  61. DEBUG_MSG("Task %p depends on events:", task);
  62. for (i=0; i<num_events; i++)
  63. {
  64. tags[i] = events[i]->id;
  65. DEBUG_MSG_NOHEAD(" %d", events[i]->id);
  66. }
  67. DEBUG_MSG_NOHEAD("\n");
  68. starpu_tag_declare_deps_array(task->tag_id, num_events, tags);
  69. free(tags);
  70. }
  71. }
  72. cl_int task_submit_ex(starpu_task task, cl_command cmd)
  73. {
  74. /* Associated the task to the command */
  75. cmd->task = task;
  76. cl_uint num_events = command_num_events_get_ex(cmd);
  77. cl_event * events = command_events_get_ex(cmd);
  78. task_depends_on(task, num_events, events);
  79. task->callback_func = command_completed_task_callback;
  80. gc_entity_store(&task->callback_arg, cmd);
  81. cl_event ev = command_event_get_ex(cmd);
  82. ev->prof_submit = _socl_nanotime();
  83. gc_entity_release(ev);
  84. /* Submit task */
  85. int ret = (task->cl != NULL && task->where == STARPU_OPENCL ?
  86. starpu_task_submit_to_ctx(task, cmd->event->cq->context->sched_ctx) :
  87. starpu_task_submit(task));
  88. if (ret != 0)
  89. DEBUG_ERROR("Unable to submit a task. Error %d\n", ret);
  90. return CL_SUCCESS;
  91. }
  92. /*********************************
  93. * CPU task helper
  94. *********************************/
  95. struct cputask_arg
  96. {
  97. void (*callback)(void*);
  98. void * arg;
  99. int free_arg;
  100. cl_command cmd;
  101. int complete_cmd;
  102. };
  103. static void cputask_task(void *args)
  104. {
  105. struct cputask_arg * arg = (struct cputask_arg*)args;
  106. arg->callback(arg->arg);
  107. if (arg->complete_cmd)
  108. command_completed(arg->cmd);
  109. if (arg->free_arg)
  110. {
  111. assert(arg->arg != NULL);
  112. free(arg->arg);
  113. arg->arg = NULL;
  114. }
  115. gc_entity_unstore(&arg->cmd);
  116. free(arg);
  117. }
  118. void cpu_task_submit_ex(cl_command cmd, void (*callback)(void*), void *arg, int free_arg, int complete_cmd, struct starpu_codelet * codelet, unsigned num_events, cl_event * events)
  119. {
  120. struct cputask_arg * a = malloc(sizeof(struct cputask_arg));
  121. a->callback = callback;
  122. a->arg = arg;
  123. a->free_arg = free_arg;
  124. gc_entity_store(&a->cmd, cmd);
  125. a->complete_cmd = complete_cmd;
  126. codelet->where = STARPU_OPENCL | STARPU_CPU | STARPU_CUDA;
  127. starpu_task task = task_create();
  128. if (num_events != 0)
  129. {
  130. task_depends_on(task, num_events, events);
  131. }
  132. task->callback_func = cputask_task;
  133. task->callback_arg = a;
  134. cmd->task = task;
  135. int ret = starpu_task_submit(task);
  136. if (ret != 0)
  137. DEBUG_ERROR("Unable to submit a task. Error %d\n", ret);
  138. }