Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How do I count CPU cores?

How do I count CPU cores?

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++comtutorial
11 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    khb
    wrote on last edited by
    #1

    Hi together, I'd like to determine the number of cores a CPU has using a C++ program. I found a solution at http://www.intel.com on how to do it for Intel CPUs. But I didn't find out how to do it for AMD CPUs (i.e., Athlon X2). Moreover, the Intel solution states that it might give wrong results for CPUs with a certain stepping if it's not compiled on that CPU type :( Therefore I'd like to ask if anybody has a robust solution for counting the cores of Intel and AMD CPUs. Thank you for any ideas in advance! Regards, Marcus.

    D 1 Reply Last reply
    0
    • K khb

      Hi together, I'd like to determine the number of cores a CPU has using a C++ program. I found a solution at http://www.intel.com on how to do it for Intel CPUs. But I didn't find out how to do it for AMD CPUs (i.e., Athlon X2). Moreover, the Intel solution states that it might give wrong results for CPUs with a certain stepping if it's not compiled on that CPU type :( Therefore I'd like to ask if anybody has a robust solution for counting the cores of Intel and AMD CPUs. Thank you for any ideas in advance! Regards, Marcus.

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      Use the Win32_Processor class.


      "Money talks. When my money starts to talk, I get a bill to shut it up." - Frank

      "Judge not by the eye but by the heart." - Native American Proverb

      K 1 Reply Last reply
      0
      • D David Crow

        Use the Win32_Processor class.


        "Money talks. When my money starts to talk, I get a bill to shut it up." - Frank

        "Judge not by the eye but by the heart." - Native American Proverb

        K Offline
        K Offline
        khb
        wrote on last edited by
        #3

        Hi David, thank you for the hint. I managed to access the Win32_Processor members. However, the members NumberOfCores and NumberOfLogicalProcessors are not valid. To get them MSDN suggests to use Win32_ComputerSystemProcessor. This class contains a reference to Win32_Processor which should contain the data I'm looking for. Now my question: How do I follow the reference from Win32_ComputerSystemProcessor to Win32_Processor in C++? In the first run I used ExecQuery() to select Win32_Processor and accessed its data using Next() and Get(). Now I guess I have to use ExecQuery() to select Win32_ComputerSystemProcessor, but how do I continue? Thank you for any help you can give! Regards, Marcus.

        D 1 Reply Last reply
        0
        • K khb

          Hi David, thank you for the hint. I managed to access the Win32_Processor members. However, the members NumberOfCores and NumberOfLogicalProcessors are not valid. To get them MSDN suggests to use Win32_ComputerSystemProcessor. This class contains a reference to Win32_Processor which should contain the data I'm looking for. Now my question: How do I follow the reference from Win32_ComputerSystemProcessor to Win32_Processor in C++? In the first run I used ExecQuery() to select Win32_Processor and accessed its data using Next() and Get(). Now I guess I have to use ExecQuery() to select Win32_ComputerSystemProcessor, but how do I continue? Thank you for any help you can give! Regards, Marcus.

          D Offline
          D Offline
          David Crow
          wrote on last edited by
          #4

          khb wrote:

          I managed to access the Win32_Processor members. However, the members NumberOfCores and NumberOfLogicalProcessors are not valid. To get them MSDN suggests to use Win32_ComputerSystemProcessor. This class contains a reference to Win32_Processor which should contain the data I'm looking for.

          This makes no sense. If the members of Win32_Processor are not valid, why would you then state that they should contain the data you're looking for?

          khb wrote:

          However, the members NumberOfCores and NumberOfLogicalProcessors are not valid.

          How are you verifying this?


          "Money talks. When my money starts to talk, I get a bill to shut it up." - Frank

          "Judge not by the eye but by the heart." - Native American Proverb

          K 1 Reply Last reply
          0
          • D David Crow

            khb wrote:

            I managed to access the Win32_Processor members. However, the members NumberOfCores and NumberOfLogicalProcessors are not valid. To get them MSDN suggests to use Win32_ComputerSystemProcessor. This class contains a reference to Win32_Processor which should contain the data I'm looking for.

            This makes no sense. If the members of Win32_Processor are not valid, why would you then state that they should contain the data you're looking for?

            khb wrote:

            However, the members NumberOfCores and NumberOfLogicalProcessors are not valid.

            How are you verifying this?


            "Money talks. When my money starts to talk, I get a bill to shut it up." - Frank

            "Judge not by the eye but by the heart." - Native American Proverb

            K Offline
            K Offline
            khb
            wrote on last edited by
            #5

            DavidCrow wrote:

            This makes no sense. If the members of Win32_Processor are not valid, why would you then state that they should contain the data you're looking for?

            Maybe I understood wrong what MSDN says. I just read: "To determine the total number of processor instances associated with a computer system object, use the Win32_ComputerSystemProcessor association class."

            DavidCrow wrote:

            How are you verifying this?

            Maybe I'm doing something wrong. I compiled the code with VC6 on my single core P4 with HT enabled (Win XP). I also run the program on a dual core Pentium D with HT enabled (also Win XP). In both cases NumberOfCores was not found and NumberOfLogicalProcessors was 0. I guess that the code works, as I can retrieve other members like, e.g., Name correctly. However, I can post the code if you like. Regards, Marcus.

            D 1 Reply Last reply
            0
            • K khb

              DavidCrow wrote:

              This makes no sense. If the members of Win32_Processor are not valid, why would you then state that they should contain the data you're looking for?

              Maybe I understood wrong what MSDN says. I just read: "To determine the total number of processor instances associated with a computer system object, use the Win32_ComputerSystemProcessor association class."

              DavidCrow wrote:

              How are you verifying this?

              Maybe I'm doing something wrong. I compiled the code with VC6 on my single core P4 with HT enabled (Win XP). I also run the program on a dual core Pentium D with HT enabled (also Win XP). In both cases NumberOfCores was not found and NumberOfLogicalProcessors was 0. I guess that the code works, as I can retrieve other members like, e.g., Name correctly. However, I can post the code if you like. Regards, Marcus.

              D Offline
              D Offline
              David Crow
              wrote on last edited by
              #6

              khb wrote:

              In both cases NumberOfCores was not found and NumberOfLogicalProcessors was 0.

              These were added for Vista. http://www.microsoft.com/technet/scriptcenter/topics/vista/wmi1.mspx[^] http://windowssdk.msdn.microsoft.com/en-us/library/ms738352.aspx[^]


              "Money talks. When my money starts to talk, I get a bill to shut it up." - Frank

              "Judge not by the eye but by the heart." - Native American Proverb

              K 1 Reply Last reply
              0
              • D David Crow

                khb wrote:

                In both cases NumberOfCores was not found and NumberOfLogicalProcessors was 0.

                These were added for Vista. http://www.microsoft.com/technet/scriptcenter/topics/vista/wmi1.mspx[^] http://windowssdk.msdn.microsoft.com/en-us/library/ms738352.aspx[^]


                "Money talks. When my money starts to talk, I get a bill to shut it up." - Frank

                "Judge not by the eye but by the heart." - Native American Proverb

                K Offline
                K Offline
                khb
                wrote on last edited by
                #7

                DavidCrow wrote:

                These were added for Vista.

                Whoops, I somehow missed that :-O Well, this brings me right back to my initial question: Is there any chance to get the processor specs on non-Vista systems? Regards, Marcus.

                D 1 Reply Last reply
                0
                • K khb

                  DavidCrow wrote:

                  These were added for Vista.

                  Whoops, I somehow missed that :-O Well, this brings me right back to my initial question: Is there any chance to get the processor specs on non-Vista systems? Regards, Marcus.

                  D Offline
                  D Offline
                  David Crow
                  wrote on last edited by
                  #8

                  khb wrote:

                  Is there any chance to get the processor specs on non-Vista systems?

                  Yes, I've done it with both Win32_ComputerSystem and Win32_Processor.


                  "Money talks. When my money starts to talk, I get a bill to shut it up." - Frank

                  "Judge not by the eye but by the heart." - Native American Proverb

                  K 1 Reply Last reply
                  0
                  • D David Crow

                    khb wrote:

                    Is there any chance to get the processor specs on non-Vista systems?

                    Yes, I've done it with both Win32_ComputerSystem and Win32_Processor.


                    "Money talks. When my money starts to talk, I get a bill to shut it up." - Frank

                    "Judge not by the eye but by the heart." - Native American Proverb

                    K Offline
                    K Offline
                    khb
                    wrote on last edited by
                    #9

                    Now I'm getting confused ;) I just tried Win32_ComputerSystem. For example, I can get NumberOfProcessors correctly. But I can't get NumberOfLogicalProcessors, it's not in the query result. But this is alright according to MSDN which says about NumberOfLogicalProcessors: Windows Server 2003, Windows XP, Windows 2000, Windows NT 4.0, and Windows Me/98/95: This property is not available. Then I tried Win32_Processor. For example, I can get Name correctly. But I get neither NumberOfLogicalProcessors nor NumberOfCores, they're both not in the query result. But again this is alright, because MSDN states for both properties what I already cited above. Or did I get something completely wrong? Thank you for your patience, David! Regards, Marcus.

                    M 1 Reply Last reply
                    0
                    • K khb

                      Now I'm getting confused ;) I just tried Win32_ComputerSystem. For example, I can get NumberOfProcessors correctly. But I can't get NumberOfLogicalProcessors, it's not in the query result. But this is alright according to MSDN which says about NumberOfLogicalProcessors: Windows Server 2003, Windows XP, Windows 2000, Windows NT 4.0, and Windows Me/98/95: This property is not available. Then I tried Win32_Processor. For example, I can get Name correctly. But I get neither NumberOfLogicalProcessors nor NumberOfCores, they're both not in the query result. But again this is alright, because MSDN states for both properties what I already cited above. Or did I get something completely wrong? Thank you for your patience, David! Regards, Marcus.

                      M Offline
                      M Offline
                      Mike ONeill
                      wrote on last edited by
                      #10

                      ::GetSystemInfo (which is available all the way back to Win95) will get you the number of processors. http://msdn.microsoft.com/library/en-us/sysinfo/base/getsysteminfo.asp[^] I don't know how it would interpret a hyper-threaded machine. For that you might try the cpuid instruction, which is available as a compiler intrinsic as __cpuid (see http://msdn2.microsoft.com/en-us/library/hskdteyh.aspx[^]) Mike

                      K 1 Reply Last reply
                      0
                      • M Mike ONeill

                        ::GetSystemInfo (which is available all the way back to Win95) will get you the number of processors. http://msdn.microsoft.com/library/en-us/sysinfo/base/getsysteminfo.asp[^] I don't know how it would interpret a hyper-threaded machine. For that you might try the cpuid instruction, which is available as a compiler intrinsic as __cpuid (see http://msdn2.microsoft.com/en-us/library/hskdteyh.aspx[^]) Mike

                        K Offline
                        K Offline
                        khb
                        wrote on last edited by
                        #11

                        Hi Mike, thanks for your answer. Currently I'm using the code from the article Counting Physical and Logical Processors here on CP. It makes also use of the functions you mentioned. I split up the code on did some tests on the following 2 PCs (I was wrong before: Pentium D has no HT):

                        1. Pentium 4, 3.06GHz, Single core, HT
                        2. Pentium D 950, 3.40GHz, Dual core, No HT

                        I'd like to calculate the following values:

                        1. CPUs: 1, #Cores: 1, #Log. CPUs per Core: 2

                        2. CPUs: 1, #Cores: 2, #Log. CPUs per Core: 1

                        Now the problem: The functions you mentioned return the following values for both(!) CPUs:

                        GetSystemInfo.dwNumberOfProcessors: 2
                        cpuid HTT bit: Set
                        cpuid # logical CPUs per physical CPU: 2

                        This shows that it's not possible to distinguish between HT and dual core :(( Regards, Marcus.

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups