topology.c 55 KB

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