To establish processor proximity using C #

I created a windowed application in C #. Now I want to establish the proximity of the processor to this application. I can have 2 processors, 4 processors, 8 processors or there can be more than 8 processors.

I want to set the affinity of cpu using the input interface.

How can i achieve this? How can affinity be set using Environment.ProcessorCount?

+4
source share
4 answers

Try the following:

Process.GetCurrentProcess().ProcessorAffinity = (System.IntPtr)2; 

Here's more about that.

ProcessorAffinity represents each processor as a bit. Bit 0 represents processor one, bit 1 represents second processor, and so on. The following table shows a subset of the possible ProcessorAffinity for a four-processor system.

 Property value (in hexadecimal) Valid processors 0x0001 1 0x0002 2 0x0003 1 or 2 0x0004 3 0x0005 1 or 3 0x0007 1, 2, or 3 0x000F 1, 2, 3, or 4 

Here's a small sample program:

 //TODO: manage exceptions class Program { static void Main(string[] args) { Console.WriteLine("Total # of processors: {0}", Environment.ProcessorCount); Console.WriteLine("Current processor affinity: {0}", Process.GetCurrentProcess().ProcessorAffinity); Console.WriteLine("*********************************"); Console.WriteLine("Insert your selected processors, separated by comma (first CPU index is 1):"); var input = Console.ReadLine(); Console.WriteLine("*********************************"); var usedProcessors = input.Split(','); //TODO: validate input int newAffinity = 0; foreach (var item in usedProcessors) { newAffinity = newAffinity | int.Parse(item); Console.WriteLine("Processor #{0} was selected for affinity.", item); } Process.GetCurrentProcess().ProcessorAffinity = (System.IntPtr)newAffinity; Console.WriteLine("*********************************"); Console.WriteLine("Current processor affinity is {0}", Process.GetCurrentProcess().ProcessorAffinity); } } 
+10
source

The provided sample program Alex Filipovivi seems to be incorrect, since it ORs the number of processors in newAffinity without first converting them to a set bit. So, if you enter 3.4 into this program, you will get an affinity mask of 7, which are kernels 1, 2, and 3! The mask should be set to 12 (hexadecimal 0xC, binary 1100, which has bits 2 and 3 if bit 0 is the least significant bit).

Replacement

 newAffinity = NewAffinity | int.Parse(item); 

with

 newAffinity = newAffinity | (1 << int.Parse(item)-1); 

This is a smart way to do it.

+4
source

For people seeking attachment to the flow.

 public class CpuAffinity { [DllImport("kernel32.dll")] static extern IntPtr GetCurrentThread(); [DllImport("kernel32.dll")] static extern IntPtr SetThreadAffinityMask(IntPtr hThread, IntPtr dwThreadAffinityMask); /// <summary> /// Sets the current Thread to have affinity to the specified cpu/processor if the system has more than one. /// /// Supports most systems as we use a signed int; Anything more than 31 CPU will not be supported. /// </summary> /// <param name="cpu">The index of CPU to set.</param> public static void SetCurrentThreadToHaveCpuAffinityFor(int cpu) { if (cpu < 0) { throw new ArgumentOutOfRangeException("cpu"); } if (Environment.ProcessorCount > 1) { var ptr = GetCurrentThread(); SetThreadAffinityMask(ptr, new IntPtr(1 << cpu)); Debug.WriteLine("Current Thread Of OS Id '{0}' Affinity Set for CPU #{1}.", ptr, cpu); }else { Debug.WriteLine("The System only has one Processor. It is impossible to set CPU affinity for other CPU that do not exist."); } } } 
+2
source

System.Diagnostics.Process.ProcessorAffinity

What do you want to use Environment.ProcessorCount ? User input confirmation? In any case, if you want to select a specific processor (# 1 or # 2 or # 3 ...), create the same mask:

 if (userSelection <= 0 || userSelection > Environment.ProcessorCount) { throw new ArgumentOutOfRangeException(); } int bitMask = 1 << (userSelection - 1); Process.GetCurrentProcess().ProcessorAffinity = (IntPtr)bitMask; 

Where userSelection is the number of processors selected.

If you want to select more than one processor, do

 bitMask |= 1 << (anotherUserSelection - 1); 

for each user choice

0
source

All Articles