parallel_eager.c 10 KB

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