.NET ProcessorAffinity calculation
本文关键字:calculation ProcessorAffinity NET | 更新日期: 2023-09-27 18:17:20
我找不到任何关于如何在c#的ProcessorAffinity上正确计算affinity设置的文档。
我正在传递选定的核心列表并添加它们以获得我的最终亲和力设置,但似乎我的计算是关闭的。
如何根据所选核心(0-32/64)的列表正确计算亲和度
如何优化?
if (!string.IsNullOrEmpty(affinitystr) && !affinitystr.Equals("-1"))
{
string[] words = affinitystr.Split(',');
int affinitytotal = 0;
foreach (string word in words)
{
int affinity = Convert.ToInt32(word);
if (affinity == 0) { affinity = 1; } // core 0
else if (affinity == 1) { affinity = 2; } // core 1
else if (affinity == 2) { affinity = 4; } // core 2
else if (affinity == 3) { affinity = 8; } // core 3
else if (affinity == 4) { affinity = 10; } // core 4
else if (affinity == 5) { affinity = 20; } // core 5
else if (affinity == 6) { affinity = 40; } // core 6
else if (affinity == 7) { affinity = 80; } // core 7
else if (affinity == 8) { affinity = 100; } // core 8
else if (affinity == 9) { affinity = 200; } // core 9
else if (affinity == 10) { affinity = 400; } // core 10
else if (affinity == 11) { affinity = 800; } // core 11
else if (affinity == 12) { affinity = 1000; } // core 12
else if (affinity == 13) { affinity = 2000; } // core 13
else if (affinity == 14) { affinity = 4000; } // core 14
else if (affinity == 15) { affinity = 8000; } // core 15
else if (affinity == 16) { affinity = 10000; } // core 16
else if (affinity == 17) { affinity = 20000; } // core 17
else if (affinity == 18) { affinity = 40000; } // core 18
else if (affinity == 19) { affinity = 80000; } // core 19
else if (affinity == 20) { affinity = 100000; } // core 20
else if (affinity == 21) { affinity = 200000; } // core 21
else if (affinity == 22) { affinity = 400000; } // core 22
else if (affinity == 23) { affinity = 800000; } // core 23
else if (affinity == 24) { affinity = 1000000; } // core 24
else if (affinity == 25) { affinity = 2000000; } // core 25
else if (affinity == 26) { affinity = 4000000; } // core 26
else if (affinity == 27) { affinity = 8000000; } // core 27
else if (affinity == 28) { affinity = 10000000; } // core 28
else if (affinity == 29) { affinity = 20000000; } // core 29
else if (affinity == 30) { affinity = 40000000; } // core 30
else if (affinity == 31) { affinity = 80000000; } // core 31
else if (affinity == 32) { affinity = 100000000; } // core 32
affinitytotal = affinitytotal + affinity;
}
scanProcess.ProcessorAffinity = (System.IntPtr)affinitytotal;
}
要获得处理器n
(从零开始的id)的亲和掩码,您只需将值1
向右移动n
个位置。
string affinitystr = "3,8,13";
long affinityMask = affinitystr.Split(',').Select(int.Parse).Sum(i => 1L << i);
使用ulong
来正确表示最多64个内核的替代公式:
string affinitystr = "3,8,13";
ulong affinityMask = affinitystr.Split(',')
.Select(int.Parse)
.Aggregate(0UL, (mask, id) => mask | (1UL << id));