driver_cpu.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010, 2011 Université de Bordeaux 1
  4. * Copyright (C) 2010 Mehdi Juhoor <mjuhoor@gmail.com>
  5. * Copyright (C) 2010, 2011 Centre National de la Recherche Scientifique
  6. * Copyright (C) 2011 Télécom-SudParis
  7. *
  8. * StarPU is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU Lesser General Public License as published by
  10. * the Free Software Foundation; either version 2.1 of the License, or (at
  11. * your option) any later version.
  12. *
  13. * StarPU is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  16. *
  17. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  18. */
  19. #include <math.h>
  20. #include <starpu.h>
  21. #include <drivers/driver_common/driver_common.h>
  22. #include <common/utils.h>
  23. #include <core/debug.h>
  24. #include "driver_cpu.h"
  25. #include <core/sched_policy.h>
  26. static int execute_job_on_cpu(starpu_job_t j, struct starpu_worker_s *cpu_args, int is_parallel_task, int rank, enum starpu_perf_archtype perf_arch)
  27. {
  28. int ret;
  29. struct timespec codelet_start, codelet_end;
  30. struct starpu_task *task = j->task;
  31. struct starpu_codelet_t *cl = task->cl;
  32. STARPU_ASSERT(cl);
  33. STARPU_ASSERT(cl->cpu_func);
  34. if (rank == 0)
  35. {
  36. ret = _starpu_fetch_task_input(task, 0);
  37. if (ret != 0)
  38. {
  39. /* there was not enough memory so the codelet cannot be executed right now ... */
  40. /* push the codelet back and try another one ... */
  41. return -EAGAIN;
  42. }
  43. }
  44. if (is_parallel_task)
  45. _STARPU_PTHREAD_BARRIER_WAIT(&j->before_work_barrier);
  46. _starpu_driver_start_job(cpu_args, j, &codelet_start, rank);
  47. /* In case this is a Fork-join parallel task, the worker does not
  48. * execute the kernel at all. */
  49. if ((rank == 0) || (cl->type != STARPU_FORKJOIN))
  50. {
  51. if (cl->cpu_func != STARPU_MULTIPLE_CPU_IMPLEMENTATIONS) {
  52. cl_func func = cl->cpu_func;
  53. STARPU_ASSERT(func);
  54. func(task->interfaces, task->cl_arg);
  55. }
  56. else {
  57. /* _STARPU_DEBUG("CPU driver : running kernel (%d)\n", j->nimpl); */
  58. cl_func func = cl->cpu_funcs[j->nimpl];
  59. STARPU_ASSERT(func);
  60. func(task->interfaces, task->cl_arg);
  61. }
  62. }
  63. _starpu_driver_end_job(cpu_args, j, &codelet_end, rank);
  64. if (is_parallel_task)
  65. _STARPU_PTHREAD_BARRIER_WAIT(&j->after_work_barrier);
  66. if (rank == 0)
  67. {
  68. _starpu_driver_update_job_feedback(j, cpu_args,
  69. perf_arch, &codelet_start, &codelet_end, 0);
  70. _starpu_push_task_output(task, 0);
  71. }
  72. return 0;
  73. }
  74. void *_starpu_cpu_worker(void *arg)
  75. {
  76. struct starpu_worker_s *cpu_arg = (struct starpu_worker_s *) arg;
  77. unsigned memnode = cpu_arg->memory_node;
  78. int workerid = cpu_arg->workerid;
  79. int devid = cpu_arg->devid;
  80. #ifdef STARPU_USE_FXT
  81. _starpu_fxt_register_thread(cpu_arg->bindid);
  82. #endif
  83. STARPU_TRACE_WORKER_INIT_START(STARPU_FUT_CPU_KEY, devid, memnode);
  84. _starpu_bind_thread_on_cpu(cpu_arg->config, cpu_arg->bindid);
  85. _STARPU_DEBUG("cpu worker %d is ready on logical cpu %d\n", devid, cpu_arg->bindid);
  86. _starpu_set_local_memory_node_key(&memnode);
  87. _starpu_set_local_worker_key(cpu_arg);
  88. snprintf(cpu_arg->name, sizeof(cpu_arg->name), "CPU %d", devid);
  89. snprintf(cpu_arg->short_name, sizeof(cpu_arg->short_name), "CPU %d", devid);
  90. cpu_arg->status = STATUS_UNKNOWN;
  91. STARPU_TRACE_WORKER_INIT_END
  92. /* tell the main thread that we are ready */
  93. _STARPU_PTHREAD_MUTEX_LOCK(&cpu_arg->mutex);
  94. cpu_arg->worker_is_initialized = 1;
  95. _STARPU_PTHREAD_COND_SIGNAL(&cpu_arg->ready_cond);
  96. _STARPU_PTHREAD_MUTEX_UNLOCK(&cpu_arg->mutex);
  97. starpu_job_t j;
  98. struct starpu_task *task;
  99. int res;
  100. while (_starpu_machine_is_running())
  101. {
  102. STARPU_TRACE_START_PROGRESS(memnode);
  103. _starpu_datawizard_progress(memnode, 1);
  104. STARPU_TRACE_END_PROGRESS(memnode);
  105. _STARPU_PTHREAD_MUTEX_LOCK(cpu_arg->sched_mutex);
  106. task = _starpu_pop_task(cpu_arg);
  107. if (!task)
  108. {
  109. if (_starpu_worker_can_block(memnode))
  110. _starpu_block_worker(workerid, cpu_arg->sched_cond, cpu_arg->sched_mutex);
  111. _STARPU_PTHREAD_MUTEX_UNLOCK(cpu_arg->sched_mutex);
  112. continue;
  113. };
  114. _STARPU_PTHREAD_MUTEX_UNLOCK(cpu_arg->sched_mutex);
  115. STARPU_ASSERT(task);
  116. j = _starpu_get_job_associated_to_task(task);
  117. /* can a cpu perform that task ? */
  118. if (!STARPU_CPU_MAY_PERFORM(j))
  119. {
  120. /* put it and the end of the queue ... XXX */
  121. _starpu_push_task(j, 0);
  122. continue;
  123. }
  124. int rank = 0;
  125. int is_parallel_task = (j->task_size > 1);
  126. enum starpu_perf_archtype perf_arch;
  127. /* Get the rank in case it is a parallel task */
  128. if (is_parallel_task)
  129. {
  130. /* We can release the fake task */
  131. STARPU_ASSERT(task != j->task);
  132. free(task);
  133. _STARPU_PTHREAD_MUTEX_LOCK(&j->sync_mutex);
  134. rank = j->active_task_alias_count++;
  135. _STARPU_PTHREAD_MUTEX_UNLOCK(&j->sync_mutex);
  136. struct starpu_combined_worker_s *combined_worker;
  137. combined_worker = _starpu_get_combined_worker_struct(j->combined_workerid);
  138. cpu_arg->combined_workerid = j->combined_workerid;
  139. cpu_arg->worker_size = combined_worker->worker_size;
  140. cpu_arg->current_rank = rank;
  141. perf_arch = combined_worker->perf_arch;
  142. }
  143. else {
  144. cpu_arg->combined_workerid = cpu_arg->workerid;
  145. cpu_arg->worker_size = 1;
  146. cpu_arg->current_rank = 0;
  147. perf_arch = cpu_arg->perf_arch;
  148. }
  149. _starpu_set_current_task(j->task);
  150. res = execute_job_on_cpu(j, cpu_arg, is_parallel_task, rank, perf_arch);
  151. _starpu_set_current_task(NULL);
  152. if (res) {
  153. switch (res) {
  154. case -EAGAIN:
  155. _starpu_push_task(j, 0);
  156. continue;
  157. default:
  158. assert(0);
  159. }
  160. }
  161. if (rank == 0)
  162. _starpu_handle_job_termination(j, 0);
  163. }
  164. STARPU_TRACE_WORKER_DEINIT_START
  165. _starpu_handle_all_pending_node_data_requests(memnode);
  166. /* In case there remains some memory that was automatically
  167. * allocated by StarPU, we release it now. Note that data
  168. * coherency is not maintained anymore at that point ! */
  169. _starpu_free_all_automatically_allocated_buffers(memnode);
  170. STARPU_TRACE_WORKER_DEINIT_END(STARPU_FUT_CPU_KEY);
  171. pthread_exit(NULL);
  172. return NULL;
  173. }