gpt4 book ai didi

c++ - 在 openmp 中,omp_get_thread_num 是否绑定(bind)到物理线程?

转载 作者:搜寻专家 更新时间:2023-10-31 02:04:51 26 4
gpt4 key购买 nike

我了解到 OpenMP 使用线程池来重用物理线程。我的问题是从omp_get_thread_num 获取的线程号是否绑定(bind)到物理线程?

换句话说,omp_get_thread_numgettid ( gettid man page ) 的映射在所有并行区域中是否始终相同?


OpenMP 规范的第 3.2.4 节 (link)

Binding

The binding thread set for an omp_get_thread_num region is the current team. The binding region for an omp_get_thread_num region is the innermost enclosing parallel region.

Effect

The omp_get_thread_num routine returns the thread number of the calling thread, within the 10 team executing the parallel region to which the routine region binds. The thread number is an integer between 0 and one less than the value returned by omp_get_num_threads , inclusive. The thread number of the master thread of the team is 0. The routine returns 0 if it is called from the sequential part of a program.


使用gettid系统调用的简单测试

使用 GCC 的 CentOS 7 下面的代码为我提供了两个并行区域的相同映射。但我不确定这是否只是一个特例。

#include <unistd.h>
#include <sys/syscall.h>
#include <iostream>
#include <omp.h>

int main(int argc, char *argv[]) {

std::cout << "Entering region 1:" << std::endl;

#pragma omp parallel
{
#pragma omp critical
std::cout << "num: "<< omp_get_thread_num() << " => tid: " << syscall(__NR_gettid) << std::endl;
}

std::cout << "------------------------------------------------------------" << std::endl;
std::cout << "Entering region 2:" << std::endl;


#pragma omp parallel
{
#pragma omp critical
std::cout << "num: "<< omp_get_thread_num() << " => tid: " << syscall(__NR_gettid) << std::endl;
}


return 0;
}

这是我在 CentOS 7 中使用 GCC (5.2) 获得的输出。

Entering region 1:
num: 0 => tid: 625
num: 5 => tid: 630
num: 7 => tid: 632
num: 11 => tid: 636
num: 3 => tid: 628
num: 13 => tid: 638
num: 1 => tid: 626
num: 9 => tid: 634
num: 6 => tid: 631
num: 10 => tid: 635
num: 12 => tid: 637
num: 2 => tid: 627
num: 4 => tid: 629
num: 8 => tid: 633
num: 14 => tid: 639
num: 15 => tid: 640
------------------------------------------------------------
Entering region 2:
num: 4 => tid: 629
num: 12 => tid: 637
num: 15 => tid: 640
num: 5 => tid: 630
num: 8 => tid: 633
num: 13 => tid: 638
num: 0 => tid: 625
num: 9 => tid: 634
num: 1 => tid: 626
num: 6 => tid: 631
num: 3 => tid: 628
num: 7 => tid: 632
num: 10 => tid: 635
num: 11 => tid: 636
num: 2 => tid: 627
num: 14 => tid: 639

编译:g++ toy.cpp -fopenmp

最佳答案

不能保证跨多个并行区域。这是一个稍微修改过的示例:

int main(int argc, char *argv[]) {

std::cout << "Entering region 1:" << std::endl;

#pragma omp parallel
{
#pragma omp critical
std::cout << "num: "<< omp_get_thread_num() << " => tid: " << syscall(__NR_gettid) << std::endl;
}

std::cout << "------------------------------------------------------------" << std::endl;
std::cout << "Entering region 2:" << std::endl;

// shrinks the threadpool for libgomp
#pragma omp parallel num_threads(2)
{
#pragma omp critical
std::cout << "num: "<< omp_get_thread_num() << " => tid: " << syscall(__NR_gettid) << std::endl;
}

std::cout << "------------------------------------------------------------" << std::endl;
std::cout << "Entering region 3:" << std::endl;

#pragma omp parallel
{
#pragma omp critical
std::cout << "num: "<< omp_get_thread_num() << " => tid: " << syscall(__NR_gettid) << std::endl;
}

return 0;
}

这是输出(gcc 8.2.1):

Entering region 1:
num: 0 => tid: 11845
num: 6 => tid: 11851
num: 3 => tid: 11848
num: 5 => tid: 11850
num: 7 => tid: 11852
num: 4 => tid: 11849
num: 2 => tid: 11847
num: 1 => tid: 11846
------------------------------------------------------------
Entering region 2:
num: 1 => tid: 11846
num: 0 => tid: 11845
------------------------------------------------------------
Entering region 3:
num: 2 => tid: 11853
num: 7 => tid: 11858
num: 5 => tid: 11856
num: 4 => tid: 11855
num: 1 => tid: 11846
num: 3 => tid: 11854
num: 0 => tid: 11845
num: 6 => tid: 11857

OpenMP 标准未指定跨并行区域的线程池。

关于c++ - 在 openmp 中,omp_get_thread_num 是否绑定(bind)到物理线程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52907823/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com