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

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