考慮超執行緒確定實體處理器和核心數量
偵測實體處理器和核心的數量對於最佳化多執行緒至關重要應用程式高效運作。要準確地確定這些計數,請考慮可能啟用的超線程依賴項。
逐步流程:
區分物理核心計數:
<code class="c++">#include <iostream> #include <string> void cpuID(unsigned i, unsigned regs[4]); int main() { unsigned regs[4]; char vendor[12]; // Get vendor cpuID(0, regs); ((unsigned *)vendor)[0] = regs[1]; ((unsigned *)vendor)[1] = regs[3]; ((unsigned *)vendor)[2] = regs[2]; string cpuVendor = string(vendor, 12); // Get CPU features cpuID(1, regs); unsigned cpuFeatures = regs[3]; // Logical core count per CPU cpuID(1, regs); unsigned logical = (regs[1] >> 16) & 0xff; cout << " logical cpus: " << logical << endl; unsigned cores = logical; if (cpuVendor == "GenuineIntel") { // Get DCP cache info cpuID(4, regs); cores = ((regs[0] >> 26) & 0x3f) + 1; } else if (cpuVendor == "AuthenticAMD") { // Get NC: Number of CPU cores - 1 cpuID(0x80000008, regs); cores = ((unsigned)(regs[2] & 0xff)) + 1; } cout << " cpu cores: " << cores << endl; // Detect hyper-threads bool hyperThreads = cpuFeatures & (1 << 28) && cores < logical; cout << "hyper-threads: " << (hyperThreads ? "true" : "false") << endl; return 0; }</code>
以上是如何在考慮超線程配置的情況下準確地確定實體處理器和核心的數量?的詳細內容。更多資訊請關注PHP中文網其他相關文章!