locked
About the __cpuid on windows XP x64 RRS feed

  • Question

  • I coded the following code to get the number of the logical processors.

    int CPUInfo[4] = {-1};
        u32 isSSSE3;
        int nBuff[4];
        char szMan[13];
        u32 nProcessorType = 1, nHighestFeature;

        // Get CPU manufacturer and highest CPUID
        __cpuid(nBuff, 0);
        nHighestFeature = (u32)nBuff[0];
        if(nHighestFeature == 0)
            return 0;
    printf("nHighestFeature%4d\n",nHighestFeature);
        *(int*)&szMan[0] = nBuff[1];
        *(int*)&szMan[4] = nBuff[3];
        *(int*)&szMan[8] = nBuff[2];
        szMan[12] = 0;
        if(strcmp(szMan, "AuthenticAMD") == 0)
        {
            nProcessorType = 0;//PROCESSOR_AMD;
        }

        __cpuid(CPUInfo, 1);
        nLogicalProcessors = ((CPUInfo[1] >> 16) & 0xff);
        nFeatureInfo = CPUInfo[3];
        bMultithreading = (nFeatureInfo>>28) & 1;
        bSSE2Extensions = (nFeatureInfo>>26) & 1;

        isSSSE3 = (CPUInfo[2]&0x200);
        if(nProcessorType==0||nHighestFeature<=6) isSSSE3 = 0;
    printf("nLogicalProcessors %4d\n", nLogicalProcessors);

     

    But I am reported that the XP64 get the wrong number.

    The number is 32, why does it do such error?

    Sunday, August 29, 2010 5:30 PM

Answers

  • Your code examines the APIC Physical ID. I have no clue how that relates to the number of logical processors.

    Why not simply examine the environment variable NUMBER_OF_PROCESSORS? The coding is trivial.

     

    • Marked as answer by Yi Feng Li Monday, September 6, 2010 2:46 AM
    Sunday, August 29, 2010 5:51 PM
  • A more question: how many threads is most efficient for Core i7 quadcore? 4 or 8?

    Some quad core processors use hyperthreading, so there are 8 logical processors. One can't always specify how many threads an application should use, since one or more threads may be in a blocked state at any point in time; it depends on the application and the cleverness of the programmer.

    Obviously, if there are threads that don't block, one thread per logical processor can saturate cpu throughput.

    • Marked as answer by Yi Feng Li Monday, September 6, 2010 2:46 AM
    Sunday, August 29, 2010 6:22 PM
  • Technically there are only 8 bits dedicated for the cpu count so you should put

    nLogicalProcessors = ((CPUInfo[1] >> 16) & 0xf);

    But if you get 32 as result it means something's wrong with CPU initialization during the system startup. A new cpu in outdated motherboard perhaps?

    Runtime does nothing more that execute a cpuid processor instruction that takes an argument in eax register and stores cpu data after it executes between sever registers. That 4 word array is the contents of those registers and has nothing to do with OS it's executed under but rather the hardware state. I'd look at the hardware before I'd look at OS to troubleshoot the strange number.

    As for optimal number of parallel thereads per i7, "it depends".

    Supposedly it's ~8 (n of logical cores) for well parallelized tasks but it all depends on the worksets being executed and OS that executes (Only vista++ distinguishes between logical cores versus physical cores to schedule tasks on real available hardware)

    • Marked as answer by Yi Feng Li Monday, September 6, 2010 2:46 AM
    Monday, August 30, 2010 8:21 PM

All replies

  • Your code examines the APIC Physical ID. I have no clue how that relates to the number of logical processors.

    Why not simply examine the environment variable NUMBER_OF_PROCESSORS? The coding is trivial.

     

    • Marked as answer by Yi Feng Li Monday, September 6, 2010 2:46 AM
    Sunday, August 29, 2010 5:51 PM
  • Your code examines the APIC Physical ID. I have no clue how that relates to the number of logical processors.

    Why not simply examine the environment variable NUMBER_OF_PROCESSORS? The coding is trivial.

     

    Is there some sample code about it?
    Sunday, August 29, 2010 6:05 PM
  • A more question: how many threads is most efficient for Core i7 quadcore? 4 or 8?
    Sunday, August 29, 2010 6:07 PM
  • Is there some sample code about it?

    Read up on GetEnvironmentVariable()
    Sunday, August 29, 2010 6:17 PM
  • A more question: how many threads is most efficient for Core i7 quadcore? 4 or 8?

    Some quad core processors use hyperthreading, so there are 8 logical processors. One can't always specify how many threads an application should use, since one or more threads may be in a blocked state at any point in time; it depends on the application and the cleverness of the programmer.

    Obviously, if there are threads that don't block, one thread per logical processor can saturate cpu throughput.

    • Marked as answer by Yi Feng Li Monday, September 6, 2010 2:46 AM
    Sunday, August 29, 2010 6:22 PM
  • Technically there are only 8 bits dedicated for the cpu count so you should put

    nLogicalProcessors = ((CPUInfo[1] >> 16) & 0xf);

    But if you get 32 as result it means something's wrong with CPU initialization during the system startup. A new cpu in outdated motherboard perhaps?

    Runtime does nothing more that execute a cpuid processor instruction that takes an argument in eax register and stores cpu data after it executes between sever registers. That 4 word array is the contents of those registers and has nothing to do with OS it's executed under but rather the hardware state. I'd look at the hardware before I'd look at OS to troubleshoot the strange number.

    As for optimal number of parallel thereads per i7, "it depends".

    Supposedly it's ~8 (n of logical cores) for well parallelized tasks but it all depends on the worksets being executed and OS that executes (Only vista++ distinguishes between logical cores versus physical cores to schedule tasks on real available hardware)

    • Marked as answer by Yi Feng Li Monday, September 6, 2010 2:46 AM
    Monday, August 30, 2010 8:21 PM
  • hurric wrote:

    I coded the following code to get the number of the logical  processors.

    Why not just call GetSystemInfo?


    Igor Tandetnik

    Wednesday, September 1, 2010 2:14 AM