topology.c 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613
  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 cpu 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. * cpus. */
  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 cpu 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. * cpus. */
  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, l;
  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. while(nbindids < STARPU_NMAXWORKERS)
  482. {
  483. k=0;
  484. l= nhyperthreads-nth_per_core;
  485. /* by default, we take a round robin policy */
  486. for (i = 0; i < STARPU_NMAXWORKERS; i++)
  487. {
  488. if(k >= nth_per_core)
  489. {
  490. if(l == 0)
  491. {
  492. k=0;
  493. l= nhyperthreads-nth_per_core;
  494. }
  495. else
  496. l--;
  497. }
  498. if(k < nth_per_core || nhyperthreads == nth_per_core)
  499. {
  500. topology->workers_bindid[nbindids++] =
  501. (unsigned)(i % topology->nhwpus);
  502. k++;
  503. }
  504. }
  505. }
  506. }
  507. }
  508. /* This function gets the identifier of the next cpu on which to bind a
  509. * worker. In case a list of preferred cpus was specified, we look for a an
  510. * available cpu among the list if possible, otherwise a round-robin policy is
  511. * used. */
  512. static inline int
  513. _starpu_get_next_bindid (struct _starpu_machine_config *config,
  514. int *preferred_binding, int npreferred)
  515. {
  516. struct _starpu_machine_topology *topology = &config->topology;
  517. unsigned found = 0;
  518. int current_preferred;
  519. for (current_preferred = 0;
  520. current_preferred < npreferred;
  521. current_preferred++)
  522. {
  523. if (found)
  524. break;
  525. unsigned requested_cpu = preferred_binding[current_preferred];
  526. /* can we bind the worker on the requested cpu ? */
  527. unsigned ind;
  528. for (ind = config->current_bindid;
  529. ind < topology->nhwpus;
  530. ind++)
  531. {
  532. if (topology->workers_bindid[ind] == requested_cpu)
  533. {
  534. /* the cpu is available, we use it ! In order
  535. * to make sure that it will not be used again
  536. * later on, we remove the entry from the
  537. * list */
  538. topology->workers_bindid[ind] =
  539. topology->workers_bindid[config->current_bindid];
  540. topology->workers_bindid[config->current_bindid] = requested_cpu;
  541. found = 1;
  542. break;
  543. }
  544. }
  545. }
  546. unsigned i = ((config->current_bindid++) % STARPU_NMAXWORKERS);
  547. return (int)topology->workers_bindid[i];
  548. }
  549. unsigned
  550. _starpu_topology_get_nhwcpu (struct _starpu_machine_config *config)
  551. {
  552. #if defined(STARPU_USE_OPENCL) || defined(STARPU_SIMGRID)
  553. _starpu_opencl_init();
  554. #endif
  555. #if defined(STARPU_USE_CUDA) || defined(STARPU_SIMGRID)
  556. _starpu_init_cuda();
  557. #endif
  558. _starpu_init_topology(config);
  559. return config->topology.nhwcpus;
  560. }
  561. unsigned
  562. _starpu_topology_get_nhwpu (struct _starpu_machine_config *config)
  563. {
  564. #if defined(STARPU_USE_OPENCL) || defined(STARPU_SIMGRID)
  565. _starpu_opencl_init();
  566. #endif
  567. #if defined(STARPU_USE_CUDA) || defined(STARPU_SIMGRID)
  568. _starpu_init_cuda();
  569. #endif
  570. _starpu_init_topology(config);
  571. return config->topology.nhwpus;
  572. }
  573. #ifdef STARPU_USE_MIC
  574. static void
  575. _starpu_init_mic_config (struct _starpu_machine_config *config,
  576. struct starpu_conf *user_conf,
  577. unsigned mic_idx)
  578. {
  579. // Configure the MIC device of index MIC_IDX.
  580. struct _starpu_machine_topology *topology = &config->topology;
  581. topology->nhwmiccores[mic_idx] = 0;
  582. _starpu_init_mic_topology (config, mic_idx);
  583. int nmiccores;
  584. nmiccores = starpu_get_env_number("STARPU_NMIC");
  585. /* STARPU_NMIC is not set. Did the user specify anything ? */
  586. if (nmiccores == -1 && user_conf)
  587. nmiccores = user_conf->nmic;
  588. if (nmiccores != 0)
  589. {
  590. if (nmiccores == -1)
  591. {
  592. /* Nothing was specified, so let's use the number of
  593. * detected mic cores. ! */
  594. nmiccores = topology->nhwmiccores[mic_idx];
  595. }
  596. else
  597. {
  598. if ((unsigned) nmiccores > topology->nhwmiccores[mic_idx])
  599. {
  600. /* The user requires more MIC devices than there is available */
  601. fprintf(stderr,
  602. "# Warning: %d MIC devices requested. Only %d available.\n",
  603. nmiccores, topology->nhwmiccores[mic_idx]);
  604. nmiccores = topology->nhwmiccores[mic_idx];
  605. }
  606. }
  607. }
  608. topology->nmiccores[mic_idx] = nmiccores;
  609. STARPU_ASSERT(topology->nmiccores[mic_idx] + topology->nworkers <= STARPU_NMAXWORKERS);
  610. /* _starpu_initialize_workers_mic_deviceid (config); */
  611. unsigned miccore_id;
  612. for (miccore_id = 0; miccore_id < topology->nmiccores[mic_idx]; miccore_id++)
  613. {
  614. int worker_idx = topology->nworkers + miccore_id;
  615. struct starpu_perfmodel_arch arch;
  616. arch.type = STARPU_MIC_WORKER;
  617. arch.devid = mic_idx;
  618. arch.ncore = 0;
  619. config->workers[worker_idx].arch = STARPU_MIC_WORKER;
  620. config->workers[worker_idx].perf_arch = arch;
  621. config->workers[worker_idx].devid = mic_idx;
  622. config->workers[worker_idx].subworkerid = miccore_id;
  623. config->workers[worker_idx].worker_mask = STARPU_MIC;
  624. config->worker_mask |= STARPU_MIC;
  625. }
  626. topology->nworkers += topology->nmiccores[mic_idx];
  627. }
  628. #ifdef STARPU_USE_MIC
  629. static COIENGINE handles[2];
  630. static COIPROCESS process[2];
  631. #endif
  632. static void
  633. _starpu_init_mp_config (struct _starpu_machine_config *config,
  634. struct starpu_conf *user_conf)
  635. {
  636. /* Discover and configure the mp topology. That means:
  637. * - discover the number of mp nodes;
  638. * - initialize each discovered node;
  639. * - discover the local topology (number of PUs/devices) of each node;
  640. * - configure the workers accordingly.
  641. */
  642. struct _starpu_machine_topology *topology = &config->topology;
  643. // We currently only support MIC at this level.
  644. #ifdef STARPU_USE_MIC
  645. /* Discover and initialize the number of MIC nodes through the mp
  646. * infrastructure. */
  647. unsigned nhwmicdevices = _starpu_mic_src_get_device_count();
  648. int reqmicdevices = starpu_get_env_number("STARPU_NMICDEVS");
  649. if (-1 == reqmicdevices)
  650. reqmicdevices = nhwmicdevices;
  651. topology->nmicdevices = 0;
  652. unsigned i;
  653. for (i = 0; i < STARPU_MIN (nhwmicdevices, (unsigned) reqmicdevices); i++)
  654. if (0 == _starpu_init_mic_node (config, i, &handles[i], &process[i]))
  655. topology->nmicdevices++;
  656. for (i = 0; i < topology->nmicdevices; i++)
  657. _starpu_init_mic_config (config, user_conf, i);
  658. #endif
  659. }
  660. static void
  661. _starpu_deinit_mic_node (unsigned mic_idx)
  662. {
  663. _starpu_mp_common_send_command(mic_nodes[mic_idx], STARPU_EXIT, NULL, 0);
  664. COIProcessDestroy(process[mic_idx], -1, 0, NULL, NULL);
  665. _starpu_mp_common_node_destroy(mic_nodes[mic_idx]);
  666. }
  667. static void
  668. _starpu_deinit_mp_config (struct _starpu_machine_config *config)
  669. {
  670. struct _starpu_machine_topology *topology = &config->topology;
  671. unsigned i;
  672. for (i = 0; i < topology->nmicdevices; i++)
  673. _starpu_deinit_mic_node (i);
  674. _starpu_mic_clear_kernels();
  675. }
  676. #endif
  677. static int
  678. _starpu_init_machine_config (struct _starpu_machine_config *config, int no_mp_config STARPU_ATTRIBUTE_UNUSED)
  679. {
  680. int i;
  681. for (i = 0; i < STARPU_NMAXWORKERS; i++)
  682. config->workers[i].workerid = i;
  683. struct _starpu_machine_topology *topology = &config->topology;
  684. topology->nworkers = 0;
  685. topology->ncombinedworkers = 0;
  686. topology->nsched_ctxs = 0;
  687. #if defined(STARPU_USE_OPENCL) || defined(STARPU_SIMGRID)
  688. _starpu_opencl_init();
  689. #endif
  690. #if defined(STARPU_USE_CUDA) || defined(STARPU_SIMGRID)
  691. _starpu_init_cuda();
  692. #endif
  693. _starpu_init_topology(config);
  694. _starpu_initialize_workers_bindid(config);
  695. #if defined(STARPU_USE_CUDA) || defined(STARPU_SIMGRID)
  696. int ncuda = config->conf->ncuda;
  697. if (ncuda != 0)
  698. {
  699. /* The user did not disable CUDA. We need to initialize CUDA
  700. * early to count the number of devices */
  701. _starpu_init_cuda();
  702. int nb_devices = _starpu_get_cuda_device_count();
  703. if (ncuda == -1)
  704. {
  705. /* Nothing was specified, so let's choose ! */
  706. ncuda = nb_devices;
  707. }
  708. else
  709. {
  710. if (ncuda > nb_devices)
  711. {
  712. /* The user requires more CUDA devices than
  713. * there is available */
  714. _STARPU_DISP("Warning: %d CUDA devices requested. Only %d available.\n", ncuda, nb_devices);
  715. ncuda = nb_devices;
  716. }
  717. }
  718. }
  719. /* Now we know how many CUDA devices will be used */
  720. topology->ncudagpus = ncuda;
  721. STARPU_ASSERT(topology->ncudagpus <= STARPU_MAXCUDADEVS);
  722. _starpu_initialize_workers_cuda_gpuid(config);
  723. unsigned cudagpu;
  724. for (cudagpu = 0; cudagpu < topology->ncudagpus; cudagpu++)
  725. {
  726. int worker_idx = topology->nworkers + cudagpu;
  727. config->workers[worker_idx].arch = STARPU_CUDA_WORKER;
  728. int devid = _starpu_get_next_cuda_gpuid(config);
  729. config->workers[worker_idx].perf_arch.type = STARPU_CUDA_WORKER;
  730. config->workers[worker_idx].perf_arch.devid = cudagpu;
  731. config->workers[worker_idx].perf_arch.ncore = 0;
  732. config->workers[worker_idx].devid = devid;
  733. config->workers[worker_idx].subworkerid = 0;
  734. config->workers[worker_idx].worker_mask = STARPU_CUDA;
  735. config->worker_mask |= STARPU_CUDA;
  736. struct handle_entry *entry;
  737. entry = (struct handle_entry *) malloc(sizeof(*entry));
  738. STARPU_ASSERT(entry != NULL);
  739. entry->gpuid = devid;
  740. HASH_ADD_INT(devices_using_cuda, gpuid, entry);
  741. }
  742. topology->nworkers += topology->ncudagpus;
  743. #endif
  744. #if defined(STARPU_USE_OPENCL) || defined(STARPU_SIMGRID)
  745. int nopencl = config->conf->nopencl;
  746. if (nopencl != 0)
  747. {
  748. /* The user did not disable OPENCL. We need to initialize
  749. * OpenCL early to count the number of devices */
  750. _starpu_opencl_init();
  751. int nb_devices;
  752. nb_devices = _starpu_opencl_get_device_count();
  753. if (nopencl == -1)
  754. {
  755. /* Nothing was specified, so let's choose ! */
  756. nopencl = nb_devices;
  757. if (nopencl > STARPU_MAXOPENCLDEVS)
  758. {
  759. _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);
  760. nopencl = STARPU_MAXOPENCLDEVS;
  761. }
  762. }
  763. else
  764. {
  765. /* Let's make sure this value is OK. */
  766. if (nopencl > nb_devices)
  767. {
  768. /* The user requires more OpenCL devices than
  769. * there is available */
  770. _STARPU_DISP("Warning: %d OpenCL devices requested. Only %d available.\n", nopencl, nb_devices);
  771. nopencl = nb_devices;
  772. }
  773. /* Let's make sure this value is OK. */
  774. if (nopencl > STARPU_MAXOPENCLDEVS)
  775. {
  776. _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);
  777. nopencl = STARPU_MAXOPENCLDEVS;
  778. }
  779. }
  780. }
  781. topology->nopenclgpus = nopencl;
  782. STARPU_ASSERT(topology->nopenclgpus + topology->nworkers <= STARPU_NMAXWORKERS);
  783. _starpu_initialize_workers_opencl_gpuid(config);
  784. unsigned openclgpu;
  785. for (openclgpu = 0; openclgpu < topology->nopenclgpus; openclgpu++)
  786. {
  787. int worker_idx = topology->nworkers + openclgpu;
  788. int devid = _starpu_get_next_opencl_gpuid(config);
  789. if (devid == -1)
  790. { // There is no more devices left
  791. topology->nopenclgpus = openclgpu;
  792. break;
  793. }
  794. config->workers[worker_idx].arch = STARPU_OPENCL_WORKER;
  795. config->workers[worker_idx].perf_arch.type = STARPU_OPENCL_WORKER;
  796. config->workers[worker_idx].perf_arch.devid = devid;
  797. config->workers[worker_idx].perf_arch.ncore = 0;
  798. config->workers[worker_idx].subworkerid = 0;
  799. config->workers[worker_idx].devid = devid;
  800. config->workers[worker_idx].worker_mask = STARPU_OPENCL;
  801. config->worker_mask |= STARPU_OPENCL;
  802. }
  803. topology->nworkers += topology->nopenclgpus;
  804. #endif
  805. #ifdef STARPU_USE_SCC
  806. int nscc = config->conf->nscc;
  807. unsigned nb_scc_nodes = _starpu_scc_src_get_device_count();
  808. if (nscc != 0)
  809. {
  810. /* The user did not disable SCC. We need to count
  811. * the number of devices */
  812. int nb_devices = nb_scc_nodes;
  813. if (nscc == -1)
  814. {
  815. /* Nothing was specified, so let's choose ! */
  816. nscc = nb_devices;
  817. if (nscc > STARPU_MAXSCCDEVS)
  818. {
  819. _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);
  820. nscc = STARPU_MAXSCCDEVS;
  821. }
  822. }
  823. else
  824. {
  825. /* Let's make sure this value is OK. */
  826. if (nscc > nb_devices)
  827. {
  828. /* The user requires more SCC devices than there is available */
  829. _STARPU_DISP("Warning: %d SCC devices requested. Only %d available.\n", nscc, nb_devices);
  830. nscc = nb_devices;
  831. }
  832. /* Let's make sure this value is OK. */
  833. if (nscc > STARPU_MAXSCCDEVS)
  834. {
  835. _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);
  836. nscc = STARPU_MAXSCCDEVS;
  837. }
  838. }
  839. }
  840. /* Now we know how many SCC devices will be used */
  841. topology->nsccdevices = nscc;
  842. STARPU_ASSERT(topology->nsccdevices + topology->nworkers <= STARPU_NMAXWORKERS);
  843. _starpu_initialize_workers_scc_deviceid(config);
  844. unsigned sccdev;
  845. for (sccdev = 0; sccdev < topology->nsccdevices; sccdev++)
  846. {
  847. config->workers[topology->nworkers + sccdev].arch = STARPU_SCC_WORKER;
  848. int devid = _starpu_get_next_scc_deviceid(config);
  849. config->workers[topology->nworkers + sccdev].perf_arch.type = STARPU_SCC_WORKER;
  850. config->workers[topology->nworkers + sccdev].perf_arch.devid = sccdev;
  851. config->workers[topology->nworkers + sccdev].perf_arch.ncore = 0;
  852. config->workers[topology->nworkers + sccdev].subworkerid = 0;
  853. config->workers[topology->nworkers + sccdev].devid = devid;
  854. config->workers[topology->nworkers + sccdev].worker_mask = STARPU_SCC;
  855. config->worker_mask |= STARPU_SCC;
  856. }
  857. for (; sccdev < nb_scc_nodes; ++sccdev)
  858. _starpu_scc_exit_useless_node(sccdev);
  859. topology->nworkers += topology->nsccdevices;
  860. #endif /* STARPU_USE_SCC */
  861. /* Unless not requested, we need to complete configuration with the
  862. * ones of the mp nodes. */
  863. #ifdef STARPU_USE_MIC
  864. if (! no_mp_config)
  865. _starpu_init_mp_config (config, config->conf);
  866. #endif
  867. /* we put the CPU section after the accelerator : in case there was an
  868. * accelerator found, we devote one cpu */
  869. #if defined(STARPU_USE_CPU) || defined(STARPU_SIMGRID)
  870. int ncpu = config->conf->ncpus;
  871. if (ncpu != 0)
  872. {
  873. if (ncpu == -1)
  874. {
  875. unsigned mic_busy_cpus = 0;
  876. unsigned j = 0;
  877. for (j = 0; j < STARPU_MAXMICDEVS; j++)
  878. mic_busy_cpus += (topology->nmiccores[j] ? 1 : 0);
  879. unsigned already_busy_cpus = mic_busy_cpus + topology->ncudagpus
  880. + topology->nopenclgpus + topology->nsccdevices;
  881. long avail_cpus = (long) topology->nhwcpus - (long) already_busy_cpus;
  882. if (avail_cpus < 0)
  883. avail_cpus = 0;
  884. int nth_per_core = starpu_get_env_number_default("STARPU_NTHREADS_PER_CORE", 1);
  885. avail_cpus *= nth_per_core;
  886. ncpu = STARPU_MIN(avail_cpus, STARPU_MAXCPUS);
  887. }
  888. else
  889. {
  890. if (ncpu > STARPU_MAXCPUS)
  891. {
  892. _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);
  893. ncpu = STARPU_MAXCPUS;
  894. }
  895. }
  896. }
  897. topology->ncpus = ncpu;
  898. STARPU_ASSERT(topology->ncpus + topology->nworkers <= STARPU_NMAXWORKERS);
  899. unsigned cpu;
  900. for (cpu = 0; cpu < topology->ncpus; cpu++)
  901. {
  902. int worker_idx = topology->nworkers + cpu;
  903. config->workers[worker_idx].arch = STARPU_CPU_WORKER;
  904. config->workers[worker_idx].perf_arch.type = STARPU_CPU_WORKER;
  905. config->workers[worker_idx].perf_arch.devid = 0;
  906. config->workers[worker_idx].perf_arch.ncore = 0;
  907. config->workers[worker_idx].subworkerid = 0;
  908. config->workers[worker_idx].devid = cpu;
  909. config->workers[worker_idx].worker_mask = STARPU_CPU;
  910. config->worker_mask |= STARPU_CPU;
  911. }
  912. topology->nworkers += topology->ncpus;
  913. #endif
  914. if (topology->nworkers == 0)
  915. {
  916. _STARPU_DEBUG("No worker found, aborting ...\n");
  917. return -ENODEV;
  918. }
  919. return 0;
  920. }
  921. void
  922. _starpu_bind_thread_on_cpu (
  923. struct _starpu_machine_config *config STARPU_ATTRIBUTE_UNUSED,
  924. int cpuid)
  925. {
  926. #ifdef STARPU_SIMGRID
  927. return;
  928. #endif
  929. if (starpu_get_env_number("STARPU_WORKERS_NOBIND") > 0)
  930. return;
  931. if (cpuid < 0)
  932. return;
  933. #ifdef STARPU_HAVE_HWLOC
  934. const struct hwloc_topology_support *support;
  935. #ifdef STARPU_USE_OPENCL
  936. _starpu_opencl_init();
  937. #endif
  938. #ifdef STARPU_USE_CUDA
  939. _starpu_init_cuda();
  940. #endif
  941. _starpu_init_topology(config);
  942. support = hwloc_topology_get_support (config->topology.hwtopology);
  943. if (support->cpubind->set_thisthread_cpubind)
  944. {
  945. hwloc_obj_t obj =
  946. hwloc_get_obj_by_depth (config->topology.hwtopology,
  947. config->pu_depth, cpuid);
  948. hwloc_bitmap_t set = obj->cpuset;
  949. int ret;
  950. hwloc_bitmap_singlify(set);
  951. ret = hwloc_set_cpubind (config->topology.hwtopology, set,
  952. HWLOC_CPUBIND_THREAD);
  953. if (ret)
  954. {
  955. perror("hwloc_set_cpubind");
  956. STARPU_ABORT();
  957. }
  958. }
  959. #elif defined(HAVE_PTHREAD_SETAFFINITY_NP) && defined(__linux__)
  960. int ret;
  961. /* fix the thread on the correct cpu */
  962. cpu_set_t aff_mask;
  963. CPU_ZERO(&aff_mask);
  964. CPU_SET(cpuid, &aff_mask);
  965. starpu_pthread_t self = pthread_self();
  966. ret = pthread_setaffinity_np(self, sizeof(aff_mask), &aff_mask);
  967. if (ret)
  968. {
  969. perror("binding thread");
  970. STARPU_ABORT();
  971. }
  972. #elif defined(__MINGW32__) || defined(__CYGWIN__)
  973. DWORD mask = 1 << cpuid;
  974. if (!SetThreadAffinityMask(GetCurrentThread(), mask))
  975. {
  976. _STARPU_ERROR("SetThreadMaskAffinity(%lx) failed\n", mask);
  977. }
  978. #else
  979. #warning no CPU binding support
  980. #endif
  981. }
  982. void
  983. _starpu_bind_thread_on_cpus (
  984. struct _starpu_machine_config *config STARPU_ATTRIBUTE_UNUSED,
  985. struct _starpu_combined_worker *combined_worker STARPU_ATTRIBUTE_UNUSED)
  986. {
  987. #ifdef STARPU_SIMGRID
  988. return;
  989. #endif
  990. #ifdef STARPU_HAVE_HWLOC
  991. const struct hwloc_topology_support *support;
  992. #ifdef STARPU_USE_OPENC
  993. _starpu_opencl_init();
  994. #endif
  995. #ifdef STARPU_USE_CUDA
  996. _starpu_init_cuda();
  997. #endif
  998. _starpu_init_topology(config);
  999. support = hwloc_topology_get_support(config->topology.hwtopology);
  1000. if (support->cpubind->set_thisthread_cpubind)
  1001. {
  1002. hwloc_bitmap_t set = combined_worker->hwloc_cpu_set;
  1003. int ret;
  1004. ret = hwloc_set_cpubind (config->topology.hwtopology, set,
  1005. HWLOC_CPUBIND_THREAD);
  1006. if (ret)
  1007. {
  1008. perror("binding thread");
  1009. STARPU_ABORT();
  1010. }
  1011. }
  1012. #else
  1013. #ifdef __GLIBC__
  1014. sched_setaffinity(0,sizeof(combined_worker->cpu_set),&combined_worker->cpu_set);
  1015. #else
  1016. # warning no parallel worker CPU binding support
  1017. #endif
  1018. #endif
  1019. }
  1020. static void
  1021. _starpu_init_workers_binding (struct _starpu_machine_config *config, int no_mp_config STARPU_ATTRIBUTE_UNUSED)
  1022. {
  1023. /* launch one thread per CPU */
  1024. unsigned ram_memory_node;
  1025. /* note that even if the CPU cpu are not used, we always have a RAM
  1026. * node */
  1027. /* TODO : support NUMA ;) */
  1028. ram_memory_node = _starpu_memory_node_register(STARPU_CPU_RAM, 0);
  1029. STARPU_ASSERT(ram_memory_node == STARPU_MAIN_RAM);
  1030. #ifdef STARPU_SIMGRID
  1031. char name[16];
  1032. msg_host_t host = MSG_get_host_by_name("RAM");
  1033. STARPU_ASSERT(host);
  1034. _starpu_simgrid_memory_node_set_host(STARPU_MAIN_RAM, host);
  1035. #endif
  1036. /* We will store all the busid of the different (src, dst)
  1037. * combinations in a matrix which we initialize here. */
  1038. _starpu_initialize_busid_matrix();
  1039. /* Each device is initialized,
  1040. * giving it a memory node and a core bind id.
  1041. */
  1042. /* TODO: STARPU_MAXNUMANODES */
  1043. unsigned numa_init[1] = { 1 };
  1044. unsigned numa_memory_nodes[1] = { ram_memory_node };
  1045. #if defined(STARPU_USE_CUDA) || defined(STARPU_SIMGRID)
  1046. unsigned cuda_init[STARPU_MAXCUDADEVS] = { };
  1047. unsigned cuda_memory_nodes[STARPU_MAXCUDADEVS];
  1048. unsigned cuda_bindid[STARPU_MAXCUDADEVS];
  1049. #endif
  1050. #if defined(STARPU_USE_OPENCL) || defined(STARPU_SIMGRID)
  1051. unsigned opencl_init[STARPU_MAXOPENCLDEVS] = { };
  1052. unsigned opencl_memory_nodes[STARPU_MAXOPENCLDEVS];
  1053. unsigned opencl_bindid[STARPU_MAXOPENCLDEVS];
  1054. #endif
  1055. #ifdef STARPU_USE_MIC
  1056. unsigned mic_init[STARPU_MAXMICDEVS] = { };
  1057. unsigned mic_memory_nodes[STARPU_MAXMICDEVS];
  1058. unsigned mic_bindid[STARPU_MAXMICDEVS];
  1059. #endif
  1060. unsigned worker;
  1061. for (worker = 0; worker < config->topology.nworkers; worker++)
  1062. {
  1063. unsigned memory_node = -1;
  1064. struct _starpu_worker *workerarg = &config->workers[worker];
  1065. unsigned devid = workerarg->devid;
  1066. /* Perhaps the worker has some "favourite" bindings */
  1067. int *preferred_binding = NULL;
  1068. int npreferred = 0;
  1069. /* select the memory node that contains worker's memory */
  1070. switch (workerarg->arch)
  1071. {
  1072. case STARPU_CPU_WORKER:
  1073. {
  1074. /* TODO: NUMA */
  1075. int numaid = 0;
  1076. /* "dedicate" a cpu core to that worker */
  1077. if (numa_init[numaid])
  1078. {
  1079. memory_node = numa_memory_nodes[numaid];
  1080. }
  1081. else
  1082. {
  1083. numa_init[numaid] = 1;
  1084. memory_node = numa_memory_nodes[numaid] = _starpu_memory_node_register(STARPU_CPU_RAM, numaid);
  1085. #ifdef STARPU_SIMGRID
  1086. snprintf(name, sizeof(name), "RAM%d", numaid);
  1087. host = MSG_get_host_by_name(name);
  1088. STARPU_ASSERT(host);
  1089. _starpu_simgrid_memory_node_set_host(memory_node, host);
  1090. #endif
  1091. }
  1092. workerarg->bindid = _starpu_get_next_bindid(config, NULL, 0);
  1093. _starpu_memory_node_add_nworkers(memory_node);
  1094. break;
  1095. }
  1096. #if defined(STARPU_USE_CUDA) || defined(STARPU_SIMGRID)
  1097. case STARPU_CUDA_WORKER:
  1098. #ifndef STARPU_SIMGRID
  1099. if (may_bind_automatically)
  1100. {
  1101. /* StarPU is allowed to bind threads automatically */
  1102. preferred_binding = _starpu_get_cuda_affinity_vector(devid);
  1103. npreferred = config->topology.nhwpus;
  1104. }
  1105. #endif /* SIMGRID */
  1106. if (cuda_init[devid])
  1107. {
  1108. memory_node = cuda_memory_nodes[devid];
  1109. #ifndef STARPU_SIMGRID
  1110. workerarg->bindid = cuda_bindid[devid];
  1111. #endif /* SIMGRID */
  1112. }
  1113. else
  1114. {
  1115. cuda_init[devid] = 1;
  1116. #ifndef STARPU_SIMGRID
  1117. workerarg->bindid = cuda_bindid[devid] = _starpu_get_next_bindid(config, preferred_binding, npreferred);
  1118. #endif /* SIMGRID */
  1119. memory_node = cuda_memory_nodes[devid] = _starpu_memory_node_register(STARPU_CUDA_RAM, devid);
  1120. _starpu_register_bus(STARPU_MAIN_RAM, memory_node);
  1121. _starpu_register_bus(memory_node, STARPU_MAIN_RAM);
  1122. #ifdef STARPU_SIMGRID
  1123. snprintf(name, sizeof(name), "CUDA%d", devid);
  1124. host = MSG_get_host_by_name(name);
  1125. STARPU_ASSERT(host);
  1126. _starpu_simgrid_memory_node_set_host(memory_node, host);
  1127. #endif /* SIMGRID */
  1128. #ifdef HAVE_CUDA_MEMCPY_PEER
  1129. unsigned worker2;
  1130. for (worker2 = 0; worker2 < worker; worker2++)
  1131. {
  1132. struct _starpu_worker *workerarg2 = &config->workers[worker2];
  1133. if (workerarg2->arch == STARPU_CUDA_WORKER)
  1134. {
  1135. unsigned memory_node2 = starpu_worker_get_memory_node(worker2);
  1136. _starpu_register_bus(memory_node2, memory_node);
  1137. _starpu_register_bus(memory_node, memory_node2);
  1138. }
  1139. }
  1140. #endif /* MEMCPY_PEER */
  1141. }
  1142. _starpu_memory_node_add_nworkers(memory_node);
  1143. break;
  1144. #endif
  1145. #if defined(STARPU_USE_OPENCL) || defined(STARPU_SIMGRID)
  1146. case STARPU_OPENCL_WORKER:
  1147. #ifndef STARPU_SIMGRID
  1148. if (may_bind_automatically)
  1149. {
  1150. /* StarPU is allowed to bind threads automatically */
  1151. preferred_binding = _starpu_get_opencl_affinity_vector(devid);
  1152. npreferred = config->topology.nhwpus;
  1153. }
  1154. #endif /* SIMGRID */
  1155. if (opencl_init[devid])
  1156. {
  1157. memory_node = opencl_memory_nodes[devid];
  1158. #ifndef STARPU_SIMGRID
  1159. workerarg->bindid = opencl_bindid[devid];
  1160. #endif /* SIMGRID */
  1161. }
  1162. else
  1163. {
  1164. opencl_init[devid] = 1;
  1165. #ifndef STARPU_SIMGRID
  1166. workerarg->bindid = opencl_bindid[devid] = _starpu_get_next_bindid(config, preferred_binding, npreferred);
  1167. #endif /* SIMGRID */
  1168. memory_node = opencl_memory_nodes[devid] = _starpu_memory_node_register(STARPU_OPENCL_RAM, devid);
  1169. _starpu_register_bus(STARPU_MAIN_RAM, memory_node);
  1170. _starpu_register_bus(memory_node, STARPU_MAIN_RAM);
  1171. #ifdef STARPU_SIMGRID
  1172. snprintf(name, sizeof(name), "OpenCL%d", devid);
  1173. host = MSG_get_host_by_name(name);
  1174. STARPU_ASSERT(host);
  1175. _starpu_simgrid_memory_node_set_host(memory_node, host);
  1176. #endif /* SIMGRID */
  1177. }
  1178. _starpu_memory_node_add_nworkers(memory_node);
  1179. break;
  1180. #endif
  1181. #ifdef STARPU_USE_MIC
  1182. case STARPU_MIC_WORKER:
  1183. if (mic_init[devid])
  1184. {
  1185. memory_node = mic_memory_nodes[devid];
  1186. }
  1187. else
  1188. {
  1189. mic_init[devid] = 1;
  1190. #ifndef STARPU_SIMGRID
  1191. /* TODO */
  1192. //if (may_bind_automatically)
  1193. //{
  1194. // /* StarPU is allowed to bind threads automatically */
  1195. // preferred_binding = _starpu_get_mic_affinity_vector(devid);
  1196. // npreferred = config->topology.nhwpus;
  1197. //}
  1198. mic_bindid[devid] = _starpu_get_next_bindid(config, preferred_binding, npreferred);
  1199. #endif /* SIMGRID */
  1200. memory_node = mic_memory_nodes[devid] = _starpu_memory_node_register(STARPU_MIC_RAM, devid);
  1201. _starpu_register_bus(STARPU_MAIN_RAM, memory_node);
  1202. _starpu_register_bus(memory_node, STARPU_MAIN_RAM);
  1203. }
  1204. workerarg->bindid = mic_bindid[devid];
  1205. _starpu_memory_node_add_nworkers(memory_node);
  1206. break;
  1207. #endif /* STARPU_USE_MIC */
  1208. #ifdef STARPU_USE_SCC
  1209. case STARPU_SCC_WORKER:
  1210. {
  1211. /* Node 0 represents the SCC shared memory when we're on SCC. */
  1212. struct _starpu_memory_node_descr *descr = _starpu_memory_node_get_description();
  1213. descr->nodes[ram_memory_node] = STARPU_SCC_SHM;
  1214. memory_node = ram_memory_node;
  1215. _starpu_memory_node_add_nworkers(memory_node);
  1216. }
  1217. break;
  1218. #endif
  1219. default:
  1220. STARPU_ABORT();
  1221. }
  1222. workerarg->memory_node = memory_node;
  1223. _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);
  1224. #ifdef __GLIBC__
  1225. if (workerarg->bindid != -1)
  1226. {
  1227. /* Save the initial cpuset */
  1228. CPU_ZERO(&workerarg->cpu_set);
  1229. CPU_SET(workerarg->bindid, &workerarg->cpu_set);
  1230. }
  1231. #endif /* __GLIBC__ */
  1232. #ifdef STARPU_HAVE_HWLOC
  1233. if (workerarg->bindid == -1)
  1234. {
  1235. workerarg->hwloc_cpu_set = hwloc_bitmap_alloc();
  1236. }
  1237. else
  1238. {
  1239. /* Put the worker descriptor in the userdata field of the
  1240. * hwloc object describing the CPU */
  1241. hwloc_obj_t worker_obj = hwloc_get_obj_by_depth(config->topology.hwtopology,
  1242. config->pu_depth,
  1243. workerarg->bindid);
  1244. if (worker_obj->userdata == NULL)
  1245. {
  1246. worker_obj->userdata = _starpu_worker_list_new();
  1247. }
  1248. _starpu_worker_list_push_front(worker_obj->userdata, workerarg);
  1249. /* Clear the cpu set and set the cpu */
  1250. workerarg->hwloc_cpu_set = hwloc_bitmap_dup (worker_obj->cpuset);
  1251. }
  1252. #endif
  1253. }
  1254. }
  1255. int
  1256. _starpu_build_topology (struct _starpu_machine_config *config, int no_mp_config)
  1257. {
  1258. int ret;
  1259. unsigned i;
  1260. ret = _starpu_init_machine_config(config, no_mp_config);
  1261. if (ret)
  1262. return ret;
  1263. /* for the data management library */
  1264. _starpu_memory_nodes_init();
  1265. _starpu_init_workers_binding(config, no_mp_config);
  1266. config->cpus_nodeid = -1;
  1267. config->cuda_nodeid = -1;
  1268. config->opencl_nodeid = -1;
  1269. config->mic_nodeid = -1;
  1270. config->scc_nodeid = -1;
  1271. for (i = 0; i < starpu_worker_get_count(); i++)
  1272. {
  1273. switch (starpu_worker_get_type(i))
  1274. {
  1275. case STARPU_CPU_WORKER:
  1276. if (config->cpus_nodeid == -1)
  1277. config->cpus_nodeid = starpu_worker_get_memory_node(i);
  1278. else if (config->cpus_nodeid != starpu_worker_get_memory_node(i))
  1279. config->cpus_nodeid = -2;
  1280. break;
  1281. case STARPU_CUDA_WORKER:
  1282. if (config->cuda_nodeid == -1)
  1283. config->cuda_nodeid = starpu_worker_get_memory_node(i);
  1284. else if (config->cuda_nodeid != starpu_worker_get_memory_node(i))
  1285. config->cuda_nodeid = -2;
  1286. break;
  1287. case STARPU_OPENCL_WORKER:
  1288. if (config->opencl_nodeid == -1)
  1289. config->opencl_nodeid = starpu_worker_get_memory_node(i);
  1290. else if (config->opencl_nodeid != starpu_worker_get_memory_node(i))
  1291. config->opencl_nodeid = -2;
  1292. break;
  1293. case STARPU_MIC_WORKER:
  1294. if (config->mic_nodeid == -1)
  1295. config->mic_nodeid = starpu_worker_get_memory_node(i);
  1296. else if (config->mic_nodeid != starpu_worker_get_memory_node(i))
  1297. config->mic_nodeid = -2;
  1298. break;
  1299. case STARPU_SCC_WORKER:
  1300. if (config->scc_nodeid == -1)
  1301. config->scc_nodeid = starpu_worker_get_memory_node(i);
  1302. else if (config->scc_nodeid != starpu_worker_get_memory_node(i))
  1303. config->scc_nodeid = -2;
  1304. break;
  1305. case STARPU_ANY_WORKER:
  1306. STARPU_ASSERT(0);
  1307. }
  1308. }
  1309. return 0;
  1310. }
  1311. void
  1312. _starpu_destroy_topology (
  1313. struct _starpu_machine_config *config STARPU_ATTRIBUTE_UNUSED)
  1314. {
  1315. #ifdef STARPU_USE_MIC
  1316. _starpu_deinit_mp_config(config);
  1317. #endif
  1318. /* cleanup StarPU internal data structures */
  1319. _starpu_memory_nodes_deinit();
  1320. unsigned worker;
  1321. for (worker = 0; worker < config->topology.nworkers; worker++)
  1322. {
  1323. #ifdef STARPU_HAVE_HWLOC
  1324. struct _starpu_worker *workerarg = &config->workers[worker];
  1325. hwloc_bitmap_free(workerarg->hwloc_cpu_set);
  1326. if (workerarg->bindid != -1)
  1327. {
  1328. hwloc_obj_t worker_obj = hwloc_get_obj_by_depth(config->topology.hwtopology,
  1329. config->pu_depth,
  1330. workerarg->bindid);
  1331. if (worker_obj->userdata)
  1332. {
  1333. _starpu_worker_list_delete(worker_obj->userdata);
  1334. worker_obj->userdata = NULL;
  1335. }
  1336. }
  1337. #endif
  1338. }
  1339. #ifdef STARPU_HAVE_HWLOC
  1340. hwloc_topology_destroy(config->topology.hwtopology);
  1341. #endif
  1342. topology_is_initialized = 0;
  1343. #ifdef STARPU_USE_CUDA
  1344. struct handle_entry *entry, *tmp;
  1345. HASH_ITER(hh, devices_using_cuda, entry, tmp)
  1346. {
  1347. HASH_DEL(devices_using_cuda, entry);
  1348. free(entry);
  1349. }
  1350. devices_using_cuda = NULL;
  1351. #endif
  1352. #if defined(STARPU_USE_CUDA) || defined(STARPU_USE_OPENCL)
  1353. may_bind_automatically = 0;
  1354. #endif
  1355. }
  1356. void
  1357. starpu_topology_print (FILE *output)
  1358. {
  1359. struct _starpu_machine_config *config = _starpu_get_machine_config();
  1360. struct _starpu_machine_topology *topology = &config->topology;
  1361. unsigned pu;
  1362. unsigned worker;
  1363. unsigned nworkers = starpu_worker_get_count();
  1364. unsigned ncombinedworkers = topology->ncombinedworkers;
  1365. unsigned nthreads_per_core = topology->nhwpus / topology->nhwcpus;
  1366. for (pu = 0; pu < topology->nhwpus; pu++)
  1367. {
  1368. if ((pu % nthreads_per_core) == 0)
  1369. fprintf(output, "core %u", pu / nthreads_per_core);
  1370. fprintf(output, "\tPU %u\t", pu);
  1371. for (worker = 0;
  1372. worker < nworkers + ncombinedworkers;
  1373. worker++)
  1374. {
  1375. if (worker < nworkers)
  1376. {
  1377. if (topology->workers_bindid[worker] == pu)
  1378. {
  1379. char name[256];
  1380. starpu_worker_get_name (worker, name,
  1381. sizeof(name));
  1382. fprintf(output, "%s\t", name);
  1383. }
  1384. }
  1385. else
  1386. {
  1387. int worker_size, i;
  1388. int *combined_workerid;
  1389. starpu_combined_worker_get_description(worker, &worker_size, &combined_workerid);
  1390. for (i = 0; i < worker_size; i++)
  1391. {
  1392. if (topology->workers_bindid[combined_workerid[i]] == pu)
  1393. fprintf(output, "comb %u\t", worker-nworkers);
  1394. }
  1395. }
  1396. }
  1397. fprintf(output, "\n");
  1398. }
  1399. }