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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856
  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. #define MAX_NODES 16
  76. #define MAX_CPUS 256
  77. #define NCPUBITS (8*sizeof(unsigned long))
  78. #define MAX_BITMASK_LEN (MAX_CPUS/NCPUBITS)
  79. #define CPUELT(cpu) ((cpu) / NCPUBITS)
  80. #define CPUMASK(cpu) ((unsigned long) 1UL << ((cpu) % NCPUBITS))
  81. #define SH_MAGIC 0x510510
  82. #define CPUMAP_NAME "/sys/devices/system/node/node%d/cpumap"
  83. #define SHARE_NAME "/sys/devices/system/cpu/cpu%d/cache/index%d/shared_cpu_map"
  84. #define NODE_DIR "/sys/devices/system/node"
  85. //#undef DEBUG
  86. /* Private variables */
  87. typedef struct {
  88. unsigned long lock;
  89. unsigned int magic;
  90. unsigned int shmid;
  91. int num_nodes;
  92. int num_procs;
  93. int final_num_procs;
  94. unsigned long avail [MAX_BITMASK_LEN];
  95. int avail_count;
  96. unsigned long cpu_info [MAX_CPUS];
  97. unsigned long node_info [MAX_NODES][MAX_BITMASK_LEN];
  98. int cpu_use[MAX_CPUS];
  99. } shm_t;
  100. static cpu_set_t cpu_orig_mask[4];
  101. static int cpu_mapping[MAX_CPUS];
  102. static int node_mapping[MAX_CPUS * 4];
  103. static int cpu_sub_mapping[MAX_CPUS];
  104. static int disable_mapping;
  105. /* Number of cores per nodes */
  106. static int node_cpu[MAX_NODES];
  107. static int node_equal = 0;
  108. static shm_t *common = (void *)-1;
  109. static int shmid, pshmid;
  110. static void *paddr;
  111. static unsigned long lprocmask[MAX_BITMASK_LEN], lnodemask;
  112. static int lprocmask_count = 0;
  113. static int numprocs = 1;
  114. static int numnodes = 1;
  115. #if 1
  116. #define READ_CPU(x) ( (x) & 0xff)
  117. #define READ_NODE(x) (((x) >> 8) & 0xff)
  118. #define READ_CORE(x) (((x) >> 16) & 0xff)
  119. #define WRITE_CPU(x) (x)
  120. #define WRITE_NODE(x) ((x) << 8)
  121. #define WRITE_CORE(x) ((x) << 16)
  122. #else
  123. #define READ_CPU(x) ( (x) & 0xff)
  124. #define READ_CORE(x) (((x) >> 8) & 0xff)
  125. #define READ_NODE(x) (((x) >> 16) & 0xff)
  126. #define WRITE_CPU(x) (x)
  127. #define WRITE_CORE(x) ((x) << 8)
  128. #define WRITE_NODE(x) ((x) << 16)
  129. #endif
  130. static inline int popcount(unsigned long number) {
  131. int count = 0;
  132. while (number > 0) {
  133. if (number & 1) count ++;
  134. number >>= 1;
  135. }
  136. return count;
  137. }
  138. static inline int rcount(unsigned long number) {
  139. int count = -1;
  140. while ((number > 0) && ((number & 0)) == 0) {
  141. count ++;
  142. number >>= 1;
  143. }
  144. return count;
  145. }
  146. /***
  147. Known issue: The number of CPUs/cores should less
  148. than sizeof(unsigned long). On 64 bits, the limit
  149. is 64. On 32 bits, it is 32.
  150. ***/
  151. static inline void get_cpumap(int node, unsigned long * node_info) {
  152. int infile;
  153. unsigned long affinity[32];
  154. char name[160];
  155. char cpumap[160];
  156. char *dummy;
  157. int i=0;
  158. int count=0;
  159. int k=0;
  160. sprintf(name, CPUMAP_NAME, node);
  161. infile = open(name, O_RDONLY);
  162. for(i=0; i<32; i++){
  163. affinity[i] = 0;
  164. }
  165. if (infile != -1) {
  166. read(infile, cpumap, sizeof(cpumap));
  167. for(i=0; i<160; i++){
  168. if(cpumap[i] == '\n')
  169. break;
  170. if(cpumap[i] != ','){
  171. name[k++]=cpumap[i];
  172. //Enough data for Hex
  173. if(k >= NCPUBITS/4){
  174. affinity[count++] = strtoul(name, &dummy, 16);
  175. k=0;
  176. }
  177. }
  178. }
  179. if(k!=0){
  180. name[k]='\0';
  181. affinity[count++] = strtoul(name, &dummy, 16);
  182. k=0;
  183. }
  184. // 0-63bit -> node_info[0], 64-128bit -> node_info[1] ....
  185. // revert the sequence
  186. for(i=0; i<count && i<MAX_BITMASK_LEN; i++){
  187. node_info[i]=affinity[count-i-1];
  188. }
  189. close(infile);
  190. }
  191. return ;
  192. }
  193. static inline void get_share(int cpu, int level, unsigned long * share) {
  194. int infile;
  195. unsigned long affinity[32];
  196. char cpumap[160];
  197. char name[160];
  198. char *dummy;
  199. int count=0;
  200. int i=0,k=0;
  201. int bitmask_idx = 0;
  202. sprintf(name, SHARE_NAME, cpu, level);
  203. infile = open(name, O_RDONLY);
  204. // Init share
  205. for(i=0; i<MAX_BITMASK_LEN; i++){
  206. share[i]=0;
  207. }
  208. bitmask_idx = CPUELT(cpu);
  209. share[bitmask_idx] = CPUMASK(cpu);
  210. if (infile != -1) {
  211. read(infile, cpumap, sizeof(cpumap));
  212. for(i=0; i<160; i++){
  213. if(cpumap[i] == '\n')
  214. break;
  215. if(cpumap[i] != ','){
  216. name[k++]=cpumap[i];
  217. //Enough data
  218. if(k >= NCPUBITS/4){
  219. affinity[count++] = strtoul(name, &dummy, 16);
  220. k=0;
  221. }
  222. }
  223. }
  224. if(k!=0){
  225. name[k]='\0';
  226. affinity[count++] = strtoul(name, &dummy, 16);
  227. k=0;
  228. }
  229. // 0-63bit -> node_info[0], 64-128bit -> node_info[1] ....
  230. // revert the sequence
  231. for(i=0; i<count && i<MAX_BITMASK_LEN; i++){
  232. share[i]=affinity[count-i-1];
  233. }
  234. close(infile);
  235. }
  236. return ;
  237. }
  238. static int numa_check(void) {
  239. DIR *dp;
  240. struct dirent *dir;
  241. int node;
  242. int j;
  243. common -> num_nodes = 0;
  244. dp = opendir(NODE_DIR);
  245. if (dp == NULL) {
  246. common -> num_nodes = 1;
  247. return 0;
  248. }
  249. for (node = 0; node < MAX_NODES; node ++) {
  250. for (j = 0; j<MAX_BITMASK_LEN; j++) common -> node_info[node][j] = 0;
  251. }
  252. while ((dir = readdir(dp)) != NULL) {
  253. if (*(unsigned int *) dir -> d_name == 0x065646f6eU) {
  254. node = atoi(&dir -> d_name[4]);
  255. if (node > MAX_NODES) {
  256. fprintf(stderr, "\nOpenBLAS Warning : MAX_NODES (NUMA) is too small. Terminated.\n");
  257. exit(1);
  258. }
  259. common -> num_nodes ++;
  260. get_cpumap(node, common->node_info[node]);
  261. }
  262. }
  263. closedir(dp);
  264. if (common -> num_nodes == 1) return 1;
  265. #ifdef DEBUG
  266. fprintf(stderr, "Numa found : number of Nodes = %2d\n", common -> num_nodes);
  267. for (node = 0; node < common -> num_nodes; node ++)
  268. fprintf(stderr, "MASK (%2d) : %08lx\n", node, common -> node_info[node][0]);
  269. #endif
  270. return common -> num_nodes;
  271. }
  272. static void numa_mapping(void) {
  273. int node, cpu, core;
  274. int i, j, h;
  275. unsigned long work, bit;
  276. int count = 0;
  277. int bitmask_idx = 0;
  278. for (node = 0; node < common -> num_nodes; node ++) {
  279. core = 0;
  280. for (cpu = 0; cpu < common -> num_procs; cpu ++) {
  281. bitmask_idx = CPUELT(cpu);
  282. if (common -> node_info[node][bitmask_idx] & common -> avail[bitmask_idx] & CPUMASK(cpu)) {
  283. common -> cpu_info[count] = WRITE_CORE(core) | WRITE_NODE(node) | WRITE_CPU(cpu);
  284. count ++;
  285. core ++;
  286. }
  287. }
  288. }
  289. #ifdef DEBUG
  290. fprintf(stderr, "\nFrom /sys ...\n\n");
  291. for (cpu = 0; cpu < count; cpu++)
  292. fprintf(stderr, "CPU (%2d) : %08lx\n", cpu, common -> cpu_info[cpu]);
  293. #endif
  294. h = 1;
  295. while (h < count) h = 2 * h + 1;
  296. while (h > 1) {
  297. h /= 2;
  298. for (i = h; i < count; i++) {
  299. work = common -> cpu_info[i];
  300. bit = CPU_ISSET(i, &cpu_orig_mask[0]);
  301. j = i - h;
  302. while (work < common -> cpu_info[j]) {
  303. common -> cpu_info[j + h] = common -> cpu_info[j];
  304. if (CPU_ISSET(j, &cpu_orig_mask[0])) {
  305. CPU_SET(j + h, &cpu_orig_mask[0]);
  306. } else {
  307. CPU_CLR(j + h, &cpu_orig_mask[0]);
  308. }
  309. j -= h;
  310. if (j < 0) break;
  311. }
  312. common -> cpu_info[j + h] = work;
  313. if (bit) {
  314. CPU_SET(j + h, &cpu_orig_mask[0]);
  315. } else {
  316. CPU_CLR(j + h, &cpu_orig_mask[0]);
  317. }
  318. }
  319. }
  320. #ifdef DEBUG
  321. fprintf(stderr, "\nSorting ...\n\n");
  322. for (cpu = 0; cpu < count; cpu++)
  323. fprintf(stderr, "CPU (%2d) : %08lx\n", cpu, common -> cpu_info[cpu]);
  324. #endif
  325. }
  326. static void disable_hyperthread(void) {
  327. unsigned long share[MAX_BITMASK_LEN];
  328. int cpu;
  329. int bitmask_idx = 0;
  330. int i=0, count=0;
  331. bitmask_idx = CPUELT(common -> num_procs);
  332. for(i=0; i< bitmask_idx; i++){
  333. common -> avail[count++] = 0xFFFFFFFFFFFFFFFFUL;
  334. }
  335. if(CPUMASK(common -> num_procs) != 1){
  336. common -> avail[count++] = CPUMASK(common -> num_procs) - 1;
  337. }
  338. common -> avail_count = count;
  339. /* if(common->num_procs > 64){ */
  340. /* fprintf(stderr, "\nOpenBLAS Warning : The number of CPU/Cores(%d) is beyond the limit(64). Terminated.\n", common->num_procs); */
  341. /* exit(1); */
  342. /* }else if(common->num_procs == 64){ */
  343. /* common -> avail = 0xFFFFFFFFFFFFFFFFUL; */
  344. /* }else */
  345. /* common -> avail = (1UL << common -> num_procs) - 1; */
  346. #ifdef DEBUG
  347. fprintf(stderr, "\nAvail CPUs : ");
  348. for(i=0; i<count; i++)
  349. fprintf(stderr, "%04lx ", common -> avail[i]);
  350. fprintf(stderr, ".\n");
  351. #endif
  352. for (cpu = 0; cpu < common -> num_procs; cpu ++) {
  353. get_share(cpu, 1, share);
  354. //When the shared cpu are in different element of share & avail array, this may be a bug.
  355. for (i = 0; i < count ; i++){
  356. share[i] &= common->avail[i];
  357. if (popcount(share[i]) > 1) {
  358. #ifdef DEBUG
  359. fprintf(stderr, "Detected Hyper Threading on CPU %4x; disabled CPU %04lx.\n",
  360. cpu, share[i] & ~(CPUMASK(cpu)));
  361. #endif
  362. common -> avail[i] &= ~((share[i] & ~ CPUMASK(cpu)));
  363. }
  364. }
  365. }
  366. }
  367. static void disable_affinity(void) {
  368. int i=0;
  369. int bitmask_idx=0;
  370. int count=0;
  371. #ifdef DEBUG
  372. fprintf(stderr, "Final all available CPUs : %04lx.\n\n", common -> avail[0]);
  373. fprintf(stderr, "CPU mask : %04lx.\n\n", *(unsigned long *)&cpu_orig_mask[0]);
  374. #endif
  375. /* if(common->final_num_procs > 64){ */
  376. /* fprintf(stderr, "\nOpenBLAS Warining : The number of CPU/Cores(%d) is beyond the limit(64). Terminated.\n", common->final_num_procs); */
  377. /* exit(1); */
  378. /* }else if(common->final_num_procs == 64){ */
  379. /* lprocmask = 0xFFFFFFFFFFFFFFFFUL; */
  380. /* }else */
  381. /* lprocmask = (1UL << common -> final_num_procs) - 1; */
  382. bitmask_idx = CPUELT(common -> final_num_procs);
  383. for(i=0; i< bitmask_idx; i++){
  384. lprocmask[count++] = 0xFFFFFFFFFFFFFFFFUL;
  385. }
  386. if(CPUMASK(common -> final_num_procs) != 1){
  387. lprocmask[count++] = CPUMASK(common -> final_num_procs) - 1;
  388. }
  389. lprocmask_count = count;
  390. #ifndef USE_OPENMP
  391. for(i=0; i< count; i++){
  392. lprocmask[i] &= ((unsigned long *)&cpu_orig_mask[0])[i];
  393. }
  394. #endif
  395. #ifdef DEBUG
  396. fprintf(stderr, "I choose these CPUs : %04lx.\n\n", lprocmask[0]);
  397. #endif
  398. }
  399. static void setup_mempolicy(void) {
  400. int cpu, mynode, maxcpu;
  401. for (cpu = 0; cpu < MAX_NODES; cpu ++) node_cpu[cpu] = 0;
  402. maxcpu = 0;
  403. for (cpu = 0; cpu < numprocs; cpu ++) {
  404. mynode = READ_NODE(common -> cpu_info[cpu_sub_mapping[cpu]]);
  405. lnodemask |= (1UL << mynode);
  406. node_cpu[mynode] ++;
  407. if (maxcpu < node_cpu[mynode]) maxcpu = node_cpu[mynode];
  408. }
  409. node_equal = 1;
  410. for (cpu = 0; cpu < MAX_NODES; cpu ++) if ((node_cpu[cpu] != 0) && (node_cpu[cpu] != maxcpu)) node_equal = 0;
  411. if (lnodemask) {
  412. #ifdef DEBUG
  413. fprintf(stderr, "Node mask = %lx\n", lnodemask);
  414. #endif
  415. my_set_mempolicy(MPOL_INTERLEAVE, &lnodemask, sizeof(lnodemask) * 8);
  416. numnodes = popcount(lnodemask);
  417. }
  418. }
  419. static inline int is_dead(int id) {
  420. struct shmid_ds ds;
  421. return shmctl(id, IPC_STAT, &ds);
  422. }
  423. static void open_shmem(void) {
  424. int try = 0;
  425. do {
  426. shmid = shmget(SH_MAGIC, 4096, 0666);
  427. if (shmid == -1) {
  428. shmid = shmget(SH_MAGIC, 4096, IPC_CREAT | 0666);
  429. }
  430. try ++;
  431. } while ((try < 10) && (shmid == -1));
  432. if (shmid == -1) {
  433. fprintf(stderr, "GotoBLAS : Can't open shared memory. Terminated.\n");
  434. exit(1);
  435. }
  436. if (shmid != -1) common = (shm_t *)shmat(shmid, NULL, 0);
  437. #ifdef DEBUG
  438. fprintf(stderr, "Shared Memory id = %x Address = %p\n", shmid, common);
  439. #endif
  440. }
  441. static void create_pshmem(void) {
  442. pshmid = shmget(IPC_PRIVATE, 4096, IPC_CREAT | 0666);
  443. paddr = shmat(pshmid, NULL, 0);
  444. shmctl(pshmid, IPC_RMID, 0);
  445. #ifdef DEBUG
  446. fprintf(stderr, "Private Shared Memory id = %x Address = %p\n", pshmid, paddr);
  447. #endif
  448. }
  449. static void local_cpu_map(void) {
  450. int cpu, id, mapping;
  451. int bitmask_idx = 0;
  452. cpu = 0;
  453. mapping = 0;
  454. do {
  455. id = common -> cpu_use[cpu];
  456. if (id > 0) {
  457. if (is_dead(id)) common -> cpu_use[cpu] = 0;
  458. }
  459. bitmask_idx = CPUELT(cpu);
  460. if ((common -> cpu_use[cpu] == 0) && (lprocmask[bitmask_idx] & CPUMASK(cpu))) {
  461. common -> cpu_use[cpu] = pshmid;
  462. cpu_mapping[mapping] = READ_CPU(common -> cpu_info[cpu]);
  463. cpu_sub_mapping[mapping] = cpu;
  464. mapping ++;
  465. }
  466. cpu ++;
  467. } while ((mapping < numprocs) && (cpu < common -> final_num_procs));
  468. disable_mapping = 0;
  469. if ((mapping < numprocs) || (numprocs == 1)) {
  470. for (cpu = 0; cpu < common -> final_num_procs; cpu ++) {
  471. if (common -> cpu_use[cpu] == pshmid) common -> cpu_use[cpu] = 0;
  472. }
  473. disable_mapping = 1;
  474. }
  475. #ifdef DEBUG
  476. for (cpu = 0; cpu < numprocs; cpu ++) {
  477. fprintf(stderr, "Local Mapping : %2d --> %2d (%2d)\n", cpu, cpu_mapping[cpu], cpu_sub_mapping[cpu]);
  478. }
  479. #endif
  480. }
  481. /* Public Functions */
  482. int get_num_procs(void) { return numprocs; }
  483. int get_num_nodes(void) { return numnodes; }
  484. int get_node_equal(void) {
  485. return (((blas_cpu_number % numnodes) == 0) && node_equal);
  486. }
  487. int gotoblas_set_affinity(int pos) {
  488. cpu_set_t cpu_mask;
  489. int mynode = 1;
  490. /* if number of threads is larger than inital condition */
  491. if (pos < 0) {
  492. sched_setaffinity(0, sizeof(cpu_orig_mask), &cpu_orig_mask[0]);
  493. return 0;
  494. }
  495. if (!disable_mapping) {
  496. mynode = READ_NODE(common -> cpu_info[cpu_sub_mapping[pos]]);
  497. #ifdef DEBUG
  498. fprintf(stderr, "Giving Affinity[%4d %3d] --> %3d My node = %3d\n", getpid(), pos, cpu_mapping[pos], mynode);
  499. #endif
  500. CPU_ZERO(&cpu_mask);
  501. CPU_SET (cpu_mapping[pos], &cpu_mask);
  502. sched_setaffinity(0, sizeof(cpu_mask), &cpu_mask);
  503. node_mapping[WhereAmI()] = mynode;
  504. }
  505. return mynode;
  506. }
  507. int get_node(void) {
  508. if (!disable_mapping) return node_mapping[WhereAmI()];
  509. return 1;
  510. }
  511. static int initialized = 0;
  512. void gotoblas_affinity_init(void) {
  513. int cpu, num_avail;
  514. #ifndef USE_OPENMP
  515. cpu_set_t cpu_mask;
  516. #endif
  517. int i;
  518. if (initialized) return;
  519. initialized = 1;
  520. sched_getaffinity(0, sizeof(cpu_orig_mask), &cpu_orig_mask[0]);
  521. #ifdef USE_OPENMP
  522. numprocs = 0;
  523. #else
  524. numprocs = readenv("OPENBLAS_NUM_THREADS");
  525. if (numprocs == 0) numprocs = readenv("GOTO_NUM_THREADS");
  526. #endif
  527. if (numprocs == 0) numprocs = readenv("OMP_NUM_THREADS");
  528. numnodes = 1;
  529. if (numprocs == 1) {
  530. disable_mapping = 1;
  531. return;
  532. }
  533. create_pshmem();
  534. open_shmem();
  535. while ((common -> lock) && (common -> magic != SH_MAGIC)) {
  536. if (is_dead(common -> shmid)) {
  537. common -> lock = 0;
  538. common -> shmid = 0;
  539. common -> magic = 0;
  540. } else {
  541. sched_yield();
  542. }
  543. }
  544. blas_lock(&common -> lock);
  545. if ((common -> shmid) && is_dead(common -> shmid)) common -> magic = 0;
  546. common -> shmid = pshmid;
  547. if (common -> magic != SH_MAGIC) {
  548. #ifdef DEBUG
  549. fprintf(stderr, "Shared Memory Initialization.\n");
  550. #endif
  551. //returns the number of processors which are currently online
  552. common -> num_procs = sysconf(_SC_NPROCESSORS_ONLN);;
  553. if(common -> num_procs > MAX_CPUS) {
  554. fprintf(stderr, "\nOpenBLAS Warining : The number of CPU/Cores(%d) is beyond the limit(%d). Terminated.\n", common->num_procs, MAX_CPUS);
  555. exit(1);
  556. }
  557. for (cpu = 0; cpu < common -> num_procs; cpu++) common -> cpu_info[cpu] = cpu;
  558. numa_check();
  559. disable_hyperthread();
  560. if (common -> num_nodes > 1) numa_mapping();
  561. common -> final_num_procs = 0;
  562. for(i = 0; i < common -> avail_count; i++) common -> final_num_procs += popcount(common -> avail[i]);
  563. for (cpu = 0; cpu < common -> final_num_procs; cpu ++) common -> cpu_use[cpu] = 0;
  564. common -> magic = SH_MAGIC;
  565. }
  566. disable_affinity();
  567. num_avail = 0;
  568. for(i=0; i<lprocmask_count; i++) num_avail += popcount(lprocmask[i]);
  569. if ((numprocs <= 0) || (numprocs > num_avail)) numprocs = num_avail;
  570. #ifdef DEBUG
  571. fprintf(stderr, "Number of threads = %d\n", numprocs);
  572. #endif
  573. local_cpu_map();
  574. blas_unlock(&common -> lock);
  575. #ifndef USE_OPENMP
  576. if (!disable_mapping) {
  577. #ifdef DEBUG
  578. fprintf(stderr, "Giving Affinity[%3d] --> %3d\n", 0, cpu_mapping[0]);
  579. #endif
  580. CPU_ZERO(&cpu_mask);
  581. CPU_SET (cpu_mapping[0], &cpu_mask);
  582. sched_setaffinity(0, sizeof(cpu_mask), &cpu_mask);
  583. node_mapping[WhereAmI()] = READ_NODE(common -> cpu_info[cpu_sub_mapping[0]]);
  584. setup_mempolicy();
  585. if (readenv("OPENBLAS_MAIN_FREE") || readenv("GOTOBLAS_MAIN_FREE")) {
  586. sched_setaffinity(0, sizeof(cpu_orig_mask), &cpu_orig_mask[0]);
  587. }
  588. }
  589. #endif
  590. #ifdef DEBUG
  591. fprintf(stderr, "Initialization is done.\n");
  592. #endif
  593. }
  594. void gotoblas_affinity_quit(void) {
  595. int i;
  596. struct shmid_ds ds;
  597. #ifdef DEBUG
  598. fprintf(stderr, "Terminating ..\n");
  599. #endif
  600. if ((numprocs == 1) || (initialized == 0)) return;
  601. if (!disable_mapping) {
  602. blas_lock(&common -> lock);
  603. for (i = 0; i < numprocs; i ++) common -> cpu_use[cpu_mapping[i]] = -1;
  604. blas_unlock(&common -> lock);
  605. }
  606. shmctl(shmid, IPC_STAT, &ds);
  607. if (ds.shm_nattch == 1) shmctl(shmid, IPC_RMID, 0);
  608. shmdt(common);
  609. shmdt(paddr);
  610. initialized = 0;
  611. }
  612. #else
  613. void gotoblas_affinity_init(void) {};
  614. void gotoblas_set_affinity(int threads) {};
  615. void gotoblas_set_affinity2(int threads) {};
  616. void gotoblas_affinity_reschedule(void) {};
  617. int get_num_procs(void) { return get_nprocs(); }
  618. int get_num_nodes(void) { return 1; }
  619. int get_node(void) { return 1;}
  620. #endif