.NET/C# で CPU コアの数を調べるにはどうすればいいですか? 質問する

.NET/C# で CPU コアの数を調べるにはどうすればいいですか? 質問する

.NET/C#経由でCPU コアの数を確認する方法はありますか?

PS これは「マルチスレッドを使うべきか?」という質問ではなく、コードそのものに関する質問です! :-)

ベストアンサー1

プロセッサに関して取得できるさまざまな情報があります。

  1. 物理プロセッサの数
  2. コア数
  3. 論理プロセッサの数。

これらはすべて異なる場合があります。たとえば、ハイパースレッディング対応のデュアルコア プロセッサを 2 つ搭載したマシンの場合、物理プロセッサが 2 つ、コアが 4 つ、論理プロセッサが 8 つあります。

論理プロセッサの数は、環境クラスですが、その他の情報はウィキペディア(そして、いくつかの修正プログラムまたはサービスパック一部のシステムで取得するには):

プロジェクトに System.Management.dll への参照を必ず追加してください。.NET Core では、これは NuGet パッケージとして利用できます (Windows のみ)。

物理プロセッサ:

foreach (var item in new System.Management.ManagementObjectSearcher("Select * from Win32_ComputerSystem").Get())
{
    Console.WriteLine("Number Of Physical Processors: {0} ", item["NumberOfProcessors"]);
}

コア:

int coreCount = 0;
foreach (var item in new System.Management.ManagementObjectSearcher("Select * from Win32_Processor").Get())
{
    coreCount += int.Parse(item["NumberOfCores"].ToString());
}
Console.WriteLine("Number Of Cores: {0}", coreCount);

論理プロセッサ:

Console.WriteLine("Number Of Logical Processors: {0}", Environment.ProcessorCount);

または

foreach (var item in new System.Management.ManagementObjectSearcher("Select * from Win32_ComputerSystem").Get())
{
    Console.WriteLine("Number Of Logical Processors: {0}", item["NumberOfLogicalProcessors"]);
}

Windows から除外されるプロセッサ:

また、 setupapi.dllの Windows API 呼び出しを使用して、Windows から除外され (たとえば、ブート設定によって)、上記の方法では検出できないプロセッサを検出することもできます。以下のコードは、Windows から除外されたものも含め、存在する論理プロセッサの合計数を示します (物理プロセッサと論理プロセッサを区別する方法がわかりません)。

static void Main(string[] args)
{
    int deviceCount = 0;
    IntPtr deviceList = IntPtr.Zero;
    // GUID for processor classid
    Guid processorGuid = new Guid("{50127dc3-0f36-415e-a6cc-4cb3be910b65}");

    try
    {
        // get a list of all processor devices
        deviceList = SetupDiGetClassDevs(ref processorGuid, "ACPI", IntPtr.Zero, (int)DIGCF.PRESENT);
        // attempt to process each item in the list
        for (int deviceNumber = 0; ; deviceNumber++)
        {
            SP_DEVINFO_DATA deviceInfo = new SP_DEVINFO_DATA();
            deviceInfo.cbSize = Marshal.SizeOf(deviceInfo);

            // attempt to read the device info from the list, if this fails, we're at the end of the list
            if (!SetupDiEnumDeviceInfo(deviceList, deviceNumber, ref deviceInfo))
            {
                deviceCount = deviceNumber;
                break;
            }
        }
    }
    finally
    {
        if (deviceList != IntPtr.Zero) { SetupDiDestroyDeviceInfoList(deviceList); }
    }
    Console.WriteLine("Number of cores: {0}", deviceCount);
}

[DllImport("setupapi.dll", SetLastError = true)]
private static extern IntPtr SetupDiGetClassDevs(ref Guid ClassGuid,
    [MarshalAs(UnmanagedType.LPStr)]String enumerator,
    IntPtr hwndParent,
    Int32 Flags);

[DllImport("setupapi.dll", SetLastError = true)]
private static extern Int32 SetupDiDestroyDeviceInfoList(IntPtr DeviceInfoSet);

[DllImport("setupapi.dll", SetLastError = true)]
private static extern bool SetupDiEnumDeviceInfo(IntPtr DeviceInfoSet,
    Int32 MemberIndex,
    ref SP_DEVINFO_DATA DeviceInterfaceData);

[StructLayout(LayoutKind.Sequential)]
private struct SP_DEVINFO_DATA
{
    public int cbSize;
    public Guid ClassGuid;
    public uint DevInst;
    public IntPtr Reserved;
}

private enum DIGCF
{
    DEFAULT = 0x1,
    PRESENT = 0x2,
    ALLCLASSES = 0x4,
    PROFILE = 0x8,
    DEVICEINTERFACE = 0x10,
}

おすすめ記事