task.c 3.3 KB

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