如何让程序运行在所有CPU核心上
先解釋幾個概念:多CPU,多核,超線程
多CPU:(一臺主機(jī)的)主板上有多個CPU。
多核:一個CPU有多個核心(下圖是6個物理核心)。
超線程:CPU有一個重要的參數(shù)是某個型號的CPU是否支持超線程。例如,某個CPU有4個物理核心,它支持超線程技術(shù),那么在邏輯上可以看作8個核心(也叫4核8線程)。比如我們在中關(guān)村在線上看到某款CPU的參數(shù)如下:
表明這款CPU是支持超線程技術(shù)的。
?
在Windows系統(tǒng)下如何設(shè)置一個進(jìn)程能在所有的(邏輯)核心上執(zhí)行呢?
方法一:在任務(wù)管理器中設(shè)置進(jìn)程的相關(guān)性。
方法二:在C#中Environment.ProcessorCount可以得到當(dāng)前CPU的所有邏輯核心。
1 static void SetProcessorAffinity() 2 { 3 pfcLogger logger = new pfcLogger(); 4 5 try 6 { 7 Process proc = Process.GetCurrentProcess(); 8 long affinitymask = (long)proc.ProcessorAffinity; 9 10 switch (Environment.ProcessorCount) 11 { 12 //Environment.ProcessorCount includes any hyperthreaded processors 13 14 case 2: affinitymask = 0x0003; break; // number of logical processors: 2 15 case 4: affinitymask = 0x000F; break; // number of logical processors: 4 16 case 8: affinitymask = 0x00FF; break; // number of logical processors: 8 17 case 12: affinitymask = 0x0FFF; break; // number of logical processors: 12 18 case 16: affinitymask = 0xFFFF; break; // number of logical processors: 16 19 default: break; // keep its default processorAffinity 20 } 21 proc.ProcessorAffinity = (IntPtr)affinitymask; 22 } 23 catch (System.ComponentModel.Win32Exception cwe) 24 { 25 MessageBox.Show("Fail to set processor affinity attribute. " + cwe.Message); 26 logger.Error(DateTime.Now, null, "PackOne.Server.Startup.Program", MethodBase.GetCurrentMethod().Name, cwe); 27 } 28 catch (System.NotSupportedException nse) 29 { 30 MessageBox.Show("Fail to set processor affinity attribute. " + nse.Message); 31 logger.Error(DateTime.Now, null, "PackOne.Server.Startup.Program", MethodBase.GetCurrentMethod().Name, nse); 32 } 33 catch (System.InvalidOperationException ioe) 34 { 35 MessageBox.Show("Fail to set processor affinity attribute. " + ioe.Message); 36 logger.Error(DateTime.Now, null, "PackOne.Server.Startup.Program", MethodBase.GetCurrentMethod().Name, ioe); 37 } 38 catch (Exception e) 39 { 40 MessageBox.Show("Fail to set processor affinity attribute. " + e.Message); 41 logger.Error(DateTime.Now, null, "PackOne.Server.Startup.Program", MethodBase.GetCurrentMethod().Name, e); 42 } 43 }說明:上面的代碼的關(guān)鍵部分是這樣:
Process proc = Process.GetCurrentProcess();long affinitymask = (long)proc.ProcessorAffinity;switch (Environment.ProcessorCount){//Environment.ProcessorCount includes any hyperthreaded processorscase 2: affinitymask = 0x0003; break; // number of logical processors: 2case 4: affinitymask = 0x000F; break; // number of logical processors: 4case 8: affinitymask = 0x00FF; break; // number of logical processors: 8case 12: affinitymask = 0x0FFF; break; // number of logical processors: 12case 16: affinitymask = 0xFFFF; break; // number of logical processors: 16default: break; // keep its default processorAffinity }proc.ProcessorAffinity = (IntPtr)affinitymask;其他代碼是用于寫日志和異常處理的。
?
另外,如何在windows上查看某個CPU的物理核心和邏輯核心呢?
使用命令:WMIC CPU Get DeviceID,NumberOfCores,NumberOfLogicalProcessors
?如果NumberOfCores =?NumberOfLogicalProcessors ,則表明該CPU不支持超線程。
?
轉(zhuǎn)載于:https://www.cnblogs.com/freecodeX/p/4333259.html
總結(jié)
以上是生活随笔為你收集整理的如何让程序运行在所有CPU核心上的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: jQuery获取Select选择的Tex
- 下一篇: 移动端设置点击屏幕闪烁的问题