【问题标题】:Program does not use all cores程序未使用所有内核
【发布时间】:2019-01-05 08:58:51
【问题描述】:

我有一个复杂的程序,它应该使用所有内核来执行复杂的数学计算。

我有一个带有两个 Intel Xeon Platinum 8160 的系统。他们每个人都有24 cores,所以我在一起有48 cores96 threads

我的程序只使用24 cores 而不是所有48。它适用于第一个 CPU 的 24 或第二个 CPU 的 24,但不是全部一起使用。

当我启动程序的第二个实例时,没有任何变化,只使用了一个 CPU。

我附上一些截图。

我将一些代码提取到一个最小的工作示例中,该示例检查有多少线程可用。仅检测到 48 而不是所有 96 threads

#include <stdlib.h>
#include <stdio.h>
#include <winsock.h>
#include <math.h>
#include <process.h>

static void thread_start(void *thread) {
    int i;
    i = *(int*)thread;
    for (;;) {
        i = (int)sqrt(i++);
    }
}

int main (int argc, char * argv[]) {
    SYSTEM_INFO sysi;
    int thread_max, i;

    argc = argc;
    argv = argv;

    GetSystemInfo(&sysi);
    thread_max = sysi.dwNumberOfProcessors;

    printf("\n... thread_max=%d\n", thread_max);

    printf("\n\n");

    for (i = 0; i < thread_max *2; i++) {
        _beginthread(thread_start, 0, &i);
    }

    for (;;) i = i;

    // return EXIT_SUCCESS;

}

我的机器在 Windows 10 64-bit Pro 下运行。可能是什么问题?

【问题讨论】:

标签: c windows multithreading ansi-c


【解决方案1】:

GetSystemInfodwNumberOfProcessors 返回当前组中的处理器数量,而不是硬件处理器的总数。 See Microsoft docs

当前组中的逻辑处理器数。检索 这个值,使用GetLogicalProcessorInformation函数。

您应该改为使用ALL_PROCESSOR_GROUPS 参数调用Get­Active­Processor­Count。这将计算所有组中的所有处理器。由Raymond Chen推荐

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多