How to set the mask for 64 core 128 thread CPU( about Process.ProcessorAffinity )
Ma Linru (马 林茹)
1
Reputation point
Source
Could you help me ?How to set the mask for 64 core 128 thread CPU( about Process.ProcessorAffinity )
The following code can only solve the problem of 64 processors. Now I have 128 processors problem。I don't know how to write code.
private void SetProcessorAffinity()
{
int count = Math.Min(Environment.ProcessorCount, userSetting.MaxProcessorCount);
long affinity = 1;
for (int index = 1; index < count; index++)
{
affinity = (affinity << 1) + 1;
}
logger.Debug($"CPU Processor={count},Mask={affinity}");
Process.GetCurrentProcess().ProcessorAffinity = (IntPtr)affinity;
}
Consider defining 128bit, but I do not know if the following code is correct.
private void SetProcessorAffinity()
{
int count = Math.Min(Environment.ProcessorCount, userSetting.MaxProcessorCount);
long[] affinity = { 0L, 0L, 0L, 1L };
int loop_counter_max = count / 64;
for(int loop_counter = 0; loop_counter < loop_counter_max; loop_counter++)
{
affinity[loop_counter] = 0xFFFF;
}
int remainder = count % 64;
for (int index = 0; index < remainder; index++)
{
affinity[loop_counter_max + 1] = affinity[loop_counter_max + 1]<<1 + 1;
}
logger.Debug($"CPU Processor={count},Mask={affinity}");
Process.GetCurrentProcess().ProcessorAffinity = (IntPtr)affinity[0];
}
Sign in to answer