eager_central_policy.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2008-2021 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
  4. * Copyright (C) 2013 Simon Archipoff
  5. * Copyright (C) 2016 Uppsala University
  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. /*
  19. * This is just the trivial policy where every worker use the same
  20. * JOB QUEUE.
  21. */
  22. #include <starpu_scheduler.h>
  23. #include <sched_policies/fifo_queues.h>
  24. #include <common/thread.h>
  25. #include <starpu_bitmap.h>
  26. #include <core/workers.h>
  27. struct _starpu_eager_center_policy_data
  28. {
  29. struct _starpu_fifo_taskq fifo;
  30. starpu_pthread_mutex_t policy_mutex;
  31. struct starpu_bitmap waiters;
  32. };
  33. static void initialize_eager_center_policy(unsigned sched_ctx_id)
  34. {
  35. struct _starpu_eager_center_policy_data *data;
  36. _STARPU_MALLOC(data, sizeof(struct _starpu_eager_center_policy_data));
  37. /* there is only a single queue in that trivial design */
  38. _starpu_init_fifo(&data->fifo);
  39. starpu_bitmap_init(&data->waiters);
  40. starpu_sched_ctx_set_policy_data(sched_ctx_id, (void*)data);
  41. STARPU_PTHREAD_MUTEX_INIT(&data->policy_mutex, NULL);
  42. }
  43. static void deinitialize_eager_center_policy(unsigned sched_ctx_id)
  44. {
  45. struct _starpu_eager_center_policy_data *data = (struct _starpu_eager_center_policy_data*)starpu_sched_ctx_get_policy_data(sched_ctx_id);
  46. struct _starpu_fifo_taskq *fifo = &data->fifo;
  47. STARPU_ASSERT(starpu_task_list_empty(&fifo->taskq));
  48. STARPU_PTHREAD_MUTEX_DESTROY(&data->policy_mutex);
  49. free(data);
  50. }
  51. static int push_task_eager_policy(struct starpu_task *task)
  52. {
  53. unsigned sched_ctx_id = task->sched_ctx;
  54. struct _starpu_eager_center_policy_data *data = (struct _starpu_eager_center_policy_data*)starpu_sched_ctx_get_policy_data(sched_ctx_id);
  55. starpu_worker_relax_on();
  56. STARPU_PTHREAD_MUTEX_LOCK(&data->policy_mutex);
  57. starpu_worker_relax_off();
  58. starpu_task_list_push_back(&data->fifo.taskq,task);
  59. data->fifo.ntasks++;
  60. data->fifo.nprocessed++;
  61. if (_starpu_get_nsched_ctxs() > 1)
  62. {
  63. starpu_worker_relax_on();
  64. _starpu_sched_ctx_lock_write(sched_ctx_id);
  65. starpu_worker_relax_off();
  66. starpu_sched_ctx_list_task_counters_increment_all_ctx_locked(task, sched_ctx_id);
  67. _starpu_sched_ctx_unlock_write(sched_ctx_id);
  68. }
  69. starpu_push_task_end(task);
  70. /*if there are no tasks block */
  71. /* wake people waiting for a task */
  72. struct starpu_worker_collection *workers = starpu_sched_ctx_get_worker_collection(sched_ctx_id);
  73. struct starpu_sched_ctx_iterator it;
  74. #ifndef STARPU_NON_BLOCKING_DRIVERS
  75. char dowake[STARPU_NMAXWORKERS] = { 0 };
  76. #endif
  77. workers->init_iterator_for_parallel_tasks(workers, &it, task);
  78. while(workers->has_next(workers, &it))
  79. {
  80. unsigned worker = workers->get_next(workers, &it);
  81. #ifdef STARPU_NON_BLOCKING_DRIVERS
  82. if (!starpu_bitmap_get(&data->waiters, worker))
  83. /* This worker is not waiting for a task */
  84. continue;
  85. #endif
  86. if (starpu_worker_can_execute_task_first_impl(worker, task, NULL))
  87. {
  88. /* It can execute this one, tell him! */
  89. #ifdef STARPU_NON_BLOCKING_DRIVERS
  90. starpu_bitmap_unset(&data->waiters, worker);
  91. /* We really woke at least somebody, no need to wake somebody else */
  92. break;
  93. #else
  94. dowake[worker] = 1;
  95. #endif
  96. }
  97. }
  98. /* Let the task free */
  99. STARPU_PTHREAD_MUTEX_UNLOCK(&data->policy_mutex);
  100. #if !defined(STARPU_NON_BLOCKING_DRIVERS) || defined(STARPU_SIMGRID)
  101. /* Now that we have a list of potential workers, try to wake one */
  102. workers->init_iterator_for_parallel_tasks(workers, &it, task);
  103. while(workers->has_next(workers, &it))
  104. {
  105. unsigned worker = workers->get_next(workers, &it);
  106. if (dowake[worker])
  107. if (starpu_wake_worker_relax_light(worker))
  108. break; // wake up a single worker
  109. }
  110. #endif
  111. return 0;
  112. }
  113. static struct starpu_task *pop_every_task_eager_policy(unsigned sched_ctx_id)
  114. {
  115. struct _starpu_eager_center_policy_data *data = (struct _starpu_eager_center_policy_data*)starpu_sched_ctx_get_policy_data(sched_ctx_id);
  116. unsigned workerid = starpu_worker_get_id_check();
  117. STARPU_PTHREAD_MUTEX_LOCK(&data->policy_mutex);
  118. struct starpu_task* task = _starpu_fifo_pop_every_task(&data->fifo, workerid);
  119. STARPU_PTHREAD_MUTEX_UNLOCK(&data->policy_mutex);
  120. starpu_sched_ctx_list_task_counters_reset_all(task, sched_ctx_id);
  121. return task;
  122. }
  123. static struct starpu_task *pop_task_eager_policy(unsigned sched_ctx_id)
  124. {
  125. struct starpu_task *chosen_task = NULL;
  126. unsigned workerid = starpu_worker_get_id_check();
  127. struct _starpu_eager_center_policy_data *data = (struct _starpu_eager_center_policy_data*)starpu_sched_ctx_get_policy_data(sched_ctx_id);
  128. /* Here helgrind would shout that this is unprotected, this is just an
  129. * integer access, and we hold the sched mutex, so we can not miss any
  130. * wake up. */
  131. if (!STARPU_RUNNING_ON_VALGRIND && _starpu_fifo_empty(&data->fifo))
  132. {
  133. return NULL;
  134. }
  135. #ifdef STARPU_NON_BLOCKING_DRIVERS
  136. if (!STARPU_RUNNING_ON_VALGRIND && starpu_bitmap_get(&data->waiters, workerid))
  137. /* Nobody woke us, avoid bothering the mutex */
  138. {
  139. return NULL;
  140. }
  141. #endif
  142. starpu_worker_relax_on();
  143. STARPU_PTHREAD_MUTEX_LOCK(&data->policy_mutex);
  144. starpu_worker_relax_off();
  145. chosen_task = _starpu_fifo_pop_task(&data->fifo, workerid);
  146. if (!chosen_task)
  147. /* Tell pushers that we are waiting for tasks for us */
  148. starpu_bitmap_set(&data->waiters, workerid);
  149. STARPU_PTHREAD_MUTEX_UNLOCK(&data->policy_mutex);
  150. if(chosen_task &&_starpu_get_nsched_ctxs() > 1)
  151. {
  152. starpu_worker_relax_on();
  153. _starpu_sched_ctx_lock_write(sched_ctx_id);
  154. starpu_worker_relax_off();
  155. starpu_sched_ctx_list_task_counters_decrement_all_ctx_locked(chosen_task, sched_ctx_id);
  156. if (_starpu_sched_ctx_worker_is_master_for_child_ctx(sched_ctx_id, workerid, chosen_task))
  157. chosen_task = NULL;
  158. _starpu_sched_ctx_unlock_write(sched_ctx_id);
  159. }
  160. return chosen_task;
  161. }
  162. static void eager_add_workers(unsigned sched_ctx_id, int *workerids, unsigned nworkers)
  163. {
  164. unsigned i;
  165. for (i = 0; i < nworkers; i++)
  166. {
  167. int workerid = workerids[i];
  168. int curr_workerid = _starpu_worker_get_id();
  169. if(workerid != curr_workerid)
  170. starpu_wake_worker_locked(workerid);
  171. starpu_sched_ctx_worker_shares_tasks_lists(workerid, sched_ctx_id);
  172. }
  173. }
  174. struct starpu_sched_policy _starpu_sched_eager_policy =
  175. {
  176. .init_sched = initialize_eager_center_policy,
  177. .deinit_sched = deinitialize_eager_center_policy,
  178. .add_workers = eager_add_workers,
  179. .remove_workers = NULL,
  180. .push_task = push_task_eager_policy,
  181. .pop_task = pop_task_eager_policy,
  182. .pre_exec_hook = NULL,
  183. .post_exec_hook = NULL,
  184. .pop_every_task = pop_every_task_eager_policy,
  185. .policy_name = "eager",
  186. .policy_description = "eager policy with a central queue",
  187. .worker_type = STARPU_WORKER_LIST,
  188. };