driver_common.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2012 Université de Bordeaux 1
  4. * Copyright (C) 2010, 2011, 2012 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_task_profiling_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_perf_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_task_profiling_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_perf_archtype perf_arch,
  88. struct timespec *codelet_start, struct timespec *codelet_end, int profiling)
  89. {
  90. struct starpu_task_profiling_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. /* Workers may block when there is no work to do at all. */
  127. struct starpu_task *_starpu_get_worker_task(struct _starpu_worker *args, int workerid, unsigned memnode)
  128. {
  129. struct starpu_task *task;
  130. task = _starpu_pop_task(args);
  131. if (task == NULL)
  132. {
  133. /*TODO: check this out after the merge */
  134. /* Note: we need to keep the sched condition mutex all along the path
  135. * from popping a task from the scheduler to blocking. Otherwise the
  136. * driver may go block just after the scheduler got a new task to be
  137. * executed, and thus hanging. */
  138. _STARPU_PTHREAD_MUTEX_LOCK(&args->sched_mutex);
  139. if (_starpu_worker_get_status(workerid) != STATUS_SLEEPING)
  140. {
  141. _STARPU_TRACE_WORKER_SLEEP_START
  142. _starpu_worker_restart_sleeping(workerid);
  143. _starpu_worker_set_status(workerid, STATUS_SLEEPING);
  144. }
  145. if (_starpu_worker_can_block(memnode))
  146. _STARPU_PTHREAD_COND_WAIT(&args->sched_cond, &args->sched_mutex);
  147. _STARPU_PTHREAD_MUTEX_UNLOCK(&args->sched_mutex);
  148. return NULL;
  149. }
  150. if (_starpu_worker_get_status(workerid) == STATUS_SLEEPING)
  151. {
  152. _STARPU_TRACE_WORKER_SLEEP_END
  153. _starpu_worker_stop_sleeping(workerid);
  154. _starpu_worker_set_status(workerid, STATUS_UNKNOWN);
  155. }
  156. #ifdef HAVE_AYUDAME_H
  157. if (AYU_event) {
  158. int id = workerid;
  159. AYU_event(AYU_PRERUNTASK, _starpu_get_job_associated_to_task(task)->job_id, &id);
  160. }
  161. #endif
  162. return task;
  163. }