parallel_greedy.c 7.2 KB

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