topology.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  1. /*
  2. * StarPU
  3. * Copyright (C) INRIA 2008-2009 (see AUTHORS file)
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation; either version 2.1 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. *
  14. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  15. */
  16. #include <stdlib.h>
  17. #include <stdio.h>
  18. #include <common/config.h>
  19. #include <core/workers.h>
  20. #include <core/debug.h>
  21. #include <core/topology.h>
  22. #include <drivers/cuda/driver_cuda.h>
  23. #ifdef STARPU_HAVE_HWLOC
  24. #include <hwloc.h>
  25. #endif
  26. #if defined(__MINGW32__) || defined(__CYGWIN__)
  27. #include <windows.h>
  28. #endif
  29. static unsigned topology_is_initialized = 0;
  30. static void _starpu_initialize_workers_bindid(struct starpu_machine_config_s *config);
  31. #ifdef STARPU_USE_CUDA
  32. static void _starpu_initialize_workers_gpuid(struct starpu_machine_config_s *config);
  33. static unsigned may_bind_automatically = 0;
  34. #endif
  35. /*
  36. * Discover the topology of the machine
  37. */
  38. #ifdef STARPU_USE_CUDA
  39. static void _starpu_initialize_workers_gpuid(struct starpu_machine_config_s *config)
  40. {
  41. char *strval;
  42. unsigned i;
  43. config->current_gpuid = 0;
  44. /* conf->workers_bindid indicates the successive cpu identifier that
  45. * should be used to bind the workers. It should be either filled
  46. * according to the user's explicit parameters (from starpu_conf) or
  47. * according to the STARPU_WORKERS_CPUID env. variable. Otherwise, a
  48. * round-robin policy is used to distributed the workers over the
  49. * cpus. */
  50. /* what do we use, explicit value, env. variable, or round-robin ? */
  51. if (config->user_conf && config->user_conf->use_explicit_workers_gpuid)
  52. {
  53. /* we use the explicit value from the user */
  54. memcpy(config->workers_gpuid,
  55. config->user_conf->workers_gpuid,
  56. STARPU_NMAXWORKERS*sizeof(unsigned));
  57. }
  58. else if ((strval = getenv("STARPU_WORKERS_CUDAID")))
  59. {
  60. /* STARPU_WORKERS_CUDAID certainly contains less entries than
  61. * STARPU_NMAXWORKERS, so we reuse its entries in a round robin
  62. * fashion: "1 2" is equivalent to "1 2 1 2 1 2 .... 1 2". */
  63. unsigned wrap = 0;
  64. unsigned number_of_entries = 0;
  65. char *endptr;
  66. /* we use the content of the STARPU_WORKERS_CUDAID env. variable */
  67. for (i = 0; i < STARPU_NMAXWORKERS; i++)
  68. {
  69. if (!wrap) {
  70. long int val;
  71. val = strtol(strval, &endptr, 10);
  72. if (endptr != strval)
  73. {
  74. config->workers_gpuid[i] = (unsigned)val;
  75. strval = endptr;
  76. }
  77. else {
  78. /* there must be at least one entry */
  79. STARPU_ASSERT(i != 0);
  80. number_of_entries = i;
  81. /* there is no more values in the string */
  82. wrap = 1;
  83. config->workers_gpuid[i] = config->workers_gpuid[0];
  84. }
  85. }
  86. else {
  87. config->workers_gpuid[i] = config->workers_gpuid[i % number_of_entries];
  88. }
  89. }
  90. }
  91. else
  92. {
  93. /* by default, we take a round robin policy */
  94. for (i = 0; i < STARPU_NMAXWORKERS; i++)
  95. config->workers_gpuid[i] = (unsigned)i;
  96. /* StarPU can use sampling techniques to bind threads correctly */
  97. may_bind_automatically = 1;
  98. }
  99. }
  100. #endif
  101. static inline int _starpu_get_next_gpuid(struct starpu_machine_config_s *config)
  102. {
  103. unsigned i = ((config->current_gpuid++) % config->ncudagpus);
  104. return (int)config->workers_gpuid[i];
  105. }
  106. static void _starpu_init_topology(struct starpu_machine_config_s *config)
  107. {
  108. if (!topology_is_initialized)
  109. {
  110. #ifdef STARPU_HAVE_HWLOC
  111. hwloc_topology_init(&config->hwtopology);
  112. hwloc_topology_load(config->hwtopology);
  113. config->cpu_depth = hwloc_get_type_depth(config->hwtopology, HWLOC_OBJ_CORE);
  114. /* Would be very odd */
  115. STARPU_ASSERT(config->cpu_depth != HWLOC_TYPE_DEPTH_MULTIPLE);
  116. if (config->cpu_depth == HWLOC_TYPE_DEPTH_UNKNOWN)
  117. /* unknown, using logical procesors as fallback */
  118. config->cpu_depth = hwloc_get_type_depth(config->hwtopology, HWLOC_OBJ_PROC);
  119. config->nhwcpus = hwloc_get_nbobjs_by_depth(config->hwtopology, config->cpu_depth);
  120. #elif defined(__MINGW32__) || defined(__CYGWIN__)
  121. SYSTEM_INFO sysinfo;
  122. GetSystemInfo(&sysinfo);
  123. config->nhwcpus += sysinfo.dwNumberOfProcessors;
  124. #elif defined(HAVE_SYSCONF)
  125. config->nhwcpus = sysconf(_SC_NPROCESSORS_ONLN);
  126. #else
  127. #warning no way to know number of cores, assuming 1
  128. config->nhwcpus = 1;
  129. #endif
  130. topology_is_initialized = 1;
  131. }
  132. }
  133. unsigned _starpu_topology_get_nhwcpu(struct starpu_machine_config_s *config)
  134. {
  135. _starpu_init_topology(config);
  136. return config->nhwcpus;
  137. }
  138. static int _starpu_init_machine_config(struct starpu_machine_config_s *config,
  139. struct starpu_conf *user_conf)
  140. {
  141. int explicitval __attribute__((unused));
  142. unsigned use_accelerator = 0;
  143. config->nworkers = 0;
  144. _starpu_init_topology(config);
  145. _starpu_initialize_workers_bindid(config);
  146. #ifdef STARPU_USE_CUDA
  147. if (user_conf && (user_conf->ncuda == 0))
  148. {
  149. /* the user explicitely disabled CUDA */
  150. config->ncudagpus = 0;
  151. }
  152. else {
  153. /* we need to initialize CUDA early to count the number of devices */
  154. _starpu_init_cuda();
  155. if (user_conf && (user_conf->ncuda != -1))
  156. {
  157. explicitval = user_conf->ncuda;
  158. }
  159. else {
  160. explicitval = starpu_get_env_number("STARPU_NCUDA");
  161. }
  162. if (explicitval < 0) {
  163. config->ncudagpus =
  164. STARPU_MIN(_starpu_get_cuda_device_count(), STARPU_MAXCUDADEVS);
  165. } else {
  166. /* use the specified value */
  167. config->ncudagpus = (unsigned)explicitval;
  168. STARPU_ASSERT(config->ncudagpus <= STARPU_MAXCUDADEVS);
  169. }
  170. STARPU_ASSERT(config->ncudagpus + config->nworkers <= STARPU_NMAXWORKERS);
  171. }
  172. if (config->ncudagpus > 0)
  173. use_accelerator = 1;
  174. _starpu_initialize_workers_gpuid(config);
  175. unsigned cudagpu;
  176. for (cudagpu = 0; cudagpu < config->ncudagpus; cudagpu++)
  177. {
  178. config->workers[config->nworkers + cudagpu].arch = STARPU_CUDA_WORKER;
  179. int devid = _starpu_get_next_gpuid(config);
  180. enum starpu_perf_archtype arch = STARPU_CUDA_DEFAULT + devid;
  181. config->workers[config->nworkers + cudagpu].devid = devid;
  182. config->workers[config->nworkers + cudagpu].perf_arch = arch;
  183. config->workers[config->nworkers + cudagpu].worker_mask = STARPU_CUDA;
  184. config->worker_mask |= STARPU_CUDA;
  185. }
  186. config->nworkers += config->ncudagpus;
  187. #endif
  188. #ifdef STARPU_USE_GORDON
  189. if (user_conf && (user_conf->ncuda != -1)) {
  190. explicitval = user_conf->ncuda;
  191. }
  192. else {
  193. explicitval = starpu_get_env_number("STARPU_NGORDON");
  194. }
  195. if (explicitval < 0) {
  196. config->ngordon_spus = spe_cpu_info_get(SPE_COUNT_USABLE_SPES, -1);
  197. } else {
  198. /* use the specified value */
  199. config->ngordon_spus = (unsigned)explicitval;
  200. STARPU_ASSERT(config->ngordon_spus <= NMAXGORDONSPUS);
  201. }
  202. STARPU_ASSERT(config->ngordon_spus + config->nworkers <= STARPU_NMAXWORKERS);
  203. if (config->ngordon_spus > 0)
  204. use_accelerator = 1;
  205. unsigned spu;
  206. for (spu = 0; spu < config->ngordon_spus; spu++)
  207. {
  208. config->workers[config->nworkers + spu].arch = STARPU_GORDON_WORKER;
  209. config->workers[config->nworkers + spu].perf_arch = STARPU_GORDON_DEFAULT;
  210. config->workers[config->nworkers + spu].id = spu;
  211. config->workers[config->nworkers + spu].worker_is_running = 0;
  212. config->workers[config->nworkers + spu].worker_mask = STARPU_GORDON;
  213. config->worker_mask |= STARPU_GORDON;
  214. }
  215. config->nworkers += config->ngordon_spus;
  216. #endif
  217. /* we put the CPU section after the accelerator : in case there was an
  218. * accelerator found, we devote one cpu */
  219. #ifdef STARPU_USE_CPU
  220. if (user_conf && (user_conf->ncpus != -1)) {
  221. explicitval = user_conf->ncpus;
  222. }
  223. else {
  224. explicitval = starpu_get_env_number("STARPU_NCPUS");
  225. }
  226. if (explicitval < 0) {
  227. unsigned already_busy_cpus = (config->ngordon_spus?1:0) + config->ncudagpus;
  228. long avail_cpus = config->nhwcpus - (use_accelerator?already_busy_cpus:0);
  229. config->ncpus = STARPU_MIN(avail_cpus, STARPU_NMAXCPUS);
  230. } else {
  231. /* use the specified value */
  232. config->ncpus = (unsigned)explicitval;
  233. STARPU_ASSERT(config->ncpus <= STARPU_NMAXCPUS);
  234. }
  235. STARPU_ASSERT(config->ncpus + config->nworkers <= STARPU_NMAXWORKERS);
  236. unsigned cpu;
  237. for (cpu = 0; cpu < config->ncpus; cpu++)
  238. {
  239. config->workers[config->nworkers + cpu].arch = STARPU_CPU_WORKER;
  240. config->workers[config->nworkers + cpu].perf_arch = STARPU_CPU_DEFAULT;
  241. config->workers[config->nworkers + cpu].devid = cpu;
  242. config->workers[config->nworkers + cpu].worker_mask = STARPU_CPU;
  243. config->worker_mask |= STARPU_CPU;
  244. }
  245. config->nworkers += config->ncpus;
  246. #endif
  247. if (config->nworkers == 0)
  248. {
  249. #ifdef STARPU_VERBOSE
  250. fprintf(stderr, "No worker found, aborting ...\n");
  251. #endif
  252. return -ENODEV;
  253. }
  254. return 0;
  255. }
  256. /*
  257. * Bind workers on the different processors
  258. */
  259. static void _starpu_initialize_workers_bindid(struct starpu_machine_config_s *config)
  260. {
  261. char *strval;
  262. unsigned i;
  263. config->current_bindid = 0;
  264. /* conf->workers_bindid indicates the successive cpu identifier that
  265. * should be used to bind the workers. It should be either filled
  266. * according to the user's explicit parameters (from starpu_conf) or
  267. * according to the STARPU_WORKERS_CPUID env. variable. Otherwise, a
  268. * round-robin policy is used to distributed the workers over the
  269. * cpus. */
  270. /* what do we use, explicit value, env. variable, or round-robin ? */
  271. if (config->user_conf && config->user_conf->use_explicit_workers_bindid)
  272. {
  273. /* we use the explicit value from the user */
  274. memcpy(config->workers_bindid,
  275. config->user_conf->workers_bindid,
  276. STARPU_NMAXWORKERS*sizeof(unsigned));
  277. }
  278. else if ((strval = getenv("STARPU_WORKERS_CPUID")))
  279. {
  280. /* STARPU_WORKERS_CPUID certainly contains less entries than
  281. * STARPU_NMAXWORKERS, so we reuse its entries in a round robin
  282. * fashion: "1 2" is equivalent to "1 2 1 2 1 2 .... 1 2". */
  283. unsigned wrap = 0;
  284. unsigned number_of_entries = 0;
  285. char *endptr;
  286. /* we use the content of the STARPU_WORKERS_CUDAID env. variable */
  287. for (i = 0; i < STARPU_NMAXWORKERS; i++)
  288. {
  289. if (!wrap) {
  290. long int val;
  291. val = strtol(strval, &endptr, 10);
  292. if (endptr != strval)
  293. {
  294. config->workers_bindid[i] = (unsigned)(val % config->nhwcpus);
  295. strval = endptr;
  296. }
  297. else {
  298. /* there must be at least one entry */
  299. STARPU_ASSERT(i != 0);
  300. number_of_entries = i;
  301. /* there is no more values in the string */
  302. wrap = 1;
  303. config->workers_bindid[i] = config->workers_bindid[0];
  304. }
  305. }
  306. else {
  307. config->workers_bindid[i] = config->workers_bindid[i % number_of_entries];
  308. }
  309. }
  310. }
  311. else
  312. {
  313. /* by default, we take a round robin policy */
  314. for (i = 0; i < STARPU_NMAXWORKERS; i++)
  315. config->workers_bindid[i] = (unsigned)(i % config->nhwcpus);
  316. }
  317. }
  318. /* This function gets the identifier of the next cpu on which to bind a
  319. * worker. In case a list of preferred cpus was specified, we look for a an
  320. * available cpu among the list if possible, otherwise a round-robin policy is
  321. * used. */
  322. static inline int _starpu_get_next_bindid(struct starpu_machine_config_s *config,
  323. int *preferred_binding, int npreferred)
  324. {
  325. unsigned found = 0;
  326. int current_preferred;
  327. for (current_preferred = 0; current_preferred < npreferred; current_preferred++)
  328. {
  329. if (found)
  330. break;
  331. unsigned requested_cpu = preferred_binding[current_preferred];
  332. /* can we bind the worker on the requested cpu ? */
  333. unsigned ind;
  334. for (ind = config->current_bindid; ind < config->nhwcpus; ind++)
  335. {
  336. if (config->workers_bindid[ind] == requested_cpu)
  337. {
  338. /* the cpu is available, we use it ! In order
  339. * to make sure that it will not be used again
  340. * later on, we remove the entry from the list
  341. * */
  342. config->workers_bindid[ind] =
  343. config->workers_bindid[config->current_bindid];
  344. config->workers_bindid[config->current_bindid] = requested_cpu;
  345. found = 1;
  346. break;
  347. }
  348. }
  349. }
  350. unsigned i = ((config->current_bindid++) % STARPU_NMAXWORKERS);
  351. return (int)config->workers_bindid[i];
  352. }
  353. void _starpu_bind_thread_on_cpu(struct starpu_machine_config_s *config __attribute__((unused)), unsigned cpuid)
  354. {
  355. #ifdef STARPU_HAVE_HWLOC
  356. int ret;
  357. _starpu_init_topology(config);
  358. hwloc_obj_t obj = hwloc_get_obj_by_depth(config->hwtopology, config->cpu_depth, cpuid);
  359. hwloc_cpuset_t set = obj->cpuset;
  360. hwloc_cpuset_singlify(set);
  361. ret = hwloc_set_cpubind(config->hwtopology, set, HWLOC_CPUBIND_THREAD);
  362. if (ret)
  363. {
  364. perror("binding thread");
  365. STARPU_ABORT();
  366. }
  367. #elif defined(HAVE_PTHREAD_SETAFFINITY_NP)
  368. int ret;
  369. /* fix the thread on the correct cpu */
  370. cpu_set_t aff_mask;
  371. CPU_ZERO(&aff_mask);
  372. CPU_SET(cpuid, &aff_mask);
  373. pthread_t self = pthread_self();
  374. ret = pthread_setaffinity_np(self, sizeof(aff_mask), &aff_mask);
  375. if (ret)
  376. {
  377. perror("binding thread");
  378. STARPU_ABORT();
  379. }
  380. #elif defined(__MINGW32__) || defined(__CYGWIN__)
  381. DWORD mask = 1 << cpuid;
  382. if (!SetThreadAffinityMask(GetCurrentThread(), mask)) {
  383. fprintf(stderr,"SetThreadMaskAffinity(%lx) failed\n", mask);
  384. STARPU_ABORT();
  385. }
  386. #else
  387. #warning no CPU binding support
  388. #endif
  389. }
  390. static void _starpu_init_workers_binding(struct starpu_machine_config_s *config)
  391. {
  392. /* launch one thread per CPU */
  393. unsigned ram_memory_node;
  394. /* a single cpu is dedicated for the accelerators */
  395. int accelerator_bindid = -1;
  396. /* note that even if the CPU cpu are not used, we always have a STARPU_RAM node */
  397. /* TODO : support NUMA ;) */
  398. ram_memory_node = _starpu_register_memory_node(STARPU_RAM);
  399. unsigned worker;
  400. for (worker = 0; worker < config->nworkers; worker++)
  401. {
  402. unsigned memory_node = -1;
  403. unsigned is_a_set_of_accelerators = 0;
  404. struct starpu_worker_s *workerarg = &config->workers[worker];
  405. /* Perhaps the worker has some "favourite" bindings */
  406. int *preferred_binding = NULL;
  407. int npreferred = 0;
  408. /* select the memory node that contains worker's memory */
  409. switch (workerarg->arch) {
  410. case STARPU_CPU_WORKER:
  411. /* "dedicate" a cpu cpu to that worker */
  412. is_a_set_of_accelerators = 0;
  413. memory_node = ram_memory_node;
  414. break;
  415. #ifdef STARPU_USE_GORDON
  416. case STARPU_GORDON_WORKER:
  417. is_a_set_of_accelerators = 1;
  418. memory_node = ram_memory_node;
  419. break;
  420. #endif
  421. #ifdef STARPU_USE_CUDA
  422. case STARPU_CUDA_WORKER:
  423. if (may_bind_automatically)
  424. {
  425. /* StarPU is allowed to bind threads automatically */
  426. preferred_binding = _starpu_get_gpu_affinity_vector(workerarg->devid);
  427. npreferred = config->nhwcpus;
  428. }
  429. is_a_set_of_accelerators = 0;
  430. memory_node = _starpu_register_memory_node(STARPU_CUDA_RAM);
  431. break;
  432. #endif
  433. default:
  434. STARPU_ABORT();
  435. }
  436. if (is_a_set_of_accelerators) {
  437. if (accelerator_bindid == -1)
  438. accelerator_bindid = _starpu_get_next_bindid(config, preferred_binding, npreferred);
  439. workerarg->bindid = accelerator_bindid;
  440. }
  441. else {
  442. workerarg->bindid = _starpu_get_next_bindid(config, preferred_binding, npreferred);
  443. }
  444. workerarg->memory_node = memory_node;
  445. }
  446. }
  447. int _starpu_build_topology(struct starpu_machine_config_s *config)
  448. {
  449. int ret;
  450. struct starpu_conf *user_conf = config->user_conf;
  451. ret = _starpu_init_machine_config(config, user_conf);
  452. if (ret)
  453. return ret;
  454. /* for the data management library */
  455. _starpu_init_memory_nodes();
  456. _starpu_init_workers_binding(config);
  457. return 0;
  458. }
  459. void _starpu_destroy_topology(struct starpu_machine_config_s *config __attribute__ ((unused)))
  460. {
  461. /* cleanup StarPU internal data structures */
  462. _starpu_deinit_memory_nodes();
  463. #ifdef STARPU_HAVE_HWLOC
  464. hwloc_topology_destroy(config->hwtopology);
  465. #endif
  466. topology_is_initialized = 0;
  467. }