driver_common.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2017 Université de Bordeaux
  4. * Copyright (C) 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017 CNRS
  5. * Copyright (C) 2011 Télécom-SudParis
  6. * Copyright (C) 2014, 2016 INRIA
  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 <starpu_profiling.h>
  22. #include <profiling/profiling.h>
  23. #include <common/utils.h>
  24. #include <core/debug.h>
  25. #include <core/sched_ctx.h>
  26. #include <drivers/driver_common/driver_common.h>
  27. #include <starpu_top.h>
  28. #include <core/sched_policy.h>
  29. #include <top/starpu_top_core.h>
  30. #include <core/debug.h>
  31. #include <core/task.h>
  32. #define BACKOFF_MAX 32 /* TODO : use parameter to define them */
  33. #define BACKOFF_MIN 1
  34. void _starpu_driver_start_job(struct _starpu_worker *worker, struct _starpu_job *j, struct starpu_perfmodel_arch* perf_arch STARPU_ATTRIBUTE_UNUSED, struct timespec *codelet_start, int rank, int profiling)
  35. {
  36. struct starpu_task *task = j->task;
  37. struct starpu_codelet *cl = task->cl;
  38. int starpu_top=_starpu_top_status_get();
  39. int workerid = worker->workerid;
  40. unsigned calibrate_model = 0;
  41. if (cl->model && cl->model->benchmarking)
  42. calibrate_model = 1;
  43. /* If the job is executed on a combined worker there is no need for the
  44. * scheduler to process it : it doesn't contain any valuable data
  45. * as it's not linked to an actual worker */
  46. if (j->task_size == 1 && rank == 0)
  47. _starpu_sched_pre_exec_hook(task);
  48. _starpu_set_worker_status(worker, STATUS_EXECUTING);
  49. task->status = STARPU_TASK_RUNNING;
  50. if (rank == 0)
  51. {
  52. STARPU_AYU_RUNTASK(j->job_id);
  53. cl->per_worker_stats[workerid]++;
  54. struct starpu_profiling_task_info *profiling_info = task->profiling_info;
  55. if ((profiling && profiling_info) || calibrate_model || starpu_top)
  56. {
  57. _starpu_clock_gettime(codelet_start);
  58. _starpu_worker_register_executing_start_date(workerid, codelet_start);
  59. }
  60. }
  61. if (starpu_top)
  62. _starpu_top_task_started(task,workerid,codelet_start);
  63. // Find out if the worker is the master of a parallel context
  64. struct _starpu_sched_ctx *sched_ctx = _starpu_sched_ctx_get_sched_ctx_for_worker_and_job(worker, j);
  65. if(!sched_ctx)
  66. sched_ctx = _starpu_get_sched_ctx_struct(j->task->sched_ctx);
  67. if(!sched_ctx->sched_policy)
  68. {
  69. if(!sched_ctx->awake_workers && sched_ctx->main_master == worker->workerid)
  70. {
  71. struct starpu_worker_collection *workers = sched_ctx->workers;
  72. struct starpu_sched_ctx_iterator it;
  73. int new_rank = 0;
  74. if (workers->init_iterator)
  75. workers->init_iterator(workers, &it);
  76. while (workers->has_next(workers, &it))
  77. {
  78. int _workerid = workers->get_next(workers, &it);
  79. if (_workerid != workerid)
  80. {
  81. new_rank++;
  82. struct _starpu_worker *_worker = _starpu_get_worker_struct(_workerid);
  83. _starpu_driver_start_job(_worker, j, &_worker->perf_arch, codelet_start, new_rank, profiling);
  84. }
  85. }
  86. }
  87. _STARPU_TRACE_START_CODELET_BODY(j, j->nimpl, &sched_ctx->perf_arch, workerid);
  88. }
  89. else
  90. _STARPU_TRACE_START_CODELET_BODY(j, j->nimpl, perf_arch, workerid);
  91. }
  92. void _starpu_driver_end_job(struct _starpu_worker *worker, struct _starpu_job *j, struct starpu_perfmodel_arch* perf_arch STARPU_ATTRIBUTE_UNUSED, struct timespec *codelet_end, int rank, int profiling)
  93. {
  94. struct starpu_task *task = j->task;
  95. struct starpu_codelet *cl = task->cl;
  96. int starpu_top=_starpu_top_status_get();
  97. int workerid = worker->workerid;
  98. unsigned calibrate_model = 0;
  99. // Find out if the worker is the master of a parallel context
  100. struct _starpu_sched_ctx *sched_ctx = _starpu_sched_ctx_get_sched_ctx_for_worker_and_job(worker, j);
  101. if(!sched_ctx)
  102. sched_ctx = _starpu_get_sched_ctx_struct(j->task->sched_ctx);
  103. if (!sched_ctx->sched_policy)
  104. {
  105. _starpu_perfmodel_create_comb_if_needed(&(sched_ctx->perf_arch));
  106. _STARPU_TRACE_END_CODELET_BODY(j, j->nimpl, &(sched_ctx->perf_arch), workerid);
  107. }
  108. else
  109. {
  110. _starpu_perfmodel_create_comb_if_needed(perf_arch);
  111. _STARPU_TRACE_END_CODELET_BODY(j, j->nimpl, perf_arch, workerid);
  112. }
  113. if (cl && cl->model && cl->model->benchmarking)
  114. calibrate_model = 1;
  115. if (rank == 0)
  116. {
  117. struct starpu_profiling_task_info *profiling_info = task->profiling_info;
  118. if ((profiling && profiling_info) || calibrate_model || starpu_top)
  119. {
  120. _starpu_clock_gettime(codelet_end);
  121. _starpu_worker_register_executing_end(workerid);
  122. }
  123. STARPU_AYU_POSTRUNTASK(j->job_id);
  124. }
  125. if (starpu_top)
  126. _starpu_top_task_ended(task,workerid,codelet_end);
  127. _starpu_set_worker_status(worker, STATUS_UNKNOWN);
  128. if(!sched_ctx->sched_policy && !sched_ctx->awake_workers &&
  129. sched_ctx->main_master == worker->workerid)
  130. {
  131. struct starpu_worker_collection *workers = sched_ctx->workers;
  132. struct starpu_sched_ctx_iterator it;
  133. int new_rank = 0;
  134. if (workers->init_iterator)
  135. workers->init_iterator(workers, &it);
  136. while (workers->has_next(workers, &it))
  137. {
  138. int _workerid = workers->get_next(workers, &it);
  139. if (_workerid != workerid)
  140. {
  141. new_rank++;
  142. struct _starpu_worker *_worker = _starpu_get_worker_struct(_workerid);
  143. _starpu_driver_end_job(_worker, j, &_worker->perf_arch, codelet_end, new_rank, profiling);
  144. }
  145. }
  146. }
  147. }
  148. void _starpu_driver_update_job_feedback(struct _starpu_job *j, struct _starpu_worker *worker,
  149. struct starpu_perfmodel_arch* perf_arch,
  150. struct timespec *codelet_start, struct timespec *codelet_end, int profiling)
  151. {
  152. struct starpu_profiling_task_info *profiling_info = j->task->profiling_info;
  153. struct timespec measured_ts;
  154. int workerid = worker->workerid;
  155. struct starpu_codelet *cl = j->task->cl;
  156. int calibrate_model = 0;
  157. int updated = 0;
  158. _starpu_perfmodel_create_comb_if_needed(perf_arch);
  159. #ifndef STARPU_SIMGRID
  160. if (cl->model && cl->model->benchmarking)
  161. calibrate_model = 1;
  162. #endif
  163. if ((profiling && profiling_info) || calibrate_model)
  164. {
  165. double measured;
  166. starpu_timespec_sub(codelet_end, codelet_start, &measured_ts);
  167. measured = starpu_timing_timespec_to_us(&measured_ts);
  168. if (profiling && profiling_info)
  169. {
  170. memcpy(&profiling_info->start_time, codelet_start, sizeof(struct timespec));
  171. memcpy(&profiling_info->end_time, codelet_end, sizeof(struct timespec));
  172. profiling_info->workerid = workerid;
  173. _starpu_worker_update_profiling_info_executing(workerid, &measured_ts, 1,
  174. profiling_info->used_cycles,
  175. profiling_info->stall_cycles,
  176. profiling_info->energy_consumed,
  177. j->task->flops);
  178. updated = 1;
  179. }
  180. if (calibrate_model)
  181. {
  182. #ifdef STARPU_OPENMP
  183. double time_consumed = measured;
  184. unsigned do_update_time_model;
  185. if (j->continuation)
  186. {
  187. /* The job is only paused, thus we accumulate
  188. * its timing, but we don't update its
  189. * perfmodel now. */
  190. starpu_timespec_accumulate(&j->cumulated_ts, &measured_ts);
  191. do_update_time_model = 0;
  192. }
  193. else
  194. {
  195. if (j->discontinuous)
  196. {
  197. /* The job was paused at least once but is now
  198. * really completing. We need to take into
  199. * account its past execution time in its
  200. * perfmodel. */
  201. starpu_timespec_accumulate(&measured_ts, &j->cumulated_ts);
  202. time_consumed = starpu_timing_timespec_to_us(&measured_ts);
  203. }
  204. do_update_time_model = 1;
  205. }
  206. #else
  207. const unsigned do_update_time_model = 1;
  208. const double time_consumed = measured;
  209. #endif
  210. if (do_update_time_model)
  211. {
  212. _starpu_update_perfmodel_history(j, j->task->cl->model, perf_arch, worker->devid, time_consumed, j->nimpl);
  213. }
  214. }
  215. }
  216. if (!updated)
  217. _starpu_worker_update_profiling_info_executing(workerid, NULL, 1, 0, 0, 0, 0);
  218. if (profiling_info && profiling_info->energy_consumed && cl->energy_model && cl->energy_model->benchmarking)
  219. {
  220. #ifdef STARPU_OPENMP
  221. double energy_consumed = profiling_info->energy_consumed;
  222. unsigned do_update_energy_model;
  223. if (j->continuation)
  224. {
  225. j->cumulated_energy_consumed += energy_consumed;
  226. do_update_energy_model = 0;
  227. }
  228. else
  229. {
  230. if (j->discontinuous)
  231. {
  232. energy_consumed += j->cumulated_energy_consumed;
  233. }
  234. do_update_energy_model = 1;
  235. }
  236. #else
  237. const double energy_consumed = profiling_info->energy_consumed;
  238. const unsigned do_update_energy_model = 1;
  239. #endif
  240. if (do_update_energy_model)
  241. {
  242. _starpu_update_perfmodel_history(j, j->task->cl->energy_model, perf_arch, worker->devid, energy_consumed, j->nimpl);
  243. }
  244. }
  245. }
  246. static void _starpu_worker_set_status_scheduling(int workerid)
  247. {
  248. if (_starpu_worker_get_status(workerid) != STATUS_SLEEPING
  249. && _starpu_worker_get_status(workerid) != STATUS_SCHEDULING)
  250. {
  251. _STARPU_TRACE_WORKER_SCHEDULING_START;
  252. _starpu_worker_set_status(workerid, STATUS_SCHEDULING);
  253. }
  254. }
  255. static void _starpu_worker_set_status_scheduling_done(int workerid)
  256. {
  257. if (_starpu_worker_get_status(workerid) == STATUS_SCHEDULING)
  258. {
  259. _STARPU_TRACE_WORKER_SCHEDULING_END;
  260. _starpu_worker_set_status(workerid, STATUS_UNKNOWN);
  261. }
  262. }
  263. static void _starpu_worker_set_status_sleeping(int workerid)
  264. {
  265. if ( _starpu_worker_get_status(workerid) == STATUS_WAKING_UP)
  266. _starpu_worker_set_status(workerid, STATUS_SLEEPING);
  267. else if (_starpu_worker_get_status(workerid) != STATUS_SLEEPING)
  268. {
  269. _STARPU_TRACE_WORKER_SLEEP_START;
  270. _starpu_worker_restart_sleeping(workerid);
  271. _starpu_worker_set_status(workerid, STATUS_SLEEPING);
  272. }
  273. }
  274. static void _starpu_worker_set_status_wakeup(int workerid)
  275. {
  276. if (_starpu_worker_get_status(workerid) == STATUS_SLEEPING || _starpu_worker_get_status(workerid) == STATUS_WAKING_UP)
  277. {
  278. _STARPU_TRACE_WORKER_SLEEP_END;
  279. _starpu_worker_stop_sleeping(workerid);
  280. _starpu_worker_set_status(workerid, STATUS_UNKNOWN);
  281. }
  282. }
  283. #if !defined(STARPU_SIMGRID)
  284. static void _starpu_exponential_backoff(struct _starpu_worker *worker)
  285. {
  286. int delay = worker->spinning_backoff;
  287. if (worker->spinning_backoff < BACKOFF_MAX)
  288. worker->spinning_backoff<<=1;
  289. while(delay--)
  290. STARPU_UYIELD();
  291. }
  292. #endif
  293. /* Workers may block when there is no work to do at all. */
  294. struct starpu_task *_starpu_get_worker_task(struct _starpu_worker *worker, int workerid, unsigned memnode STARPU_ATTRIBUTE_UNUSED)
  295. {
  296. STARPU_PTHREAD_MUTEX_LOCK_SCHED(&worker->sched_mutex);
  297. struct starpu_task *task;
  298. unsigned needed = 1;
  299. unsigned executing STARPU_ATTRIBUTE_UNUSED = 0;
  300. _starpu_worker_set_status_scheduling(workerid);
  301. while(needed)
  302. {
  303. struct _starpu_sched_ctx *sched_ctx = NULL;
  304. struct _starpu_sched_ctx_elt *e = NULL;
  305. struct _starpu_sched_ctx_list_iterator list_it;
  306. _starpu_sched_ctx_list_iterator_init(worker->sched_ctx_list, &list_it);
  307. while (_starpu_sched_ctx_list_iterator_has_next(&list_it))
  308. {
  309. e = _starpu_sched_ctx_list_iterator_get_next(&list_it);
  310. sched_ctx = _starpu_get_sched_ctx_struct(e->sched_ctx);
  311. if(sched_ctx && sched_ctx->id > 0 && sched_ctx->id < STARPU_NMAX_SCHED_CTXS)
  312. {
  313. if(!sched_ctx->sched_policy)
  314. worker->is_slave_somewhere = sched_ctx->main_master != workerid;
  315. if(sched_ctx->parallel_sect[workerid])
  316. {
  317. /* don't let the worker sleep with the sched_mutex taken */
  318. /* we need it until here bc of the list of ctxs of the workers
  319. that can change in another thread */
  320. needed = 0;
  321. _starpu_sched_ctx_signal_worker_blocked(sched_ctx->id, workerid);
  322. sched_ctx->busy[workerid] = 1;
  323. STARPU_PTHREAD_COND_WAIT(&sched_ctx->parallel_sect_cond[workerid], &worker->sched_mutex);
  324. sched_ctx->busy[workerid] = 0;
  325. STARPU_PTHREAD_COND_SIGNAL(&sched_ctx->parallel_sect_cond_busy[workerid]);
  326. _starpu_sched_ctx_signal_worker_woke_up(sched_ctx->id, workerid);
  327. sched_ctx->parallel_sect[workerid] = 0;
  328. }
  329. }
  330. if(!needed)
  331. break;
  332. }
  333. /* don't worry if the value is not correct (no lock) it will do it next time */
  334. if(worker->tmp_sched_ctx != -1)
  335. {
  336. sched_ctx = _starpu_get_sched_ctx_struct(worker->tmp_sched_ctx);
  337. if(sched_ctx->parallel_sect[workerid])
  338. {
  339. // needed = 0;
  340. _starpu_sched_ctx_signal_worker_blocked(sched_ctx->id, workerid);
  341. sched_ctx->busy[workerid] = 1;
  342. STARPU_PTHREAD_COND_WAIT(&sched_ctx->parallel_sect_cond[workerid], &worker->sched_mutex);
  343. sched_ctx->busy[workerid] = 0;
  344. STARPU_PTHREAD_COND_SIGNAL(&sched_ctx->parallel_sect_cond_busy[workerid]);
  345. _starpu_sched_ctx_signal_worker_woke_up(sched_ctx->id, workerid);
  346. sched_ctx->parallel_sect[workerid] = 0;
  347. }
  348. }
  349. needed = !needed;
  350. }
  351. if ((worker->pipeline_length == 0 && worker->current_task)
  352. || (worker->pipeline_length != 0 && worker->ntasks))
  353. /* This worker is executing something */
  354. executing = 1;
  355. /*if the worker is already executing a task then */
  356. if (worker->pipeline_length && (worker->ntasks == worker->pipeline_length || worker->pipeline_stuck))
  357. task = NULL;
  358. /* don't push a task if we are already transferring one */
  359. else if (worker->task_transferring != NULL)
  360. task = NULL;
  361. /*else try to pop a task*/
  362. else
  363. {
  364. _starpu_worker_enter_transient_sched_op(worker);
  365. task = _starpu_pop_task(worker);
  366. _starpu_worker_leave_transient_sched_op(worker);
  367. }
  368. #if !defined(STARPU_SIMGRID)
  369. if (task == NULL && !executing)
  370. {
  371. /* Didn't get a task to run and none are running, go to sleep */
  372. /* Note: we need to keep the sched condition mutex all along the path
  373. * from popping a task from the scheduler to blocking. Otherwise the
  374. * driver may go block just after the scheduler got a new task to be
  375. * executed, and thus hanging. */
  376. _starpu_worker_set_status_sleeping(workerid);
  377. if (_starpu_worker_can_block(memnode, worker)
  378. && !_starpu_sched_ctx_last_worker_awake(worker))
  379. {
  380. do
  381. {
  382. STARPU_PTHREAD_COND_WAIT(&worker->sched_cond, &worker->sched_mutex);
  383. }
  384. while (worker->status == STATUS_SLEEPING);
  385. STARPU_PTHREAD_MUTEX_UNLOCK_SCHED(&worker->sched_mutex);
  386. }
  387. else
  388. {
  389. STARPU_PTHREAD_MUTEX_UNLOCK_SCHED(&worker->sched_mutex);
  390. if (_starpu_machine_is_running())
  391. _starpu_exponential_backoff(worker);
  392. }
  393. return NULL;
  394. }
  395. #endif
  396. if (task)
  397. {
  398. _starpu_worker_set_status_scheduling_done(workerid);
  399. _starpu_worker_set_status_wakeup(workerid);
  400. }
  401. else
  402. {
  403. _starpu_worker_set_status_sleeping(workerid);
  404. }
  405. worker->spinning_backoff = BACKOFF_MIN;
  406. STARPU_PTHREAD_MUTEX_UNLOCK_SCHED(&worker->sched_mutex);
  407. STARPU_AYU_PRERUNTASK(_starpu_get_job_associated_to_task(task)->job_id, workerid);
  408. return task;
  409. }
  410. int _starpu_get_multi_worker_task(struct _starpu_worker *workers, struct starpu_task ** tasks, int nworkers, unsigned memnode STARPU_ATTRIBUTE_UNUSED)
  411. {
  412. int i, count = 0;
  413. struct _starpu_job * j;
  414. int is_parallel_task;
  415. struct _starpu_combined_worker *combined_worker;
  416. int executing STARPU_ATTRIBUTE_UNUSED = 0;
  417. /*for each worker*/
  418. #ifndef STARPU_NON_BLOCKING_DRIVERS
  419. /* This assumes only 1 worker */
  420. STARPU_ASSERT_MSG(nworkers == 1, "Multiple workers is not yet possible in blocking drivers mode\n");
  421. STARPU_PTHREAD_MUTEX_LOCK_SCHED(&workers[0].sched_mutex);
  422. #endif
  423. for (i = 0; i < nworkers; i++)
  424. {
  425. if ((workers[i].pipeline_length == 0 && workers[i].current_task)
  426. || (workers[i].pipeline_length != 0 && workers[i].ntasks))
  427. /* At least this worker is executing something */
  428. executing = 1;
  429. /*if the worker is already executing a task then */
  430. if((workers[i].pipeline_length == 0 && workers[i].current_task)
  431. || (workers[i].pipeline_length != 0 &&
  432. (workers[i].ntasks == workers[i].pipeline_length
  433. || workers[i].pipeline_stuck)))
  434. {
  435. tasks[i] = NULL;
  436. }
  437. /* don't push a task if we are already transferring one */
  438. else if (workers[i].task_transferring != NULL)
  439. {
  440. tasks[i] = NULL;
  441. }
  442. /*else try to pop a task*/
  443. else
  444. {
  445. #ifdef STARPU_NON_BLOCKING_DRIVERS
  446. STARPU_PTHREAD_MUTEX_LOCK_SCHED(&workers[i].sched_mutex);
  447. #endif
  448. _starpu_worker_set_status_scheduling(workers[i].workerid);
  449. _starpu_set_local_worker_key(&workers[i]);
  450. _starpu_worker_enter_transient_sched_op(&workers[i]);
  451. tasks[i] = _starpu_pop_task(&workers[i]);
  452. _starpu_worker_leave_transient_sched_op(&workers[i]);
  453. if(tasks[i] != NULL)
  454. {
  455. _starpu_worker_set_status_scheduling_done(workers[i].workerid);
  456. _starpu_worker_set_status_wakeup(workers[i].workerid);
  457. #ifdef STARPU_NON_BLOCKING_DRIVERS
  458. STARPU_PTHREAD_MUTEX_UNLOCK_SCHED(&workers[i].sched_mutex);
  459. #endif
  460. count ++;
  461. j = _starpu_get_job_associated_to_task(tasks[i]);
  462. is_parallel_task = (j->task_size > 1);
  463. if (workers[i].pipeline_length)
  464. workers[i].current_tasks[(workers[i].first_task + workers[i].ntasks)%STARPU_MAX_PIPELINE] = tasks[i];
  465. else
  466. workers[i].current_task = j->task;
  467. workers[i].ntasks++;
  468. /* Get the rank in case it is a parallel task */
  469. if (is_parallel_task)
  470. {
  471. STARPU_PTHREAD_MUTEX_LOCK(&j->sync_mutex);
  472. workers[i].current_rank = j->active_task_alias_count++;
  473. STARPU_PTHREAD_MUTEX_UNLOCK(&j->sync_mutex);
  474. if(j->combined_workerid != -1)
  475. {
  476. combined_worker = _starpu_get_combined_worker_struct(j->combined_workerid);
  477. workers[i].combined_workerid = j->combined_workerid;
  478. workers[i].worker_size = combined_worker->worker_size;
  479. }
  480. }
  481. else
  482. {
  483. workers[i].combined_workerid = workers[i].workerid;
  484. workers[i].worker_size = 1;
  485. workers[i].current_rank = 0;
  486. }
  487. STARPU_AYU_PRERUNTASK(_starpu_get_job_associated_to_task(tasks[i])->job_id, workers[i].workerid);
  488. }
  489. else
  490. {
  491. _starpu_worker_set_status_sleeping(workers[i].workerid);
  492. #ifdef STARPU_NON_BLOCKING_DRIVERS
  493. STARPU_PTHREAD_MUTEX_UNLOCK_SCHED(&workers[i].sched_mutex);
  494. #endif
  495. }
  496. }
  497. }
  498. #if !defined(STARPU_NON_BLOCKING_DRIVERS)
  499. #if !defined(STARPU_SIMGRID)
  500. /* Block the assumed-to-be-only worker */
  501. struct _starpu_worker *worker = &workers[0];
  502. unsigned workerid = workers[0].workerid;
  503. if (!count && !executing)
  504. {
  505. /* Didn't get a task to run and none are running, go to sleep */
  506. /* Note: we need to keep the sched condition mutex all along the path
  507. * from popping a task from the scheduler to blocking. Otherwise the
  508. * driver may go block just after the scheduler got a new task to be
  509. * executed, and thus hanging. */
  510. _starpu_worker_set_status_sleeping(workerid);
  511. if (_starpu_worker_can_block(memnode, worker)
  512. && !_starpu_sched_ctx_last_worker_awake(worker))
  513. {
  514. do
  515. {
  516. STARPU_PTHREAD_COND_WAIT(&worker->sched_cond, &worker->sched_mutex);
  517. }
  518. while (worker->status == STATUS_SLEEPING);
  519. STARPU_PTHREAD_MUTEX_UNLOCK_SCHED(&worker->sched_mutex);
  520. }
  521. else
  522. {
  523. STARPU_PTHREAD_MUTEX_UNLOCK_SCHED(&worker->sched_mutex);
  524. if (_starpu_machine_is_running())
  525. _starpu_exponential_backoff(worker);
  526. }
  527. return 0;
  528. }
  529. _starpu_worker_set_status_wakeup(workerid);
  530. worker->spinning_backoff = BACKOFF_MIN;
  531. #endif /* !STARPU_SIMGRID */
  532. STARPU_PTHREAD_MUTEX_UNLOCK_SCHED(&workers[0].sched_mutex);
  533. #endif /* !STARPU_NON_BLOCKING_DRIVERS */
  534. return count;
  535. }