task.c 4.2 KB

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