detect_combined_workers.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2014 Université de Bordeaux 1
  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 == 0)
  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 == 0)
  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 j;
  204. unsigned sched_ctx_id = starpu_sched_ctx_get_context();
  205. if(sched_ctx_id == STARPU_NMAX_SCHED_CTXS)
  206. sched_ctx_id = 0;
  207. int min, max, mic_min, mic_max;
  208. struct starpu_worker_collection* workers = starpu_sched_ctx_get_worker_collection(sched_ctx_id);
  209. /* We put the id of all CPU workers in this array */
  210. int cpu_workers[STARPU_NMAXWORKERS];
  211. unsigned ncpus = 0;
  212. #ifdef STARPU_USE_MIC
  213. unsigned nb_mics = _starpu_get_machine_config()->topology.nmicdevices;
  214. unsigned * nmics_table;
  215. int * mic_id;
  216. int ** mic_workers;
  217. mic_id = malloc(sizeof(int)*nb_mics);
  218. nmics_table = malloc(sizeof(unsigned)*nb_mics);
  219. mic_workers = malloc(sizeof(int*)*nb_mics);
  220. for(j=0; j<nb_mics; j++)
  221. {
  222. mic_id[j] = -1;
  223. nmics_table[j] = 0;
  224. mic_workers[j] = malloc(sizeof(int)*STARPU_NMAXWORKERS);
  225. }
  226. #endif /* STARPU_USE_MIC */
  227. struct _starpu_worker *worker;
  228. for (i = 0; i < nworkers; i++)
  229. {
  230. worker = _starpu_get_worker_struct(workerids[i]);
  231. if (worker->arch == STARPU_CPU_WORKER)
  232. cpu_workers[ncpus++] = i;
  233. #ifdef STARPU_USE_MIC
  234. else if(worker->arch == STARPU_MIC_WORKER)
  235. {
  236. for(j=0; mic_id[j] != worker->devid && mic_id[j] != -1 && j<nb_mics; j++);
  237. if(j<nb_mics)
  238. {
  239. if(mic_id[j] == -1)
  240. {
  241. mic_id[j] = worker->devid;
  242. }
  243. mic_workers[j][nmics_table[j]++] = i;
  244. }
  245. }
  246. #endif /* STARPU_USE_MIC */
  247. }
  248. min = starpu_get_env_number("STARPU_MIN_WORKERSIZE");
  249. if (min < 2)
  250. min = 2;
  251. max = starpu_get_env_number("STARPU_MAX_WORKERSIZE");
  252. if (max == -1 || max > (int) ncpus)
  253. max = ncpus;
  254. assign_combinations_without_hwloc(workers,cpu_workers,ncpus,min,max);
  255. #ifdef STARPU_USE_MIC
  256. mic_min = starpu_get_env_number("STARPU_MIN_WORKERSIZE");
  257. if (mic_min < 2)
  258. mic_min = 2;
  259. for(j=0; j<nb_mics; j++)
  260. {
  261. mic_max = starpu_get_env_number("STARPU_MAX_WORKERSIZE");
  262. if (mic_max == -1 || mic_max > (int) nmics_table[j])
  263. mic_max = nmics_table[j];
  264. assign_combinations_without_hwloc(workers,mic_workers[j],nmics_table[j],mic_min,mic_max);
  265. free(mic_workers[j]);
  266. }
  267. free(mic_id);
  268. free(nmics_table);
  269. free(mic_workers);
  270. #endif /* STARPU_USE_MIC */
  271. }
  272. #endif /* STARPU_HAVE_HWLOC */
  273. static void combine_all_cpu_workers(int *workerids, int nworkers)
  274. {
  275. unsigned sched_ctx_id = starpu_sched_ctx_get_context();
  276. if(sched_ctx_id == STARPU_NMAX_SCHED_CTXS)
  277. sched_ctx_id = 0;
  278. struct starpu_worker_collection* workers = starpu_sched_ctx_get_worker_collection(sched_ctx_id);
  279. int cpu_workers[STARPU_NMAXWORKERS];
  280. int ncpus = 0;
  281. struct _starpu_worker *worker;
  282. int i;
  283. int min;
  284. int max;
  285. for (i = 0; i < nworkers; i++)
  286. {
  287. worker = _starpu_get_worker_struct(workerids[i]);
  288. if (worker->arch == STARPU_CPU_WORKER)
  289. cpu_workers[ncpus++] = workerids[i];
  290. }
  291. min = starpu_get_env_number("STARPU_MIN_WORKERSIZE");
  292. if (min < 1)
  293. min = 1;
  294. max = starpu_get_env_number("STARPU_MAX_WORKERSIZE");
  295. if (max == -1 || max > ncpus)
  296. max = ncpus;
  297. for (i = min; i <= max; i++)
  298. {
  299. int newworkerid;
  300. newworkerid = starpu_combined_worker_assign_workerid(i, cpu_workers);
  301. STARPU_ASSERT(newworkerid >= 0);
  302. workers->add(workers, newworkerid);
  303. }
  304. }
  305. void _starpu_sched_find_worker_combinations(int *workerids, int nworkers)
  306. {
  307. struct _starpu_machine_config *config = _starpu_get_machine_config();
  308. if (config->conf->single_combined_worker > 0)
  309. combine_all_cpu_workers(workerids, nworkers);
  310. else
  311. {
  312. #ifdef STARPU_HAVE_HWLOC
  313. find_and_assign_combinations_with_hwloc(workerids, nworkers);
  314. #else
  315. find_and_assign_combinations_without_hwloc(workerids, nworkers);
  316. #endif
  317. }
  318. }