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

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