简体   繁体   English

Windows Server / Datacenter:将CPU关联设置为> 64个核心

[英]Windows Server / Datacenter: set CPU affinity with > 64 cores

SetThreadAffinityMask() allows one to set an affinity mask for 64 logical cores (processors). SetThreadAffinityMask()允许为64个逻辑核心(处理器)设置关联掩码。 However, Windows Datacenter can have up to 64 CPUs, each with many cores (see here ). 但是,Windows Datacenter最多可以有64个CPU,每个CPU都有许多内核(参见此处 )。

How does one set threads for > 64 cores? 如何为> 64个内核设置线程?

Ps. PS。 I am coding in C#, so a .Net answer is ideal, but an API one in C is fine too. 我在C#编码,所以.Net的答案是理想的,但C语言中的API也很好。

According to MSDN, SetThreadAffinityMask() sets the affinity for your current processor group, each of which can have 64 logical cores each. 根据MSDN,SetThreadAffinityMask()设置当前处理器组的关联,每个处理器组可以有64个逻辑核心。 Change your group with SetThreadGroupAffinity(). 使用SetThreadGroupAffinity()更改您的组。 See http://msdn.microsoft.com/en-us/library/windows/desktop/dd405503(v=vs.85).aspx for more. 有关详细信息,请参阅http://msdn.microsoft.com/en-us/library/windows/desktop/dd405503(v=vs.85).aspx

I use the following code to set the affinity for processor groups and CPUs: 我使用以下代码设置处理器组和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);
}

In order to work with more than 64 CPUs, you have to take processor groups into account. 要使用超过64个CPU,您必须考虑处理器组。 Refer to MSDN for details: 有关详细信息,请参阅MSDN:

Processor Groups 处理器组

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM