How to choose a group when setting affinity to a process for multi-processor system

28 ビュー (過去 30 日間)
I have a windows machine with 2 processors 64 cores each. Before with one processor I was able to use the code below. Now I have two processor groups and didn't find any info how to manipulate it.
For example, Process_1 - group 0 CPU 23-63 or Process_2 - group 1 CPU 1-63. Also, Is it possible to set both groups to one process?
Process_1 = System.Diagnostics.Process.GetProcessById(my_process_ID);
aff = Process_1.ProcessorAffinity.ToInt32; % get current affinity mask
fprintf('Current affinity mask: %s\n', dec2bin(aff, 36));
proc.ProcessorAffinity = System.IntPtr(int32(2)); % set affinity mask
fprintf('Adjusted affinity to: %s\n', dec2bin(proc.ProcessorAffinity.ToInt32, 36));

採用された回答

Avni Agrawal
Avni Agrawal 2024 年 6 月 26 日
In Windows, when you have more than 64 logical processors, the system groups them into processor groups. Each group can have a maximum of 64 processors. Unfortunately, the .NET `System.Diagnostics.Process` class does not directly support setting processor affinity across multiple processor groups.
However, you can use P/Invoke to call Windows API functions to set processor affinity across different processor groups. Here's an example of how you can do this in C#:
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
class Program
{
[StructLayout(LayoutKind.Sequential)]
struct GROUP_AFFINITY
{
public UIntPtr Mask;
public ushort Group;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
public ushort[] Reserved;
}
[DllImport("kernel32.dll", SetLastError = true)]
static extern bool SetThreadGroupAffinity(IntPtr hThread, ref GROUP_AFFINITY GroupAffinity, IntPtr PreviousGroupAffinity);
[DllImport("kernel32.dll", SetLastError = true)]
static extern IntPtr OpenThread(uint dwDesiredAccess, bool bInheritHandle, uint dwThreadId);
[DllImport("kernel32.dll", SetLastError = true)]
static extern bool CloseHandle(IntPtr hObject);
const uint THREAD_SET_INFORMATION = 0x0020;
static void Main(string[] args)
{
int myProcessId = 1234; // Replace with your process ID
Process process = Process.GetProcessById(myProcessId);
foreach (ProcessThread thread in process.Threads)
{
IntPtr hThread = OpenThread(THREAD_SET_INFORMATION, false, (uint)thread.Id);
if (hThread == IntPtr.Zero)
{
Console.WriteLine($"Failed to open thread {thread.Id}");
continue;
}
GROUP_AFFINITY groupAffinity = new GROUP_AFFINITY
{
Mask = (UIntPtr)0xFFFFFFFF, // Set the mask to use all CPUs in the group, adjust as needed
Group = 0, // Set to the desired processor group
Reserved = new ushort[3]
};
if (!SetThreadGroupAffinity(hThread, ref groupAffinity, IntPtr.Zero))
{
Console.WriteLine($"Failed to set thread group affinity for thread {thread.Id}");
}
CloseHandle(hThread);
}
}
}
In this example, you need to replace `myProcessId` with your actual process ID. This code sets the affinity for each thread in the process to use all CPUs in group 0. You can adjust the `Mask` and `Group` fields of the `GROUP_AFFINITY` structure as needed.
Note that this example only sets the affinity for one processor group. Setting affinity across multiple groups for a single process is more complex and might not be supported directly. You would need to ensure that threads are distributed across groups as needed.
I hope this helps.

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeApplication Deployment についてさらに検索

製品


リリース

R2024a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by