task.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. cl_event task_event(starpu_task *task) {
  20. return (cl_event)task->callback_arg;
  21. }
  22. static void task_release_callback(void *arg) {
  23. starpu_task *task = starpu_get_current_task();
  24. cl_event ev = (cl_event)arg;
  25. ev->status = CL_COMPLETE;
  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. }
  32. /*
  33. * Create a StarPU task
  34. *
  35. * Task's callback_arg is event
  36. * Task's tag is set to event ID
  37. */
  38. starpu_task * task_create(cl_command_type type) {
  39. cl_event event;
  40. struct starpu_task * task;
  41. /* Create event */
  42. event = event_create();
  43. event->type = type;
  44. /* Create StarPU task */
  45. task = starpu_task_create();
  46. /* Task tag is set to event id */
  47. task->use_tag = 1;
  48. task->tag_id = event->id;
  49. /* Set task common settings */
  50. task->destroy = 1;
  51. task->detach = 1;
  52. task->callback_func = task_release_callback;
  53. task->callback_arg = event;
  54. return task;
  55. }
  56. void task_dependency_add(starpu_task * task, cl_uint num, const cl_event *events) {
  57. unsigned int i;
  58. for (i=0; i<num; i++) {
  59. starpu_tag_t tag = events[i]->id;
  60. DEBUG_MSG("Event %d depends on event %d\n", task->tag_id, events[i]->id);
  61. starpu_tag_declare_deps_array(task->tag_id, 1, &tag);
  62. }
  63. }
  64. /*********************************
  65. * CPU task helper
  66. *********************************/
  67. struct cputask_arg {
  68. void (*callback)(void*);
  69. void * arg;
  70. int free_arg;
  71. };
  72. static void cputask_task(__attribute__((unused)) void *descr[], void *args) {
  73. struct cputask_arg * arg = (struct cputask_arg*)args;
  74. arg->callback(arg->arg);
  75. if (arg->free_arg)
  76. free(arg->arg);
  77. free(arg);
  78. }
  79. static starpu_codelet cputask_codelet = {
  80. .where = STARPU_CPU,
  81. .model = NULL,
  82. .cpu_func = &cputask_task
  83. };
  84. starpu_task * task_create_cpu(cl_command_type type, void (*callback)(void*), void *arg, int free_arg) {
  85. struct cputask_arg * a = malloc(sizeof(struct cputask_arg));
  86. a->callback = callback;
  87. a->arg = arg;
  88. a->free_arg = free_arg;
  89. starpu_task *task = task_create(type);
  90. task->cl = &cputask_codelet;
  91. task->cl_arg = a;
  92. return task;
  93. }