detect_combined_workers.c 11 KB

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