driver_cpu.c 7.0 KB

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