gpt4 book ai didi

c# - Windows 服务器/数据中心 : set CPU affinity with > 64 cores

转载 作者:可可西里 更新时间:2023-11-01 14:13:52 25 4
gpt4 key购买 nike

SetThreadAffinityMask() 允许为 64 个逻辑核心(处理器)设置关联掩码。但是,Windows 数据中心最多可以有 64 个 CPU,每个 CPU 都有很多内核(请参阅 here )。

>64核怎么设置线程?

附言。我在 C# 中编码,因此 .Net 答案是理想的,但 C 中的 API 也很好。

最佳答案

我使用以下代码设置处理器组和 CPU 的亲和性:

[StructLayout(LayoutKind.Sequential, Pack = 4)]
private struct _GROUP_AFFINITY
{
public UIntPtr Mask;
[MarshalAs(UnmanagedType.U2)]
public ushort Group;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 3, ArraySubType = UnmanagedType.U2)]
public ushort[] Reserved;
}

[DllImport("kernel32", SetLastError = true)]
private static extern Boolean SetThreadGroupAffinity(
IntPtr hThread,
ref _GROUP_AFFINITY GroupAffinity,
ref _GROUP_AFFINITY PreviousGroupAffinity);

[DllImport("kernel32", SetLastError = true)]
private static extern IntPtr GetCurrentThread();

/// <summary>
/// Sets the processor group and the processor cpu affinity of the current thread.
/// </summary>
/// <param name="group">A processor group number.</param>
/// <param name="cpus">A list of CPU numbers. The values should be
/// between 0 and <see cref="Environment.ProcessorCount"/>.</param>
public static void SetThreadProcessorAffinity(ushort groupId, params int[] cpus)
{
if (cpus == null) throw new ArgumentNullException(nameof(cpus));
if (cpus.Length == 0) throw new ArgumentException("You must specify at least one CPU.", nameof(cpus));

// Supports up to 64 processors
long cpuMask = 0;
foreach (var cpu in cpus)
{
if (cpu < 0 || cpu >= Environment.ProcessorCount)
throw new ArgumentException("Invalid CPU number.");

cpuMask |= 1L << cpu;
}

var hThread = GetCurrentThread();
var previousAffinity = new _GROUP_AFFINITY {Reserved = new ushort[3]};
var newAffinity = new _GROUP_AFFINITY
{
Group = groupId,
Mask = new UIntPtr((ulong) cpuMask),
Reserved = new ushort[3]
};

SetThreadGroupAffinity(hThread, ref newAffinity, ref previousAffinity);
}

关于c# - Windows 服务器/数据中心 : set CPU affinity with > 64 cores,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13386855/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com