物理コアと論理コアの数を取得

Windows 7以降。
論理コアはGetSystemInfoで簡単(Groupが1個じゃない場合はダメ?)。
HTの有無はGetLogicalProcessorInformationExをRelationProcessorCoreで。
2CPUとか128コアとかで試したいが、さすがに。

#include <windows.h>
#include <stdio.h>

int main()
{
    SYSTEM_INFO systemInfo;
    GetSystemInfo(&systemInfo);
    printf("logical processors (current group) %d\n", systemInfo.dwNumberOfProcessors);

    PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX Buffer;
    DWORD ReturnedLength;
    BOOL ret;
    auto printMask = [](KAFFINITY mask){
        for (int i = sizeof mask * 8 - 1; i >= 0; i--){
            printf("%d", mask >> i & 1);
        }
        printf("\n");
    };

    ReturnedLength = 0;
    ret = GetLogicalProcessorInformationEx(RelationProcessorPackage, nullptr, &ReturnedLength);
    if (ret == FALSE && GetLastError() == ERROR_INSUFFICIENT_BUFFER){
        Buffer = (decltype(Buffer))HeapAlloc(GetProcessHeap(), 0, ReturnedLength);
        ret = GetLogicalProcessorInformationEx(RelationProcessorPackage, Buffer, &ReturnedLength);
        if (ret){
            DWORD k = Buffer->Processor.GroupCount;
            printf("GroupCount %d\n", k);
            for (DWORD i = 0; i < k; i++){
                printf("%d ", Buffer->Processor.GroupMask[i].Group);
                printMask(Buffer->Processor.GroupMask[i].Mask);
            }
            HeapFree(GetProcessHeap(), 0, Buffer);
        }
    }

    ReturnedLength = 0;
    ret = GetLogicalProcessorInformationEx(RelationProcessorCore, nullptr, &ReturnedLength);
    if (ret == FALSE && GetLastError() == ERROR_INSUFFICIENT_BUFFER){
        Buffer = (decltype(Buffer))HeapAlloc(GetProcessHeap(), 0, ReturnedLength);
        ret = GetLogicalProcessorInformationEx(RelationProcessorCore, Buffer, &ReturnedLength);
        if (ret){
            BYTE smt = (Buffer->Processor.Flags == LTP_PC_SMT);
            printf("SMT %d\n", smt);
            printf("physical processors (current group) %d\n", systemInfo.dwNumberOfProcessors / (smt ? 2 : 1));
            DWORD k = ReturnedLength / Buffer->Size;
            for (DWORD i = 0; i < k; i++){
                printf("%d ", Buffer->Processor.GroupMask[0].Group);
                printMask(decltype(Buffer)((char *)Buffer + Buffer->Size * i)->Processor.GroupMask[0].Mask);
            }
            HeapFree(GetProcessHeap(), 0, Buffer);
        }
    }

    return 0;
}
/* Core i7-2670QM (x64、HTオン、4コア8スレッド) での出力例
logical processors (current group) 8
GroupCount 1
0 0000000000000000000000000000000000000000000000000000000011111111
SMT 1
physical processors (current group) 4
0 0000000000000000000000000000000000000000000000000000000000000011
0 0000000000000000000000000000000000000000000000000000000000001100
0 0000000000000000000000000000000000000000000000000000000000110000
0 0000000000000000000000000000000000000000000000000000000011000000
*/