task.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. static void task_release_callback(void *arg) {
  20. starpu_task task = starpu_get_current_task();
  21. cl_command cmd = (cl_command)arg;
  22. cl_event ev = command_event_get(cmd);
  23. ev->status = CL_COMPLETE;
  24. DEBUG_MSG("notifying tag %x as well as task tag %x\n", ev->id, task->tag_id);
  25. /* Trigger the tag associated to the command event */
  26. starpu_tag_notify_from_apps(ev->id);
  27. if (task->profiling_info != NULL && (intptr_t)task->profiling_info != -ENOSYS) {
  28. ev->profiling_info = malloc(sizeof(*task->profiling_info));
  29. memcpy(ev->profiling_info, task->profiling_info, sizeof(*task->profiling_info));
  30. }
  31. gc_entity_release(ev);
  32. /* Release the command */
  33. //TODO
  34. }
  35. /*
  36. * Create a StarPU task
  37. */
  38. starpu_task task_create() {
  39. struct starpu_task * task;
  40. /* Create StarPU task */
  41. task = starpu_task_create();
  42. /* Set task common settings */
  43. task->destroy = 1;
  44. task->detach = 1;
  45. task->use_tag = 1;
  46. task->tag_id = event_unique_id();
  47. DEBUG_MSG("creating task with tag %x\n", task->tag_id);
  48. return task;
  49. }
  50. void task_depends_on(starpu_task task, cl_uint num_events, cl_event *events) {
  51. if (num_events != 0) {
  52. cl_uint i;
  53. starpu_tag_t * tags = malloc(num_events * sizeof(starpu_tag_t));
  54. if (num_events != 0)
  55. DEBUG_MSG("Tag %d depends on %u tags:", task->tag_id, num_events);
  56. for (i=0; i<num_events; i++) {
  57. tags[i] = events[i]->id;
  58. DEBUG_MSG_NOHEAD(" %u", events[i]->id);
  59. }
  60. DEBUG_MSG_NOHEAD("\n");
  61. starpu_tag_declare_deps_array(task->tag_id, num_events, tags);
  62. free(tags);
  63. }
  64. }
  65. cl_int task_submit_ex(starpu_task task, cl_command cmd) {
  66. /* Associated the task to the command */
  67. cmd->task = task;
  68. task_depends_on(task, command_num_events_get(cmd), command_events_get(cmd));
  69. task->callback_func = task_release_callback;
  70. task->callback_arg = cmd;
  71. /* Submit task */
  72. int ret = starpu_task_submit(task);
  73. if (ret != 0)
  74. DEBUG_ERROR("Unable to submit a task. Error %d\n", ret);
  75. return CL_SUCCESS;
  76. }
  77. /*********************************
  78. * CPU task helper
  79. *********************************/
  80. struct cputask_arg {
  81. void (*callback)(void*);
  82. void * arg;
  83. int free_arg;
  84. };
  85. static void cputask_task(__attribute__((unused)) void *descr[], void *args) {
  86. struct cputask_arg * arg = (struct cputask_arg*)args;
  87. arg->callback(arg->arg);
  88. if (arg->free_arg)
  89. free(arg->arg);
  90. free(arg);
  91. }
  92. static starpu_codelet cputask_codelet = {
  93. .where = STARPU_CPU,
  94. .model = NULL,
  95. .cpu_func = &cputask_task
  96. };
  97. starpu_task task_create_cpu(void (*callback)(void*), void *arg, int free_arg) {
  98. struct cputask_arg * a = malloc(sizeof(struct cputask_arg));
  99. a->callback = callback;
  100. a->arg = arg;
  101. a->free_arg = free_arg;
  102. starpu_task task = task_create();
  103. task->cl = &cputask_codelet;
  104. task->cl_arg = a;
  105. return task;
  106. }