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 24 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933
  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. static void numa_mapping(void) {
  282. int node, cpu, core;
  283. int i, j, h;
  284. unsigned long work, bit;
  285. int count = 0;
  286. int bitmask_idx = 0;
  287. int current_cpu;
  288. int current_node = 0;
  289. int cpu_count = 0;
  290. for (node = 0; node < common -> num_nodes; node ++) {
  291. core = 0;
  292. for (cpu = 0; cpu < common -> num_procs; cpu ++) {
  293. bitmask_idx = CPUELT(cpu);
  294. if (common -> node_info[node][bitmask_idx] & common -> avail[bitmask_idx] & CPUMASK(cpu)) {
  295. common -> cpu_info[count] = WRITE_CORE(core) | WRITE_NODE(node) | WRITE_CPU(cpu);
  296. count ++;
  297. core ++;
  298. }
  299. }
  300. }
  301. #ifdef DEBUG
  302. fprintf(stderr, "\nFrom /sys ...\n\n");
  303. for (cpu = 0; cpu < count; cpu++)
  304. fprintf(stderr, "CPU (%2d) : %08lx\n", cpu, common -> cpu_info[cpu]);
  305. #endif
  306. current_cpu = sched_getcpu();
  307. for (cpu = 0; cpu < count; cpu++) {
  308. if (READ_CPU(common -> cpu_info[cpu]) == current_cpu) {
  309. current_node = READ_NODE(common -> cpu_info[cpu]);
  310. break;
  311. }
  312. }
  313. for (i = 0; i < MAX_BITMASK_LEN; i++)
  314. cpu_count += popcount(common -> node_info[current_node][i] & common -> avail[i]);
  315. /*
  316. * If all the processes can be accommodated in the
  317. * in the current node itself, then bind to cores
  318. * from the current node only
  319. */
  320. if (numprocs <= cpu_count) {
  321. /*
  322. * First sort all the cores in order from the current node.
  323. * Then take remaining nodes one by one in order,
  324. * and sort their cores in order.
  325. */
  326. for (i = 0; i < count; i++) {
  327. for (j = 0; j < count - 1; j++) {
  328. int node_1, node_2;
  329. int core_1, core_2;
  330. int swap = 0;
  331. node_1 = READ_NODE(common -> cpu_info[j]);
  332. node_2 = READ_NODE(common -> cpu_info[j + 1]);
  333. core_1 = READ_CORE(common -> cpu_info[j]);
  334. core_2 = READ_CORE(common -> cpu_info[j + 1]);
  335. if (node_1 == node_2) {
  336. if (core_1 > core_2)
  337. swap = 1;
  338. } else {
  339. if ((node_2 == current_node) ||
  340. ((node_1 != current_node) && (node_1 > node_2)))
  341. swap = 1;
  342. }
  343. if (swap) {
  344. unsigned long temp;
  345. temp = common->cpu_info[j];
  346. common->cpu_info[j] = common->cpu_info[j + 1];
  347. common->cpu_info[j + 1] = temp;
  348. }
  349. }
  350. }
  351. } else {
  352. h = 1;
  353. while (h < count) h = 2 * h + 1;
  354. while (h > 1) {
  355. h /= 2;
  356. for (i = h; i < count; i++) {
  357. work = common -> cpu_info[i];
  358. bit = CPU_ISSET(i, &cpu_orig_mask[0]);
  359. j = i - h;
  360. while (work < common -> cpu_info[j]) {
  361. common -> cpu_info[j + h] = common -> cpu_info[j];
  362. if (CPU_ISSET(j, &cpu_orig_mask[0])) {
  363. CPU_SET(j + h, &cpu_orig_mask[0]);
  364. } else {
  365. CPU_CLR(j + h, &cpu_orig_mask[0]);
  366. }
  367. j -= h;
  368. if (j < 0) break;
  369. }
  370. common -> cpu_info[j + h] = work;
  371. if (bit) {
  372. CPU_SET(j + h, &cpu_orig_mask[0]);
  373. } else {
  374. CPU_CLR(j + h, &cpu_orig_mask[0]);
  375. }
  376. }
  377. }
  378. }
  379. #ifdef DEBUG
  380. fprintf(stderr, "\nSorting ...\n\n");
  381. for (cpu = 0; cpu < count; cpu++)
  382. fprintf(stderr, "CPUINFO (%2d) : %08lx (CPU=%3lu CORE=%3lu NODE=%3lu)\n", cpu, common -> cpu_info[cpu],
  383. READ_CPU(common -> cpu_info[cpu]),
  384. READ_CORE(common -> cpu_info[cpu]),
  385. READ_NODE(common -> cpu_info[cpu]));
  386. #endif
  387. }
  388. static void disable_hyperthread(void) {
  389. unsigned long share[MAX_BITMASK_LEN];
  390. int cpu;
  391. int bitmask_idx = 0;
  392. int i=0, count=0;
  393. bitmask_idx = CPUELT(common -> num_procs);
  394. for(i=0; i< bitmask_idx; i++){
  395. common -> avail[count++] = 0xFFFFFFFFFFFFFFFFUL;
  396. }
  397. if(CPUMASK(common -> num_procs) != 1){
  398. common -> avail[count++] = CPUMASK(common -> num_procs) - 1;
  399. }
  400. common -> avail_count = count;
  401. /* if(common->num_procs > 64){ */
  402. /* fprintf(stderr, "\nOpenBLAS Warning : The number of CPU/Cores(%d) is beyond the limit(64). Terminated.\n", common->num_procs); */
  403. /* exit(1); */
  404. /* }else if(common->num_procs == 64){ */
  405. /* common -> avail = 0xFFFFFFFFFFFFFFFFUL; */
  406. /* }else */
  407. /* common -> avail = (1UL << common -> num_procs) - 1; */
  408. #ifdef DEBUG
  409. fprintf(stderr, "\nAvail CPUs : ");
  410. for(i=0; i<count; i++)
  411. fprintf(stderr, "%04lx ", common -> avail[i]);
  412. fprintf(stderr, ".\n");
  413. #endif
  414. for (cpu = 0; cpu < common -> num_procs; cpu ++) {
  415. get_share(cpu, 1, share);
  416. //When the shared cpu are in different element of share & avail array, this may be a bug.
  417. for (i = 0; i < count ; i++){
  418. share[i] &= common->avail[i];
  419. if (popcount(share[i]) > 1) {
  420. #ifdef DEBUG
  421. fprintf(stderr, "Detected Hyper Threading on CPU %4x; disabled CPU %04lx.\n",
  422. cpu, share[i] & ~(CPUMASK(cpu)));
  423. #endif
  424. common -> avail[i] &= ~((share[i] & ~ CPUMASK(cpu)));
  425. }
  426. }
  427. }
  428. }
  429. static void disable_affinity(void) {
  430. int i=0;
  431. int bitmask_idx=0;
  432. int count=0;
  433. #ifdef DEBUG
  434. fprintf(stderr, "Final all available CPUs : %04lx.\n\n", common -> avail[0]);
  435. fprintf(stderr, "CPU mask : %04lx.\n\n", *(unsigned long *)&cpu_orig_mask[0]);
  436. #endif
  437. /* if(common->final_num_procs > 64){ */
  438. /* fprintf(stderr, "\nOpenBLAS Warining : The number of CPU/Cores(%d) is beyond the limit(64). Terminated.\n", common->final_num_procs); */
  439. /* exit(1); */
  440. /* }else if(common->final_num_procs == 64){ */
  441. /* lprocmask = 0xFFFFFFFFFFFFFFFFUL; */
  442. /* }else */
  443. /* lprocmask = (1UL << common -> final_num_procs) - 1; */
  444. bitmask_idx = CPUELT(common -> final_num_procs);
  445. for(i=0; i< bitmask_idx; i++){
  446. lprocmask[count++] = 0xFFFFFFFFFFFFFFFFUL;
  447. }
  448. if(CPUMASK(common -> final_num_procs) != 1){
  449. lprocmask[count++] = CPUMASK(common -> final_num_procs) - 1;
  450. }
  451. lprocmask_count = count;
  452. #ifndef USE_OPENMP
  453. for(i=0; i< count; i++){
  454. lprocmask[i] &= common->avail[i];
  455. }
  456. #endif
  457. #ifdef DEBUG
  458. fprintf(stderr, "I choose these CPUs : %04lx.\n\n", lprocmask[0]);
  459. #endif
  460. }
  461. static void setup_mempolicy(void) {
  462. int cpu, mynode, maxcpu;
  463. for (cpu = 0; cpu < MAX_NODES; cpu ++) node_cpu[cpu] = 0;
  464. maxcpu = 0;
  465. for (cpu = 0; cpu < numprocs; cpu ++) {
  466. mynode = READ_NODE(common -> cpu_info[cpu_sub_mapping[cpu]]);
  467. lnodemask |= (1UL << mynode);
  468. node_cpu[mynode] ++;
  469. if (maxcpu < node_cpu[mynode]) maxcpu = node_cpu[mynode];
  470. }
  471. node_equal = 1;
  472. for (cpu = 0; cpu < MAX_NODES; cpu ++) if ((node_cpu[cpu] != 0) && (node_cpu[cpu] != maxcpu)) node_equal = 0;
  473. if (lnodemask) {
  474. #ifdef DEBUG
  475. fprintf(stderr, "Node mask = %lx\n", lnodemask);
  476. #endif
  477. my_set_mempolicy(MPOL_INTERLEAVE, &lnodemask, sizeof(lnodemask) * 8);
  478. numnodes = popcount(lnodemask);
  479. }
  480. }
  481. static inline int is_dead(int id) {
  482. struct shmid_ds ds;
  483. return shmctl(id, IPC_STAT, &ds);
  484. }
  485. static void open_shmem(void) {
  486. int try = 0;
  487. do {
  488. #if defined(BIGNUMA)
  489. // raised to 32768, enough for 128 nodes and 1024 cups
  490. shmid = shmget(SH_MAGIC, 32768, 0666);
  491. #else
  492. shmid = shmget(SH_MAGIC, 4096, 0666);
  493. #endif
  494. if (shmid == -1) {
  495. #if defined(BIGNUMA)
  496. shmid = shmget(SH_MAGIC, 32768, IPC_CREAT | 0666);
  497. #else
  498. shmid = shmget(SH_MAGIC, 4096, IPC_CREAT | 0666);
  499. #endif
  500. }
  501. try ++;
  502. } while ((try < 10) && (shmid == -1));
  503. if (shmid == -1) {
  504. fprintf(stderr, "GotoBLAS : Can't open shared memory. Terminated.\n");
  505. exit(1);
  506. }
  507. if (shmid != -1) common = (shm_t *)shmat(shmid, NULL, 0);
  508. #ifdef DEBUG
  509. fprintf(stderr, "Shared Memory id = %x Address = %p\n", shmid, common);
  510. #endif
  511. }
  512. static void create_pshmem(void) {
  513. pshmid = shmget(IPC_PRIVATE, 4096, IPC_CREAT | 0666);
  514. paddr = shmat(pshmid, NULL, 0);
  515. shmctl(pshmid, IPC_RMID, 0);
  516. #ifdef DEBUG
  517. fprintf(stderr, "Private Shared Memory id = %x Address = %p\n", pshmid, paddr);
  518. #endif
  519. }
  520. static void local_cpu_map(void) {
  521. int cpu, id, mapping;
  522. int bitmask_idx = 0;
  523. cpu = 0;
  524. mapping = 0;
  525. do {
  526. id = common -> cpu_use[cpu];
  527. if (id > 0) {
  528. if (is_dead(id)) common -> cpu_use[cpu] = 0;
  529. }
  530. bitmask_idx = CPUELT(cpu);
  531. if ((common -> cpu_use[cpu] == 0) && (lprocmask[bitmask_idx] & CPUMASK(cpu))) {
  532. common -> cpu_use[cpu] = pshmid;
  533. cpu_mapping[mapping] = READ_CPU(common -> cpu_info[cpu]);
  534. cpu_sub_mapping[mapping] = cpu;
  535. mapping ++;
  536. }
  537. cpu ++;
  538. } while ((mapping < numprocs) && (cpu < common -> final_num_procs));
  539. disable_mapping = 0;
  540. if ((mapping < numprocs) || (numprocs == 1)) {
  541. for (cpu = 0; cpu < common -> final_num_procs; cpu ++) {
  542. if (common -> cpu_use[cpu] == pshmid) common -> cpu_use[cpu] = 0;
  543. }
  544. disable_mapping = 1;
  545. }
  546. #ifdef DEBUG
  547. for (cpu = 0; cpu < numprocs; cpu ++) {
  548. fprintf(stderr, "Local Mapping : %2d --> %2d (%2d)\n", cpu, cpu_mapping[cpu], cpu_sub_mapping[cpu]);
  549. }
  550. #endif
  551. }
  552. /* Public Functions */
  553. int get_num_procs(void) { return numprocs; }
  554. int get_num_nodes(void) { return numnodes; }
  555. int get_node_equal(void) {
  556. return (((blas_cpu_number % numnodes) == 0) && node_equal);
  557. }
  558. int gotoblas_set_affinity(int pos) {
  559. cpu_set_t cpu_mask;
  560. int mynode = 1;
  561. /* if number of threads is larger than inital condition */
  562. if (pos < 0) {
  563. sched_setaffinity(0, sizeof(cpu_orig_mask), &cpu_orig_mask[0]);
  564. return 0;
  565. }
  566. if (!disable_mapping) {
  567. mynode = READ_NODE(common -> cpu_info[cpu_sub_mapping[pos]]);
  568. #ifdef DEBUG
  569. fprintf(stderr, "Giving Affinity[%4d %3d] --> %3d My node = %3d\n", getpid(), pos, cpu_mapping[pos], mynode);
  570. #endif
  571. CPU_ZERO(&cpu_mask);
  572. CPU_SET (cpu_mapping[pos], &cpu_mask);
  573. sched_setaffinity(0, sizeof(cpu_mask), &cpu_mask);
  574. node_mapping[WhereAmI()] = mynode;
  575. }
  576. return mynode;
  577. }
  578. int get_node(void) {
  579. if (!disable_mapping) return node_mapping[WhereAmI()];
  580. return 1;
  581. }
  582. static int initialized = 0;
  583. void gotoblas_affinity_init(void) {
  584. int cpu, num_avail;
  585. #ifndef USE_OPENMP
  586. cpu_set_t cpu_mask;
  587. #endif
  588. int i;
  589. if (initialized) return;
  590. initialized = 1;
  591. sched_getaffinity(0, sizeof(cpu_orig_mask), &cpu_orig_mask[0]);
  592. #ifdef USE_OPENMP
  593. numprocs = 0;
  594. #else
  595. numprocs = readenv_atoi("OPENBLAS_NUM_THREADS");
  596. if (numprocs == 0) numprocs = readenv_atoi("GOTO_NUM_THREADS");
  597. #endif
  598. if (numprocs == 0) numprocs = readenv_atoi("OMP_NUM_THREADS");
  599. numnodes = 1;
  600. if (numprocs == 1) {
  601. disable_mapping = 1;
  602. return;
  603. }
  604. create_pshmem();
  605. open_shmem();
  606. while ((common -> lock) && (common -> magic != SH_MAGIC)) {
  607. if (is_dead(common -> shmid)) {
  608. common -> lock = 0;
  609. common -> shmid = 0;
  610. common -> magic = 0;
  611. } else {
  612. sched_yield();
  613. }
  614. }
  615. blas_lock(&common -> lock);
  616. if ((common -> shmid) && is_dead(common -> shmid)) common -> magic = 0;
  617. common -> shmid = pshmid;
  618. if (common -> magic != SH_MAGIC) {
  619. #ifdef DEBUG
  620. fprintf(stderr, "Shared Memory Initialization.\n");
  621. #endif
  622. //returns the number of processors which are currently online
  623. common -> num_procs = sysconf(_SC_NPROCESSORS_ONLN);;
  624. if(common -> num_procs > MAX_CPUS) {
  625. fprintf(stderr, "\nOpenBLAS Warining : The number of CPU/Cores(%d) is beyond the limit(%d). Terminated.\n", common->num_procs, MAX_CPUS);
  626. exit(1);
  627. }
  628. for (cpu = 0; cpu < common -> num_procs; cpu++) common -> cpu_info[cpu] = cpu;
  629. numa_check();
  630. disable_hyperthread();
  631. if (common -> num_nodes > 1) numa_mapping();
  632. common -> final_num_procs = 0;
  633. for(i = 0; i < common -> avail_count; i++) common -> final_num_procs += rcount(common -> avail[i]) + 1; //Make the max cpu number.
  634. for (cpu = 0; cpu < common -> final_num_procs; cpu ++) common -> cpu_use[cpu] = 0;
  635. common -> magic = SH_MAGIC;
  636. }
  637. disable_affinity();
  638. num_avail = 0;
  639. for(i=0; i<lprocmask_count; i++) num_avail += popcount(lprocmask[i]);
  640. if ((numprocs <= 0) || (numprocs > num_avail)) numprocs = num_avail;
  641. #ifdef DEBUG
  642. fprintf(stderr, "Number of threads = %d\n", numprocs);
  643. #endif
  644. local_cpu_map();
  645. blas_unlock(&common -> lock);
  646. #ifndef USE_OPENMP
  647. if (!disable_mapping) {
  648. #ifdef DEBUG
  649. fprintf(stderr, "Giving Affinity[%3d] --> %3d\n", 0, cpu_mapping[0]);
  650. #endif
  651. CPU_ZERO(&cpu_mask);
  652. CPU_SET (cpu_mapping[0], &cpu_mask);
  653. sched_setaffinity(0, sizeof(cpu_mask), &cpu_mask);
  654. node_mapping[WhereAmI()] = READ_NODE(common -> cpu_info[cpu_sub_mapping[0]]);
  655. setup_mempolicy();
  656. if (readenv_atoi("OPENBLAS_MAIN_FREE") || readenv_atoi("GOTOBLAS_MAIN_FREE")) {
  657. sched_setaffinity(0, sizeof(cpu_orig_mask), &cpu_orig_mask[0]);
  658. }
  659. }
  660. #endif
  661. #ifdef DEBUG
  662. fprintf(stderr, "Initialization is done.\n");
  663. #endif
  664. }
  665. void gotoblas_affinity_quit(void) {
  666. int i;
  667. struct shmid_ds ds;
  668. #ifdef DEBUG
  669. fprintf(stderr, "Terminating ..\n");
  670. #endif
  671. if ((numprocs == 1) || (initialized == 0)) return;
  672. if (!disable_mapping) {
  673. blas_lock(&common -> lock);
  674. for (i = 0; i < numprocs; i ++) common -> cpu_use[cpu_mapping[i]] = -1;
  675. blas_unlock(&common -> lock);
  676. }
  677. shmctl(shmid, IPC_STAT, &ds);
  678. if (ds.shm_nattch == 1) shmctl(shmid, IPC_RMID, 0);
  679. shmdt(common);
  680. shmdt(paddr);
  681. initialized = 0;
  682. }
  683. #else
  684. void gotoblas_affinity_init(void) {};
  685. void gotoblas_set_affinity(int threads) {};
  686. void gotoblas_set_affinity2(int threads) {};
  687. void gotoblas_affinity_reschedule(void) {};
  688. int get_num_procs(void) { return sysconf(_SC_NPROCESSORS_ONLN); }
  689. int get_num_nodes(void) { return 1; }
  690. int get_node(void) { return 1;}
  691. #endif