topology.c 46 KB

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