topology.c 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-2016 Université de Bordeaux
  4. * Copyright (C) 2010, 2011, 2012, 2013, 2014, 2015 CNRS
  5. * Copyright (C) 2011 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 <stdlib.h>
  19. #include <stdio.h>
  20. #include <common/config.h>
  21. #include <core/workers.h>
  22. #include <core/debug.h>
  23. #include <core/topology.h>
  24. #include <drivers/cuda/driver_cuda.h>
  25. #include <drivers/mic/driver_mic_source.h>
  26. #include <drivers/scc/driver_scc_source.h>
  27. #include <drivers/mp_common/source_common.h>
  28. #include <drivers/opencl/driver_opencl.h>
  29. #include <profiling/profiling.h>
  30. #include <datawizard/datastats.h>
  31. #include <datawizard/memory_nodes.h>
  32. #include <common/uthash.h>
  33. #ifdef STARPU_HAVE_HWLOC
  34. #include <hwloc.h>
  35. #ifndef HWLOC_API_VERSION
  36. #define HWLOC_OBJ_PU HWLOC_OBJ_PROC
  37. #endif
  38. #endif
  39. #ifdef STARPU_HAVE_WINDOWS
  40. #include <windows.h>
  41. #endif
  42. #ifdef STARPU_SIMGRID
  43. #include <core/simgrid.h>
  44. #endif
  45. static unsigned topology_is_initialized = 0;
  46. static int nobind;
  47. /* For checking whether two workers share the same PU, indexed by PU number */
  48. static int cpu_worker[STARPU_MAXCPUS];
  49. #if defined(STARPU_USE_CUDA) || defined(STARPU_USE_OPENCL) || defined(STARPU_USE_SCC) || defined(STARPU_SIMGRID)
  50. struct handle_entry
  51. {
  52. UT_hash_handle hh;
  53. unsigned gpuid;
  54. };
  55. # if defined(STARPU_USE_CUDA) || defined(STARPU_SIMGRID)
  56. /* Entry in the `devices_using_cuda' hash table. */
  57. static struct handle_entry *devices_using_cuda;
  58. # endif
  59. static unsigned may_bind_automatically[STARPU_NARCH] = { 0 };
  60. #endif // defined(STARPU_USE_CUDA) || defined(STARPU_USE_OPENCL)
  61. #if defined(STARPU_USE_CUDA) || defined(STARPU_SIMGRID)
  62. static struct _starpu_worker_set cuda_worker_set[STARPU_MAXCUDADEVS];
  63. #endif
  64. #ifdef STARPU_USE_MIC
  65. static struct _starpu_worker_set mic_worker_set[STARPU_MAXMICDEVS];
  66. #endif
  67. void *
  68. _starpu_get_worker_from_driver(struct starpu_driver *d)
  69. {
  70. unsigned nworkers = starpu_worker_get_count();
  71. unsigned workerid;
  72. #ifdef STARPU_USE_CUDA
  73. if (d->type == STARPU_CUDA_WORKER)
  74. return &cuda_worker_set[d->id.cuda_id];
  75. #endif
  76. for (workerid = 0; workerid < nworkers; workerid++)
  77. {
  78. if (starpu_worker_get_type(workerid) == d->type)
  79. {
  80. struct _starpu_worker *worker;
  81. worker = _starpu_get_worker_struct(workerid);
  82. switch (d->type)
  83. {
  84. #ifdef STARPU_USE_CPU
  85. case STARPU_CPU_WORKER:
  86. if (worker->devid == d->id.cpu_id)
  87. return worker;
  88. break;
  89. #endif
  90. #ifdef STARPU_USE_OPENCL
  91. case STARPU_OPENCL_WORKER:
  92. {
  93. cl_device_id device;
  94. starpu_opencl_get_device(worker->devid, &device);
  95. if (device == d->id.opencl_id)
  96. return worker;
  97. break;
  98. }
  99. #endif
  100. default:
  101. _STARPU_DEBUG("Invalid device type\n");
  102. return NULL;
  103. }
  104. }
  105. }
  106. return NULL;
  107. }
  108. /*
  109. * Discover the topology of the machine
  110. */
  111. #if defined(STARPU_USE_CUDA) || defined(STARPU_USE_OPENCL) || defined(STARPU_USE_SCC) || defined(STARPU_SIMGRID)
  112. static void
  113. _starpu_initialize_workers_deviceid (int *explicit_workers_gpuid,
  114. int *current, int *workers_gpuid,
  115. const char *varname, unsigned nhwgpus,
  116. enum starpu_worker_archtype type)
  117. {
  118. char *strval;
  119. unsigned i;
  120. *current = 0;
  121. /* conf->workers_gpuid indicates the successive GPU identifier that
  122. * should be used to bind the workers. It should be either filled
  123. * according to the user's explicit parameters (from starpu_conf) or
  124. * according to the STARPU_WORKERS_CUDAID env. variable. Otherwise, a
  125. * round-robin policy is used to distributed the workers over the
  126. * cores. */
  127. /* what do we use, explicit value, env. variable, or round-robin ? */
  128. if ((strval = starpu_getenv(varname)))
  129. {
  130. /* STARPU_WORKERS_CUDAID certainly contains less entries than
  131. * STARPU_NMAXWORKERS, so we reuse its entries in a round
  132. * robin fashion: "1 2" is equivalent to "1 2 1 2 1 2 .... 1
  133. * 2". */
  134. unsigned wrap = 0;
  135. unsigned number_of_entries = 0;
  136. char *endptr;
  137. /* we use the content of the STARPU_WORKERS_CUDAID
  138. * env. variable */
  139. for (i = 0; i < STARPU_NMAXWORKERS; i++)
  140. {
  141. if (!wrap)
  142. {
  143. long int val;
  144. val = strtol(strval, &endptr, 10);
  145. if (endptr != strval)
  146. {
  147. workers_gpuid[i] = (unsigned)val;
  148. strval = endptr;
  149. }
  150. else
  151. {
  152. /* there must be at least one entry */
  153. STARPU_ASSERT(i != 0);
  154. number_of_entries = i;
  155. /* there is no more values in the
  156. * string */
  157. wrap = 1;
  158. workers_gpuid[i] = workers_gpuid[0];
  159. }
  160. }
  161. else
  162. {
  163. workers_gpuid[i] =
  164. workers_gpuid[i % number_of_entries];
  165. }
  166. }
  167. }
  168. else if (explicit_workers_gpuid)
  169. {
  170. /* we use the explicit value from the user */
  171. memcpy(workers_gpuid,
  172. explicit_workers_gpuid,
  173. STARPU_NMAXWORKERS*sizeof(unsigned));
  174. }
  175. else
  176. {
  177. /* by default, we take a round robin policy */
  178. if (nhwgpus > 0)
  179. for (i = 0; i < STARPU_NMAXWORKERS; i++)
  180. workers_gpuid[i] = (unsigned)(i % nhwgpus);
  181. /* StarPU can use sampling techniques to bind threads
  182. * correctly */
  183. may_bind_automatically[type] = 1;
  184. }
  185. }
  186. #endif
  187. #if defined(STARPU_USE_CUDA) || defined(STARPU_SIMGRID)
  188. static void
  189. _starpu_initialize_workers_cuda_gpuid (struct _starpu_machine_config *config)
  190. {
  191. struct _starpu_machine_topology *topology = &config->topology;
  192. struct starpu_conf *uconf = &config->conf;
  193. _starpu_initialize_workers_deviceid (
  194. uconf->use_explicit_workers_cuda_gpuid == 0
  195. ? NULL
  196. : (int *)uconf->workers_cuda_gpuid,
  197. &(config->current_cuda_gpuid),
  198. (int *)topology->workers_cuda_gpuid,
  199. "STARPU_WORKERS_CUDAID",
  200. topology->nhwcudagpus,
  201. STARPU_CUDA_WORKER);
  202. }
  203. static inline int
  204. _starpu_get_next_cuda_gpuid (struct _starpu_machine_config *config)
  205. {
  206. unsigned i =
  207. ((config->current_cuda_gpuid++) % config->topology.ncudagpus);
  208. return (int)config->topology.workers_cuda_gpuid[i];
  209. }
  210. #endif
  211. #if defined(STARPU_USE_OPENCL) || defined(STARPU_SIMGRID)
  212. static void
  213. _starpu_initialize_workers_opencl_gpuid (struct _starpu_machine_config*config)
  214. {
  215. struct _starpu_machine_topology *topology = &config->topology;
  216. struct starpu_conf *uconf = &config->conf;
  217. _starpu_initialize_workers_deviceid(
  218. uconf->use_explicit_workers_opencl_gpuid == 0
  219. ? NULL
  220. : (int *)uconf->workers_opencl_gpuid,
  221. &(config->current_opencl_gpuid),
  222. (int *)topology->workers_opencl_gpuid,
  223. "STARPU_WORKERS_OPENCLID",
  224. topology->nhwopenclgpus,
  225. STARPU_OPENCL_WORKER);
  226. #if defined(STARPU_USE_CUDA) || defined(STARPU_SIMGRID)
  227. // Detect devices which are already used with CUDA
  228. {
  229. unsigned tmp[STARPU_NMAXWORKERS];
  230. unsigned nb=0;
  231. int i;
  232. for(i=0 ; i<STARPU_NMAXWORKERS ; i++)
  233. {
  234. struct handle_entry *entry;
  235. int devid = config->topology.workers_opencl_gpuid[i];
  236. HASH_FIND_INT(devices_using_cuda, &devid, entry);
  237. if (entry == NULL)
  238. {
  239. tmp[nb] = topology->workers_opencl_gpuid[i];
  240. nb++;
  241. }
  242. }
  243. for (i=nb ; i<STARPU_NMAXWORKERS ; i++)
  244. tmp[i] = -1;
  245. memcpy (topology->workers_opencl_gpuid, tmp,
  246. sizeof(unsigned)*STARPU_NMAXWORKERS);
  247. }
  248. #endif /* STARPU_USE_CUDA */
  249. {
  250. // Detect identical devices
  251. struct handle_entry *devices_already_used = NULL;
  252. unsigned tmp[STARPU_NMAXWORKERS];
  253. unsigned nb=0;
  254. int i;
  255. for(i=0 ; i<STARPU_NMAXWORKERS ; i++)
  256. {
  257. int devid = topology->workers_opencl_gpuid[i];
  258. struct handle_entry *entry;
  259. HASH_FIND_INT(devices_already_used, &devid, entry);
  260. if (entry == NULL)
  261. {
  262. struct handle_entry *entry2;
  263. entry2 = (struct handle_entry *) malloc(sizeof(*entry2));
  264. STARPU_ASSERT(entry2 != NULL);
  265. entry2->gpuid = devid;
  266. HASH_ADD_INT(devices_already_used, gpuid,
  267. entry2);
  268. tmp[nb] = devid;
  269. nb ++;
  270. }
  271. }
  272. struct handle_entry *entry, *tempo;
  273. HASH_ITER(hh, devices_already_used, entry, tempo)
  274. {
  275. HASH_DEL(devices_already_used, entry);
  276. free(entry);
  277. }
  278. for (i=nb ; i<STARPU_NMAXWORKERS ; i++)
  279. tmp[i] = -1;
  280. memcpy (topology->workers_opencl_gpuid, tmp,
  281. sizeof(unsigned)*STARPU_NMAXWORKERS);
  282. }
  283. }
  284. static inline int
  285. _starpu_get_next_opencl_gpuid (struct _starpu_machine_config *config)
  286. {
  287. unsigned i =
  288. ((config->current_opencl_gpuid++) % config->topology.nopenclgpus);
  289. return (int)config->topology.workers_opencl_gpuid[i];
  290. }
  291. #endif
  292. #if 0
  293. #if defined(STARPU_USE_MIC) || defined(STARPU_SIMGRID)
  294. static void _starpu_initialize_workers_mic_deviceid(struct _starpu_machine_config *config)
  295. {
  296. struct _starpu_machine_topology *topology = &config->topology;
  297. struct starpu_conf *uconf = &config->conf;
  298. _starpu_initialize_workers_deviceid(
  299. uconf->use_explicit_workers_mic_deviceid == 0
  300. ? NULL
  301. : (int *)config->user_conf->workers_mic_deviceid,
  302. &(config->current_mic_deviceid),
  303. (int *)topology->workers_mic_deviceid,
  304. "STARPU_WORKERS_MICID",
  305. topology->nhwmiccores,
  306. STARPU_MIC_WORKER);
  307. }
  308. #endif
  309. #endif
  310. #ifdef STARPU_USE_SCC
  311. static void _starpu_initialize_workers_scc_deviceid(struct _starpu_machine_config *config)
  312. {
  313. struct _starpu_machine_topology *topology = &config->topology;
  314. struct starpu_conf *uconf = &config->conf;
  315. _starpu_initialize_workers_deviceid(
  316. uconf->use_explicit_workers_scc_deviceid == 0
  317. ? NULL
  318. : (int *) uconf->workers_scc_deviceid,
  319. &(config->current_scc_deviceid),
  320. (int *)topology->workers_scc_deviceid,
  321. "STARPU_WORKERS_SCCID",
  322. topology->nhwscc,
  323. STARPU_SCC_WORKER);
  324. }
  325. #endif /* STARPU_USE_SCC */
  326. #if 0
  327. #ifdef STARPU_USE_MIC
  328. static inline int _starpu_get_next_mic_deviceid(struct _starpu_machine_config *config)
  329. {
  330. unsigned i = ((config->current_mic_deviceid++) % config->topology.nmicdevices);
  331. return (int)config->topology.workers_mic_deviceid[i];
  332. }
  333. #endif
  334. #endif
  335. #ifdef STARPU_USE_SCC
  336. static inline int _starpu_get_next_scc_deviceid(struct _starpu_machine_config *config)
  337. {
  338. unsigned i = ((config->current_scc_deviceid++) % config->topology.nsccdevices);
  339. return (int)config->topology.workers_scc_deviceid[i];
  340. }
  341. #endif
  342. #ifdef STARPU_USE_MIC
  343. static void
  344. _starpu_init_mic_topology (struct _starpu_machine_config *config, long mic_idx)
  345. {
  346. /* Discover the topology of the mic node identifier by MIC_IDX. That
  347. * means, make this StarPU instance aware of the number of cores available
  348. * on this MIC device. Update the `nhwmiccores' topology field
  349. * accordingly. */
  350. struct _starpu_machine_topology *topology = &config->topology;
  351. int nbcores;
  352. _starpu_src_common_sink_nbcores (mic_nodes[mic_idx], &nbcores);
  353. topology->nhwmiccores[mic_idx] = nbcores;
  354. }
  355. static int
  356. _starpu_init_mic_node (struct _starpu_machine_config *config, int mic_idx,
  357. COIENGINE *coi_handle, COIPROCESS *coi_process)
  358. {
  359. /* Initialize the MIC node of index MIC_IDX. */
  360. struct starpu_conf *user_conf = &config->conf;
  361. char ***argv = _starpu_get_argv();
  362. const char *suffixes[] = {"-mic", "_mic", NULL};
  363. /* Environment variables to send to the Sink, it informs it what kind
  364. * of node it is (architecture and type) as there is no way to discover
  365. * it itself */
  366. char mic_idx_env[32];
  367. sprintf(mic_idx_env, "_STARPU_MIC_DEVID=%d", mic_idx);
  368. /* XXX: this is currently necessary so that the remote process does not
  369. * segfault. */
  370. char nb_mic_env[32];
  371. sprintf(nb_mic_env, "_STARPU_MIC_NB=%d", 2);
  372. const char *mic_sink_env[] = {"STARPU_SINK=STARPU_MIC", mic_idx_env, nb_mic_env, NULL};
  373. char mic_sink_program_path[1024];
  374. /* Let's get the helper program to run on the MIC device */
  375. int mic_file_found =
  376. _starpu_src_common_locate_file (mic_sink_program_path,
  377. starpu_getenv("STARPU_MIC_SINK_PROGRAM_NAME"),
  378. starpu_getenv("STARPU_MIC_SINK_PROGRAM_PATH"),
  379. user_conf->mic_sink_program_path,
  380. (argv ? (*argv)[0] : NULL),
  381. suffixes);
  382. if (0 != mic_file_found)
  383. {
  384. fprintf(stderr, "No MIC program specified, use the environment\n"
  385. "variable STARPU_MIC_SINK_PROGRAM_NAME or the environment\n"
  386. "or the field 'starpu_conf.mic_sink_program_path'\n"
  387. "to define it.\n");
  388. return -1;
  389. }
  390. COIRESULT res;
  391. /* Let's get the handle which let us manage the remote MIC device */
  392. res = COIEngineGetHandle(COI_ISA_MIC, mic_idx, coi_handle);
  393. if (STARPU_UNLIKELY(res != COI_SUCCESS))
  394. STARPU_MIC_SRC_REPORT_COI_ERROR(res);
  395. /* We launch the helper on the MIC device, which will wait for us
  396. * to give it work to do.
  397. * As we will communicate further with the device throught scif we
  398. * don't need to keep the process pointer */
  399. res = COIProcessCreateFromFile(*coi_handle, mic_sink_program_path, 0, NULL, 0,
  400. mic_sink_env, 1, NULL, 0, NULL,
  401. coi_process);
  402. if (STARPU_UNLIKELY(res != COI_SUCCESS))
  403. STARPU_MIC_SRC_REPORT_COI_ERROR(res);
  404. /* Let's create the node structure, we'll communicate with the peer
  405. * through scif thanks to it */
  406. mic_nodes[mic_idx] =
  407. _starpu_mp_common_node_create(STARPU_MIC_SOURCE, mic_idx);
  408. return 0;
  409. }
  410. #endif
  411. static void
  412. _starpu_init_topology (struct _starpu_machine_config *config)
  413. {
  414. /* Discover the topology, meaning finding all the available PUs for
  415. the compiled drivers. These drivers MUST have been initialized
  416. before calling this function. The discovered topology is filled in
  417. CONFIG. */
  418. struct _starpu_machine_topology *topology = &config->topology;
  419. if (topology_is_initialized)
  420. return;
  421. nobind = starpu_get_env_number("STARPU_WORKERS_NOBIND");
  422. topology->nhwcpus = 0;
  423. topology->nhwpus = 0;
  424. #ifndef STARPU_SIMGRID
  425. #ifdef STARPU_HAVE_HWLOC
  426. hwloc_topology_init(&topology->hwtopology);
  427. hwloc_topology_load(topology->hwtopology);
  428. #endif
  429. #endif
  430. #ifdef STARPU_SIMGRID
  431. config->topology.nhwcpus = config->topology.nhwpus = _starpu_simgrid_get_nbhosts("CPU");
  432. #elif defined(STARPU_HAVE_HWLOC)
  433. /* Discover the CPUs relying on the hwloc interface and fills CONFIG
  434. * accordingly. */
  435. config->cpu_depth = hwloc_get_type_depth (topology->hwtopology,
  436. HWLOC_OBJ_CORE);
  437. config->pu_depth = hwloc_get_type_depth (topology->hwtopology,
  438. HWLOC_OBJ_PU);
  439. /* Would be very odd */
  440. STARPU_ASSERT(config->cpu_depth != HWLOC_TYPE_DEPTH_MULTIPLE);
  441. if (config->cpu_depth == HWLOC_TYPE_DEPTH_UNKNOWN)
  442. {
  443. /* unknown, using logical procesors as fallback */
  444. _STARPU_DISP("Warning: The OS did not report CPU cores. Assuming there is only one hardware thread per core.\n");
  445. config->cpu_depth = hwloc_get_type_depth(topology->hwtopology,
  446. HWLOC_OBJ_PU);
  447. }
  448. topology->nhwcpus = hwloc_get_nbobjs_by_depth (topology->hwtopology,
  449. config->cpu_depth);
  450. topology->nhwpus = hwloc_get_nbobjs_by_depth (topology->hwtopology,
  451. config->pu_depth);
  452. #elif defined(HAVE_SYSCONF)
  453. /* Discover the CPUs relying on the sysconf(3) function and fills
  454. * CONFIG accordingly. */
  455. config->topology.nhwcpus = config->topology.nhwpus = sysconf(_SC_NPROCESSORS_ONLN);
  456. #elif defined(_WIN32)
  457. /* Discover the CPUs on Cygwin and MinGW systems. */
  458. SYSTEM_INFO sysinfo;
  459. GetSystemInfo(&sysinfo);
  460. config->topology.nhwcpus = config->topology.nhwpus = sysinfo.dwNumberOfProcessors;
  461. #else
  462. #warning no way to know number of cores, assuming 1
  463. config->topology.nhwcpus = config->topology.nhwpus = 1;
  464. #endif
  465. _starpu_cuda_discover_devices(config);
  466. _starpu_opencl_discover_devices(config);
  467. #ifdef STARPU_USE_SCC
  468. config->topology.nhwscc = _starpu_scc_src_get_device_count();
  469. #endif
  470. topology_is_initialized = 1;
  471. }
  472. /*
  473. * Bind workers on the different processors
  474. */
  475. static void
  476. _starpu_initialize_workers_bindid (struct _starpu_machine_config *config)
  477. {
  478. char *strval;
  479. unsigned i;
  480. struct _starpu_machine_topology *topology = &config->topology;
  481. config->current_bindid = 0;
  482. /* conf->workers_bindid indicates the successive logical PU identifier that
  483. * should be used to bind the workers. It should be either filled
  484. * according to the user's explicit parameters (from starpu_conf) or
  485. * according to the STARPU_WORKERS_CPUID env. variable. Otherwise, a
  486. * round-robin policy is used to distributed the workers over the
  487. * cores. */
  488. /* what do we use, explicit value, env. variable, or round-robin ? */
  489. if ((strval = starpu_getenv("STARPU_WORKERS_CPUID")))
  490. {
  491. /* STARPU_WORKERS_CPUID certainly contains less entries than
  492. * STARPU_NMAXWORKERS, so we reuse its entries in a round
  493. * robin fashion: "1 2" is equivalent to "1 2 1 2 1 2 .... 1
  494. * 2". */
  495. unsigned wrap = 0;
  496. unsigned number_of_entries = 0;
  497. char *endptr;
  498. /* we use the content of the STARPU_WORKERS_CPUID
  499. * env. variable */
  500. for (i = 0; i < STARPU_NMAXWORKERS; i++)
  501. {
  502. if (!wrap)
  503. {
  504. long int val;
  505. val = strtol(strval, &endptr, 10);
  506. if (endptr != strval)
  507. {
  508. topology->workers_bindid[i] =
  509. (unsigned)(val % topology->nhwpus);
  510. strval = endptr;
  511. if (*strval == '-')
  512. {
  513. /* range of values */
  514. long int endval;
  515. strval++;
  516. if (*strval && *strval != ' ' && *strval != ',')
  517. {
  518. endval = strtol(strval, &endptr, 10);
  519. strval = endptr;
  520. }
  521. else
  522. {
  523. endval = topology->nhwpus-1;
  524. if (*strval)
  525. strval++;
  526. }
  527. for (val++; val <= endval && i < STARPU_NMAXWORKERS-1; val++)
  528. {
  529. i++;
  530. topology->workers_bindid[i] =
  531. (unsigned)(val % topology->nhwpus);
  532. }
  533. }
  534. if (*strval == ',')
  535. strval++;
  536. }
  537. else
  538. {
  539. /* there must be at least one entry */
  540. STARPU_ASSERT(i != 0);
  541. number_of_entries = i;
  542. /* there is no more values in the
  543. * string */
  544. wrap = 1;
  545. topology->workers_bindid[i] =
  546. topology->workers_bindid[0];
  547. }
  548. }
  549. else
  550. {
  551. topology->workers_bindid[i] =
  552. topology->workers_bindid[i % number_of_entries];
  553. }
  554. }
  555. }
  556. else if (config->conf.use_explicit_workers_bindid)
  557. {
  558. /* we use the explicit value from the user */
  559. memcpy(topology->workers_bindid,
  560. config->conf.workers_bindid,
  561. STARPU_NMAXWORKERS*sizeof(unsigned));
  562. }
  563. else
  564. {
  565. int nth_per_core = starpu_get_env_number_default("STARPU_NTHREADS_PER_CORE", 1);
  566. int k;
  567. int nbindids=0;
  568. int nhyperthreads = topology->nhwpus / topology->nhwcpus;
  569. STARPU_ASSERT_MSG(nth_per_core > 0 && nth_per_core <= nhyperthreads , "Incorrect number of hyperthreads");
  570. i = 0; /* PU number currently assigned */
  571. k = 0; /* Number of threads already put on the current core */
  572. while(nbindids < STARPU_NMAXWORKERS)
  573. {
  574. if (k >= nth_per_core)
  575. {
  576. /* We have already put enough workers on this
  577. * core, skip remaining PUs from this core, and
  578. * proceed with next core */
  579. i += nhyperthreads-nth_per_core;
  580. k = 0;
  581. continue;
  582. }
  583. /* Add a worker to this core, by using this logical PU */
  584. topology->workers_bindid[nbindids++] =
  585. (unsigned)(i % topology->nhwpus);
  586. k++;
  587. i++;
  588. }
  589. }
  590. for (i = 0; i < STARPU_MAXCPUS;i++)
  591. cpu_worker[i] = STARPU_NOWORKERID;
  592. }
  593. /* This function gets the identifier of the next core on which to bind a
  594. * worker. In case a list of preferred cores was specified (logical indexes),
  595. * we look for a an available core among the list if possible, otherwise a
  596. * round-robin policy is used. */
  597. static inline int
  598. _starpu_get_next_bindid (struct _starpu_machine_config *config,
  599. int *preferred_binding, int npreferred)
  600. {
  601. struct _starpu_machine_topology *topology = &config->topology;
  602. unsigned found = 0;
  603. int current_preferred;
  604. int nhyperthreads = topology->nhwpus / topology->nhwcpus;
  605. /* loop over the preference list */
  606. for (current_preferred = 0;
  607. current_preferred < npreferred;
  608. current_preferred++)
  609. {
  610. if (found)
  611. break;
  612. /* Try to get this core */
  613. unsigned requested_core = preferred_binding[current_preferred];
  614. /* can we bind the worker on the preferred core ? */
  615. unsigned ind;
  616. /* Look at the remaining cores to be bound to */
  617. for (ind = config->current_bindid;
  618. ind < topology->nhwpus / nhyperthreads;
  619. ind++)
  620. {
  621. if (topology->workers_bindid[ind] == requested_core * nhyperthreads)
  622. {
  623. /* the cpu is available, we use it ! In order
  624. * to make sure that it will not be used again
  625. * later on, we exchange it with the next bindid we were supposed to use */
  626. topology->workers_bindid[ind] =
  627. topology->workers_bindid[config->current_bindid];
  628. topology->workers_bindid[config->current_bindid] = requested_core * nhyperthreads;
  629. found = 1;
  630. break;
  631. }
  632. }
  633. }
  634. unsigned i = ((config->current_bindid++) % STARPU_NMAXWORKERS);
  635. return (int)topology->workers_bindid[i];
  636. }
  637. unsigned
  638. _starpu_topology_get_nhwcpu (struct _starpu_machine_config *config)
  639. {
  640. #if defined(STARPU_USE_OPENCL) || defined(STARPU_SIMGRID)
  641. _starpu_opencl_init();
  642. #endif
  643. #if defined(STARPU_USE_CUDA) || defined(STARPU_SIMGRID)
  644. _starpu_init_cuda();
  645. #endif
  646. _starpu_init_topology(config);
  647. return config->topology.nhwcpus;
  648. }
  649. unsigned
  650. _starpu_topology_get_nhwpu (struct _starpu_machine_config *config)
  651. {
  652. #if defined(STARPU_USE_OPENCL) || defined(STARPU_SIMGRID)
  653. _starpu_opencl_init();
  654. #endif
  655. #if defined(STARPU_USE_CUDA) || defined(STARPU_SIMGRID)
  656. _starpu_init_cuda();
  657. #endif
  658. _starpu_init_topology(config);
  659. return config->topology.nhwpus;
  660. }
  661. #ifdef STARPU_USE_MIC
  662. static void
  663. _starpu_init_mic_config (struct _starpu_machine_config *config,
  664. struct starpu_conf *user_conf,
  665. unsigned mic_idx)
  666. {
  667. // Configure the MIC device of index MIC_IDX.
  668. struct _starpu_machine_topology *topology = &config->topology;
  669. topology->nhwmiccores[mic_idx] = 0;
  670. _starpu_init_mic_topology (config, mic_idx);
  671. int nmiccores;
  672. nmiccores = starpu_get_env_number("STARPU_NMICCORES");
  673. if (nmiccores == -1)
  674. {
  675. /* Nothing was specified, so let's use the number of
  676. * detected mic cores. ! */
  677. nmiccores = topology->nhwmiccores[mic_idx];
  678. }
  679. else
  680. {
  681. if ((unsigned) nmiccores > topology->nhwmiccores[mic_idx])
  682. {
  683. /* The user requires more MIC cores than there is available */
  684. fprintf(stderr,
  685. "# Warning: %d MIC cores requested. Only %d available.\n",
  686. nmiccores, topology->nhwmiccores[mic_idx]);
  687. nmiccores = topology->nhwmiccores[mic_idx];
  688. }
  689. }
  690. topology->nmiccores[mic_idx] = nmiccores;
  691. STARPU_ASSERT_MSG(topology->nmiccores[mic_idx] + topology->nworkers <= STARPU_NMAXWORKERS,
  692. "topology->nmiccores[mic_idx(%d)] (%d) + topology->nworkers (%d) <= STARPU_NMAXWORKERS (%d)",
  693. mic_idx, topology->nmiccores[mic_idx], topology->nworkers, STARPU_NMAXWORKERS);
  694. /* _starpu_initialize_workers_mic_deviceid (config); */
  695. mic_worker_set[mic_idx].workers = &config->workers[topology->nworkers];
  696. unsigned miccore_id;
  697. for (miccore_id = 0; miccore_id < topology->nmiccores[mic_idx]; miccore_id++)
  698. {
  699. int worker_idx = topology->nworkers + miccore_id;
  700. config->workers[worker_idx].set = &mic_worker_set[mic_idx];
  701. config->workers[worker_idx].arch = STARPU_MIC_WORKER;
  702. config->workers[worker_idx].perf_arch.devices = (struct starpu_perfmodel_device *) malloc(sizeof(struct starpu_perfmodel_device));
  703. config->workers[worker_idx].perf_arch.ndevices = 1;
  704. config->workers[worker_idx].perf_arch.devices[0].type = STARPU_MIC_WORKER;
  705. config->workers[worker_idx].perf_arch.devices[0].devid = mic_idx;
  706. config->workers[worker_idx].perf_arch.devices[0].ncores = 1;
  707. config->workers[worker_idx].devid = mic_idx;
  708. config->workers[worker_idx].subworkerid = miccore_id;
  709. config->workers[worker_idx].worker_mask = STARPU_MIC;
  710. config->worker_mask |= STARPU_MIC;
  711. }
  712. topology->nworkers += topology->nmiccores[mic_idx];
  713. }
  714. #ifdef STARPU_USE_MIC
  715. static COIENGINE mic_handles[STARPU_MAXMICDEVS];
  716. COIPROCESS _starpu_mic_process[STARPU_MAXMICDEVS];
  717. #endif
  718. static void
  719. _starpu_init_mp_config (struct _starpu_machine_config *config,
  720. struct starpu_conf *user_conf)
  721. {
  722. /* Discover and configure the mp topology. That means:
  723. * - discover the number of mp nodes;
  724. * - initialize each discovered node;
  725. * - discover the local topology (number of PUs/devices) of each node;
  726. * - configure the workers accordingly.
  727. */
  728. struct _starpu_machine_topology *topology = &config->topology;
  729. // We currently only support MIC at this level.
  730. #ifdef STARPU_USE_MIC
  731. /* Discover and initialize the number of MIC nodes through the mp
  732. * infrastructure. */
  733. unsigned nhwmicdevices = _starpu_mic_src_get_device_count();
  734. int reqmicdevices = starpu_get_env_number("STARPU_NMIC");
  735. if (reqmicdevices == -1 && user_conf)
  736. reqmicdevices = user_conf->nmic;
  737. if (reqmicdevices == -1)
  738. reqmicdevices = nhwmicdevices;
  739. if (reqmicdevices == -1)
  740. {
  741. /* Nothing was specified, so let's use the number of
  742. * detected mic devices. ! */
  743. reqmicdevices = nhwmicdevices;
  744. }
  745. else
  746. {
  747. if ((unsigned) reqmicdevices > nhwmicdevices)
  748. {
  749. /* The user requires more MIC devices than there is available */
  750. fprintf(stderr,
  751. "# Warning: %d MIC devices requested. Only %d available.\n",
  752. reqmicdevices, nhwmicdevices);
  753. reqmicdevices = nhwmicdevices;
  754. }
  755. }
  756. topology->nmicdevices = 0;
  757. unsigned i;
  758. for (i = 0; i < (unsigned) reqmicdevices; i++)
  759. if (0 == _starpu_init_mic_node (config, i, &mic_handles[i], &_starpu_mic_process[i]))
  760. topology->nmicdevices++;
  761. for (i = 0; i < topology->nmicdevices; i++)
  762. _starpu_init_mic_config (config, user_conf, i);
  763. #endif
  764. }
  765. static void
  766. _starpu_deinit_mic_node (unsigned mic_idx)
  767. {
  768. _starpu_mp_common_send_command(mic_nodes[mic_idx], STARPU_EXIT, NULL, 0);
  769. COIProcessDestroy(_starpu_mic_process[mic_idx], -1, 0, NULL, NULL);
  770. _starpu_mp_common_node_destroy(mic_nodes[mic_idx]);
  771. }
  772. static void
  773. _starpu_deinit_mp_config (struct _starpu_machine_config *config)
  774. {
  775. struct _starpu_machine_topology *topology = &config->topology;
  776. unsigned i;
  777. for (i = 0; i < topology->nmicdevices; i++)
  778. _starpu_deinit_mic_node (i);
  779. _starpu_mic_clear_kernels();
  780. }
  781. #endif
  782. static int
  783. _starpu_init_machine_config(struct _starpu_machine_config *config, int no_mp_config STARPU_ATTRIBUTE_UNUSED)
  784. {
  785. int i;
  786. for (i = 0; i < STARPU_NMAXWORKERS; i++)
  787. {
  788. config->workers[i].workerid = i;
  789. config->workers[i].set = NULL;
  790. }
  791. struct _starpu_machine_topology *topology = &config->topology;
  792. topology->nworkers = 0;
  793. topology->ncombinedworkers = 0;
  794. topology->nsched_ctxs = 0;
  795. #if defined(STARPU_USE_OPENCL) || defined(STARPU_SIMGRID)
  796. _starpu_opencl_init();
  797. #endif
  798. #if defined(STARPU_USE_CUDA) || defined(STARPU_SIMGRID)
  799. _starpu_init_cuda();
  800. #endif
  801. _starpu_init_topology(config);
  802. _starpu_initialize_workers_bindid(config);
  803. #if defined(STARPU_USE_CUDA) || defined(STARPU_SIMGRID)
  804. for (i = 0; i < (int) (sizeof(cuda_worker_set)/sizeof(cuda_worker_set[0])); i++)
  805. cuda_worker_set[i].workers = NULL;
  806. #endif
  807. #ifdef STARPU_USE_MIC
  808. for (i = 0; i < (int) (sizeof(mic_worker_set)/sizeof(mic_worker_set[0])); i++)
  809. mic_worker_set[i].workers = NULL;
  810. #endif
  811. #if defined(STARPU_USE_CUDA) || defined(STARPU_SIMGRID)
  812. int ncuda = config->conf.ncuda;
  813. int nworker_per_cuda = starpu_get_env_number_default("STARPU_NWORKER_PER_CUDA", 1);
  814. STARPU_ASSERT_MSG(nworker_per_cuda > 0, "STARPU_NWORKER_PER_CUDA has to be > 0");
  815. #ifndef STARPU_NON_BLOCKING_DRIVERS
  816. if (nworker_per_cuda > 1)
  817. {
  818. _STARPU_DISP("Warning: reducing STARPU_NWORKER_PER_CUDA to 1 because blocking drivers are enabled\n");
  819. nworker_per_cuda = 1;
  820. }
  821. #endif
  822. if (ncuda != 0)
  823. {
  824. /* The user did not disable CUDA. We need to initialize CUDA
  825. * early to count the number of devices */
  826. _starpu_init_cuda();
  827. int nb_devices = _starpu_get_cuda_device_count();
  828. if (ncuda == -1)
  829. {
  830. /* Nothing was specified, so let's choose ! */
  831. ncuda = nb_devices;
  832. }
  833. else
  834. {
  835. if (ncuda > nb_devices)
  836. {
  837. /* The user requires more CUDA devices than
  838. * there is available */
  839. _STARPU_DISP("Warning: %d CUDA devices requested. Only %d available.\n", ncuda, nb_devices);
  840. ncuda = nb_devices;
  841. }
  842. }
  843. }
  844. /* Now we know how many CUDA devices will be used */
  845. topology->ncudagpus = ncuda;
  846. STARPU_ASSERT(topology->ncudagpus <= STARPU_MAXCUDADEVS);
  847. _starpu_initialize_workers_cuda_gpuid(config);
  848. unsigned cudagpu;
  849. for (cudagpu = 0; cudagpu < topology->ncudagpus; cudagpu++)
  850. {
  851. int devid = _starpu_get_next_cuda_gpuid(config);
  852. int worker_idx0 = topology->nworkers + cudagpu * nworker_per_cuda;
  853. cuda_worker_set[devid].workers = &config->workers[worker_idx0];
  854. for (i = 0; i < nworker_per_cuda; i++)
  855. {
  856. int worker_idx = worker_idx0 + i;
  857. config->workers[worker_idx].set = &cuda_worker_set[devid];
  858. config->workers[worker_idx].arch = STARPU_CUDA_WORKER;
  859. config->workers[worker_idx].perf_arch.devices = (struct starpu_perfmodel_device*)malloc(sizeof(struct starpu_perfmodel_device));
  860. config->workers[worker_idx].perf_arch.ndevices = 1;
  861. config->workers[worker_idx].perf_arch.devices[0].type = STARPU_CUDA_WORKER;
  862. config->workers[worker_idx].perf_arch.devices[0].devid = devid;
  863. // TODO: fix perfmodels etc.
  864. //config->workers[worker_idx].perf_arch.ncore = nworker_per_cuda - 1;
  865. config->workers[worker_idx].perf_arch.devices[0].ncores = 1;
  866. config->workers[worker_idx].devid = devid;
  867. config->workers[worker_idx].subworkerid = i;
  868. config->workers[worker_idx].worker_mask = STARPU_CUDA;
  869. config->worker_mask |= STARPU_CUDA;
  870. struct handle_entry *entry;
  871. entry = (struct handle_entry *) malloc(sizeof(*entry));
  872. STARPU_ASSERT(entry != NULL);
  873. entry->gpuid = devid;
  874. HASH_ADD_INT(devices_using_cuda, gpuid, entry);
  875. }
  876. }
  877. topology->nworkers += topology->ncudagpus * nworker_per_cuda;
  878. #endif
  879. #if defined(STARPU_USE_OPENCL) || defined(STARPU_SIMGRID)
  880. int nopencl = config->conf.nopencl;
  881. if (nopencl != 0)
  882. {
  883. /* The user did not disable OPENCL. We need to initialize
  884. * OpenCL early to count the number of devices */
  885. _starpu_opencl_init();
  886. int nb_devices;
  887. nb_devices = _starpu_opencl_get_device_count();
  888. if (nopencl == -1)
  889. {
  890. /* Nothing was specified, so let's choose ! */
  891. nopencl = nb_devices;
  892. if (nopencl > STARPU_MAXOPENCLDEVS)
  893. {
  894. _STARPU_DISP("Warning: %d OpenCL devices available. Only %d enabled. Use configure option --enable-maxopencldadev=xxx to update the maximum value of supported OpenCL devices.\n", nb_devices, STARPU_MAXOPENCLDEVS);
  895. nopencl = STARPU_MAXOPENCLDEVS;
  896. }
  897. }
  898. else
  899. {
  900. /* Let's make sure this value is OK. */
  901. if (nopencl > nb_devices)
  902. {
  903. /* The user requires more OpenCL devices than
  904. * there is available */
  905. _STARPU_DISP("Warning: %d OpenCL devices requested. Only %d available.\n", nopencl, nb_devices);
  906. nopencl = nb_devices;
  907. }
  908. /* Let's make sure this value is OK. */
  909. if (nopencl > STARPU_MAXOPENCLDEVS)
  910. {
  911. _STARPU_DISP("Warning: %d OpenCL devices requested. Only %d enabled. Use configure option --enable-maxopencldev=xxx to update the maximum value of supported OpenCL devices.\n", nopencl, STARPU_MAXOPENCLDEVS);
  912. nopencl = STARPU_MAXOPENCLDEVS;
  913. }
  914. }
  915. }
  916. topology->nopenclgpus = nopencl;
  917. STARPU_ASSERT(topology->nopenclgpus + topology->nworkers <= STARPU_NMAXWORKERS);
  918. _starpu_initialize_workers_opencl_gpuid(config);
  919. unsigned openclgpu;
  920. for (openclgpu = 0; openclgpu < topology->nopenclgpus; openclgpu++)
  921. {
  922. int worker_idx = topology->nworkers + openclgpu;
  923. int devid = _starpu_get_next_opencl_gpuid(config);
  924. if (devid == -1)
  925. { // There is no more devices left
  926. topology->nopenclgpus = openclgpu;
  927. break;
  928. }
  929. config->workers[worker_idx].arch = STARPU_OPENCL_WORKER;
  930. config->workers[worker_idx].perf_arch.devices = (struct starpu_perfmodel_device*)malloc(sizeof(struct starpu_perfmodel_device));
  931. config->workers[worker_idx].perf_arch.ndevices = 1;
  932. config->workers[worker_idx].perf_arch.devices[0].type = STARPU_OPENCL_WORKER;
  933. config->workers[worker_idx].perf_arch.devices[0].devid = devid;
  934. config->workers[worker_idx].perf_arch.devices[0].ncores = 1;
  935. config->workers[worker_idx].subworkerid = 0;
  936. config->workers[worker_idx].devid = devid;
  937. config->workers[worker_idx].worker_mask = STARPU_OPENCL;
  938. config->worker_mask |= STARPU_OPENCL;
  939. }
  940. topology->nworkers += topology->nopenclgpus;
  941. #endif
  942. #ifdef STARPU_USE_SCC
  943. int nscc = config->conf.nscc;
  944. unsigned nb_scc_nodes = _starpu_scc_src_get_device_count();
  945. if (nscc != 0)
  946. {
  947. /* The user did not disable SCC. We need to count
  948. * the number of devices */
  949. int nb_devices = nb_scc_nodes;
  950. if (nscc == -1)
  951. {
  952. /* Nothing was specified, so let's choose ! */
  953. nscc = nb_devices;
  954. if (nscc > STARPU_MAXSCCDEVS)
  955. {
  956. _STARPU_DISP("Warning: %d SCC devices available. Only %d enabled. Use configuration option --enable-maxsccdev=xxx to update the maximum value of supported SCC devices.\n", nb_devices, STARPU_MAXSCCDEVS);
  957. nscc = STARPU_MAXSCCDEVS;
  958. }
  959. }
  960. else
  961. {
  962. /* Let's make sure this value is OK. */
  963. if (nscc > nb_devices)
  964. {
  965. /* The user requires more SCC devices than there is available */
  966. _STARPU_DISP("Warning: %d SCC devices requested. Only %d available.\n", nscc, nb_devices);
  967. nscc = nb_devices;
  968. }
  969. /* Let's make sure this value is OK. */
  970. if (nscc > STARPU_MAXSCCDEVS)
  971. {
  972. _STARPU_DISP("Warning: %d SCC devices requested. Only %d enabled. Use configure option --enable-maxsccdev=xxx to update the maximum value of supported SCC devices.\n", nscc, STARPU_MAXSCCDEVS);
  973. nscc = STARPU_MAXSCCDEVS;
  974. }
  975. }
  976. }
  977. /* Now we know how many SCC devices will be used */
  978. topology->nsccdevices = nscc;
  979. STARPU_ASSERT(topology->nsccdevices + topology->nworkers <= STARPU_NMAXWORKERS);
  980. _starpu_initialize_workers_scc_deviceid(config);
  981. unsigned sccdev;
  982. for (sccdev = 0; sccdev < topology->nsccdevices; sccdev++)
  983. {
  984. config->workers[topology->nworkers + sccdev].arch = STARPU_SCC_WORKER;
  985. int devid = _starpu_get_next_scc_deviceid(config);
  986. config->workers[topology->nworkers + sccdev].perf_arch.devices = (struct starpu_perfmodel_device)malloc(sizeof(struct starpu_perfmodel_device));
  987. config->workers[topology->nworkers + sccdev].perf_arch.ndevices = 1;
  988. config->workers[topology->nworkers + sccdev].perf_arch.devices[0].type = STARPU_SCC_WORKER;
  989. config->workers[topology->nworkers + sccdev].perf_arch.devices[0].devid = sccdev;
  990. config->workers[topology->nworkers + sccdev].perf_arch.devices[0].ncore = 1;
  991. config->workers[topology->nworkers + sccdev].subworkerid = 0;
  992. config->workers[topology->nworkers + sccdev].devid = devid;
  993. config->workers[topology->nworkers + sccdev].worker_mask = STARPU_SCC;
  994. config->worker_mask |= STARPU_SCC;
  995. }
  996. for (; sccdev < nb_scc_nodes; ++sccdev)
  997. _starpu_scc_exit_useless_node(sccdev);
  998. topology->nworkers += topology->nsccdevices;
  999. #endif /* STARPU_USE_SCC */
  1000. /* Unless not requested, we need to complete configuration with the
  1001. * ones of the mp nodes. */
  1002. #ifdef STARPU_USE_MIC
  1003. if (! no_mp_config)
  1004. _starpu_init_mp_config (config, &config->conf);
  1005. #endif
  1006. /* we put the CPU section after the accelerator : in case there was an
  1007. * accelerator found, we devote one cpu */
  1008. #if defined(STARPU_USE_CPU) || defined(STARPU_SIMGRID)
  1009. int ncpu = config->conf.ncpus;
  1010. if (ncpu != 0)
  1011. {
  1012. if (ncpu == -1)
  1013. {
  1014. unsigned mic_busy_cpus = 0;
  1015. unsigned j = 0;
  1016. for (j = 0; j < STARPU_MAXMICDEVS; j++)
  1017. mic_busy_cpus += (topology->nmiccores[j] ? 1 : 0);
  1018. unsigned already_busy_cpus = mic_busy_cpus + topology->ncudagpus
  1019. + topology->nopenclgpus + topology->nsccdevices;
  1020. long avail_cpus = (long) topology->nhwcpus - (long) already_busy_cpus;
  1021. if (avail_cpus < 0)
  1022. avail_cpus = 0;
  1023. int nth_per_core = starpu_get_env_number_default("STARPU_NTHREADS_PER_CORE", 1);
  1024. avail_cpus *= nth_per_core;
  1025. ncpu = STARPU_MIN(avail_cpus, STARPU_MAXCPUS);
  1026. }
  1027. else
  1028. {
  1029. if (ncpu > STARPU_MAXCPUS)
  1030. {
  1031. _STARPU_DISP("Warning: %d CPU devices requested. Only %d enabled. Use configure option --enable-maxcpus=xxx to update the maximum value of supported CPU devices.\n", ncpu, STARPU_MAXCPUS);
  1032. ncpu = STARPU_MAXCPUS;
  1033. }
  1034. }
  1035. }
  1036. topology->ncpus = ncpu;
  1037. STARPU_ASSERT(topology->ncpus + topology->nworkers <= STARPU_NMAXWORKERS);
  1038. unsigned cpu;
  1039. for (cpu = 0; cpu < topology->ncpus; cpu++)
  1040. {
  1041. int worker_idx = topology->nworkers + cpu;
  1042. config->workers[worker_idx].arch = STARPU_CPU_WORKER;
  1043. config->workers[worker_idx].perf_arch.devices = (struct starpu_perfmodel_device*)malloc(sizeof(struct starpu_perfmodel_device));
  1044. config->workers[worker_idx].perf_arch.ndevices = 1;
  1045. config->workers[worker_idx].perf_arch.devices[0].type = STARPU_CPU_WORKER;
  1046. config->workers[worker_idx].perf_arch.devices[0].devid = 0;
  1047. config->workers[worker_idx].perf_arch.devices[0].ncores = 1;
  1048. config->workers[worker_idx].subworkerid = 0;
  1049. config->workers[worker_idx].devid = cpu;
  1050. config->workers[worker_idx].worker_mask = STARPU_CPU;
  1051. config->worker_mask |= STARPU_CPU;
  1052. }
  1053. topology->nworkers += topology->ncpus;
  1054. #endif
  1055. if (topology->nworkers == 0)
  1056. {
  1057. _STARPU_DEBUG("No worker found, aborting ...\n");
  1058. return -ENODEV;
  1059. }
  1060. return 0;
  1061. }
  1062. void _starpu_destroy_machine_config(struct _starpu_machine_config *config)
  1063. {
  1064. _starpu_close_debug_logfile();
  1065. unsigned worker;
  1066. for (worker = 0; worker < config->topology.nworkers; worker++)
  1067. {
  1068. struct _starpu_worker *workerarg = &config->workers[worker];
  1069. unsigned bindid = workerarg->bindid;
  1070. free(workerarg->perf_arch.devices);
  1071. #ifdef STARPU_HAVE_HWLOC
  1072. hwloc_bitmap_free(workerarg->hwloc_cpu_set);
  1073. if (workerarg->bindid != -1)
  1074. {
  1075. hwloc_obj_t worker_obj = hwloc_get_obj_by_depth(config->topology.hwtopology,
  1076. config->pu_depth,
  1077. workerarg->bindid);
  1078. if (worker_obj->userdata)
  1079. {
  1080. _starpu_worker_list_delete(worker_obj->userdata);
  1081. worker_obj->userdata = NULL;
  1082. }
  1083. }
  1084. #endif
  1085. if (bindid != -1)
  1086. {
  1087. free(config->bindid_workers[bindid].workerids);
  1088. config->bindid_workers[bindid].workerids = NULL;
  1089. }
  1090. }
  1091. free(config->bindid_workers);
  1092. config->bindid_workers = NULL;
  1093. config->nbindid = 0;
  1094. unsigned combined_worker_id;
  1095. for(combined_worker_id=0 ; combined_worker_id < config->topology.ncombinedworkers ; combined_worker_id++)
  1096. {
  1097. struct _starpu_combined_worker *combined_worker = &config->combined_workers[combined_worker_id];
  1098. #ifdef STARPU_HAVE_HWLOC
  1099. hwloc_bitmap_free(combined_worker->hwloc_cpu_set);
  1100. #endif
  1101. free(combined_worker->perf_arch.devices);
  1102. }
  1103. #ifdef STARPU_HAVE_HWLOC
  1104. hwloc_topology_destroy(config->topology.hwtopology);
  1105. #endif
  1106. topology_is_initialized = 0;
  1107. #ifdef STARPU_USE_CUDA
  1108. struct handle_entry *entry, *tmp;
  1109. HASH_ITER(hh, devices_using_cuda, entry, tmp)
  1110. {
  1111. HASH_DEL(devices_using_cuda, entry);
  1112. free(entry);
  1113. }
  1114. devices_using_cuda = NULL;
  1115. #endif
  1116. #if defined(STARPU_USE_CUDA) || defined(STARPU_USE_OPENCL)
  1117. int i;
  1118. for (i=0; i<STARPU_NARCH; i++)
  1119. may_bind_automatically[i] = 0;
  1120. #endif
  1121. }
  1122. void
  1123. _starpu_bind_thread_on_cpu (
  1124. struct _starpu_machine_config *config STARPU_ATTRIBUTE_UNUSED,
  1125. int cpuid STARPU_ATTRIBUTE_UNUSED, int workerid STARPU_ATTRIBUTE_UNUSED)
  1126. {
  1127. #ifdef STARPU_SIMGRID
  1128. return;
  1129. #else
  1130. if (nobind > 0)
  1131. return;
  1132. if (cpuid < 0)
  1133. return;
  1134. if (workerid != STARPU_NOWORKERID && cpuid < STARPU_MAXCPUS)
  1135. {
  1136. int previous = cpu_worker[cpuid];
  1137. if (previous != STARPU_NOWORKERID && previous != workerid)
  1138. _STARPU_DISP("Warning: both workers %d and %d are bound to the same PU %d, this will strongly degrade performance\n", previous, workerid, cpuid);
  1139. else
  1140. cpu_worker[cpuid] = workerid;
  1141. }
  1142. #ifdef STARPU_HAVE_HWLOC
  1143. const struct hwloc_topology_support *support;
  1144. #ifdef STARPU_USE_OPENCL
  1145. _starpu_opencl_init();
  1146. #endif
  1147. #ifdef STARPU_USE_CUDA
  1148. _starpu_init_cuda();
  1149. #endif
  1150. _starpu_init_topology(config);
  1151. support = hwloc_topology_get_support (config->topology.hwtopology);
  1152. if (support->cpubind->set_thisthread_cpubind)
  1153. {
  1154. hwloc_obj_t obj =
  1155. hwloc_get_obj_by_depth (config->topology.hwtopology,
  1156. config->pu_depth, cpuid);
  1157. hwloc_bitmap_t set = obj->cpuset;
  1158. int ret;
  1159. hwloc_bitmap_singlify(set);
  1160. ret = hwloc_set_cpubind (config->topology.hwtopology, set,
  1161. HWLOC_CPUBIND_THREAD);
  1162. if (ret)
  1163. {
  1164. perror("hwloc_set_cpubind");
  1165. STARPU_ABORT();
  1166. }
  1167. }
  1168. #elif defined(HAVE_PTHREAD_SETAFFINITY_NP) && defined(__linux__)
  1169. int ret;
  1170. /* fix the thread on the correct cpu */
  1171. cpu_set_t aff_mask;
  1172. CPU_ZERO(&aff_mask);
  1173. CPU_SET(cpuid, &aff_mask);
  1174. starpu_pthread_t self = pthread_self();
  1175. ret = pthread_setaffinity_np(self, sizeof(aff_mask), &aff_mask);
  1176. if (ret)
  1177. {
  1178. perror("binding thread");
  1179. STARPU_ABORT();
  1180. }
  1181. #elif defined(_WIN32)
  1182. DWORD mask = 1 << cpuid;
  1183. if (!SetThreadAffinityMask(GetCurrentThread(), mask))
  1184. {
  1185. _STARPU_ERROR("SetThreadMaskAffinity(%lx) failed\n", mask);
  1186. }
  1187. #else
  1188. #warning no CPU binding support
  1189. #endif
  1190. #endif
  1191. }
  1192. void
  1193. _starpu_bind_thread_on_cpus (
  1194. struct _starpu_machine_config *config STARPU_ATTRIBUTE_UNUSED,
  1195. struct _starpu_combined_worker *combined_worker STARPU_ATTRIBUTE_UNUSED)
  1196. {
  1197. #ifdef STARPU_SIMGRID
  1198. return;
  1199. #endif
  1200. #ifdef STARPU_HAVE_HWLOC
  1201. const struct hwloc_topology_support *support;
  1202. #ifdef STARPU_USE_OPENC
  1203. _starpu_opencl_init();
  1204. #endif
  1205. #ifdef STARPU_USE_CUDA
  1206. _starpu_init_cuda();
  1207. #endif
  1208. _starpu_init_topology(config);
  1209. support = hwloc_topology_get_support(config->topology.hwtopology);
  1210. if (support->cpubind->set_thisthread_cpubind)
  1211. {
  1212. hwloc_bitmap_t set = combined_worker->hwloc_cpu_set;
  1213. int ret;
  1214. ret = hwloc_set_cpubind (config->topology.hwtopology, set,
  1215. HWLOC_CPUBIND_THREAD);
  1216. if (ret)
  1217. {
  1218. perror("binding thread");
  1219. STARPU_ABORT();
  1220. }
  1221. }
  1222. #else
  1223. #ifdef __GLIBC__
  1224. sched_setaffinity(0,sizeof(combined_worker->cpu_set),&combined_worker->cpu_set);
  1225. #else
  1226. # warning no parallel worker CPU binding support
  1227. #endif
  1228. #endif
  1229. }
  1230. static void
  1231. _starpu_init_workers_binding (struct _starpu_machine_config *config, int no_mp_config STARPU_ATTRIBUTE_UNUSED)
  1232. {
  1233. /* launch one thread per CPU */
  1234. unsigned ram_memory_node;
  1235. /* note that even if the CPU cpu are not used, we always have a RAM
  1236. * node */
  1237. /* TODO : support NUMA ;) */
  1238. ram_memory_node = _starpu_memory_node_register(STARPU_CPU_RAM, 0);
  1239. STARPU_ASSERT(ram_memory_node == STARPU_MAIN_RAM);
  1240. #ifdef STARPU_SIMGRID
  1241. char name[16];
  1242. msg_host_t host = _starpu_simgrid_get_host_by_name("RAM");
  1243. STARPU_ASSERT(host);
  1244. _starpu_simgrid_memory_node_set_host(STARPU_MAIN_RAM, host);
  1245. #endif
  1246. /* We will store all the busid of the different (src, dst)
  1247. * combinations in a matrix which we initialize here. */
  1248. _starpu_initialize_busid_matrix();
  1249. /* Each device is initialized,
  1250. * giving it a memory node and a core bind id.
  1251. */
  1252. /* TODO: STARPU_MAXNUMANODES */
  1253. unsigned numa_init[1] = { 1 };
  1254. unsigned numa_memory_nodes[1] = { ram_memory_node };
  1255. #if defined(STARPU_USE_CUDA) || defined(STARPU_SIMGRID)
  1256. unsigned cuda_init[STARPU_MAXCUDADEVS] = { };
  1257. unsigned cuda_memory_nodes[STARPU_MAXCUDADEVS];
  1258. unsigned cuda_bindid[STARPU_MAXCUDADEVS];
  1259. #endif
  1260. #if defined(STARPU_USE_OPENCL) || defined(STARPU_SIMGRID)
  1261. unsigned opencl_init[STARPU_MAXOPENCLDEVS] = { };
  1262. unsigned opencl_memory_nodes[STARPU_MAXOPENCLDEVS];
  1263. unsigned opencl_bindid[STARPU_MAXOPENCLDEVS];
  1264. #endif
  1265. #ifdef STARPU_USE_MIC
  1266. unsigned mic_init[STARPU_MAXMICDEVS] = { };
  1267. unsigned mic_memory_nodes[STARPU_MAXMICDEVS];
  1268. unsigned mic_bindid[STARPU_MAXMICDEVS];
  1269. #endif
  1270. unsigned bindid;
  1271. for (bindid = 0; bindid < config->nbindid; bindid++)
  1272. {
  1273. free(config->bindid_workers[bindid].workerids);
  1274. config->bindid_workers[bindid].workerids = NULL;
  1275. config->bindid_workers[bindid].nworkers = 0;
  1276. }
  1277. unsigned worker;
  1278. for (worker = 0; worker < config->topology.nworkers; worker++)
  1279. {
  1280. unsigned memory_node = -1;
  1281. struct _starpu_worker *workerarg = &config->workers[worker];
  1282. unsigned devid = workerarg->devid;
  1283. #if defined(STARPU_USE_CUDA) || defined(STARPU_USE_OPENCL) || defined(STARPU_USE_MIC) || defined(STARPU_SIMGRID)
  1284. /* Perhaps the worker has some "favourite" bindings */
  1285. int *preferred_binding = NULL;
  1286. int npreferred = 0;
  1287. #endif
  1288. /* select the memory node that contains worker's memory */
  1289. switch (workerarg->arch)
  1290. {
  1291. case STARPU_CPU_WORKER:
  1292. {
  1293. /* TODO: NUMA */
  1294. int numaid = 0;
  1295. /* "dedicate" a cpu core to that worker */
  1296. if (numa_init[numaid])
  1297. {
  1298. memory_node = numa_memory_nodes[numaid];
  1299. }
  1300. else
  1301. {
  1302. numa_init[numaid] = 1;
  1303. memory_node = numa_memory_nodes[numaid] = _starpu_memory_node_register(STARPU_CPU_RAM, numaid);
  1304. #ifdef STARPU_SIMGRID
  1305. snprintf(name, sizeof(name), "RAM%d", numaid);
  1306. host = _starpu_simgrid_get_host_by_name(name);
  1307. STARPU_ASSERT(host);
  1308. _starpu_simgrid_memory_node_set_host(memory_node, host);
  1309. #endif
  1310. }
  1311. workerarg->bindid = _starpu_get_next_bindid(config, NULL, 0);
  1312. _starpu_memory_node_add_nworkers(memory_node);
  1313. #ifdef STARPU_SIMGRID
  1314. starpu_pthread_queue_register(&workerarg->wait, &_starpu_simgrid_transfer_queue[memory_node]);
  1315. if (memory_node != STARPU_MAIN_RAM)
  1316. starpu_pthread_queue_register(&workerarg->wait, &_starpu_simgrid_transfer_queue[STARPU_MAIN_RAM]);
  1317. #endif
  1318. break;
  1319. }
  1320. #if defined(STARPU_USE_CUDA) || defined(STARPU_SIMGRID)
  1321. case STARPU_CUDA_WORKER:
  1322. #ifndef STARPU_SIMGRID
  1323. if (may_bind_automatically[STARPU_CUDA_WORKER])
  1324. {
  1325. /* StarPU is allowed to bind threads automatically */
  1326. preferred_binding = _starpu_get_cuda_affinity_vector(devid);
  1327. npreferred = config->topology.nhwpus;
  1328. }
  1329. #endif /* SIMGRID */
  1330. if (cuda_init[devid])
  1331. {
  1332. memory_node = cuda_memory_nodes[devid];
  1333. #ifndef STARPU_SIMGRID
  1334. workerarg->bindid = cuda_bindid[devid];
  1335. #endif /* SIMGRID */
  1336. }
  1337. else
  1338. {
  1339. cuda_init[devid] = 1;
  1340. workerarg->bindid = cuda_bindid[devid] = _starpu_get_next_bindid(config, preferred_binding, npreferred);
  1341. memory_node = cuda_memory_nodes[devid] = _starpu_memory_node_register(STARPU_CUDA_RAM, devid);
  1342. _starpu_register_bus(STARPU_MAIN_RAM, memory_node);
  1343. _starpu_register_bus(memory_node, STARPU_MAIN_RAM);
  1344. #ifdef STARPU_SIMGRID
  1345. const char* cuda_memcpy_peer;
  1346. snprintf(name, sizeof(name), "CUDA%d", devid);
  1347. host = _starpu_simgrid_get_host_by_name(name);
  1348. STARPU_ASSERT(host);
  1349. _starpu_simgrid_memory_node_set_host(memory_node, host);
  1350. cuda_memcpy_peer = MSG_host_get_property_value(host, "memcpy_peer");
  1351. #endif /* SIMGRID */
  1352. if (
  1353. #ifdef STARPU_SIMGRID
  1354. cuda_memcpy_peer && atoll(cuda_memcpy_peer)
  1355. #elif defined(HAVE_CUDA_MEMCPY_PEER)
  1356. 1
  1357. #else /* MEMCPY_PEER */
  1358. 0
  1359. #endif /* MEMCPY_PEER */
  1360. )
  1361. {
  1362. unsigned worker2;
  1363. for (worker2 = 0; worker2 < worker; worker2++)
  1364. {
  1365. struct _starpu_worker *workerarg2 = &config->workers[worker2];
  1366. if (workerarg2->arch == STARPU_CUDA_WORKER)
  1367. {
  1368. unsigned memory_node2 = starpu_worker_get_memory_node(worker2);
  1369. _starpu_register_bus(memory_node2, memory_node);
  1370. _starpu_register_bus(memory_node, memory_node2);
  1371. }
  1372. }
  1373. }
  1374. }
  1375. _starpu_memory_node_add_nworkers(memory_node);
  1376. #ifdef STARPU_SIMGRID
  1377. starpu_pthread_queue_register(&workerarg->set->workers[0].wait, &_starpu_simgrid_transfer_queue[memory_node]);
  1378. starpu_pthread_queue_register(&workerarg->set->workers[0].wait, &_starpu_simgrid_transfer_queue[STARPU_MAIN_RAM]);
  1379. #endif
  1380. break;
  1381. #endif
  1382. #if defined(STARPU_USE_OPENCL) || defined(STARPU_SIMGRID)
  1383. case STARPU_OPENCL_WORKER:
  1384. #ifndef STARPU_SIMGRID
  1385. if (may_bind_automatically[STARPU_OPENCL_WORKER])
  1386. {
  1387. /* StarPU is allowed to bind threads automatically */
  1388. preferred_binding = _starpu_get_opencl_affinity_vector(devid);
  1389. npreferred = config->topology.nhwpus;
  1390. }
  1391. #endif /* SIMGRID */
  1392. if (opencl_init[devid])
  1393. {
  1394. memory_node = opencl_memory_nodes[devid];
  1395. #ifndef STARPU_SIMGRID
  1396. workerarg->bindid = opencl_bindid[devid];
  1397. #endif /* SIMGRID */
  1398. }
  1399. else
  1400. {
  1401. opencl_init[devid] = 1;
  1402. workerarg->bindid = opencl_bindid[devid] = _starpu_get_next_bindid(config, preferred_binding, npreferred);
  1403. memory_node = opencl_memory_nodes[devid] = _starpu_memory_node_register(STARPU_OPENCL_RAM, devid);
  1404. _starpu_register_bus(STARPU_MAIN_RAM, memory_node);
  1405. _starpu_register_bus(memory_node, STARPU_MAIN_RAM);
  1406. #ifdef STARPU_SIMGRID
  1407. snprintf(name, sizeof(name), "OpenCL%d", devid);
  1408. host = _starpu_simgrid_get_host_by_name(name);
  1409. STARPU_ASSERT(host);
  1410. _starpu_simgrid_memory_node_set_host(memory_node, host);
  1411. #endif /* SIMGRID */
  1412. }
  1413. _starpu_memory_node_add_nworkers(memory_node);
  1414. #ifdef STARPU_SIMGRID
  1415. starpu_pthread_queue_register(&workerarg->wait, &_starpu_simgrid_transfer_queue[memory_node]);
  1416. starpu_pthread_queue_register(&workerarg->wait, &_starpu_simgrid_transfer_queue[STARPU_MAIN_RAM]);
  1417. #endif
  1418. break;
  1419. #endif
  1420. #ifdef STARPU_USE_MIC
  1421. case STARPU_MIC_WORKER:
  1422. if (mic_init[devid])
  1423. {
  1424. memory_node = mic_memory_nodes[devid];
  1425. }
  1426. else
  1427. {
  1428. mic_init[devid] = 1;
  1429. /* TODO */
  1430. //if (may_bind_automatically)
  1431. //{
  1432. // /* StarPU is allowed to bind threads automatically */
  1433. // preferred_binding = _starpu_get_mic_affinity_vector(devid);
  1434. // npreferred = config->topology.nhwpus;
  1435. //}
  1436. mic_bindid[devid] = _starpu_get_next_bindid(config, preferred_binding, npreferred);
  1437. memory_node = mic_memory_nodes[devid] = _starpu_memory_node_register(STARPU_MIC_RAM, devid);
  1438. _starpu_register_bus(STARPU_MAIN_RAM, memory_node);
  1439. _starpu_register_bus(memory_node, STARPU_MAIN_RAM);
  1440. }
  1441. workerarg->bindid = mic_bindid[devid];
  1442. _starpu_memory_node_add_nworkers(memory_node);
  1443. #ifdef STARPU_SIMGRID
  1444. starpu_pthread_queue_register(&workerarg->set->workers[0].wait, &_starpu_simgrid_transfer_queue[memory_node]);
  1445. starpu_pthread_queue_register(&workerarg->set->workers[0].wait, &_starpu_simgrid_transfer_queue[STARPU_MAIN_RAM]);
  1446. #endif
  1447. break;
  1448. #endif /* STARPU_USE_MIC */
  1449. #ifdef STARPU_USE_SCC
  1450. case STARPU_SCC_WORKER:
  1451. {
  1452. /* Node 0 represents the SCC shared memory when we're on SCC. */
  1453. struct _starpu_memory_node_descr *descr = _starpu_memory_node_get_description();
  1454. descr->nodes[ram_memory_node] = STARPU_SCC_SHM;
  1455. memory_node = ram_memory_node;
  1456. _starpu_memory_node_add_nworkers(memory_node);
  1457. #ifdef STARPU_SIMGRID
  1458. starpu_pthread_queue_register(&workerarg->wait, &_starpu_simgrid_transfer_queue[memory_node]);
  1459. starpu_pthread_queue_register(&workerarg->wait, &_starpu_simgrid_transfer_queue[STARPU_MAIN_RAM]);
  1460. #endif
  1461. }
  1462. break;
  1463. #endif
  1464. default:
  1465. STARPU_ABORT();
  1466. }
  1467. workerarg->memory_node = memory_node;
  1468. _STARPU_DEBUG("worker %d type %d devid %d bound to cpu %d, STARPU memory node %d\n", worker, workerarg->arch, devid, workerarg->bindid, memory_node);
  1469. #ifdef __GLIBC__
  1470. if (workerarg->bindid != -1)
  1471. {
  1472. /* Save the initial cpuset */
  1473. CPU_ZERO(&workerarg->cpu_set);
  1474. CPU_SET(workerarg->bindid, &workerarg->cpu_set);
  1475. }
  1476. #endif /* __GLIBC__ */
  1477. #ifdef STARPU_HAVE_HWLOC
  1478. if (workerarg->bindid == -1)
  1479. {
  1480. workerarg->hwloc_cpu_set = hwloc_bitmap_alloc();
  1481. }
  1482. else
  1483. {
  1484. /* Put the worker descriptor in the userdata field of the
  1485. * hwloc object describing the CPU */
  1486. hwloc_obj_t worker_obj = hwloc_get_obj_by_depth(config->topology.hwtopology,
  1487. config->pu_depth,
  1488. workerarg->bindid);
  1489. if (worker_obj->userdata == NULL)
  1490. {
  1491. worker_obj->userdata = _starpu_worker_list_new();
  1492. }
  1493. _starpu_worker_list_push_front(worker_obj->userdata, workerarg);
  1494. /* Clear the cpu set and set the cpu */
  1495. workerarg->hwloc_cpu_set = hwloc_bitmap_dup (worker_obj->cpuset);
  1496. }
  1497. #endif
  1498. if (workerarg->bindid != -1)
  1499. {
  1500. bindid = workerarg->bindid;
  1501. unsigned old_nbindid = config->nbindid;
  1502. if (bindid >= old_nbindid)
  1503. {
  1504. /* More room needed */
  1505. if (!old_nbindid)
  1506. config->nbindid = STARPU_NMAXWORKERS;
  1507. else
  1508. config->nbindid = 2 * old_nbindid;
  1509. config->bindid_workers = realloc(config->bindid_workers, config->nbindid * sizeof(config->bindid_workers[0]));
  1510. memset(&config->bindid_workers[old_nbindid], 0, (config->nbindid - old_nbindid) * sizeof(config->bindid_workers[0]));
  1511. }
  1512. /* Add slot for this worker */
  1513. /* Don't care about amortizing the cost, there are usually very few workers sharing the same bindid */
  1514. config->bindid_workers[bindid].nworkers++;
  1515. config->bindid_workers[bindid].workerids = realloc(config->bindid_workers[bindid].workerids, config->bindid_workers[bindid].nworkers * sizeof(config->bindid_workers[bindid].workerids[0]));
  1516. config->bindid_workers[bindid].workerids[config->bindid_workers[bindid].nworkers-1] = worker;
  1517. }
  1518. }
  1519. }
  1520. int
  1521. _starpu_build_topology (struct _starpu_machine_config *config, int no_mp_config)
  1522. {
  1523. int ret;
  1524. unsigned i;
  1525. ret = _starpu_init_machine_config(config, no_mp_config);
  1526. if (ret)
  1527. return ret;
  1528. /* for the data management library */
  1529. _starpu_memory_nodes_init();
  1530. _starpu_datastats_init();
  1531. _starpu_init_workers_binding(config, no_mp_config);
  1532. config->cpus_nodeid = -1;
  1533. config->cuda_nodeid = -1;
  1534. config->opencl_nodeid = -1;
  1535. config->mic_nodeid = -1;
  1536. config->scc_nodeid = -1;
  1537. for (i = 0; i < starpu_worker_get_count(); i++)
  1538. {
  1539. switch (starpu_worker_get_type(i))
  1540. {
  1541. case STARPU_CPU_WORKER:
  1542. if (config->cpus_nodeid == -1)
  1543. config->cpus_nodeid = starpu_worker_get_memory_node(i);
  1544. else if (config->cpus_nodeid != (int) starpu_worker_get_memory_node(i))
  1545. config->cpus_nodeid = -2;
  1546. break;
  1547. case STARPU_CUDA_WORKER:
  1548. if (config->cuda_nodeid == -1)
  1549. config->cuda_nodeid = starpu_worker_get_memory_node(i);
  1550. else if (config->cuda_nodeid != (int) starpu_worker_get_memory_node(i))
  1551. config->cuda_nodeid = -2;
  1552. break;
  1553. case STARPU_OPENCL_WORKER:
  1554. if (config->opencl_nodeid == -1)
  1555. config->opencl_nodeid = starpu_worker_get_memory_node(i);
  1556. else if (config->opencl_nodeid != (int) starpu_worker_get_memory_node(i))
  1557. config->opencl_nodeid = -2;
  1558. break;
  1559. case STARPU_MIC_WORKER:
  1560. if (config->mic_nodeid == -1)
  1561. config->mic_nodeid = starpu_worker_get_memory_node(i);
  1562. else if (config->mic_nodeid != (int) starpu_worker_get_memory_node(i))
  1563. config->mic_nodeid = -2;
  1564. break;
  1565. case STARPU_SCC_WORKER:
  1566. if (config->scc_nodeid == -1)
  1567. config->scc_nodeid = starpu_worker_get_memory_node(i);
  1568. else if (config->scc_nodeid != (int) starpu_worker_get_memory_node(i))
  1569. config->scc_nodeid = -2;
  1570. break;
  1571. case STARPU_ANY_WORKER:
  1572. STARPU_ASSERT(0);
  1573. }
  1574. }
  1575. return 0;
  1576. }
  1577. void _starpu_destroy_topology(struct _starpu_machine_config *config STARPU_ATTRIBUTE_UNUSED)
  1578. {
  1579. #ifdef STARPU_USE_MIC
  1580. _starpu_deinit_mp_config(config);
  1581. #endif
  1582. /* cleanup StarPU internal data structures */
  1583. _starpu_memory_nodes_deinit();
  1584. _starpu_destroy_machine_config(config);
  1585. }
  1586. void
  1587. starpu_topology_print (FILE *output)
  1588. {
  1589. struct _starpu_machine_config *config = _starpu_get_machine_config();
  1590. struct _starpu_machine_topology *topology = &config->topology;
  1591. unsigned pu;
  1592. unsigned worker;
  1593. unsigned nworkers = starpu_worker_get_count();
  1594. unsigned ncombinedworkers = topology->ncombinedworkers;
  1595. unsigned nthreads_per_core = topology->nhwpus / topology->nhwcpus;
  1596. for (pu = 0; pu < topology->nhwpus; pu++)
  1597. {
  1598. if ((pu % nthreads_per_core) == 0)
  1599. fprintf(output, "core %u", pu / nthreads_per_core);
  1600. fprintf(output, "\tPU %u\t", pu);
  1601. for (worker = 0;
  1602. worker < nworkers + ncombinedworkers;
  1603. worker++)
  1604. {
  1605. if (worker < nworkers)
  1606. {
  1607. struct _starpu_worker *workerarg = &config->workers[worker];
  1608. if (workerarg->bindid == (int) pu)
  1609. {
  1610. char name[256];
  1611. starpu_worker_get_name (worker, name,
  1612. sizeof(name));
  1613. fprintf(output, "%s\t", name);
  1614. }
  1615. }
  1616. else
  1617. {
  1618. int worker_size, i;
  1619. int *combined_workerid;
  1620. starpu_combined_worker_get_description(worker, &worker_size, &combined_workerid);
  1621. for (i = 0; i < worker_size; i++)
  1622. {
  1623. if (topology->workers_bindid[combined_workerid[i]] == pu)
  1624. fprintf(output, "comb %u\t", worker-nworkers);
  1625. }
  1626. }
  1627. }
  1628. fprintf(output, "\n");
  1629. }
  1630. }