首页 > 解决方案 > 在同一 CPU 内核上执行的 OpenMP 线程

问题描述

配置:Windows 10、mingw-x64、Core 2(4 个逻辑)

以下代码

#include <cstdio>
#include <omp.h>
#include <windows.h>

int main() {

    #pragma omp parallel
    {
        std::printf ("Doing Stuff on thread: %d  on Core #%d\n", omp_get_thread_num(),\
                      GetCurrentProcessorNumber());
    }

    return 0;
}

有时它会给出输出:

Doing Stuff on thread: 1  on Core #1
Doing Stuff on thread: 0  on Core #2
Doing Stuff on thread: 2  on Core #2
Doing Stuff on thread: 3  on Core #2

虽然大多数时候它给

Doing Stuff on thread: 2  on Core #1
Doing Stuff on thread: 1  on Core #3
Doing Stuff on thread: 0  on Core #2
Doing Stuff on thread: 3  on Core #0

这是预期的。

如何确保线程具有针对不同内核的亲和性?有没有办法强制调度程序这样做?

标签: c++mingwopenmpmulticoremingw-w64

解决方案


推荐阅读