c++ - 在同一 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++ - C++ 数组 - 将新数组分配给现有数组
- db2 - Bluemix 中的 DB2 或 DashDB 服务
- powershell - 使用 powershell 更改所有权标签
- java - Springboot 2.2.5版本未解决org.springframework.boot.SpringApplication
- xaml - StackLayout 透明色
- spring - 使用 Active Directory 进行 Spring Boot mvc ldap 身份验证
- html - 为什么我的图片无法填充我网站的标题部分 (HTML/CSS)?
- meteor - 如何从 Meteor-Blaze 中的子模板访问父客户端集合?
- python - 将对象转换为数据框列中的日期时间
- angular - Angular 9 停止使用 utils 中已弃用的函数