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

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