You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

init.c 25 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993
  1. /*****************************************************************************
  2. Copyright (c) 2011-2014, The OpenBLAS Project
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are
  6. met:
  7. 1. Redistributions of source code must retain the above copyright
  8. notice, this list of conditions and the following disclaimer.
  9. 2. Redistributions in binary form must reproduce the above copyright
  10. notice, this list of conditions and the following disclaimer in
  11. the documentation and/or other materials provided with the
  12. distribution.
  13. 3. Neither the name of the OpenBLAS project nor the names of
  14. its contributors may be used to endorse or promote products
  15. derived from this software without specific prior written
  16. permission.
  17. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  18. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  21. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  23. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  24. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  25. OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  26. USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. **********************************************************************************/
  28. /*********************************************************************/
  29. /* Copyright 2009, 2010 The University of Texas at Austin. */
  30. /* All rights reserved. */
  31. /* */
  32. /* Redistribution and use in source and binary forms, with or */
  33. /* without modification, are permitted provided that the following */
  34. /* conditions are met: */
  35. /* */
  36. /* 1. Redistributions of source code must retain the above */
  37. /* copyright notice, this list of conditions and the following */
  38. /* disclaimer. */
  39. /* */
  40. /* 2. Redistributions in binary form must reproduce the above */
  41. /* copyright notice, this list of conditions and the following */
  42. /* disclaimer in the documentation and/or other materials */
  43. /* provided with the distribution. */
  44. /* */
  45. /* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY OF TEXAS AT */
  46. /* AUSTIN ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, */
  47. /* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF */
  48. /* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE */
  49. /* DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OF TEXAS AT */
  50. /* AUSTIN OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, */
  51. /* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES */
  52. /* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE */
  53. /* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR */
  54. /* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF */
  55. /* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT */
  56. /* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT */
  57. /* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE */
  58. /* POSSIBILITY OF SUCH DAMAGE. */
  59. /* */
  60. /* The views and conclusions contained in the software and */
  61. /* documentation are those of the authors and should not be */
  62. /* interpreted as representing official policies, either expressed */
  63. /* or implied, of The University of Texas at Austin. */
  64. /*********************************************************************/
  65. #include "common.h"
  66. #if defined(OS_LINUX) && defined(SMP)
  67. #define _GNU_SOURCE
  68. #include <sys/sysinfo.h>
  69. #include <sys/syscall.h>
  70. #include <sys/shm.h>
  71. #include <fcntl.h>
  72. #include <sched.h>
  73. #include <dirent.h>
  74. #include <dlfcn.h>
  75. #include <unistd.h>
  76. #include <string.h>
  77. #if defined(BIGNUMA)
  78. // max number of nodes as defined in numa.h
  79. // max cpus as defined in sched.h
  80. #define MAX_NODES 128
  81. #define MAX_CPUS CPU_SETSIZE
  82. #else
  83. #define MAX_NODES 16
  84. #define MAX_CPUS 256
  85. #endif
  86. #define NCPUBITS (8*sizeof(unsigned long))
  87. #define MAX_BITMASK_LEN (MAX_CPUS/NCPUBITS)
  88. #define CPUELT(cpu) ((cpu) / NCPUBITS)
  89. #define CPUMASK(cpu) ((unsigned long) 1UL << ((cpu) % NCPUBITS))
  90. #define SH_MAGIC 0x510510
  91. #define CPUMAP_NAME "/sys/devices/system/node/node%d/cpumap"
  92. #define SHARE_NAME "/sys/devices/system/cpu/cpu%d/cache/index%d/shared_cpu_map"
  93. #define NODE_DIR "/sys/devices/system/node"
  94. //#undef DEBUG
  95. /* Private variables */
  96. typedef struct {
  97. unsigned long lock;
  98. unsigned int magic;
  99. unsigned int shmid;
  100. int num_nodes;
  101. int num_procs;
  102. int final_num_procs;
  103. unsigned long avail [MAX_BITMASK_LEN];
  104. int avail_count;
  105. unsigned long cpu_info [MAX_CPUS];
  106. unsigned long node_info [MAX_NODES][MAX_BITMASK_LEN];
  107. int cpu_use[MAX_CPUS];
  108. } shm_t;
  109. static cpu_set_t cpu_orig_mask[4];
  110. static int cpu_mapping[MAX_CPUS];
  111. static int node_mapping[MAX_CPUS * 4];
  112. static int cpu_sub_mapping[MAX_CPUS];
  113. static int disable_mapping;
  114. /* Number of cores per nodes */
  115. static int node_cpu[MAX_NODES];
  116. static int node_equal = 0;
  117. static shm_t *common = (void *)-1;
  118. static int shmid, pshmid;
  119. static void *paddr;
  120. static unsigned long lprocmask[MAX_BITMASK_LEN], lnodemask;
  121. static int lprocmask_count = 0;
  122. static int numprocs = 1;
  123. static int numnodes = 1;
  124. #if 1
  125. #define READ_CPU(x) ( (x) & 0xff)
  126. #define READ_NODE(x) (((x) >> 8) & 0xff)
  127. #define READ_CORE(x) (((x) >> 16) & 0xff)
  128. #define WRITE_CPU(x) (x)
  129. #define WRITE_NODE(x) ((x) << 8)
  130. #define WRITE_CORE(x) ((x) << 16)
  131. #else
  132. #define READ_CPU(x) ( (x) & 0xff)
  133. #define READ_CORE(x) (((x) >> 8) & 0xff)
  134. #define READ_NODE(x) (((x) >> 16) & 0xff)
  135. #define WRITE_CPU(x) (x)
  136. #define WRITE_CORE(x) ((x) << 8)
  137. #define WRITE_NODE(x) ((x) << 16)
  138. #endif
  139. static inline int popcount(unsigned long number) {
  140. int count = 0;
  141. while (number > 0) {
  142. if (number & 1) count ++;
  143. number >>= 1;
  144. }
  145. return count;
  146. }
  147. static inline int rcount(unsigned long number) {
  148. int count = -1;
  149. while ((number > 0) && ((number & 0)) == 0) {
  150. count ++;
  151. number >>= 1;
  152. }
  153. return count;
  154. }
  155. /***
  156. Known issue: The number of CPUs/cores should less
  157. than sizeof(unsigned long). On 64 bits, the limit
  158. is 64. On 32 bits, it is 32.
  159. ***/
  160. static inline void get_cpumap(int node, unsigned long * node_info) {
  161. int infile;
  162. unsigned long affinity[32];
  163. char name[160];
  164. char cpumap[160];
  165. char *dummy;
  166. int i=0;
  167. int count=0;
  168. int k=0;
  169. sprintf(name, CPUMAP_NAME, node);
  170. infile = open(name, O_RDONLY);
  171. for(i=0; i<32; i++){
  172. affinity[i] = 0;
  173. }
  174. if (infile != -1) {
  175. read(infile, cpumap, sizeof(cpumap));
  176. for(i=0; i<160; i++){
  177. if(cpumap[i] == '\n')
  178. break;
  179. if(cpumap[i] != ','){
  180. name[k++]=cpumap[i];
  181. //Enough data for Hex
  182. if(k >= NCPUBITS/4){
  183. affinity[count++] = strtoul(name, &dummy, 16);
  184. k=0;
  185. }
  186. }
  187. }
  188. if(k!=0){
  189. name[k]='\0';
  190. affinity[count++] = strtoul(name, &dummy, 16);
  191. k=0;
  192. }
  193. // 0-63bit -> node_info[0], 64-128bit -> node_info[1] ....
  194. // revert the sequence
  195. for(i=0; i<count && i<MAX_BITMASK_LEN; i++){
  196. node_info[i]=affinity[count-i-1];
  197. }
  198. close(infile);
  199. }
  200. return ;
  201. }
  202. static inline void get_share(int cpu, int level, unsigned long * share) {
  203. int infile;
  204. unsigned long affinity[32];
  205. char cpumap[160];
  206. char name[160];
  207. char *dummy;
  208. int count=0;
  209. int i=0,k=0;
  210. int bitmask_idx = 0;
  211. sprintf(name, SHARE_NAME, cpu, level);
  212. infile = open(name, O_RDONLY);
  213. // Init share
  214. for(i=0; i<MAX_BITMASK_LEN; i++){
  215. share[i]=0;
  216. }
  217. bitmask_idx = CPUELT(cpu);
  218. share[bitmask_idx] = CPUMASK(cpu);
  219. if (infile != -1) {
  220. read(infile, cpumap, sizeof(cpumap));
  221. for(i=0; i<160; i++){
  222. if(cpumap[i] == '\n')
  223. break;
  224. if(cpumap[i] != ','){
  225. name[k++]=cpumap[i];
  226. //Enough data
  227. if(k >= NCPUBITS/4){
  228. affinity[count++] = strtoul(name, &dummy, 16);
  229. k=0;
  230. }
  231. }
  232. }
  233. if(k!=0){
  234. name[k]='\0';
  235. affinity[count++] = strtoul(name, &dummy, 16);
  236. k=0;
  237. }
  238. // 0-63bit -> node_info[0], 64-128bit -> node_info[1] ....
  239. // revert the sequence
  240. for(i=0; i<count && i<MAX_BITMASK_LEN; i++){
  241. share[i]=affinity[count-i-1];
  242. }
  243. close(infile);
  244. }
  245. return ;
  246. }
  247. static int numa_check(void) {
  248. DIR *dp;
  249. struct dirent *dir;
  250. int node;
  251. int j;
  252. common -> num_nodes = 0;
  253. dp = opendir(NODE_DIR);
  254. if (dp == NULL) {
  255. common -> num_nodes = 1;
  256. return 0;
  257. }
  258. for (node = 0; node < MAX_NODES; node ++) {
  259. for (j = 0; j<MAX_BITMASK_LEN; j++) common -> node_info[node][j] = 0;
  260. }
  261. while ((dir = readdir(dp)) != NULL) {
  262. if (strncmp(dir->d_name, "node", 4)==0) {
  263. node = atoi(&dir -> d_name[4]);
  264. if (node > MAX_NODES) {
  265. fprintf(stderr, "\nOpenBLAS Warning : MAX_NODES (NUMA) is too small. Terminated.\n");
  266. exit(1);
  267. }
  268. common -> num_nodes ++;
  269. get_cpumap(node, common->node_info[node]);
  270. }
  271. }
  272. closedir(dp);
  273. if (common -> num_nodes == 1) return 1;
  274. #ifdef DEBUG
  275. fprintf(stderr, "Numa found : number of Nodes = %2d\n", common -> num_nodes);
  276. for (node = 0; node < common -> num_nodes; node ++)
  277. fprintf(stderr, "MASK (%2d) : %08lx\n", node, common -> node_info[node][0]);
  278. #endif
  279. return common -> num_nodes;
  280. }
  281. #if defined(__GLIBC_PREREQ)
  282. #if !__GLIBC_PREREQ(2, 6)
  283. int sched_getcpu(void)
  284. {
  285. int cpu;
  286. FILE *fp = NULL;
  287. if ( (fp = fopen("/proc/self/stat", "r")) == NULL)
  288. return -1;
  289. if ( fscanf( fp, "%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%d", &cpu) != 1) {
  290. fclose (fp);
  291. return -1;
  292. }
  293. fclose (fp);
  294. return(cpu);
  295. }
  296. #endif
  297. #endif
  298. static void numa_mapping(void) {
  299. int node, cpu, core;
  300. int i, j, h;
  301. unsigned long work, bit;
  302. int count = 0;
  303. int bitmask_idx = 0;
  304. int current_cpu;
  305. int current_node = 0;
  306. int cpu_count = 0;
  307. for (node = 0; node < common -> num_nodes; node ++) {
  308. core = 0;
  309. for (cpu = 0; cpu < common -> num_procs; cpu ++) {
  310. bitmask_idx = CPUELT(cpu);
  311. if (common -> node_info[node][bitmask_idx] & common -> avail[bitmask_idx] & CPUMASK(cpu)) {
  312. common -> cpu_info[count] = WRITE_CORE(core) | WRITE_NODE(node) | WRITE_CPU(cpu);
  313. count ++;
  314. core ++;
  315. }
  316. }
  317. }
  318. #ifdef DEBUG
  319. fprintf(stderr, "\nFrom /sys ...\n\n");
  320. for (cpu = 0; cpu < count; cpu++)
  321. fprintf(stderr, "CPU (%2d) : %08lx\n", cpu, common -> cpu_info[cpu]);
  322. #endif
  323. current_cpu = sched_getcpu();
  324. for (cpu = 0; cpu < count; cpu++) {
  325. if (READ_CPU(common -> cpu_info[cpu]) == current_cpu) {
  326. current_node = READ_NODE(common -> cpu_info[cpu]);
  327. break;
  328. }
  329. }
  330. for (i = 0; i < MAX_BITMASK_LEN; i++)
  331. cpu_count += popcount(common -> node_info[current_node][i] & common -> avail[i]);
  332. /*
  333. * If all the processes can be accommodated in the
  334. * in the current node itself, then bind to cores
  335. * from the current node only
  336. */
  337. if (numprocs <= cpu_count) {
  338. /*
  339. * First sort all the cores in order from the current node.
  340. * Then take remaining nodes one by one in order,
  341. * and sort their cores in order.
  342. */
  343. for (i = 0; i < count; i++) {
  344. for (j = 0; j < count - 1; j++) {
  345. int node_1, node_2;
  346. int core_1, core_2;
  347. int swap = 0;
  348. node_1 = READ_NODE(common -> cpu_info[j]);
  349. node_2 = READ_NODE(common -> cpu_info[j + 1]);
  350. core_1 = READ_CORE(common -> cpu_info[j]);
  351. core_2 = READ_CORE(common -> cpu_info[j + 1]);
  352. if (node_1 == node_2) {
  353. if (core_1 > core_2)
  354. swap = 1;
  355. } else {
  356. if ((node_2 == current_node) ||
  357. ((node_1 != current_node) && (node_1 > node_2)))
  358. swap = 1;
  359. }
  360. if (swap) {
  361. unsigned long temp;
  362. temp = common->cpu_info[j];
  363. common->cpu_info[j] = common->cpu_info[j + 1];
  364. common->cpu_info[j + 1] = temp;
  365. }
  366. }
  367. }
  368. } else {
  369. h = 1;
  370. while (h < count) h = 2 * h + 1;
  371. while (h > 1) {
  372. h /= 2;
  373. for (i = h; i < count; i++) {
  374. work = common -> cpu_info[i];
  375. bit = CPU_ISSET(i, &cpu_orig_mask[0]);
  376. j = i - h;
  377. while (work < common -> cpu_info[j]) {
  378. common -> cpu_info[j + h] = common -> cpu_info[j];
  379. if (CPU_ISSET(j, &cpu_orig_mask[0])) {
  380. CPU_SET(j + h, &cpu_orig_mask[0]);
  381. } else {
  382. CPU_CLR(j + h, &cpu_orig_mask[0]);
  383. }
  384. j -= h;
  385. if (j < 0) break;
  386. }
  387. common -> cpu_info[j + h] = work;
  388. if (bit) {
  389. CPU_SET(j + h, &cpu_orig_mask[0]);
  390. } else {
  391. CPU_CLR(j + h, &cpu_orig_mask[0]);
  392. }
  393. }
  394. }
  395. }
  396. #ifdef DEBUG
  397. fprintf(stderr, "\nSorting ...\n\n");
  398. for (cpu = 0; cpu < count; cpu++)
  399. fprintf(stderr, "CPUINFO (%2d) : %08lx (CPU=%3lu CORE=%3lu NODE=%3lu)\n", cpu, common -> cpu_info[cpu],
  400. READ_CPU(common -> cpu_info[cpu]),
  401. READ_CORE(common -> cpu_info[cpu]),
  402. READ_NODE(common -> cpu_info[cpu]));
  403. #endif
  404. }
  405. static void disable_hyperthread(void) {
  406. unsigned long share[MAX_BITMASK_LEN];
  407. int cpu;
  408. int bitmask_idx = 0;
  409. int i=0, count=0;
  410. bitmask_idx = CPUELT(common -> num_procs);
  411. for(i=0; i< bitmask_idx; i++){
  412. common -> avail[count++] = 0xFFFFFFFFFFFFFFFFUL;
  413. }
  414. if(CPUMASK(common -> num_procs) != 1){
  415. common -> avail[count++] = CPUMASK(common -> num_procs) - 1;
  416. }
  417. common -> avail_count = count;
  418. /* if(common->num_procs > 64){ */
  419. /* fprintf(stderr, "\nOpenBLAS Warning : The number of CPU/Cores(%d) is beyond the limit(64). Terminated.\n", common->num_procs); */
  420. /* exit(1); */
  421. /* }else if(common->num_procs == 64){ */
  422. /* common -> avail = 0xFFFFFFFFFFFFFFFFUL; */
  423. /* }else */
  424. /* common -> avail = (1UL << common -> num_procs) - 1; */
  425. #ifdef DEBUG
  426. fprintf(stderr, "\nAvail CPUs : ");
  427. for(i=0; i<count; i++)
  428. fprintf(stderr, "%04lx ", common -> avail[i]);
  429. fprintf(stderr, ".\n");
  430. #endif
  431. for (cpu = 0; cpu < common -> num_procs; cpu ++) {
  432. get_share(cpu, 1, share);
  433. //When the shared cpu are in different element of share & avail array, this may be a bug.
  434. for (i = 0; i < count ; i++){
  435. share[i] &= common->avail[i];
  436. if (popcount(share[i]) > 1) {
  437. #ifdef DEBUG
  438. fprintf(stderr, "Detected Hyper Threading on CPU %4x; disabled CPU %04lx.\n",
  439. cpu, share[i] & ~(CPUMASK(cpu)));
  440. #endif
  441. common -> avail[i] &= ~((share[i] & ~ CPUMASK(cpu)));
  442. }
  443. }
  444. }
  445. }
  446. static void disable_affinity(void) {
  447. int i=0;
  448. int bitmask_idx=0;
  449. int count=0;
  450. #ifdef DEBUG
  451. fprintf(stderr, "Final all available CPUs : %04lx.\n\n", common -> avail[0]);
  452. fprintf(stderr, "CPU mask : %04lx.\n\n", *(unsigned long *)&cpu_orig_mask[0]);
  453. #endif
  454. /* if(common->final_num_procs > 64){ */
  455. /* fprintf(stderr, "\nOpenBLAS Warining : The number of CPU/Cores(%d) is beyond the limit(64). Terminated.\n", common->final_num_procs); */
  456. /* exit(1); */
  457. /* }else if(common->final_num_procs == 64){ */
  458. /* lprocmask = 0xFFFFFFFFFFFFFFFFUL; */
  459. /* }else */
  460. /* lprocmask = (1UL << common -> final_num_procs) - 1; */
  461. bitmask_idx = CPUELT(common -> final_num_procs);
  462. for(i=0; i< bitmask_idx; i++){
  463. lprocmask[count++] = 0xFFFFFFFFFFFFFFFFUL;
  464. }
  465. if(CPUMASK(common -> final_num_procs) != 1){
  466. lprocmask[count++] = CPUMASK(common -> final_num_procs) - 1;
  467. }
  468. lprocmask_count = count;
  469. #ifndef USE_OPENMP
  470. for(i=0; i< count; i++){
  471. lprocmask[i] &= common->avail[i];
  472. }
  473. #endif
  474. #ifdef DEBUG
  475. fprintf(stderr, "I choose these CPUs : %04lx.\n\n", lprocmask[0]);
  476. #endif
  477. }
  478. static void setup_mempolicy(void) {
  479. int cpu, mynode, maxcpu;
  480. for (cpu = 0; cpu < MAX_NODES; cpu ++) node_cpu[cpu] = 0;
  481. maxcpu = 0;
  482. for (cpu = 0; cpu < numprocs; cpu ++) {
  483. mynode = READ_NODE(common -> cpu_info[cpu_sub_mapping[cpu]]);
  484. lnodemask |= (1UL << mynode);
  485. node_cpu[mynode] ++;
  486. if (maxcpu < node_cpu[mynode]) maxcpu = node_cpu[mynode];
  487. }
  488. node_equal = 1;
  489. for (cpu = 0; cpu < MAX_NODES; cpu ++) if ((node_cpu[cpu] != 0) && (node_cpu[cpu] != maxcpu)) node_equal = 0;
  490. if (lnodemask) {
  491. #ifdef DEBUG
  492. fprintf(stderr, "Node mask = %lx\n", lnodemask);
  493. #endif
  494. my_set_mempolicy(MPOL_INTERLEAVE, &lnodemask, sizeof(lnodemask) * 8);
  495. numnodes = popcount(lnodemask);
  496. }
  497. }
  498. static inline int is_dead(int id) {
  499. struct shmid_ds ds;
  500. return shmctl(id, IPC_STAT, &ds);
  501. }
  502. static void open_shmem(void) {
  503. int try = 0;
  504. do {
  505. #if defined(BIGNUMA)
  506. // raised to 32768, enough for 128 nodes and 1024 cups
  507. shmid = shmget(SH_MAGIC, 32768, 0666);
  508. #else
  509. shmid = shmget(SH_MAGIC, 4096, 0666);
  510. #endif
  511. if (shmid == -1) {
  512. #if defined(BIGNUMA)
  513. shmid = shmget(SH_MAGIC, 32768, IPC_CREAT | 0666);
  514. #else
  515. shmid = shmget(SH_MAGIC, 4096, IPC_CREAT | 0666);
  516. #endif
  517. }
  518. try ++;
  519. } while ((try < 10) && (shmid == -1));
  520. if (shmid == -1) {
  521. fprintf(stderr, "GotoBLAS : Can't open shared memory. Terminated.\n");
  522. exit(1);
  523. }
  524. if (shmid != -1) common = (shm_t *)shmat(shmid, NULL, 0);
  525. #ifdef DEBUG
  526. fprintf(stderr, "Shared Memory id = %x Address = %p\n", shmid, common);
  527. #endif
  528. }
  529. static void create_pshmem(void) {
  530. pshmid = shmget(IPC_PRIVATE, 4096, IPC_CREAT | 0666);
  531. paddr = shmat(pshmid, NULL, 0);
  532. shmctl(pshmid, IPC_RMID, 0);
  533. #ifdef DEBUG
  534. fprintf(stderr, "Private Shared Memory id = %x Address = %p\n", pshmid, paddr);
  535. #endif
  536. }
  537. static void local_cpu_map(void) {
  538. int cpu, id, mapping;
  539. int bitmask_idx = 0;
  540. cpu = 0;
  541. mapping = 0;
  542. do {
  543. id = common -> cpu_use[cpu];
  544. if (id > 0) {
  545. if (is_dead(id)) common -> cpu_use[cpu] = 0;
  546. }
  547. bitmask_idx = CPUELT(cpu);
  548. if ((common -> cpu_use[cpu] == 0) && (lprocmask[bitmask_idx] & CPUMASK(cpu))) {
  549. common -> cpu_use[cpu] = pshmid;
  550. cpu_mapping[mapping] = READ_CPU(common -> cpu_info[cpu]);
  551. cpu_sub_mapping[mapping] = cpu;
  552. mapping ++;
  553. }
  554. cpu ++;
  555. } while ((mapping < numprocs) && (cpu < common -> final_num_procs));
  556. disable_mapping = 0;
  557. if ((mapping < numprocs) || (numprocs == 1)) {
  558. for (cpu = 0; cpu < common -> final_num_procs; cpu ++) {
  559. if (common -> cpu_use[cpu] == pshmid) common -> cpu_use[cpu] = 0;
  560. }
  561. disable_mapping = 1;
  562. }
  563. #ifdef DEBUG
  564. for (cpu = 0; cpu < numprocs; cpu ++) {
  565. fprintf(stderr, "Local Mapping : %2d --> %2d (%2d)\n", cpu, cpu_mapping[cpu], cpu_sub_mapping[cpu]);
  566. }
  567. #endif
  568. }
  569. /* Public Functions */
  570. int get_num_procs(void) { return numprocs; }
  571. int get_num_nodes(void) { return numnodes; }
  572. int get_node_equal(void) {
  573. return (((blas_cpu_number % numnodes) == 0) && node_equal);
  574. }
  575. int gotoblas_set_affinity(int pos) {
  576. cpu_set_t cpu_mask;
  577. int mynode = 1;
  578. /* if number of threads is larger than inital condition */
  579. if (pos < 0) {
  580. sched_setaffinity(0, sizeof(cpu_orig_mask), &cpu_orig_mask[0]);
  581. return 0;
  582. }
  583. if (!disable_mapping) {
  584. mynode = READ_NODE(common -> cpu_info[cpu_sub_mapping[pos]]);
  585. #ifdef DEBUG
  586. fprintf(stderr, "Giving Affinity[%4d %3d] --> %3d My node = %3d\n", getpid(), pos, cpu_mapping[pos], mynode);
  587. #endif
  588. CPU_ZERO(&cpu_mask);
  589. CPU_SET (cpu_mapping[pos], &cpu_mask);
  590. sched_setaffinity(0, sizeof(cpu_mask), &cpu_mask);
  591. node_mapping[WhereAmI()] = mynode;
  592. }
  593. return mynode;
  594. }
  595. int get_node(void) {
  596. if (!disable_mapping) return node_mapping[WhereAmI()];
  597. return 1;
  598. }
  599. static int initialized = 0;
  600. void gotoblas_affinity_init(void) {
  601. int cpu, num_avail;
  602. #ifndef USE_OPENMP
  603. cpu_set_t cpu_mask;
  604. #endif
  605. int i;
  606. if (initialized) return;
  607. initialized = 1;
  608. sched_getaffinity(0, sizeof(cpu_orig_mask), &cpu_orig_mask[0]);
  609. #ifdef USE_OPENMP
  610. numprocs = 0;
  611. #else
  612. numprocs = readenv_atoi("OPENBLAS_NUM_THREADS");
  613. if (numprocs == 0) numprocs = readenv_atoi("GOTO_NUM_THREADS");
  614. #endif
  615. if (numprocs == 0) numprocs = readenv_atoi("OMP_NUM_THREADS");
  616. numnodes = 1;
  617. if (numprocs == 1) {
  618. disable_mapping = 1;
  619. return;
  620. }
  621. create_pshmem();
  622. open_shmem();
  623. while ((common -> lock) && (common -> magic != SH_MAGIC)) {
  624. if (is_dead(common -> shmid)) {
  625. common -> lock = 0;
  626. common -> shmid = 0;
  627. common -> magic = 0;
  628. } else {
  629. sched_yield();
  630. }
  631. }
  632. blas_lock(&common -> lock);
  633. if ((common -> shmid) && is_dead(common -> shmid)) common -> magic = 0;
  634. common -> shmid = pshmid;
  635. if (common -> magic != SH_MAGIC) {
  636. cpu_set_t *cpusetp;
  637. int nums;
  638. int ret;
  639. #ifdef DEBUG
  640. fprintf(stderr, "Shared Memory Initialization.\n");
  641. #endif
  642. //returns the number of processors which are currently online
  643. nums = sysconf(_SC_NPROCESSORS_CONF);
  644. #if !defined(__GLIBC_PREREQ)
  645. common->num_procs = nums;
  646. #else
  647. #if !__GLIBC_PREREQ(2, 3)
  648. common->num_procs = nums;
  649. #elif __GLIBC_PREREQ(2, 7)
  650. cpusetp = CPU_ALLOC(nums);
  651. if (cpusetp == NULL) {
  652. common->num_procs = nums;
  653. } else {
  654. size_t size;
  655. size = CPU_ALLOC_SIZE(nums);
  656. ret = sched_getaffinity(0,size,cpusetp);
  657. if (ret!=0)
  658. common->num_procs = nums;
  659. else
  660. common->num_procs = CPU_COUNT_S(size,cpusetp);
  661. }
  662. CPU_FREE(cpusetp);
  663. #else
  664. ret = sched_getaffinity(0,sizeof(cpu_set_t), cpusetp);
  665. if (ret!=0) {
  666. common->num_procs = nums;
  667. } else {
  668. #if !__GLIBC_PREREQ(2, 6)
  669. int i;
  670. int n = 0;
  671. for (i=0;i<nums;i++)
  672. if (CPU_ISSET(i,cpusetp)) n++;
  673. common->num_procs = n;
  674. }
  675. #else
  676. common->num_procs = CPU_COUNT(sizeof(cpu_set_t),cpusetp);
  677. #endif
  678. #endif
  679. #endif
  680. if(common -> num_procs > MAX_CPUS) {
  681. fprintf(stderr, "\nOpenBLAS Warning : The number of CPU/Cores(%d) is beyond the limit(%d). Terminated.\n", common->num_procs, MAX_CPUS);
  682. exit(1);
  683. }
  684. for (cpu = 0; cpu < common -> num_procs; cpu++) common -> cpu_info[cpu] = cpu;
  685. numa_check();
  686. disable_hyperthread();
  687. if (common -> num_nodes > 1) numa_mapping();
  688. common -> final_num_procs = 0;
  689. for(i = 0; i < common -> avail_count; i++) common -> final_num_procs += rcount(common -> avail[i]) + 1; //Make the max cpu number.
  690. for (cpu = 0; cpu < common -> final_num_procs; cpu ++) common -> cpu_use[cpu] = 0;
  691. common -> magic = SH_MAGIC;
  692. }
  693. disable_affinity();
  694. num_avail = 0;
  695. for(i=0; i<lprocmask_count; i++) num_avail += popcount(lprocmask[i]);
  696. if ((numprocs <= 0) || (numprocs > num_avail)) numprocs = num_avail;
  697. #ifdef DEBUG
  698. fprintf(stderr, "Number of threads = %d\n", numprocs);
  699. #endif
  700. local_cpu_map();
  701. blas_unlock(&common -> lock);
  702. #ifndef USE_OPENMP
  703. if (!disable_mapping) {
  704. #ifdef DEBUG
  705. fprintf(stderr, "Giving Affinity[%3d] --> %3d\n", 0, cpu_mapping[0]);
  706. #endif
  707. CPU_ZERO(&cpu_mask);
  708. CPU_SET (cpu_mapping[0], &cpu_mask);
  709. sched_setaffinity(0, sizeof(cpu_mask), &cpu_mask);
  710. node_mapping[WhereAmI()] = READ_NODE(common -> cpu_info[cpu_sub_mapping[0]]);
  711. setup_mempolicy();
  712. if (readenv_atoi("OPENBLAS_MAIN_FREE") || readenv_atoi("GOTOBLAS_MAIN_FREE")) {
  713. sched_setaffinity(0, sizeof(cpu_orig_mask), &cpu_orig_mask[0]);
  714. }
  715. }
  716. #endif
  717. #ifdef DEBUG
  718. fprintf(stderr, "Initialization is done.\n");
  719. #endif
  720. }
  721. void gotoblas_affinity_quit(void) {
  722. int i;
  723. struct shmid_ds ds;
  724. #ifdef DEBUG
  725. fprintf(stderr, "Terminating ..\n");
  726. #endif
  727. if ((numprocs == 1) || (initialized == 0)) return;
  728. if (!disable_mapping) {
  729. blas_lock(&common -> lock);
  730. for (i = 0; i < numprocs; i ++) common -> cpu_use[cpu_mapping[i]] = -1;
  731. blas_unlock(&common -> lock);
  732. }
  733. shmctl(shmid, IPC_STAT, &ds);
  734. if (ds.shm_nattch == 1) shmctl(shmid, IPC_RMID, 0);
  735. shmdt(common);
  736. shmdt(paddr);
  737. initialized = 0;
  738. }
  739. #else
  740. void gotoblas_affinity_init(void) {};
  741. void gotoblas_set_affinity(int threads) {};
  742. void gotoblas_set_affinity2(int threads) {};
  743. void gotoblas_affinity_reschedule(void) {};
  744. int get_num_procs(void) { return sysconf(_SC_NPROCESSORS_CONF); }
  745. int get_num_nodes(void) { return 1; }
  746. int get_node(void) { return 1;}
  747. #endif