detect_combined_workers.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2015 Université de Bordeaux
  4. * Copyright (C) 2011, 2012, 2013 Centre National de la Recherche Scientifique
  5. *
  6. * StarPU is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation; either version 2.1 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * StarPU is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. *
  15. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  16. */
  17. #include <starpu.h>
  18. #include <common/utils.h>
  19. #include <core/workers.h>
  20. #include <math.h>
  21. #include <core/detect_combined_workers.h>
  22. #ifdef STARPU_HAVE_HWLOC
  23. #include <hwloc.h>
  24. static void find_workers(hwloc_obj_t obj, int cpu_workers[STARPU_NMAXWORKERS], unsigned *n)
  25. {
  26. if (!obj->userdata)
  27. /* Not something we run something on, don't care */
  28. return;
  29. if (obj->userdata == (void*) -1)
  30. {
  31. /* Intra node, recurse */
  32. unsigned i;
  33. for (i = 0; i < obj->arity; i++)
  34. find_workers(obj->children[i], cpu_workers, n);
  35. return;
  36. }
  37. /* Got to a PU leaf */
  38. struct _starpu_worker_list *workers = obj->userdata;
  39. struct _starpu_worker *worker;
  40. for(worker = _starpu_worker_list_begin(workers); worker != _starpu_worker_list_end(workers); worker = _starpu_worker_list_next(worker))
  41. {
  42. /* is it a CPU worker? */
  43. if (worker->perf_arch.devices[0].type == STARPU_CPU_WORKER && worker->perf_arch.devices[0].ncores == 1)
  44. {
  45. _STARPU_DEBUG("worker %d is part of it\n", worker->workerid);
  46. /* Add it to the combined worker */
  47. cpu_workers[(*n)++] = worker->workerid;
  48. }
  49. }
  50. }
  51. static void synthesize_intermediate_workers(hwloc_obj_t *children, unsigned min, unsigned max, unsigned arity, unsigned n, unsigned synthesize_arity)
  52. {
  53. unsigned nworkers, i, j;
  54. unsigned chunk_size = (n + synthesize_arity-1) / synthesize_arity;
  55. unsigned chunk_start;
  56. int cpu_workers[STARPU_NMAXWORKERS];
  57. int ret;
  58. if (n <= synthesize_arity)
  59. /* Not too many children, do not synthesize */
  60. return;
  61. _STARPU_DEBUG("%u children > %u, synthesizing intermediate combined workers of size %u\n", n, synthesize_arity, chunk_size);
  62. n = 0;
  63. j = 0;
  64. nworkers = 0;
  65. chunk_start = 0;
  66. for (i = 0 ; i < arity; i++)
  67. {
  68. if (children[i]->userdata)
  69. {
  70. n++;
  71. _STARPU_DEBUG("child %u\n", i);
  72. find_workers(children[i], cpu_workers, &nworkers);
  73. j++;
  74. }
  75. /* Completed a chunk, or last bit (but not if it's just 1 subobject) */
  76. if (j == chunk_size || (i == arity-1 && j > 1))
  77. {
  78. if (nworkers >= min && nworkers <= max)
  79. {
  80. unsigned sched_ctx_id = starpu_sched_ctx_get_context();
  81. if(sched_ctx_id == STARPU_NMAX_SCHED_CTXS)
  82. sched_ctx_id = 0;
  83. struct starpu_worker_collection* workers = starpu_sched_ctx_get_worker_collection(sched_ctx_id);
  84. _STARPU_DEBUG("Adding it\n");
  85. ret = starpu_combined_worker_assign_workerid(nworkers, cpu_workers);
  86. STARPU_ASSERT(ret >= 0);
  87. workers->add(workers,ret);
  88. }
  89. /* Recurse there */
  90. synthesize_intermediate_workers(children+chunk_start, min, max, i - chunk_start, n, synthesize_arity);
  91. /* And restart another one */
  92. n = 0;
  93. j = 0;
  94. nworkers = 0;
  95. chunk_start = i+1;
  96. }
  97. }
  98. }
  99. static void find_and_assign_combinations(hwloc_obj_t obj, unsigned min, unsigned max, unsigned synthesize_arity)
  100. {
  101. char name[64];
  102. unsigned i, n, nworkers;
  103. int cpu_workers[STARPU_NMAXWORKERS];
  104. struct _starpu_machine_config *config = _starpu_get_machine_config();
  105. struct _starpu_machine_topology *topology = &config->topology;
  106. hwloc_obj_snprintf(name, sizeof(name), topology->hwtopology, obj, "#", 0);
  107. _STARPU_DEBUG("Looking at %s\n", name);
  108. for (n = 0, i = 0; i < obj->arity; i++)
  109. if (obj->children[i]->userdata)
  110. /* it has a CPU worker */
  111. n++;
  112. if (n == 1)
  113. {
  114. /* If there is only one child, we go to the next level right away */
  115. find_and_assign_combinations(obj->children[0], min, max, synthesize_arity);
  116. return;
  117. }
  118. /* Add this object */
  119. nworkers = 0;
  120. find_workers(obj, cpu_workers, &nworkers);
  121. if (nworkers >= min && nworkers <= max)
  122. {
  123. _STARPU_DEBUG("Adding it\n");
  124. unsigned sched_ctx_id = starpu_sched_ctx_get_context();
  125. if(sched_ctx_id == STARPU_NMAX_SCHED_CTXS)
  126. sched_ctx_id = 0;
  127. struct starpu_worker_collection* workers = starpu_sched_ctx_get_worker_collection(sched_ctx_id);
  128. int newworkerid = starpu_combined_worker_assign_workerid(nworkers, cpu_workers);
  129. STARPU_ASSERT(newworkerid >= 0);
  130. workers->add(workers,newworkerid);
  131. }
  132. /* Add artificial intermediate objects recursively */
  133. synthesize_intermediate_workers(obj->children, min, max, obj->arity, n, synthesize_arity);
  134. /* And recurse */
  135. for (i = 0; i < obj->arity; i++)
  136. if (obj->children[i]->userdata == (void*) -1)
  137. find_and_assign_combinations(obj->children[i], min, max, synthesize_arity);
  138. }
  139. static void find_and_assign_combinations_with_hwloc(int *workerids, int nworkers)
  140. {
  141. struct _starpu_machine_config *config = _starpu_get_machine_config();
  142. struct _starpu_machine_topology *topology = &config->topology;
  143. int synthesize_arity = starpu_get_env_number("STARPU_SYNTHESIZE_ARITY_COMBINED_WORKER");
  144. int min = starpu_get_env_number("STARPU_MIN_WORKERSIZE");
  145. if (min < 2)
  146. min = 2;
  147. int max = starpu_get_env_number("STARPU_MAX_WORKERSIZE");
  148. if (max == -1)
  149. max = INT_MAX;
  150. if (synthesize_arity == -1)
  151. synthesize_arity = 2;
  152. /* First, mark nodes which contain CPU workers, simply by setting their userdata field */
  153. int i;
  154. for (i = 0; i < nworkers; i++)
  155. {
  156. struct _starpu_worker *worker = _starpu_get_worker_struct(workerids[i]);
  157. if (worker->perf_arch.devices[0].type == STARPU_CPU_WORKER && worker->perf_arch.devices[0].ncores == 1)
  158. {
  159. hwloc_obj_t obj = hwloc_get_obj_by_depth(topology->hwtopology, config->pu_depth, worker->bindid);
  160. obj = obj->parent;
  161. while (obj)
  162. {
  163. obj->userdata = (void*) -1;
  164. obj = obj->parent;
  165. }
  166. }
  167. }
  168. find_and_assign_combinations(hwloc_get_root_obj(topology->hwtopology), min, max, synthesize_arity);
  169. }
  170. #else /* STARPU_HAVE_HWLOC */
  171. static void assign_combinations_without_hwloc(struct starpu_worker_collection* worker_collection, int* workers, unsigned n, int min, int max)
  172. {
  173. int size,i,count =0;
  174. //if the maximun number of worker is already reached
  175. if(worker_collection->nworkers >= STARPU_NMAXWORKERS - 1)
  176. return;
  177. for (size = min; size <= max; size *= 2)
  178. {
  179. unsigned first;
  180. for (first = 0; first < n; first += size)
  181. {
  182. if (first + size <= n)
  183. {
  184. int found_workerids[size];
  185. for (i = 0; i < size; i++)
  186. found_workerids[i] = workers[first + i];
  187. /* We register this combination */
  188. int newworkerid;
  189. newworkerid = starpu_combined_worker_assign_workerid(size, found_workerids);
  190. STARPU_ASSERT(newworkerid >= 0);
  191. count++;
  192. worker_collection->add(worker_collection, newworkerid);
  193. //if the maximun number of worker is reached, then return
  194. if(worker_collection->nworkers >= STARPU_NMAXWORKERS - 1)
  195. return;
  196. }
  197. }
  198. }
  199. }
  200. static void find_and_assign_combinations_without_hwloc(int *workerids, int nworkers)
  201. {
  202. int i;
  203. unsigned sched_ctx_id = starpu_sched_ctx_get_context();
  204. if(sched_ctx_id == STARPU_NMAX_SCHED_CTXS)
  205. sched_ctx_id = 0;
  206. int min, max;
  207. #ifdef STARPU_USE_MIC
  208. unsigned j;
  209. int mic_min, mic_max;
  210. #endif
  211. struct starpu_worker_collection* workers = starpu_sched_ctx_get_worker_collection(sched_ctx_id);
  212. /* We put the id of all CPU workers in this array */
  213. int cpu_workers[STARPU_NMAXWORKERS];
  214. unsigned ncpus = 0;
  215. #ifdef STARPU_USE_MIC
  216. unsigned nb_mics = _starpu_get_machine_config()->topology.nmicdevices;
  217. unsigned * nmics_table;
  218. int * mic_id;
  219. int ** mic_workers;
  220. mic_id = malloc(sizeof(int)*nb_mics);
  221. nmics_table = malloc(sizeof(unsigned)*nb_mics);
  222. mic_workers = malloc(sizeof(int*)*nb_mics);
  223. for(j=0; j<nb_mics; j++)
  224. {
  225. mic_id[j] = -1;
  226. nmics_table[j] = 0;
  227. mic_workers[j] = malloc(sizeof(int)*STARPU_NMAXWORKERS);
  228. }
  229. #endif /* STARPU_USE_MIC */
  230. struct _starpu_worker *worker;
  231. for (i = 0; i < nworkers; i++)
  232. {
  233. worker = _starpu_get_worker_struct(workerids[i]);
  234. if (worker->arch == STARPU_CPU_WORKER)
  235. cpu_workers[ncpus++] = i;
  236. #ifdef STARPU_USE_MIC
  237. else if(worker->arch == STARPU_MIC_WORKER)
  238. {
  239. for(j=0; mic_id[j] != worker->devid && mic_id[j] != -1 && j<nb_mics; j++);
  240. if(j<nb_mics)
  241. {
  242. if(mic_id[j] == -1)
  243. {
  244. mic_id[j] = worker->devid;
  245. }
  246. mic_workers[j][nmics_table[j]++] = i;
  247. }
  248. }
  249. #endif /* STARPU_USE_MIC */
  250. }
  251. min = starpu_get_env_number("STARPU_MIN_WORKERSIZE");
  252. if (min < 2)
  253. min = 2;
  254. max = starpu_get_env_number("STARPU_MAX_WORKERSIZE");
  255. if (max == -1 || max > (int) ncpus)
  256. max = ncpus;
  257. assign_combinations_without_hwloc(workers,cpu_workers,ncpus,min,max);
  258. #ifdef STARPU_USE_MIC
  259. mic_min = starpu_get_env_number("STARPU_MIN_WORKERSIZE");
  260. if (mic_min < 2)
  261. mic_min = 2;
  262. for(j=0; j<nb_mics; j++)
  263. {
  264. mic_max = starpu_get_env_number("STARPU_MAX_WORKERSIZE");
  265. if (mic_max == -1 || mic_max > (int) nmics_table[j])
  266. mic_max = nmics_table[j];
  267. assign_combinations_without_hwloc(workers,mic_workers[j],nmics_table[j],mic_min,mic_max);
  268. free(mic_workers[j]);
  269. }
  270. free(mic_id);
  271. free(nmics_table);
  272. free(mic_workers);
  273. #endif /* STARPU_USE_MIC */
  274. }
  275. #endif /* STARPU_HAVE_HWLOC */
  276. static void combine_all_cpu_workers(int *workerids, int nworkers)
  277. {
  278. unsigned sched_ctx_id = starpu_sched_ctx_get_context();
  279. if(sched_ctx_id == STARPU_NMAX_SCHED_CTXS)
  280. sched_ctx_id = 0;
  281. struct starpu_worker_collection* workers = starpu_sched_ctx_get_worker_collection(sched_ctx_id);
  282. int cpu_workers[STARPU_NMAXWORKERS];
  283. int ncpus = 0;
  284. struct _starpu_worker *worker;
  285. int i;
  286. int min;
  287. int max;
  288. for (i = 0; i < nworkers; i++)
  289. {
  290. worker = _starpu_get_worker_struct(workerids[i]);
  291. if (worker->arch == STARPU_CPU_WORKER)
  292. cpu_workers[ncpus++] = workerids[i];
  293. }
  294. min = starpu_get_env_number("STARPU_MIN_WORKERSIZE");
  295. if (min < 1)
  296. min = 1;
  297. max = starpu_get_env_number("STARPU_MAX_WORKERSIZE");
  298. if (max == -1 || max > ncpus)
  299. max = ncpus;
  300. for (i = min; i <= max; i++)
  301. {
  302. int newworkerid;
  303. newworkerid = starpu_combined_worker_assign_workerid(i, cpu_workers);
  304. STARPU_ASSERT(newworkerid >= 0);
  305. workers->add(workers, newworkerid);
  306. }
  307. }
  308. void _starpu_sched_find_worker_combinations(int *workerids, int nworkers)
  309. {
  310. struct _starpu_machine_config *config = _starpu_get_machine_config();
  311. if (config->conf->single_combined_worker > 0)
  312. combine_all_cpu_workers(workerids, nworkers);
  313. else
  314. {
  315. #ifdef STARPU_HAVE_HWLOC
  316. find_and_assign_combinations_with_hwloc(workerids, nworkers);
  317. #else
  318. find_and_assign_combinations_without_hwloc(workerids, nworkers);
  319. #endif
  320. }
  321. }