1 #include <cstdio> 2 3 static inline int getPossibleCPUs() 4 { 5 FILE* cpuPossible = fopen("/sys/devices/system/cpu/possible", "r"); 6 if(!cpuPossible) 7 return 1; 8 9 char buf[2000]; //big enough for 1000 CPUs in worst possible configuration 10 char* pbuf = fgets(buf, sizeof(buf), cpuPossible); 11 fclose(cpuPossible); 12 if(!pbuf) 13 return 1; 14 15 //parse string of form "0-1,3,5-7,10,13-15" 16 int cpusAvailable = 0; 17 18 while(*pbuf) 19 { 20 const char* pos = pbuf; 21 bool range = false; 22 while(*pbuf && *pbuf != ',') 23 { 24 if(*pbuf == '-') range = true; 25 ++pbuf; 26 } 27 if(*pbuf) *pbuf++ = 0; 28 if(!range) 29 ++cpusAvailable; 30 else 31 { 32 int rstart = 0, rend = 0; 33 sscanf(pos, "%d-%d", &rstart, &rend); 34 cpusAvailable += rend - rstart + 1; 35 } 36 37 } 38 return cpusAvailable ? cpusAvailable : 1; 39 } 40 41 #define __TBB_HardwareConcurrency() getPossibleCPUs() 42