parallel_greedy.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011 Université de Bordeaux 1
  4. *
  5. * StarPU is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation; either version 2.1 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * StarPU is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. *
  14. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  15. */
  16. #include <core/workers.h>
  17. #include <sched_policies/fifo_queues.h>
  18. #include <common/barrier.h>
  19. /* the former is the actual queue, the latter some container */
  20. static struct starpu_fifo_taskq_s *fifo;
  21. static struct starpu_fifo_taskq_s *local_fifo[STARPU_NMAXWORKERS];
  22. static int master_id[STARPU_NMAXWORKERS];
  23. static pthread_cond_t sched_cond;
  24. static pthread_mutex_t sched_mutex;
  25. static pthread_cond_t master_sched_cond[STARPU_NMAXWORKERS];
  26. static pthread_mutex_t master_sched_mutex[STARPU_NMAXWORKERS];
  27. /* XXX instead of 10, we should use some "MAX combination .."*/
  28. static int possible_combinations_cnt[STARPU_NMAXWORKERS];
  29. static int possible_combinations[STARPU_NMAXWORKERS][10];
  30. static int possible_combinations_size[STARPU_NMAXWORKERS][10];
  31. static void initialize_pgreedy_policy(struct starpu_machine_topology_s *topology,
  32. __attribute__ ((unused)) struct starpu_sched_policy_s *_policy)
  33. {
  34. /* masters pick tasks from that queue */
  35. fifo = _starpu_create_fifo();
  36. _starpu_sched_find_worker_combinations(topology);
  37. int workerid;
  38. unsigned ncombinedworkers, nworkers;
  39. nworkers = topology->nworkers;
  40. ncombinedworkers = starpu_combined_worker_get_count();
  41. /* Find the master of each worker. We first assign the worker as its
  42. * own master, and then iterate over the different worker combinations
  43. * to find the biggest combination containing this worker. */
  44. for (workerid = 0; workerid < nworkers; workerid++)
  45. {
  46. int cnt = possible_combinations_cnt[workerid]++;
  47. possible_combinations[workerid][cnt] = workerid;
  48. possible_combinations_size[workerid][cnt] = 1;
  49. master_id[workerid] = workerid;
  50. }
  51. unsigned i;
  52. for (i = 0; i < ncombinedworkers; i++)
  53. {
  54. workerid = nworkers + i;
  55. /* Note that we ASSUME that the workers are sorted by size ! */
  56. int *workers;
  57. int size;
  58. starpu_combined_worker_get_description(workerid, &size, &workers);
  59. int master = workers[0];
  60. int j;
  61. for (j = 0; j < size; j++)
  62. {
  63. if (master_id[workers[j]] > master)
  64. master_id[workers[j]] = master;
  65. int cnt = possible_combinations_cnt[workers[j]]++;
  66. possible_combinations[workers[j]][cnt] = workerid;
  67. possible_combinations_size[workers[j]][cnt] = size;
  68. }
  69. }
  70. PTHREAD_MUTEX_INIT(&sched_mutex, NULL);
  71. PTHREAD_COND_INIT(&sched_cond, NULL);
  72. for (workerid = 0; workerid < nworkers; workerid++)
  73. {
  74. PTHREAD_MUTEX_INIT(&master_sched_mutex[workerid], NULL);
  75. PTHREAD_COND_INIT(&master_sched_cond[workerid], NULL);
  76. }
  77. for (workerid = 0; workerid < nworkers; workerid++)
  78. {
  79. /* slaves pick up tasks from their local queue, their master
  80. * will put tasks directly in that local list when a parallel
  81. * tasks comes. */
  82. local_fifo[workerid] = _starpu_create_fifo();
  83. unsigned master = master_id[workerid];
  84. /* All masters use the same condition/mutex */
  85. if (master == workerid)
  86. {
  87. starpu_worker_set_sched_condition(workerid,
  88. &sched_cond, &sched_mutex);
  89. }
  90. else {
  91. starpu_worker_set_sched_condition(workerid,
  92. &master_sched_cond[master],
  93. &master_sched_mutex[master]);
  94. }
  95. }
  96. #if 0
  97. for (workerid = 0; workerid < nworkers; workerid++)
  98. {
  99. fprintf(stderr, "MASTER of %d = %d\n", workerid, master_id[workerid]);
  100. }
  101. #endif
  102. }
  103. static void deinitialize_pgreedy_policy(__attribute__ ((unused)) struct starpu_machine_topology_s *topology,
  104. __attribute__ ((unused)) struct starpu_sched_policy_s *_policy)
  105. {
  106. /* TODO check that there is no task left in the queue */
  107. /* deallocate the job queue */
  108. _starpu_destroy_fifo(fifo);
  109. }
  110. static int push_task_pgreedy_policy(struct starpu_task *task)
  111. {
  112. return _starpu_fifo_push_task(fifo, &sched_mutex, &sched_cond, task);
  113. }
  114. static struct starpu_task *pop_task_pgreedy_policy(void)
  115. {
  116. int workerid = starpu_worker_get_id();
  117. /* If this is not a CPU, then the worker simply grabs tasks from the fifo */
  118. if (starpu_worker_get_type(workerid) != STARPU_CPU_WORKER)
  119. return _starpu_fifo_pop_task(fifo, workerid);
  120. int master = master_id[workerid];
  121. if (master == workerid)
  122. {
  123. /* The worker is a master */
  124. struct starpu_task *task = _starpu_fifo_pop_task(fifo, workerid);
  125. if (!task)
  126. return NULL;
  127. /* Find the largest compatible worker combination */
  128. int best_size = -1;
  129. int best_workerid = -1;
  130. int i;
  131. for (i = 0; i < possible_combinations_cnt[master]; i++)
  132. {
  133. if (possible_combinations_size[workerid][i] > best_size)
  134. {
  135. int combined_worker = possible_combinations[workerid][i];
  136. if (starpu_combined_worker_may_execute_task(combined_worker, task))
  137. {
  138. best_size = possible_combinations_size[workerid][i];
  139. best_workerid = combined_worker;
  140. }
  141. }
  142. }
  143. /* In case nobody can execute this task, we let the master
  144. * worker take it anyway, so that it can discard it afterward.
  145. * */
  146. if (best_workerid == -1)
  147. return task;
  148. /* Is this a basic worker or a combined worker ? */
  149. int nbasic_workers = (int)starpu_worker_get_count();
  150. int is_basic_worker = (best_workerid < nbasic_workers);
  151. if (is_basic_worker)
  152. {
  153. /* The master is alone */
  154. return task;
  155. }
  156. else {
  157. /* The master needs to dispatch the task between the
  158. * different combined workers */
  159. struct starpu_combined_worker_s *combined_worker;
  160. combined_worker = _starpu_get_combined_worker_struct(best_workerid);
  161. int worker_size = combined_worker->worker_size;
  162. int *combined_workerid = combined_worker->combined_workerid;
  163. starpu_job_t j = _starpu_get_job_associated_to_task(task);
  164. j->task_size = worker_size;
  165. j->combined_workerid = best_workerid;
  166. j->active_task_alias_count = 0;
  167. //fprintf(stderr, "POP -> size %d best_size %d\n", worker_size, best_size);
  168. PTHREAD_BARRIER_INIT(&j->before_work_barrier, NULL, worker_size);
  169. PTHREAD_BARRIER_INIT(&j->after_work_barrier, NULL, worker_size);
  170. /* Dispatch task aliases to the different slaves */
  171. for (i = 1; i < worker_size; i++)
  172. {
  173. struct starpu_task *alias = _starpu_create_task_alias(task);
  174. int local_worker = combined_workerid[i];
  175. _starpu_fifo_push_task(local_fifo[local_worker],
  176. &master_sched_mutex[master],
  177. &master_sched_cond[master], alias);
  178. }
  179. /* The master also manipulated an alias */
  180. struct starpu_task *master_alias = _starpu_create_task_alias(task);
  181. return master_alias;
  182. }
  183. }
  184. else {
  185. /* The worker is a slave */
  186. return _starpu_fifo_pop_task(local_fifo[workerid], workerid);
  187. }
  188. }
  189. struct starpu_sched_policy_s _starpu_sched_pgreedy_policy = {
  190. .init_sched = initialize_pgreedy_policy,
  191. .deinit_sched = deinitialize_pgreedy_policy,
  192. .push_task = push_task_pgreedy_policy,
  193. .push_prio_task = push_task_pgreedy_policy,
  194. .pop_task = pop_task_pgreedy_policy,
  195. .post_exec_hook = NULL,
  196. .pop_every_task = NULL,
  197. .policy_name = "pgreedy",
  198. .policy_description = "parallel greedy policy"
  199. };