topology.c 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-2014 Université de Bordeaux
  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(_WIN32)
  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. #ifndef STARPU_NON_BLOCKING_DRIVERS
  697. if (nworker_per_cuda > 1)
  698. {
  699. _STARPU_DISP("Warning: reducing STARPU_NWORKER_PER_CUDA to 1 because blocking drivers are enabled\n");
  700. nworker_per_cuda = 1;
  701. }
  702. #endif
  703. if (ncuda != 0)
  704. {
  705. /* The user did not disable CUDA. We need to initialize CUDA
  706. * early to count the number of devices */
  707. _starpu_init_cuda();
  708. int nb_devices = _starpu_get_cuda_device_count();
  709. if (ncuda == -1)
  710. {
  711. /* Nothing was specified, so let's choose ! */
  712. ncuda = nb_devices;
  713. }
  714. else
  715. {
  716. if (ncuda > nb_devices)
  717. {
  718. /* The user requires more CUDA devices than
  719. * there is available */
  720. _STARPU_DISP("Warning: %d CUDA devices requested. Only %d available.\n", ncuda, nb_devices);
  721. ncuda = nb_devices;
  722. }
  723. }
  724. }
  725. /* Now we know how many CUDA devices will be used */
  726. topology->ncudagpus = ncuda;
  727. STARPU_ASSERT(topology->ncudagpus <= STARPU_MAXCUDADEVS);
  728. _starpu_initialize_workers_cuda_gpuid(config);
  729. unsigned cudagpu;
  730. for (cudagpu = 0; cudagpu < topology->ncudagpus; cudagpu++)
  731. {
  732. int devid = _starpu_get_next_cuda_gpuid(config);
  733. for (i = 0; i < nworker_per_cuda; i++)
  734. {
  735. int worker_idx = topology->nworkers + cudagpu * nworker_per_cuda + i;
  736. config->workers[worker_idx].arch = STARPU_CUDA_WORKER;
  737. config->workers[worker_idx].perf_arch.devices = (struct starpu_perfmodel_device*)malloc(sizeof(struct starpu_perfmodel_device));
  738. config->workers[worker_idx].perf_arch.ndevices = 1;
  739. config->workers[worker_idx].perf_arch.devices[0].type = STARPU_CUDA_WORKER;
  740. config->workers[worker_idx].perf_arch.devices[0].devid = devid;
  741. // TODO: fix perfmodels etc.
  742. //config->workers[worker_idx].perf_arch.ncore = nworker_per_cuda - 1;
  743. config->workers[worker_idx].perf_arch.devices[0].ncores = 1;
  744. config->workers[worker_idx].devid = devid;
  745. config->workers[worker_idx].subworkerid = i;
  746. config->workers[worker_idx].worker_mask = STARPU_CUDA;
  747. config->worker_mask |= STARPU_CUDA;
  748. struct handle_entry *entry;
  749. entry = (struct handle_entry *) malloc(sizeof(*entry));
  750. STARPU_ASSERT(entry != NULL);
  751. entry->gpuid = devid;
  752. HASH_ADD_INT(devices_using_cuda, gpuid, entry);
  753. }
  754. }
  755. topology->nworkers += topology->ncudagpus * nworker_per_cuda;
  756. #endif
  757. #if defined(STARPU_USE_OPENCL) || defined(STARPU_SIMGRID)
  758. int nopencl = config->conf->nopencl;
  759. if (nopencl != 0)
  760. {
  761. /* The user did not disable OPENCL. We need to initialize
  762. * OpenCL early to count the number of devices */
  763. _starpu_opencl_init();
  764. int nb_devices;
  765. nb_devices = _starpu_opencl_get_device_count();
  766. if (nopencl == -1)
  767. {
  768. /* Nothing was specified, so let's choose ! */
  769. nopencl = nb_devices;
  770. if (nopencl > STARPU_MAXOPENCLDEVS)
  771. {
  772. _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);
  773. nopencl = STARPU_MAXOPENCLDEVS;
  774. }
  775. }
  776. else
  777. {
  778. /* Let's make sure this value is OK. */
  779. if (nopencl > nb_devices)
  780. {
  781. /* The user requires more OpenCL devices than
  782. * there is available */
  783. _STARPU_DISP("Warning: %d OpenCL devices requested. Only %d available.\n", nopencl, nb_devices);
  784. nopencl = nb_devices;
  785. }
  786. /* Let's make sure this value is OK. */
  787. if (nopencl > STARPU_MAXOPENCLDEVS)
  788. {
  789. _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);
  790. nopencl = STARPU_MAXOPENCLDEVS;
  791. }
  792. }
  793. }
  794. topology->nopenclgpus = nopencl;
  795. STARPU_ASSERT(topology->nopenclgpus + topology->nworkers <= STARPU_NMAXWORKERS);
  796. _starpu_initialize_workers_opencl_gpuid(config);
  797. unsigned openclgpu;
  798. for (openclgpu = 0; openclgpu < topology->nopenclgpus; openclgpu++)
  799. {
  800. int worker_idx = topology->nworkers + openclgpu;
  801. int devid = _starpu_get_next_opencl_gpuid(config);
  802. if (devid == -1)
  803. { // There is no more devices left
  804. topology->nopenclgpus = openclgpu;
  805. break;
  806. }
  807. config->workers[worker_idx].arch = STARPU_OPENCL_WORKER;
  808. config->workers[worker_idx].perf_arch.devices = (struct starpu_perfmodel_device*)malloc(sizeof(struct starpu_perfmodel_device));
  809. config->workers[worker_idx].perf_arch.ndevices = 1;
  810. config->workers[worker_idx].perf_arch.devices[0].type = STARPU_OPENCL_WORKER;
  811. config->workers[worker_idx].perf_arch.devices[0].devid = devid;
  812. config->workers[worker_idx].perf_arch.devices[0].ncores = 1;
  813. config->workers[worker_idx].subworkerid = 0;
  814. config->workers[worker_idx].devid = devid;
  815. config->workers[worker_idx].worker_mask = STARPU_OPENCL;
  816. config->worker_mask |= STARPU_OPENCL;
  817. }
  818. topology->nworkers += topology->nopenclgpus;
  819. #endif
  820. #ifdef STARPU_USE_SCC
  821. int nscc = config->conf->nscc;
  822. unsigned nb_scc_nodes = _starpu_scc_src_get_device_count();
  823. if (nscc != 0)
  824. {
  825. /* The user did not disable SCC. We need to count
  826. * the number of devices */
  827. int nb_devices = nb_scc_nodes;
  828. if (nscc == -1)
  829. {
  830. /* Nothing was specified, so let's choose ! */
  831. nscc = nb_devices;
  832. if (nscc > STARPU_MAXSCCDEVS)
  833. {
  834. _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);
  835. nscc = STARPU_MAXSCCDEVS;
  836. }
  837. }
  838. else
  839. {
  840. /* Let's make sure this value is OK. */
  841. if (nscc > nb_devices)
  842. {
  843. /* The user requires more SCC devices than there is available */
  844. _STARPU_DISP("Warning: %d SCC devices requested. Only %d available.\n", nscc, nb_devices);
  845. nscc = nb_devices;
  846. }
  847. /* Let's make sure this value is OK. */
  848. if (nscc > STARPU_MAXSCCDEVS)
  849. {
  850. _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);
  851. nscc = STARPU_MAXSCCDEVS;
  852. }
  853. }
  854. }
  855. /* Now we know how many SCC devices will be used */
  856. topology->nsccdevices = nscc;
  857. STARPU_ASSERT(topology->nsccdevices + topology->nworkers <= STARPU_NMAXWORKERS);
  858. _starpu_initialize_workers_scc_deviceid(config);
  859. unsigned sccdev;
  860. for (sccdev = 0; sccdev < topology->nsccdevices; sccdev++)
  861. {
  862. config->workers[topology->nworkers + sccdev].arch = STARPU_SCC_WORKER;
  863. int devid = _starpu_get_next_scc_deviceid(config);
  864. config->workers[topology->nworkers + sccdev].perf_arch.devices = (struct starpu_perfmodel_device)malloc(sizeof(struct starpu_perfmodel_device));
  865. config->workers[topology->nworkers + sccdev].perf_arch.ndevices = 1;
  866. config->workers[topology->nworkers + sccdev].perf_arch.devices[0].type = STARPU_SCC_WORKER;
  867. config->workers[topology->nworkers + sccdev].perf_arch.devices[0].devid = sccdev;
  868. config->workers[topology->nworkers + sccdev].perf_arch.devices[0].ncore = 1;
  869. config->workers[topology->nworkers + sccdev].subworkerid = 0;
  870. config->workers[topology->nworkers + sccdev].devid = devid;
  871. config->workers[topology->nworkers + sccdev].worker_mask = STARPU_SCC;
  872. config->worker_mask |= STARPU_SCC;
  873. }
  874. for (; sccdev < nb_scc_nodes; ++sccdev)
  875. _starpu_scc_exit_useless_node(sccdev);
  876. topology->nworkers += topology->nsccdevices;
  877. #endif /* STARPU_USE_SCC */
  878. /* Unless not requested, we need to complete configuration with the
  879. * ones of the mp nodes. */
  880. #ifdef STARPU_USE_MIC
  881. if (! no_mp_config)
  882. _starpu_init_mp_config (config, config->conf);
  883. #endif
  884. /* we put the CPU section after the accelerator : in case there was an
  885. * accelerator found, we devote one cpu */
  886. #if defined(STARPU_USE_CPU) || defined(STARPU_SIMGRID)
  887. int ncpu = config->conf->ncpus;
  888. if (ncpu != 0)
  889. {
  890. if (ncpu == -1)
  891. {
  892. unsigned mic_busy_cpus = 0;
  893. unsigned j = 0;
  894. for (j = 0; j < STARPU_MAXMICDEVS; j++)
  895. mic_busy_cpus += (topology->nmiccores[j] ? 1 : 0);
  896. unsigned already_busy_cpus = mic_busy_cpus + topology->ncudagpus
  897. + topology->nopenclgpus + topology->nsccdevices;
  898. long avail_cpus = (long) topology->nhwcpus - (long) already_busy_cpus;
  899. if (avail_cpus < 0)
  900. avail_cpus = 0;
  901. int nth_per_core = starpu_get_env_number_default("STARPU_NTHREADS_PER_CORE", 1);
  902. avail_cpus *= nth_per_core;
  903. ncpu = STARPU_MIN(avail_cpus, STARPU_MAXCPUS);
  904. }
  905. else
  906. {
  907. if (ncpu > STARPU_MAXCPUS)
  908. {
  909. _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);
  910. ncpu = STARPU_MAXCPUS;
  911. }
  912. }
  913. }
  914. topology->ncpus = ncpu;
  915. STARPU_ASSERT(topology->ncpus + topology->nworkers <= STARPU_NMAXWORKERS);
  916. unsigned cpu;
  917. for (cpu = 0; cpu < topology->ncpus; cpu++)
  918. {
  919. int worker_idx = topology->nworkers + cpu;
  920. config->workers[worker_idx].arch = STARPU_CPU_WORKER;
  921. config->workers[worker_idx].perf_arch.devices = (struct starpu_perfmodel_device*)malloc(sizeof(struct starpu_perfmodel_device));
  922. config->workers[worker_idx].perf_arch.ndevices = 1;
  923. config->workers[worker_idx].perf_arch.devices[0].type = STARPU_CPU_WORKER;
  924. config->workers[worker_idx].perf_arch.devices[0].devid = 0;
  925. config->workers[worker_idx].perf_arch.devices[0].ncores = 1;
  926. config->workers[worker_idx].subworkerid = 0;
  927. config->workers[worker_idx].devid = cpu;
  928. config->workers[worker_idx].worker_mask = STARPU_CPU;
  929. config->worker_mask |= STARPU_CPU;
  930. }
  931. topology->nworkers += topology->ncpus;
  932. #endif
  933. if (topology->nworkers == 0)
  934. {
  935. _STARPU_DEBUG("No worker found, aborting ...\n");
  936. return -ENODEV;
  937. }
  938. return 0;
  939. }
  940. void
  941. _starpu_bind_thread_on_cpu (
  942. struct _starpu_machine_config *config STARPU_ATTRIBUTE_UNUSED,
  943. int cpuid STARPU_ATTRIBUTE_UNUSED)
  944. {
  945. #ifdef STARPU_SIMGRID
  946. return;
  947. #else
  948. if (starpu_get_env_number("STARPU_WORKERS_NOBIND") > 0)
  949. return;
  950. if (cpuid < 0)
  951. return;
  952. #ifdef STARPU_HAVE_HWLOC
  953. const struct hwloc_topology_support *support;
  954. #ifdef STARPU_USE_OPENCL
  955. _starpu_opencl_init();
  956. #endif
  957. #ifdef STARPU_USE_CUDA
  958. _starpu_init_cuda();
  959. #endif
  960. _starpu_init_topology(config);
  961. support = hwloc_topology_get_support (config->topology.hwtopology);
  962. if (support->cpubind->set_thisthread_cpubind)
  963. {
  964. hwloc_obj_t obj =
  965. hwloc_get_obj_by_depth (config->topology.hwtopology,
  966. config->pu_depth, cpuid);
  967. hwloc_bitmap_t set = obj->cpuset;
  968. int ret;
  969. hwloc_bitmap_singlify(set);
  970. ret = hwloc_set_cpubind (config->topology.hwtopology, set,
  971. HWLOC_CPUBIND_THREAD);
  972. if (ret)
  973. {
  974. perror("hwloc_set_cpubind");
  975. STARPU_ABORT();
  976. }
  977. }
  978. #elif defined(HAVE_PTHREAD_SETAFFINITY_NP) && defined(__linux__)
  979. int ret;
  980. /* fix the thread on the correct cpu */
  981. cpu_set_t aff_mask;
  982. CPU_ZERO(&aff_mask);
  983. CPU_SET(cpuid, &aff_mask);
  984. starpu_pthread_t self = pthread_self();
  985. ret = pthread_setaffinity_np(self, sizeof(aff_mask), &aff_mask);
  986. if (ret)
  987. {
  988. perror("binding thread");
  989. STARPU_ABORT();
  990. }
  991. #elif defined(_WIN32)
  992. DWORD mask = 1 << cpuid;
  993. if (!SetThreadAffinityMask(GetCurrentThread(), mask))
  994. {
  995. _STARPU_ERROR("SetThreadMaskAffinity(%lx) failed\n", mask);
  996. }
  997. #else
  998. #warning no CPU binding support
  999. #endif
  1000. #endif
  1001. }
  1002. void
  1003. _starpu_bind_thread_on_cpus (
  1004. struct _starpu_machine_config *config STARPU_ATTRIBUTE_UNUSED,
  1005. struct _starpu_combined_worker *combined_worker STARPU_ATTRIBUTE_UNUSED)
  1006. {
  1007. #ifdef STARPU_SIMGRID
  1008. return;
  1009. #endif
  1010. #ifdef STARPU_HAVE_HWLOC
  1011. const struct hwloc_topology_support *support;
  1012. #ifdef STARPU_USE_OPENC
  1013. _starpu_opencl_init();
  1014. #endif
  1015. #ifdef STARPU_USE_CUDA
  1016. _starpu_init_cuda();
  1017. #endif
  1018. _starpu_init_topology(config);
  1019. support = hwloc_topology_get_support(config->topology.hwtopology);
  1020. if (support->cpubind->set_thisthread_cpubind)
  1021. {
  1022. hwloc_bitmap_t set = combined_worker->hwloc_cpu_set;
  1023. int ret;
  1024. ret = hwloc_set_cpubind (config->topology.hwtopology, set,
  1025. HWLOC_CPUBIND_THREAD);
  1026. if (ret)
  1027. {
  1028. perror("binding thread");
  1029. STARPU_ABORT();
  1030. }
  1031. }
  1032. #else
  1033. #ifdef __GLIBC__
  1034. sched_setaffinity(0,sizeof(combined_worker->cpu_set),&combined_worker->cpu_set);
  1035. #else
  1036. # warning no parallel worker CPU binding support
  1037. #endif
  1038. #endif
  1039. }
  1040. static void
  1041. _starpu_init_workers_binding (struct _starpu_machine_config *config, int no_mp_config STARPU_ATTRIBUTE_UNUSED)
  1042. {
  1043. /* launch one thread per CPU */
  1044. unsigned ram_memory_node;
  1045. /* note that even if the CPU cpu are not used, we always have a RAM
  1046. * node */
  1047. /* TODO : support NUMA ;) */
  1048. ram_memory_node = _starpu_memory_node_register(STARPU_CPU_RAM, 0);
  1049. STARPU_ASSERT(ram_memory_node == STARPU_MAIN_RAM);
  1050. #ifdef STARPU_SIMGRID
  1051. char name[16];
  1052. msg_host_t host = _starpu_simgrid_get_host_by_name("RAM");
  1053. STARPU_ASSERT(host);
  1054. _starpu_simgrid_memory_node_set_host(STARPU_MAIN_RAM, host);
  1055. #endif
  1056. /* We will store all the busid of the different (src, dst)
  1057. * combinations in a matrix which we initialize here. */
  1058. _starpu_initialize_busid_matrix();
  1059. /* Each device is initialized,
  1060. * giving it a memory node and a core bind id.
  1061. */
  1062. /* TODO: STARPU_MAXNUMANODES */
  1063. unsigned numa_init[1] = { 1 };
  1064. unsigned numa_memory_nodes[1] = { ram_memory_node };
  1065. #if defined(STARPU_USE_CUDA) || defined(STARPU_SIMGRID)
  1066. unsigned cuda_init[STARPU_MAXCUDADEVS] = { };
  1067. unsigned cuda_memory_nodes[STARPU_MAXCUDADEVS];
  1068. unsigned cuda_bindid[STARPU_MAXCUDADEVS];
  1069. #endif
  1070. #if defined(STARPU_USE_OPENCL) || defined(STARPU_SIMGRID)
  1071. unsigned opencl_init[STARPU_MAXOPENCLDEVS] = { };
  1072. unsigned opencl_memory_nodes[STARPU_MAXOPENCLDEVS];
  1073. unsigned opencl_bindid[STARPU_MAXOPENCLDEVS];
  1074. #endif
  1075. #ifdef STARPU_USE_MIC
  1076. unsigned mic_init[STARPU_MAXMICDEVS] = { };
  1077. unsigned mic_memory_nodes[STARPU_MAXMICDEVS];
  1078. unsigned mic_bindid[STARPU_MAXMICDEVS];
  1079. #endif
  1080. unsigned worker;
  1081. for (worker = 0; worker < config->topology.nworkers; worker++)
  1082. {
  1083. unsigned memory_node = -1;
  1084. struct _starpu_worker *workerarg = &config->workers[worker];
  1085. unsigned devid = workerarg->devid;
  1086. /* Perhaps the worker has some "favourite" bindings */
  1087. int *preferred_binding = NULL;
  1088. int npreferred = 0;
  1089. /* select the memory node that contains worker's memory */
  1090. switch (workerarg->arch)
  1091. {
  1092. case STARPU_CPU_WORKER:
  1093. {
  1094. /* TODO: NUMA */
  1095. int numaid = 0;
  1096. /* "dedicate" a cpu core to that worker */
  1097. if (numa_init[numaid])
  1098. {
  1099. memory_node = numa_memory_nodes[numaid];
  1100. }
  1101. else
  1102. {
  1103. numa_init[numaid] = 1;
  1104. memory_node = numa_memory_nodes[numaid] = _starpu_memory_node_register(STARPU_CPU_RAM, numaid);
  1105. #ifdef STARPU_SIMGRID
  1106. snprintf(name, sizeof(name), "RAM%d", numaid);
  1107. host = _starpu_simgrid_get_host_by_name(name);
  1108. STARPU_ASSERT(host);
  1109. _starpu_simgrid_memory_node_set_host(memory_node, host);
  1110. #endif
  1111. }
  1112. workerarg->bindid = _starpu_get_next_bindid(config, NULL, 0);
  1113. _starpu_memory_node_add_nworkers(memory_node);
  1114. break;
  1115. }
  1116. #if defined(STARPU_USE_CUDA) || defined(STARPU_SIMGRID)
  1117. case STARPU_CUDA_WORKER:
  1118. #ifndef STARPU_SIMGRID
  1119. if (may_bind_automatically)
  1120. {
  1121. /* StarPU is allowed to bind threads automatically */
  1122. preferred_binding = _starpu_get_cuda_affinity_vector(devid);
  1123. npreferred = config->topology.nhwpus;
  1124. }
  1125. #endif /* SIMGRID */
  1126. if (cuda_init[devid])
  1127. {
  1128. memory_node = cuda_memory_nodes[devid];
  1129. #ifndef STARPU_SIMGRID
  1130. workerarg->bindid = cuda_bindid[devid];
  1131. #endif /* SIMGRID */
  1132. }
  1133. else
  1134. {
  1135. cuda_init[devid] = 1;
  1136. #ifndef STARPU_SIMGRID
  1137. workerarg->bindid = cuda_bindid[devid] = _starpu_get_next_bindid(config, preferred_binding, npreferred);
  1138. #endif /* SIMGRID */
  1139. memory_node = cuda_memory_nodes[devid] = _starpu_memory_node_register(STARPU_CUDA_RAM, devid);
  1140. _starpu_register_bus(STARPU_MAIN_RAM, memory_node);
  1141. _starpu_register_bus(memory_node, STARPU_MAIN_RAM);
  1142. #ifdef STARPU_SIMGRID
  1143. snprintf(name, sizeof(name), "CUDA%d", devid);
  1144. host = _starpu_simgrid_get_host_by_name(name);
  1145. STARPU_ASSERT(host);
  1146. _starpu_simgrid_memory_node_set_host(memory_node, host);
  1147. #endif /* SIMGRID */
  1148. #ifdef HAVE_CUDA_MEMCPY_PEER
  1149. unsigned worker2;
  1150. for (worker2 = 0; worker2 < worker; worker2++)
  1151. {
  1152. struct _starpu_worker *workerarg2 = &config->workers[worker2];
  1153. if (workerarg2->arch == STARPU_CUDA_WORKER)
  1154. {
  1155. unsigned memory_node2 = starpu_worker_get_memory_node(worker2);
  1156. _starpu_register_bus(memory_node2, memory_node);
  1157. _starpu_register_bus(memory_node, memory_node2);
  1158. }
  1159. }
  1160. #endif /* MEMCPY_PEER */
  1161. }
  1162. _starpu_memory_node_add_nworkers(memory_node);
  1163. break;
  1164. #endif
  1165. #if defined(STARPU_USE_OPENCL) || defined(STARPU_SIMGRID)
  1166. case STARPU_OPENCL_WORKER:
  1167. #ifndef STARPU_SIMGRID
  1168. if (may_bind_automatically)
  1169. {
  1170. /* StarPU is allowed to bind threads automatically */
  1171. preferred_binding = _starpu_get_opencl_affinity_vector(devid);
  1172. npreferred = config->topology.nhwpus;
  1173. }
  1174. #endif /* SIMGRID */
  1175. if (opencl_init[devid])
  1176. {
  1177. memory_node = opencl_memory_nodes[devid];
  1178. #ifndef STARPU_SIMGRID
  1179. workerarg->bindid = opencl_bindid[devid];
  1180. #endif /* SIMGRID */
  1181. }
  1182. else
  1183. {
  1184. opencl_init[devid] = 1;
  1185. #ifndef STARPU_SIMGRID
  1186. workerarg->bindid = opencl_bindid[devid] = _starpu_get_next_bindid(config, preferred_binding, npreferred);
  1187. #endif /* SIMGRID */
  1188. memory_node = opencl_memory_nodes[devid] = _starpu_memory_node_register(STARPU_OPENCL_RAM, devid);
  1189. _starpu_register_bus(STARPU_MAIN_RAM, memory_node);
  1190. _starpu_register_bus(memory_node, STARPU_MAIN_RAM);
  1191. #ifdef STARPU_SIMGRID
  1192. snprintf(name, sizeof(name), "OpenCL%d", devid);
  1193. host = _starpu_simgrid_get_host_by_name(name);
  1194. STARPU_ASSERT(host);
  1195. _starpu_simgrid_memory_node_set_host(memory_node, host);
  1196. #endif /* SIMGRID */
  1197. }
  1198. _starpu_memory_node_add_nworkers(memory_node);
  1199. break;
  1200. #endif
  1201. #ifdef STARPU_USE_MIC
  1202. case STARPU_MIC_WORKER:
  1203. if (mic_init[devid])
  1204. {
  1205. memory_node = mic_memory_nodes[devid];
  1206. }
  1207. else
  1208. {
  1209. mic_init[devid] = 1;
  1210. #ifndef STARPU_SIMGRID
  1211. /* TODO */
  1212. //if (may_bind_automatically)
  1213. //{
  1214. // /* StarPU is allowed to bind threads automatically */
  1215. // preferred_binding = _starpu_get_mic_affinity_vector(devid);
  1216. // npreferred = config->topology.nhwpus;
  1217. //}
  1218. mic_bindid[devid] = _starpu_get_next_bindid(config, preferred_binding, npreferred);
  1219. #endif /* SIMGRID */
  1220. memory_node = mic_memory_nodes[devid] = _starpu_memory_node_register(STARPU_MIC_RAM, devid);
  1221. _starpu_register_bus(STARPU_MAIN_RAM, memory_node);
  1222. _starpu_register_bus(memory_node, STARPU_MAIN_RAM);
  1223. }
  1224. workerarg->bindid = mic_bindid[devid];
  1225. _starpu_memory_node_add_nworkers(memory_node);
  1226. break;
  1227. #endif /* STARPU_USE_MIC */
  1228. #ifdef STARPU_USE_SCC
  1229. case STARPU_SCC_WORKER:
  1230. {
  1231. /* Node 0 represents the SCC shared memory when we're on SCC. */
  1232. struct _starpu_memory_node_descr *descr = _starpu_memory_node_get_description();
  1233. descr->nodes[ram_memory_node] = STARPU_SCC_SHM;
  1234. memory_node = ram_memory_node;
  1235. _starpu_memory_node_add_nworkers(memory_node);
  1236. }
  1237. break;
  1238. #endif
  1239. default:
  1240. STARPU_ABORT();
  1241. }
  1242. workerarg->memory_node = memory_node;
  1243. _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);
  1244. #ifdef __GLIBC__
  1245. if (workerarg->bindid != -1)
  1246. {
  1247. /* Save the initial cpuset */
  1248. CPU_ZERO(&workerarg->cpu_set);
  1249. CPU_SET(workerarg->bindid, &workerarg->cpu_set);
  1250. }
  1251. #endif /* __GLIBC__ */
  1252. #ifdef STARPU_HAVE_HWLOC
  1253. if (workerarg->bindid == -1)
  1254. {
  1255. workerarg->hwloc_cpu_set = hwloc_bitmap_alloc();
  1256. }
  1257. else
  1258. {
  1259. /* Put the worker descriptor in the userdata field of the
  1260. * hwloc object describing the CPU */
  1261. hwloc_obj_t worker_obj = hwloc_get_obj_by_depth(config->topology.hwtopology,
  1262. config->pu_depth,
  1263. workerarg->bindid);
  1264. if (worker_obj->userdata == NULL)
  1265. {
  1266. worker_obj->userdata = _starpu_worker_list_new();
  1267. }
  1268. _starpu_worker_list_push_front(worker_obj->userdata, workerarg);
  1269. /* Clear the cpu set and set the cpu */
  1270. workerarg->hwloc_cpu_set = hwloc_bitmap_dup (worker_obj->cpuset);
  1271. }
  1272. #endif
  1273. }
  1274. }
  1275. int
  1276. _starpu_build_topology (struct _starpu_machine_config *config, int no_mp_config)
  1277. {
  1278. int ret;
  1279. unsigned i;
  1280. ret = _starpu_init_machine_config(config, no_mp_config);
  1281. if (ret)
  1282. return ret;
  1283. /* for the data management library */
  1284. _starpu_memory_nodes_init();
  1285. _starpu_init_workers_binding(config, no_mp_config);
  1286. config->cpus_nodeid = -1;
  1287. config->cuda_nodeid = -1;
  1288. config->opencl_nodeid = -1;
  1289. config->mic_nodeid = -1;
  1290. config->scc_nodeid = -1;
  1291. for (i = 0; i < starpu_worker_get_count(); i++)
  1292. {
  1293. switch (starpu_worker_get_type(i))
  1294. {
  1295. case STARPU_CPU_WORKER:
  1296. if (config->cpus_nodeid == -1)
  1297. config->cpus_nodeid = starpu_worker_get_memory_node(i);
  1298. else if (config->cpus_nodeid != (int) starpu_worker_get_memory_node(i))
  1299. config->cpus_nodeid = -2;
  1300. break;
  1301. case STARPU_CUDA_WORKER:
  1302. if (config->cuda_nodeid == -1)
  1303. config->cuda_nodeid = starpu_worker_get_memory_node(i);
  1304. else if (config->cuda_nodeid != (int) starpu_worker_get_memory_node(i))
  1305. config->cuda_nodeid = -2;
  1306. break;
  1307. case STARPU_OPENCL_WORKER:
  1308. if (config->opencl_nodeid == -1)
  1309. config->opencl_nodeid = starpu_worker_get_memory_node(i);
  1310. else if (config->opencl_nodeid != (int) starpu_worker_get_memory_node(i))
  1311. config->opencl_nodeid = -2;
  1312. break;
  1313. case STARPU_MIC_WORKER:
  1314. if (config->mic_nodeid == -1)
  1315. config->mic_nodeid = starpu_worker_get_memory_node(i);
  1316. else if (config->mic_nodeid != (int) starpu_worker_get_memory_node(i))
  1317. config->mic_nodeid = -2;
  1318. break;
  1319. case STARPU_SCC_WORKER:
  1320. if (config->scc_nodeid == -1)
  1321. config->scc_nodeid = starpu_worker_get_memory_node(i);
  1322. else if (config->scc_nodeid != (int) starpu_worker_get_memory_node(i))
  1323. config->scc_nodeid = -2;
  1324. break;
  1325. case STARPU_ANY_WORKER:
  1326. STARPU_ASSERT(0);
  1327. }
  1328. }
  1329. return 0;
  1330. }
  1331. void
  1332. _starpu_destroy_topology (
  1333. struct _starpu_machine_config *config STARPU_ATTRIBUTE_UNUSED)
  1334. {
  1335. #ifdef STARPU_USE_MIC
  1336. _starpu_deinit_mp_config(config);
  1337. #endif
  1338. /* cleanup StarPU internal data structures */
  1339. _starpu_memory_nodes_deinit();
  1340. unsigned worker;
  1341. for (worker = 0; worker < config->topology.nworkers; worker++)
  1342. {
  1343. struct _starpu_worker *workerarg = &config->workers[worker];
  1344. free(workerarg->perf_arch.devices);
  1345. #ifdef STARPU_HAVE_HWLOC
  1346. hwloc_bitmap_free(workerarg->hwloc_cpu_set);
  1347. if (workerarg->bindid != -1)
  1348. {
  1349. hwloc_obj_t worker_obj = hwloc_get_obj_by_depth(config->topology.hwtopology,
  1350. config->pu_depth,
  1351. workerarg->bindid);
  1352. if (worker_obj->userdata)
  1353. {
  1354. _starpu_worker_list_delete(worker_obj->userdata);
  1355. worker_obj->userdata = NULL;
  1356. }
  1357. }
  1358. #endif
  1359. }
  1360. unsigned combined_worker_id;
  1361. for(combined_worker_id=0 ; combined_worker_id < config->topology.ncombinedworkers ; combined_worker_id++)
  1362. {
  1363. struct _starpu_combined_worker *combined_worker = &config->combined_workers[combined_worker_id];
  1364. free(combined_worker->perf_arch.devices);
  1365. }
  1366. #ifdef STARPU_HAVE_HWLOC
  1367. hwloc_topology_destroy(config->topology.hwtopology);
  1368. #endif
  1369. topology_is_initialized = 0;
  1370. #ifdef STARPU_USE_CUDA
  1371. struct handle_entry *entry, *tmp;
  1372. HASH_ITER(hh, devices_using_cuda, entry, tmp)
  1373. {
  1374. HASH_DEL(devices_using_cuda, entry);
  1375. free(entry);
  1376. }
  1377. devices_using_cuda = NULL;
  1378. #endif
  1379. #if defined(STARPU_USE_CUDA) || defined(STARPU_USE_OPENCL)
  1380. may_bind_automatically = 0;
  1381. #endif
  1382. }
  1383. void
  1384. starpu_topology_print (FILE *output)
  1385. {
  1386. struct _starpu_machine_config *config = _starpu_get_machine_config();
  1387. struct _starpu_machine_topology *topology = &config->topology;
  1388. unsigned pu;
  1389. unsigned worker;
  1390. unsigned nworkers = starpu_worker_get_count();
  1391. unsigned ncombinedworkers = topology->ncombinedworkers;
  1392. unsigned nthreads_per_core = topology->nhwpus / topology->nhwcpus;
  1393. for (pu = 0; pu < topology->nhwpus; pu++)
  1394. {
  1395. if ((pu % nthreads_per_core) == 0)
  1396. fprintf(output, "core %u", pu / nthreads_per_core);
  1397. fprintf(output, "\tPU %u\t", pu);
  1398. for (worker = 0;
  1399. worker < nworkers + ncombinedworkers;
  1400. worker++)
  1401. {
  1402. if (worker < nworkers)
  1403. {
  1404. if (topology->workers_bindid[worker] == pu)
  1405. {
  1406. char name[256];
  1407. starpu_worker_get_name (worker, name,
  1408. sizeof(name));
  1409. fprintf(output, "%s\t", name);
  1410. }
  1411. }
  1412. else
  1413. {
  1414. int worker_size, i;
  1415. int *combined_workerid;
  1416. starpu_combined_worker_get_description(worker, &worker_size, &combined_workerid);
  1417. for (i = 0; i < worker_size; i++)
  1418. {
  1419. if (topology->workers_bindid[combined_workerid[i]] == pu)
  1420. fprintf(output, "comb %u\t", worker-nworkers);
  1421. }
  1422. }
  1423. }
  1424. fprintf(output, "\n");
  1425. }
  1426. }