How to set the mask for 64 core 128 thread CPU( about Process.ProcessorAffinity )

Ma Linru (马 林茹) 1 Reputation point
2020-07-10T09:28:39.937+00:00

Source

https://learn.microsoft.com/zh-cn/dotnet/api/system.diagnostics.process.processoraffinity?view=netframework-4.7.2#System_Diagnostics_Process_ProcessorAffinity

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];  

    }  
Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,691 questions
{count} votes