4

SetThreadAffinityMask() allows one to set an affinity mask for 64 logical cores (processors). However, Windows Datacenter can have up to 64 CPUs, each with many cores (see here).

How does one set threads for > 64 cores?

Ps. I am coding in C#, so a .Net answer is ideal, but an API one in C is fine too.

4

3 に答える 3

2

MSDN によると、SetThreadAffinityMask() は、現在のプロセッサ グループのアフィニティを設定します。各プロセッサ グループは、それぞれ 64 個の論理コアを持つことができます。SetThreadGroupAffinity() でグループを変更します。詳細については、 http://msdn.microsoft.com/en-us/library/windows/desktop/dd405503 (v=vs.85).aspx を参照してください。

于 2012-11-14T21:00:57.610 に答える
2

I use the following code to set the affinity for processor groups and CPUs:

[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);
}
于 2017-04-07T13:45:45.430 に答える
0

64 を超える CPU を使用するには、プロセッサ グループを考慮する必要があります。詳細については、MSDN を参照してください。

プロセッサ グループ

于 2012-11-15T00:03:50.820 に答える