parallel_eager.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011-2012 Université de Bordeaux 1
  4. * Copyright (C) 2011 Télécom-SudParis
  5. * Copyright (C) 2011 INRIA
  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 <core/workers.h>
  19. #include <sched_policies/fifo_queues.h>
  20. #include <common/barrier.h>
  21. #include <sched_policies/detect_combined_workers.h>
  22. struct _starpu_peager_data
  23. {
  24. struct _starpu_fifo_taskq *fifo;
  25. struct _starpu_fifo_taskq *local_fifo[STARPU_NMAXWORKERS];
  26. int master_id[STARPU_NMAXWORKERS];
  27. };
  28. /* XXX instead of 10, we should use some "MAX combination .."*/
  29. static int possible_combinations_cnt[STARPU_NMAXWORKERS];
  30. static int possible_combinations[STARPU_NMAXWORKERS][10];
  31. static int possible_combinations_size[STARPU_NMAXWORKERS][10];
  32. /*!!!!!!! It doesn't work with several contexts because the combined workers are constructed
  33. from the workers available to the program, and not to the context !!!!!!!!!!!!!!!!!!!!!!!
  34. */
  35. static void peager_add_workers(unsigned sched_ctx_id, int *workerids, unsigned nworkers)
  36. {
  37. struct _starpu_peager_data *data = (struct _starpu_peager_data*)starpu_sched_ctx_get_policy_data(sched_ctx_id);
  38. _starpu_sched_find_worker_combinations(workerids, nworkers);
  39. unsigned workerid, i;
  40. unsigned ncombinedworkers;
  41. ncombinedworkers = starpu_combined_worker_get_count();
  42. /* Find the master of each worker. We first assign the worker as its
  43. * own master, and then iterate over the different worker combinations
  44. * to find the biggest combination containing this worker. */
  45. for(i = 0; i < nworkers; i++)
  46. {
  47. workerid = workerids[i];
  48. int cnt = possible_combinations_cnt[workerid]++;
  49. possible_combinations[workerid][cnt] = workerid;
  50. possible_combinations_size[workerid][cnt] = 1;
  51. data->master_id[workerid] = workerid;
  52. }
  53. for (i = 0; i < ncombinedworkers; i++)
  54. {
  55. workerid = nworkers + i;
  56. /* Note that we ASSUME that the workers are sorted by size ! */
  57. int *workers;
  58. int size;
  59. starpu_combined_worker_get_description(workerid, &size, &workers);
  60. int master = workers[0];
  61. int j;
  62. for (j = 0; j < size; j++)
  63. {
  64. if (data->master_id[workers[j]] > master)
  65. data->master_id[workers[j]] = master;
  66. int cnt = possible_combinations_cnt[workers[j]]++;
  67. possible_combinations[workers[j]][cnt] = workerid;
  68. possible_combinations_size[workers[j]][cnt] = size;
  69. }
  70. }
  71. for(i = 0; i < nworkers; i++)
  72. {
  73. workerid = workerids[i];
  74. /* slaves pick up tasks from their local queue, their master
  75. * will put tasks directly in that local list when a parallel
  76. * tasks comes. */
  77. data->local_fifo[workerid] = _starpu_create_fifo();
  78. }
  79. #if 0
  80. for(i = 0; i < nworkers; i++)
  81. {
  82. workerid = workerids[i];
  83. fprintf(stderr, "MASTER of %d = %d\n", workerid, master_id[workerid]);
  84. }
  85. #endif
  86. }
  87. static void peager_remove_workers(unsigned sched_ctx_id, int *workerids, unsigned nworkers)
  88. {
  89. struct _starpu_peager_data *data = (struct _starpu_peager_data*)starpu_sched_ctx_get_policy_data(sched_ctx_id);
  90. int workerid;
  91. unsigned i;
  92. for(i = 0; i < nworkers; i++)
  93. {
  94. workerid = workerids[i];
  95. _starpu_destroy_fifo(data->local_fifo[workerid]);
  96. }
  97. }
  98. static void initialize_peager_policy(unsigned sched_ctx_id)
  99. {
  100. starpu_sched_ctx_create_worker_collection(sched_ctx_id, WORKER_LIST);
  101. struct _starpu_peager_data *data = (struct _starpu_peager_data*)malloc(sizeof(struct _starpu_peager_data));
  102. /* masters pick tasks from that queue */
  103. data->fifo = _starpu_create_fifo();
  104. starpu_sched_ctx_set_policy_data(sched_ctx_id, (void*)data);
  105. }
  106. static void deinitialize_peager_policy(unsigned sched_ctx_id)
  107. {
  108. /* TODO check that there is no task left in the queue */
  109. struct _starpu_peager_data *data = (struct _starpu_peager_data*)starpu_sched_ctx_get_policy_data(sched_ctx_id);
  110. /* deallocate the job queue */
  111. _starpu_destroy_fifo(data->fifo);
  112. starpu_sched_ctx_delete_worker_collection(sched_ctx_id);
  113. free(data);
  114. }
  115. static int push_task_peager_policy(struct starpu_task *task)
  116. {
  117. unsigned sched_ctx_id = task->sched_ctx;
  118. _starpu_pthread_mutex_t *changing_ctx_mutex = starpu_get_changing_ctx_mutex(sched_ctx_id);
  119. unsigned nworkers;
  120. int ret_val = -1;
  121. /* if the context has no workers return */
  122. _STARPU_PTHREAD_MUTEX_LOCK(changing_ctx_mutex);
  123. nworkers = starpu_sched_ctx_get_nworkers(sched_ctx_id);
  124. if(nworkers == 0)
  125. {
  126. _STARPU_PTHREAD_MUTEX_UNLOCK(changing_ctx_mutex);
  127. return ret_val;
  128. }
  129. struct _starpu_peager_data *data = (struct _starpu_peager_data*)starpu_sched_ctx_get_policy_data(sched_ctx_id);
  130. int worker = 0;
  131. struct starpu_sched_ctx_worker_collection *workers = starpu_sched_ctx_get_worker_collection(sched_ctx_id);
  132. struct starpu_iterator it;
  133. if(workers->init_iterator)
  134. workers->init_iterator(workers, &it);
  135. while(workers->has_next(workers, &it))
  136. {
  137. worker = workers->get_next(workers, &it);
  138. int master = data->master_id[worker];
  139. /* If this is not a CPU, then the worker simply grabs tasks from the fifo */
  140. if (starpu_worker_get_type(worker) != STARPU_CPU_WORKER || master == worker)
  141. {
  142. _starpu_pthread_mutex_t *sched_mutex;
  143. _starpu_pthread_cond_t *sched_cond;
  144. starpu_worker_get_sched_condition(worker, &sched_mutex, &sched_cond);
  145. _STARPU_PTHREAD_MUTEX_LOCK(sched_mutex);
  146. }
  147. }
  148. ret_val = _starpu_fifo_push_task(data->fifo, task);
  149. while(workers->has_next(workers, &it))
  150. {
  151. worker = workers->get_next(workers, &it);
  152. int master = data->master_id[worker];
  153. /* If this is not a CPU, then the worker simply grabs tasks from the fifo */
  154. if (starpu_worker_get_type(worker) != STARPU_CPU_WORKER || master == worker)
  155. {
  156. _starpu_pthread_mutex_t *sched_mutex;
  157. _starpu_pthread_cond_t *sched_cond;
  158. starpu_worker_get_sched_condition(worker, &sched_mutex, &sched_cond);
  159. _STARPU_PTHREAD_COND_SIGNAL(sched_cond);
  160. _STARPU_PTHREAD_MUTEX_UNLOCK(sched_mutex);
  161. }
  162. }
  163. _STARPU_PTHREAD_MUTEX_UNLOCK(changing_ctx_mutex);
  164. return ret_val;
  165. }
  166. static struct starpu_task *pop_task_peager_policy(unsigned sched_ctx_id)
  167. {
  168. struct _starpu_peager_data *data = (struct _starpu_peager_data*)starpu_sched_ctx_get_policy_data(sched_ctx_id);
  169. int workerid = starpu_worker_get_id();
  170. /* If this is not a CPU, then the worker simply grabs tasks from the fifo */
  171. if (starpu_worker_get_type(workerid) != STARPU_CPU_WORKER)
  172. return _starpu_fifo_pop_task(data->fifo, workerid);
  173. int master = data->master_id[workerid];
  174. if (master == workerid)
  175. {
  176. /* The worker is a master */
  177. struct starpu_task *task = _starpu_fifo_pop_task(data->fifo, workerid);
  178. if (!task)
  179. return NULL;
  180. /* Find the largest compatible worker combination */
  181. int best_size = -1;
  182. int best_workerid = -1;
  183. int i;
  184. for (i = 0; i < possible_combinations_cnt[master]; i++)
  185. {
  186. if (possible_combinations_size[workerid][i] > best_size)
  187. {
  188. int combined_worker = possible_combinations[workerid][i];
  189. if (starpu_combined_worker_can_execute_task(combined_worker, task, 0))
  190. {
  191. best_size = possible_combinations_size[workerid][i];
  192. best_workerid = combined_worker;
  193. }
  194. }
  195. }
  196. /* In case nobody can execute this task, we let the master
  197. * worker take it anyway, so that it can discard it afterward.
  198. * */
  199. if (best_workerid == -1)
  200. return task;
  201. /* Is this a basic worker or a combined worker ? */
  202. int nbasic_workers = (int)starpu_worker_get_count();
  203. int is_basic_worker = (best_workerid < nbasic_workers);
  204. if (is_basic_worker)
  205. {
  206. /* The master is alone */
  207. return task;
  208. }
  209. else
  210. {
  211. /* The master needs to dispatch the task between the
  212. * different combined workers */
  213. struct _starpu_combined_worker *combined_worker;
  214. combined_worker = _starpu_get_combined_worker_struct(best_workerid);
  215. int worker_size = combined_worker->worker_size;
  216. int *combined_workerid = combined_worker->combined_workerid;
  217. struct _starpu_job *j = _starpu_get_job_associated_to_task(task);
  218. j->task_size = worker_size;
  219. j->combined_workerid = best_workerid;
  220. j->active_task_alias_count = 0;
  221. //fprintf(stderr, "POP -> size %d best_size %d\n", worker_size, best_size);
  222. _STARPU_PTHREAD_BARRIER_INIT(&j->before_work_barrier, NULL, worker_size);
  223. _STARPU_PTHREAD_BARRIER_INIT(&j->after_work_barrier, NULL, worker_size);
  224. /* Dispatch task aliases to the different slaves */
  225. for (i = 1; i < worker_size; i++)
  226. {
  227. struct starpu_task *alias = _starpu_create_task_alias(task);
  228. int local_worker = combined_workerid[i];
  229. _starpu_pthread_mutex_t *sched_mutex;
  230. _starpu_pthread_cond_t *sched_cond;
  231. starpu_worker_get_sched_condition(local_worker, &sched_mutex, &sched_cond);
  232. _STARPU_PTHREAD_MUTEX_LOCK(sched_mutex);
  233. _starpu_fifo_push_task(data->local_fifo[local_worker], alias);
  234. _STARPU_PTHREAD_COND_SIGNAL(sched_cond);
  235. _STARPU_PTHREAD_MUTEX_UNLOCK(sched_mutex);
  236. }
  237. /* The master also manipulated an alias */
  238. struct starpu_task *master_alias = _starpu_create_task_alias(task);
  239. return master_alias;
  240. }
  241. }
  242. else
  243. {
  244. /* The worker is a slave */
  245. return _starpu_fifo_pop_task(data->local_fifo[workerid], workerid);
  246. }
  247. }
  248. struct starpu_sched_policy _starpu_sched_peager_policy =
  249. {
  250. .init_sched = initialize_peager_policy,
  251. .deinit_sched = deinitialize_peager_policy,
  252. .add_workers = peager_add_workers,
  253. .remove_workers = peager_remove_workers,
  254. .push_task = push_task_peager_policy,
  255. .pop_task = pop_task_peager_policy,
  256. .pre_exec_hook = NULL,
  257. .post_exec_hook = NULL,
  258. .pop_every_task = NULL,
  259. .policy_name = "peager",
  260. .policy_description = "parallel eager policy"
  261. };