driver_common.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2013 Université de Bordeaux 1
  4. * Copyright (C) 2010, 2011, 2012, 2013 Centre National de la Recherche Scientifique
  5. * Copyright (C) 2011 Télécom-SudParis
  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 <common/utils.h>
  23. #include <core/debug.h>
  24. #include <drivers/driver_common/driver_common.h>
  25. #include <starpu_top.h>
  26. #include <core/sched_policy.h>
  27. #include <top/starpu_top_core.h>
  28. #include <core/debug.h>
  29. void _starpu_driver_start_job(struct _starpu_worker *args, struct _starpu_job *j, struct timespec *codelet_start, int rank, int profiling)
  30. {
  31. struct starpu_task *task = j->task;
  32. struct starpu_codelet *cl = task->cl;
  33. struct starpu_profiling_task_info *profiling_info;
  34. int starpu_top=_starpu_top_status_get();
  35. int workerid = args->workerid;
  36. unsigned calibrate_model = 0;
  37. if (cl->model && cl->model->benchmarking)
  38. calibrate_model = 1;
  39. /* If the job is executed on a combined worker there is no need for the
  40. * scheduler to process it : it doesn't contain any valuable data
  41. * as it's not linked to an actual worker */
  42. if (j->task_size == 1)
  43. _starpu_sched_pre_exec_hook(task);
  44. args->status = STATUS_EXECUTING;
  45. task->status = STARPU_TASK_RUNNING;
  46. if (rank == 0)
  47. {
  48. #ifdef HAVE_AYUDAME_H
  49. if (AYU_event) AYU_event(AYU_RUNTASK, j->job_id, NULL);
  50. #endif
  51. cl->per_worker_stats[workerid]++;
  52. profiling_info = task->profiling_info;
  53. if ((profiling && profiling_info) || calibrate_model || starpu_top)
  54. {
  55. _starpu_clock_gettime(codelet_start);
  56. _starpu_worker_register_executing_start_date(workerid, codelet_start);
  57. }
  58. }
  59. if (starpu_top)
  60. _starpu_top_task_started(task,workerid,codelet_start);
  61. _STARPU_TRACE_START_CODELET_BODY(j);
  62. }
  63. void _starpu_driver_end_job(struct _starpu_worker *args, struct _starpu_job *j, enum starpu_perfmodel_archtype perf_arch STARPU_ATTRIBUTE_UNUSED, struct timespec *codelet_end, int rank, int profiling)
  64. {
  65. struct starpu_task *task = j->task;
  66. struct starpu_codelet *cl = task->cl;
  67. struct starpu_profiling_task_info *profiling_info = task->profiling_info;
  68. int starpu_top=_starpu_top_status_get();
  69. int workerid = args->workerid;
  70. unsigned calibrate_model = 0;
  71. _STARPU_TRACE_END_CODELET_BODY(j, j->nimpl, perf_arch);
  72. if (cl && cl->model && cl->model->benchmarking)
  73. calibrate_model = 1;
  74. if (rank == 0)
  75. {
  76. if ((profiling && profiling_info) || calibrate_model || starpu_top)
  77. _starpu_clock_gettime(codelet_end);
  78. #ifdef HAVE_AYUDAME_H
  79. if (AYU_event) AYU_event(AYU_POSTRUNTASK, j->job_id, NULL);
  80. #endif
  81. }
  82. if (starpu_top)
  83. _starpu_top_task_ended(task,workerid,codelet_end);
  84. args->status = STATUS_UNKNOWN;
  85. }
  86. void _starpu_driver_update_job_feedback(struct _starpu_job *j, struct _starpu_worker *worker_args,
  87. enum starpu_perfmodel_archtype perf_arch,
  88. struct timespec *codelet_start, struct timespec *codelet_end, int profiling)
  89. {
  90. struct starpu_profiling_task_info *profiling_info = j->task->profiling_info;
  91. struct timespec measured_ts;
  92. double measured;
  93. int workerid = worker_args->workerid;
  94. struct starpu_codelet *cl = j->task->cl;
  95. int calibrate_model = 0;
  96. int updated = 0;
  97. #ifndef STARPU_SIMGRID
  98. if (cl->model && cl->model->benchmarking)
  99. calibrate_model = 1;
  100. #endif
  101. if ((profiling && profiling_info) || calibrate_model)
  102. {
  103. starpu_timespec_sub(codelet_end, codelet_start, &measured_ts);
  104. measured = starpu_timing_timespec_to_us(&measured_ts);
  105. if (profiling && profiling_info)
  106. {
  107. memcpy(&profiling_info->start_time, codelet_start, sizeof(struct timespec));
  108. memcpy(&profiling_info->end_time, codelet_end, sizeof(struct timespec));
  109. profiling_info->workerid = workerid;
  110. _starpu_worker_update_profiling_info_executing(workerid, &measured_ts, 1,
  111. profiling_info->used_cycles,
  112. profiling_info->stall_cycles,
  113. profiling_info->power_consumed);
  114. updated = 1;
  115. }
  116. if (calibrate_model)
  117. _starpu_update_perfmodel_history(j, j->task->cl->model, perf_arch, worker_args->devid, measured,j->nimpl);
  118. }
  119. if (!updated)
  120. _starpu_worker_update_profiling_info_executing(workerid, NULL, 1, 0, 0, 0);
  121. if (profiling_info && profiling_info->power_consumed && cl->power_model && cl->power_model->benchmarking)
  122. {
  123. _starpu_update_perfmodel_history(j, j->task->cl->power_model, perf_arch, worker_args->devid, profiling_info->power_consumed,j->nimpl);
  124. }
  125. }
  126. static void _starpu_worker_set_status_sleeping(int workerid)
  127. {
  128. if (_starpu_worker_get_status(workerid) != STATUS_SLEEPING)
  129. {
  130. _STARPU_TRACE_WORKER_SLEEP_START;
  131. _starpu_worker_restart_sleeping(workerid);
  132. _starpu_worker_set_status(workerid, STATUS_SLEEPING);
  133. }
  134. }
  135. static void _starpu_worker_set_status_wakeup(int workerid)
  136. {
  137. if (_starpu_worker_get_status(workerid) == STATUS_SLEEPING)
  138. {
  139. _STARPU_TRACE_WORKER_SLEEP_END;
  140. _starpu_worker_stop_sleeping(workerid);
  141. _starpu_worker_set_status(workerid, STATUS_UNKNOWN);
  142. }
  143. }
  144. /* Workers may block when there is no work to do at all. */
  145. struct starpu_task *_starpu_get_worker_task(struct _starpu_worker *args, int workerid, unsigned memnode)
  146. {
  147. struct starpu_task *task;
  148. STARPU_PTHREAD_MUTEX_LOCK(&args->sched_mutex);
  149. if(args->parallel_sect)
  150. {
  151. STARPU_PTHREAD_MUTEX_LOCK(&args->parallel_sect_mutex);
  152. _starpu_sched_ctx_signal_worker_blocked(args->workerid);
  153. STARPU_PTHREAD_COND_WAIT(&args->parallel_sect_cond, &args->parallel_sect_mutex);
  154. _starpu_sched_ctx_rebind_thread_to_its_cpu(args->bindid);
  155. STARPU_PTHREAD_MUTEX_UNLOCK(&args->parallel_sect_mutex);
  156. args->parallel_sect = 0;
  157. }
  158. task = _starpu_pop_task(args);
  159. if (task == NULL)
  160. {
  161. /* Note: we need to keep the sched condition mutex all along the path
  162. * from popping a task from the scheduler to blocking. Otherwise the
  163. * driver may go block just after the scheduler got a new task to be
  164. * executed, and thus hanging. */
  165. _starpu_worker_set_status_sleeping(workerid);
  166. if (_starpu_worker_can_block(memnode))
  167. STARPU_PTHREAD_COND_WAIT(&args->sched_cond, &args->sched_mutex);
  168. else
  169. {
  170. if (_starpu_machine_is_running())
  171. {
  172. STARPU_UYIELD();
  173. #ifdef STARPU_SIMGRID
  174. static int warned;
  175. if (!warned)
  176. {
  177. warned = 1;
  178. _STARPU_DISP("Has to make simgrid spin for progression hooks\n");
  179. }
  180. MSG_process_sleep(0.000010);
  181. #endif
  182. }
  183. }
  184. STARPU_PTHREAD_MUTEX_UNLOCK(&args->sched_mutex);
  185. return NULL;
  186. }
  187. STARPU_PTHREAD_MUTEX_UNLOCK(&args->sched_mutex);
  188. _starpu_worker_set_status_wakeup(workerid);
  189. #ifdef HAVE_AYUDAME_H
  190. if (AYU_event)
  191. {
  192. int id = workerid;
  193. AYU_event(AYU_PRERUNTASK, _starpu_get_job_associated_to_task(task)->job_id, &id);
  194. }
  195. #endif
  196. return task;
  197. }
  198. int _starpu_get_multi_worker_task(struct _starpu_worker *workers, struct starpu_task ** tasks, int nworkers)
  199. {
  200. int i, count = 0;
  201. /*for each worker*/
  202. for (i = 1; (i < nworkers); i++)
  203. {
  204. /*if the worker is already executinf a task then */
  205. if(workers[i].current_task)
  206. {
  207. tasks[i] = NULL;
  208. }
  209. /*else try to pop a task*/
  210. else
  211. {
  212. STARPU_PTHREAD_MUTEX_LOCK(&workers[i].sched_mutex);
  213. tasks[i] = _starpu_pop_task(&workers[i]);
  214. STARPU_PTHREAD_MUTEX_UNLOCK(&workers[i].sched_mutex);
  215. if(tasks[i] != NULL)
  216. {
  217. count ++;
  218. _starpu_worker_set_status_sleeping(workers[i].workerid);
  219. }
  220. else
  221. {
  222. _starpu_worker_set_status_wakeup(workers[i].workerid);
  223. }
  224. }
  225. }
  226. return count;
  227. }