driver_cpu.c 6.4 KB

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