starpu_clusters_create.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2015-2021 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
  4. *
  5. * StarPU is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation; either version 2.1 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * StarPU is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. *
  14. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  15. */
  16. /* This file creates an interface to manage clustering resources and make use
  17. * of parallel tasks. It entirely depends on the hwloc software. */
  18. #include <util/starpu_clusters_create.h>
  19. #ifdef STARPU_CLUSTER
  20. starpu_binding_function _starpu_cluster_type_get_func(enum starpu_cluster_types type)
  21. {
  22. starpu_binding_function prologue_func;
  23. switch (type)
  24. {
  25. case STARPU_CLUSTER_OPENMP:
  26. prologue_func = &starpu_openmp_prologue;
  27. break;
  28. case STARPU_CLUSTER_INTEL_OPENMP_MKL:
  29. prologue_func = &starpu_intel_openmp_mkl_prologue;
  30. break;
  31. #ifdef STARPU_MKL
  32. case STARPU_CLUSTER_GNU_OPENMP_MKL:
  33. prologue_func = &starpu_gnu_openmp_mkl_prologue;
  34. break;
  35. #endif
  36. default:
  37. prologue_func = NULL;
  38. }
  39. return prologue_func;
  40. }
  41. void starpu_openmp_prologue(void* arg)
  42. {
  43. (void) arg;
  44. int workerid = starpu_worker_get_id_check();
  45. if (starpu_worker_get_type(workerid) == STARPU_CPU_WORKER)
  46. {
  47. struct starpu_task *task = starpu_task_get_current();
  48. int sched_ctx = task->sched_ctx;
  49. struct _starpu_sched_ctx *ctx_struct = _starpu_get_sched_ctx_struct(sched_ctx);
  50. /* If the view of the worker doesn't correspond to the view of the task,
  51. adapt the thread team */
  52. if (ctx_struct->parallel_view != task->possibly_parallel)
  53. {
  54. int *cpuids = NULL;
  55. int ncpuids = 0;
  56. starpu_sched_ctx_get_available_cpuids(sched_ctx, &cpuids, &ncpuids);
  57. if (!task->possibly_parallel)
  58. ncpuids=1;
  59. omp_set_num_threads(ncpuids);
  60. #pragma omp parallel
  61. {
  62. starpu_sched_ctx_bind_current_thread_to_cpuid(cpuids[omp_get_thread_num()]);
  63. }
  64. free(cpuids);
  65. ctx_struct->parallel_view = !ctx_struct->parallel_view;
  66. }
  67. }
  68. return;
  69. }
  70. #ifdef STARPU_MKL
  71. void starpu_gnu_openmp_mkl_prologue(void* arg)
  72. {
  73. int workerid = starpu_worker_get_id();
  74. if (starpu_worker_get_type(workerid) == STARPU_CPU_WORKER)
  75. {
  76. struct starpu_task *task = starpu_task_get_current();
  77. int sched_ctx = task->sched_ctx;
  78. struct _starpu_sched_ctx *ctx_struct = _starpu_get_sched_ctx_struct(sched_ctx);
  79. /* If the view of the worker doesn't correspond to the view of the task,
  80. adapt the thread team */
  81. if (ctx_struct->parallel_view != task->possibly_parallel)
  82. {
  83. int *cpuids = NULL;
  84. int ncpuids = 0;
  85. starpu_sched_ctx_get_available_cpuids(sched_ctx, &cpuids, &ncpuids);
  86. if (!task->possibly_parallel)
  87. ncpuids=1;
  88. omp_set_num_threads(ncpuids);
  89. mkl_set_num_threads(ncpuids);
  90. mkl_set_dynamic(0);
  91. #pragma omp parallel
  92. {
  93. starpu_sched_ctx_bind_current_thread_to_cpuid(cpuids[omp_get_thread_num()]);
  94. }
  95. free(cpuids);
  96. ctx_struct->parallel_view = !ctx_struct->parallel_view;
  97. }
  98. }
  99. return;
  100. }
  101. #endif
  102. /* Main interface function to create a cluster view of the machine.
  103. * Its job is to capture what the user wants and store it in a standard view. */
  104. struct starpu_cluster_machine *starpu_cluster_machine(hwloc_obj_type_t cluster_level, ...)
  105. {
  106. va_list varg_list;
  107. int arg_type;
  108. struct _starpu_cluster_parameters *params;
  109. struct starpu_cluster_machine *machine;
  110. _STARPU_CALLOC(machine, 1, sizeof(struct starpu_cluster_machine));
  111. _STARPU_CALLOC(machine->params, 1, sizeof(struct _starpu_cluster_parameters));
  112. machine->id = STARPU_NMAX_SCHED_CTXS;
  113. machine->groups = _starpu_cluster_group_list_new();
  114. machine->nclusters = 0;
  115. machine->ngroups = 0;
  116. machine->topology = NULL;
  117. _starpu_cluster_init_parameters(machine->params);
  118. params = machine->params;
  119. va_start(varg_list, cluster_level);
  120. while ((arg_type = va_arg(varg_list, int)) != 0)
  121. {
  122. if (arg_type == STARPU_CLUSTER_MIN_NB)
  123. {
  124. params->min_nb = va_arg(varg_list, int);
  125. if (params->min_nb <= 0)
  126. _STARPU_DISP("Caution min number of contexts shouldn't be negative or null\n");
  127. }
  128. else if (arg_type == STARPU_CLUSTER_MAX_NB)
  129. {
  130. params->max_nb = va_arg(varg_list, int);
  131. if (params->max_nb <= 0)
  132. _STARPU_DISP("Caution max number of contexts shouldn't be negative or null\n");
  133. }
  134. else if (arg_type == STARPU_CLUSTER_NB)
  135. {
  136. params->nb = va_arg(varg_list, int);
  137. if (params->nb <= 0)
  138. _STARPU_DISP("Caution number of contexts shouldn't be negative or null\n");
  139. }
  140. else if (arg_type == STARPU_CLUSTER_POLICY_NAME)
  141. {
  142. params->sched_policy_name = va_arg(varg_list, char*);
  143. }
  144. else if (arg_type == STARPU_CLUSTER_POLICY_STRUCT)
  145. {
  146. params->sched_policy_struct = va_arg(varg_list,
  147. struct starpu_sched_policy*);
  148. }
  149. else if (arg_type == STARPU_CLUSTER_KEEP_HOMOGENEOUS)
  150. {
  151. params->keep_homogeneous = va_arg(varg_list, int); /* 0=off, other=on */
  152. }
  153. else if (arg_type == STARPU_CLUSTER_PREFERE_MIN)
  154. {
  155. params->prefere_min = va_arg(varg_list, int); /* 0=off, other=on */
  156. }
  157. else if (arg_type == STARPU_CLUSTER_CREATE_FUNC)
  158. {
  159. params->create_func = va_arg(varg_list, void (*)(void*));
  160. }
  161. else if (arg_type == STARPU_CLUSTER_CREATE_FUNC_ARG)
  162. {
  163. params->create_func_arg = va_arg(varg_list, void*);
  164. }
  165. else if (arg_type == STARPU_CLUSTER_TYPE)
  166. {
  167. params->type = va_arg(varg_list, enum starpu_cluster_types);
  168. }
  169. else if (arg_type == STARPU_CLUSTER_AWAKE_WORKERS)
  170. {
  171. params->awake_workers = va_arg(varg_list, unsigned);
  172. }
  173. else if (arg_type == STARPU_CLUSTER_PARTITION_ONE)
  174. {
  175. struct _starpu_cluster_group *group = _starpu_cluster_group_new();
  176. _starpu_cluster_group_init(group, machine);
  177. _starpu_cluster_group_list_push_back(machine->groups, group);
  178. params = group->params;
  179. }
  180. else if (arg_type == STARPU_CLUSTER_NEW)
  181. {
  182. struct _starpu_cluster *cluster = _starpu_cluster_new();
  183. struct _starpu_cluster_group *group = _starpu_cluster_group_list_back(machine->groups);
  184. if (group == NULL)
  185. {
  186. group = _starpu_cluster_group_new();
  187. _starpu_cluster_group_init(group, machine);
  188. _starpu_cluster_group_list_push_back(machine->groups, group);
  189. }
  190. _starpu_cluster_init(cluster, group);
  191. _starpu_cluster_list_push_back(group->clusters, cluster);
  192. params = cluster->params;
  193. }
  194. else if (arg_type == STARPU_CLUSTER_NCORES)
  195. {
  196. struct _starpu_cluster_group *group = _starpu_cluster_group_list_back(machine->groups);
  197. if (group == NULL)
  198. {
  199. group = _starpu_cluster_group_new();
  200. _starpu_cluster_group_init(group, machine);
  201. _starpu_cluster_group_list_push_back(machine->groups, group);
  202. }
  203. struct _starpu_cluster *cluster =_starpu_cluster_list_back(group->clusters);
  204. cluster->ncores = va_arg(varg_list, unsigned);
  205. }
  206. else
  207. {
  208. STARPU_ABORT_MSG("Unrecognized argument %d\n", arg_type);
  209. }
  210. }
  211. va_end(varg_list);
  212. switch(cluster_level)
  213. {
  214. case HWLOC_OBJ_MISC:
  215. case HWLOC_OBJ_BRIDGE:
  216. case HWLOC_OBJ_PCI_DEVICE:
  217. case HWLOC_OBJ_OS_DEVICE:
  218. STARPU_ABORT_MSG("Cluster aggregation isn't supported for level %s\n",
  219. hwloc_obj_type_string(cluster_level));
  220. break;
  221. default: /* others can pass */
  222. break;
  223. }
  224. if (_starpu_cluster_machine(cluster_level, machine) == -ENODEV)
  225. {
  226. starpu_uncluster_machine(machine);
  227. machine = NULL;
  228. }
  229. return machine;
  230. }
  231. int starpu_uncluster_machine(struct starpu_cluster_machine *machine)
  232. {
  233. if (machine == NULL)
  234. return -1;
  235. struct _starpu_cluster_group *g;
  236. struct _starpu_cluster_group_list *group_list = machine->groups;
  237. if (machine->id != STARPU_NMAX_SCHED_CTXS)
  238. starpu_sched_ctx_delete(machine->id);
  239. g = _starpu_cluster_group_list_begin(group_list);
  240. while (g != _starpu_cluster_group_list_end(group_list))
  241. {
  242. struct _starpu_cluster_group *tmp = g;
  243. g = _starpu_cluster_group_list_next(g);
  244. _starpu_cluster_group_remove(group_list, tmp);
  245. }
  246. _starpu_cluster_group_list_delete(group_list);
  247. if (machine->topology != NULL)
  248. hwloc_topology_destroy(machine->topology);
  249. free(machine->params);
  250. free(machine);
  251. starpu_sched_ctx_set_context(0);
  252. return 0;
  253. }
  254. int starpu_cluster_print(struct starpu_cluster_machine *clusters)
  255. {
  256. if (clusters == NULL)
  257. return -1;
  258. int cnt, w;
  259. struct _starpu_cluster_group *group;
  260. struct _starpu_cluster *cluster;
  261. printf("Number of clusters created: %u\n", clusters->nclusters);
  262. cnt=0;
  263. if (clusters->nclusters)
  264. {
  265. for (group = _starpu_cluster_group_list_begin(clusters->groups);
  266. group != _starpu_cluster_group_list_end(clusters->groups);
  267. group = _starpu_cluster_group_list_next(group))
  268. {
  269. for (cluster = _starpu_cluster_list_begin(group->clusters);
  270. cluster != _starpu_cluster_list_end(group->clusters);
  271. cluster = _starpu_cluster_list_next(cluster))
  272. {
  273. printf("Cluster %d contains the following logical indexes:\n\t", cnt);
  274. for (w=0; w < cluster->ncores; w++)
  275. printf("%d ", cluster->cores[w]);
  276. printf("\n");
  277. cnt++;
  278. }
  279. }
  280. }
  281. return 0;
  282. }
  283. void _starpu_cluster_create(struct _starpu_cluster *cluster)
  284. {
  285. if (cluster->params->awake_workers)
  286. cluster->id = starpu_sched_ctx_create(cluster->workerids, cluster->ncores,
  287. "clusters",
  288. STARPU_SCHED_CTX_AWAKE_WORKERS, 0);
  289. else
  290. cluster->id = starpu_sched_ctx_create(cluster->workerids, cluster->ncores,
  291. "clusters", 0);
  292. /* cluster priority can be the lowest, so let's enforce it */
  293. starpu_sched_ctx_set_priority(cluster->workerids, cluster->ncores, cluster->id, 0);
  294. return;
  295. }
  296. void _starpu_cluster_group_create(struct _starpu_cluster_group *group)
  297. {
  298. struct _starpu_cluster *c;
  299. for (c = _starpu_cluster_list_begin(group->clusters) ;
  300. c != _starpu_cluster_list_end(group->clusters) ;
  301. c = _starpu_cluster_list_next(c))
  302. {
  303. if (c->ncores == 0)
  304. continue;
  305. _starpu_cluster_create(c);
  306. if (!c->params->awake_workers)
  307. _starpu_cluster_bind(c);
  308. }
  309. return;
  310. }
  311. void _starpu_clusters_set_nesting(struct starpu_cluster_machine *m)
  312. {
  313. struct _starpu_cluster_group *g;
  314. struct _starpu_cluster *c;
  315. for (g = _starpu_cluster_group_list_begin(m->groups) ;
  316. g != _starpu_cluster_group_list_end(m->groups) ;
  317. g = _starpu_cluster_group_list_next(g))
  318. {
  319. for (c = _starpu_cluster_list_begin(g->clusters) ;
  320. c != _starpu_cluster_list_end(g->clusters) ;
  321. c = _starpu_cluster_list_next(c))
  322. _starpu_get_sched_ctx_struct(c->id)->nesting_sched_ctx = m->id;
  323. }
  324. }
  325. int _starpu_cluster_bind(struct _starpu_cluster *cluster)
  326. {
  327. starpu_binding_function func;
  328. void *func_arg;
  329. if (cluster->params->create_func)
  330. {
  331. func = cluster->params->create_func;
  332. func_arg = (void*) cluster->params->create_func_arg;
  333. }
  334. else
  335. {
  336. func = _starpu_cluster_type_get_func(cluster->params->type);
  337. func_arg = NULL;
  338. }
  339. return starpu_task_insert(&_starpu_cluster_bind_cl,
  340. STARPU_SCHED_CTX, cluster->id,
  341. STARPU_POSSIBLY_PARALLEL, 1,
  342. STARPU_PROLOGUE_CALLBACK_POP, func,
  343. STARPU_PROLOGUE_CALLBACK_POP_ARG, func_arg,
  344. 0);
  345. }
  346. void _starpu_cluster_group_init(struct _starpu_cluster_group *group,
  347. struct starpu_cluster_machine *father)
  348. {
  349. group->id = 0;
  350. group->nclusters = 0;
  351. group->clusters = _starpu_cluster_list_new();
  352. group->father = father;
  353. _STARPU_MALLOC(group->params, sizeof(struct _starpu_cluster_parameters));
  354. _starpu_cluster_copy_parameters(father->params, group->params);
  355. return;
  356. }
  357. void _starpu_cluster_init(struct _starpu_cluster *cluster,
  358. struct _starpu_cluster_group *father)
  359. {
  360. cluster->id = STARPU_NMAX_SCHED_CTXS;
  361. cluster->cpuset = hwloc_bitmap_alloc();
  362. cluster->ncores = 0;
  363. cluster->cores = NULL;
  364. cluster->workerids = NULL;
  365. cluster->father = father;
  366. _STARPU_MALLOC(cluster->params, sizeof(struct _starpu_cluster_parameters));
  367. _starpu_cluster_copy_parameters(father->params, cluster->params);
  368. }
  369. int _starpu_cluster_remove(struct _starpu_cluster_list *cluster_list,
  370. struct _starpu_cluster *cluster)
  371. {
  372. if (cluster && cluster->id != STARPU_NMAX_SCHED_CTXS)
  373. starpu_sched_ctx_delete(cluster->id);
  374. else
  375. return -1;
  376. if (cluster->cores != NULL)
  377. free(cluster->cores);
  378. if (cluster->workerids != NULL)
  379. free(cluster->workerids);
  380. hwloc_bitmap_free(cluster->cpuset);
  381. free(cluster->params);
  382. _starpu_cluster_list_erase(cluster_list, cluster);
  383. _starpu_cluster_delete(cluster);
  384. return 0;
  385. }
  386. int _starpu_cluster_group_remove(struct _starpu_cluster_group_list *group_list,
  387. struct _starpu_cluster_group *group)
  388. {
  389. struct _starpu_cluster_list *cluster_list = group->clusters;
  390. struct _starpu_cluster *c = _starpu_cluster_list_begin(cluster_list);
  391. while (c != _starpu_cluster_list_end(cluster_list))
  392. {
  393. struct _starpu_cluster *tmp = c;
  394. c = _starpu_cluster_list_next(c);
  395. _starpu_cluster_remove(cluster_list, tmp);
  396. }
  397. _starpu_cluster_list_delete(cluster_list);
  398. free(group->params);
  399. _starpu_cluster_group_list_erase(group_list, group);
  400. _starpu_cluster_group_delete(group);
  401. return 0;
  402. }
  403. void _starpu_cluster_init_parameters(struct _starpu_cluster_parameters *params)
  404. {
  405. params->min_nb = 0;
  406. params->max_nb = 0;
  407. params->nb = 0;
  408. params->sched_policy_name = NULL;
  409. params->sched_policy_struct = NULL;
  410. params->keep_homogeneous = 0;
  411. params->prefere_min = 0;
  412. params->create_func = NULL;
  413. params->create_func_arg = NULL;
  414. params->type = STARPU_CLUSTER_OPENMP;
  415. params->awake_workers = 0;
  416. return;
  417. }
  418. void _starpu_cluster_copy_parameters(struct _starpu_cluster_parameters *src, struct _starpu_cluster_parameters *dst)
  419. {
  420. dst->min_nb = src->min_nb;
  421. dst->max_nb = src->max_nb;
  422. dst->nb = src->nb;
  423. dst->sched_policy_name = src->sched_policy_name;
  424. dst->sched_policy_struct = src->sched_policy_struct;
  425. dst->keep_homogeneous = src->keep_homogeneous;
  426. dst->prefere_min = src->prefere_min;
  427. dst->create_func = src->create_func;
  428. dst->create_func_arg = src->create_func_arg;
  429. dst->type = src->type;
  430. dst->awake_workers = src->awake_workers;
  431. return;
  432. }
  433. /* Considering the resources and parameters, how many clusters should we take? */
  434. int _starpu_cluster_analyze_parameters(struct _starpu_cluster_parameters *params, int npus)
  435. {
  436. int nb_clusters = 1, j;
  437. if (params->nb)
  438. {
  439. nb_clusters = params->nb <= npus?params->nb : npus;
  440. }
  441. else if (params->min_nb && params->max_nb)
  442. {
  443. if (!params->keep_homogeneous)
  444. {
  445. if (params->prefere_min)
  446. nb_clusters = params->min_nb <= npus? params->min_nb : npus;
  447. else
  448. nb_clusters = params->max_nb <= npus? params->max_nb : npus;
  449. }
  450. else
  451. {
  452. int begin = params->prefere_min? params->min_nb:params->max_nb;
  453. int end = params->prefere_min? params->max_nb+1:params->min_nb-1;
  454. j=begin;
  455. int best = 0, second_best = 0, cpu_loss = INT_MAX;
  456. while (j != end)
  457. {
  458. if (npus%j == 0)
  459. {
  460. best = j;
  461. break;
  462. }
  463. if (npus%j < cpu_loss)
  464. {
  465. cpu_loss = npus%j;
  466. second_best = j;
  467. }
  468. j = params->prefere_min? j+1:j-1;
  469. }
  470. if (best)
  471. nb_clusters = best;
  472. else if (second_best)
  473. nb_clusters = second_best;
  474. }
  475. }
  476. return nb_clusters;
  477. }
  478. int _starpu_cluster_machine(hwloc_obj_type_t cluster_level,
  479. struct starpu_cluster_machine *machine)
  480. {
  481. struct _starpu_cluster_group *g;
  482. int ret;
  483. ret = _starpu_cluster_topology(cluster_level, machine);
  484. if (ret)
  485. return ret;
  486. for (g = _starpu_cluster_group_list_begin(machine->groups) ;
  487. g != _starpu_cluster_group_list_end(machine->groups) ;
  488. g = _starpu_cluster_group_list_next(g))
  489. _starpu_cluster_group_create(g);
  490. starpu_task_wait_for_all();
  491. /* Create containing context */
  492. if (machine->params->sched_policy_struct != NULL)
  493. {
  494. machine->id = starpu_sched_ctx_create(NULL, -1, "main sched ctx",
  495. STARPU_SCHED_CTX_POLICY_STRUCT,
  496. machine->params->sched_policy_struct,
  497. 0);
  498. }
  499. else if (machine->params->sched_policy_name != NULL)
  500. {
  501. machine->id = starpu_sched_ctx_create(NULL, -1, "main sched ctx",
  502. STARPU_SCHED_CTX_POLICY_NAME,
  503. machine->params->sched_policy_name,
  504. 0);
  505. }
  506. else
  507. {
  508. struct starpu_sched_policy *sched_policy;
  509. struct _starpu_sched_ctx *global_ctx =_starpu_get_sched_ctx_struct(STARPU_GLOBAL_SCHED_CTX);
  510. sched_policy = _starpu_get_sched_policy(global_ctx);
  511. machine->id = starpu_sched_ctx_create(NULL, -1, "main sched ctx",
  512. STARPU_SCHED_CTX_POLICY_STRUCT,
  513. sched_policy, 0);
  514. }
  515. _starpu_clusters_set_nesting(machine);
  516. starpu_sched_ctx_set_context(&machine->id);
  517. return ret;
  518. }
  519. int _starpu_cluster_topology(hwloc_obj_type_t cluster_level,
  520. struct starpu_cluster_machine *machine)
  521. {
  522. int w;
  523. hwloc_topology_t topology;
  524. hwloc_cpuset_t avail_cpus;
  525. int nworkers = starpu_worker_get_count_by_type(STARPU_CPU_WORKER);
  526. if (nworkers == 0)
  527. return -ENODEV;
  528. int *workers;
  529. _STARPU_MALLOC(workers, sizeof(int) * nworkers);
  530. starpu_worker_get_ids_by_type(STARPU_CPU_WORKER, workers, nworkers);
  531. struct _starpu_machine_config *config = _starpu_get_machine_config();
  532. STARPU_ASSERT_MSG(config->topology.hwtopology != NULL, "STARPU_CLUSTER: You "
  533. "need to call starpu_init() or make sure to activate hwloc.");
  534. hwloc_topology_dup(&topology, config->topology.hwtopology);
  535. avail_cpus = hwloc_bitmap_alloc();
  536. hwloc_bitmap_zero(avail_cpus);
  537. for (w = 0; w < nworkers ; w++)
  538. {
  539. struct _starpu_worker *worker_str = _starpu_get_worker_struct(workers[w]);
  540. hwloc_bitmap_or(avail_cpus, avail_cpus, worker_str->hwloc_cpu_set);
  541. }
  542. hwloc_topology_restrict(topology, avail_cpus, 0);
  543. free(workers);
  544. /* Use new topology to fill in the cluster list */
  545. machine->topology = topology;
  546. _starpu_cluster_group(cluster_level, machine);
  547. hwloc_bitmap_free(avail_cpus);
  548. return 0;
  549. }
  550. void _starpu_cluster_group(hwloc_obj_type_t cluster_level,
  551. struct starpu_cluster_machine *machine)
  552. {
  553. int nb_objects;
  554. int i;
  555. struct _starpu_cluster_group *group = NULL;
  556. if (machine->groups == NULL)
  557. machine->groups = _starpu_cluster_group_list_new();
  558. nb_objects = hwloc_get_nbobjs_by_type(machine->topology, cluster_level);
  559. if (nb_objects <= 0)
  560. return;
  561. /* XXX: handle nb_objects == -1 */
  562. group = _starpu_cluster_group_list_begin(machine->groups);
  563. for (i = 0 ; i < nb_objects ; i++)
  564. {
  565. hwloc_obj_t cluster_obj = hwloc_get_obj_by_type(machine->topology, cluster_level, i);
  566. if (group == NULL)
  567. {
  568. group = _starpu_cluster_group_new();
  569. _starpu_cluster_group_init(group, machine);
  570. _starpu_cluster_group_list_push_back(machine->groups, group);
  571. }
  572. group->group_obj = cluster_obj;
  573. _starpu_cluster(group);
  574. machine->ngroups++;
  575. machine->nclusters += group->nclusters;
  576. group = _starpu_cluster_group_list_next(group);
  577. }
  578. return;
  579. }
  580. void _starpu_cluster(struct _starpu_cluster_group *group)
  581. {
  582. int i, avail_pus, npus, npreset=0;
  583. struct _starpu_cluster *cluster;
  584. npus = hwloc_get_nbobjs_inside_cpuset_by_type(group->father->topology,
  585. group->group_obj->cpuset,
  586. HWLOC_OBJ_PU);
  587. /* Preset clusters */
  588. avail_pus = npus;
  589. for (cluster=_starpu_cluster_list_begin(group->clusters);
  590. cluster!=_starpu_cluster_list_end(group->clusters);
  591. cluster=_starpu_cluster_list_next(cluster))
  592. {
  593. if (cluster->ncores > avail_pus)
  594. cluster->ncores = avail_pus;
  595. else if (avail_pus == 0)
  596. cluster->ncores = 0;
  597. if (cluster->ncores > 0)
  598. {
  599. _STARPU_MALLOC(cluster->cores, sizeof(int)*cluster->ncores);
  600. _STARPU_MALLOC(cluster->workerids, sizeof(int)*cluster->ncores);
  601. avail_pus -= cluster->ncores;
  602. npreset++;
  603. }
  604. }
  605. /* Automatic clusters */
  606. group->nclusters = _starpu_cluster_analyze_parameters(group->params, avail_pus);
  607. for (i=0 ; i<group->nclusters && avail_pus>0 ; i++)
  608. {
  609. if (cluster == NULL)
  610. {
  611. cluster = _starpu_cluster_new();
  612. _starpu_cluster_init(cluster, group);
  613. _starpu_cluster_list_push_back(group->clusters, cluster);
  614. }
  615. if (cluster->ncores != 0 && cluster->ncores > avail_pus)
  616. {
  617. cluster->ncores = avail_pus;
  618. }
  619. else
  620. {
  621. if (cluster->params->keep_homogeneous)
  622. cluster->ncores = avail_pus/(group->nclusters-i);
  623. else
  624. cluster->ncores = i==group->nclusters-1?
  625. avail_pus:
  626. avail_pus/(group->nclusters-i);
  627. }
  628. avail_pus -= cluster->ncores;
  629. _STARPU_MALLOC(cluster->cores, sizeof(int)*cluster->ncores);
  630. _STARPU_MALLOC(cluster->workerids, sizeof(int)*cluster->ncores);
  631. cluster = _starpu_cluster_list_next(cluster);
  632. }
  633. group->nclusters += npreset;
  634. cluster = _starpu_cluster_list_begin(group->clusters);
  635. int count = 0;
  636. static int starpu_cluster_warned = 0;
  637. for (i=0 ; i<npus ; i++)
  638. {
  639. hwloc_obj_t pu = hwloc_get_obj_inside_cpuset_by_type(group->father->topology,
  640. group->group_obj->cpuset,
  641. HWLOC_OBJ_PU, i);
  642. /* If we have more than one worker on this resource, let's add them too --
  643. even if it's bad (they'll all be boud on the same PU) */
  644. int size = 0, j;
  645. struct _starpu_hwloc_userdata *data = pu->userdata;
  646. struct _starpu_worker_list *list = data->worker_list;
  647. struct _starpu_worker *worker_str;
  648. for (worker_str = _starpu_worker_list_begin(list);
  649. worker_str != _starpu_worker_list_end(list);
  650. worker_str = _starpu_worker_list_next(worker_str))
  651. {
  652. if (worker_str->arch == STARPU_CPU_WORKER)
  653. size++;
  654. }
  655. if (size > 1)
  656. {
  657. STARPU_HG_DISABLE_CHECKING(starpu_cluster_warned);
  658. if (!starpu_cluster_warned)
  659. {
  660. _STARPU_DISP("STARPU CLUSTERS: Caution! It seems that you have"
  661. " multiple workers bound to the same PU. If you have"
  662. " multithreading on your cores it is greatly adviced"
  663. " to export STARPU_NTHREADS_PER_CORE=nb.");
  664. starpu_cluster_warned = 1;
  665. }
  666. cluster->ncores += size-1;
  667. _STARPU_REALLOC(cluster->cores, sizeof(int)*cluster->ncores);
  668. _STARPU_REALLOC(cluster->workerids, sizeof(int)*cluster->ncores);
  669. }
  670. /* grab workerid list and return first cpu */
  671. worker_str = _starpu_worker_list_begin(list);
  672. if (worker_str)
  673. hwloc_bitmap_or(cluster->cpuset, cluster->cpuset,
  674. worker_str->hwloc_cpu_set);
  675. j = 0;
  676. while (worker_str != _starpu_worker_list_end(list))
  677. {
  678. if (worker_str->arch == STARPU_CPU_WORKER)
  679. {
  680. cluster->cores[count+j] = worker_str->bindid;
  681. cluster->workerids[count+j] = worker_str->workerid;
  682. j++;
  683. }
  684. worker_str = _starpu_worker_list_next(worker_str);
  685. }
  686. count+=size;
  687. if (cluster->ncores == count)
  688. {
  689. count = 0;
  690. cluster = _starpu_cluster_list_next(cluster);
  691. }
  692. }
  693. return;
  694. }
  695. #endif