OpenMP set_num_threads() 不工作

2022-01-17 00:00:00 multithreading numbers set openmp c++

我正在使用 C++ 中的 OpenMP 编写一个并行程序.

I am writing a parallel program using OpenMP in C++.

我想用omp_set_num_threads()控制程序中的线程数,但是不行.

I want to control the number of threads in the program using omp_set_num_threads(), but it does not work.

#include <iostream>
#include <omp.h>
#include "mpi.h"

using namespace std;

int myrank;
int groupsize;
double sum;
double t1,t2;
int n = 10000000;

int main(int argc, char *argv[])
{
    MPI_Init( &argc, &argv);
    MPI_Comm_rank( MPI_COMM_WORLD, &myrank );
    MPI_Comm_size(MPI_COMM_WORLD,&groupsize);

    omp_set_num_threads(4);

    sum = 0;
    #pragma omp for  reduction(+:sum)
    for (int i = 0; i < n; i++)
        sum+= i/(n/10);

    cout<<"sum="<<sum<<endl;
    cout<<"threads="<<omp_get_num_threads()<<endl;

    MPI_Finalize();
    return 0;
}

程序输出:

sum = 4.5e+007
threads=1

如何控制线程数?

推荐答案

除了在您的情况下在并行区域之外调用 omp_get_num_threads() 之外,还调用 omp_set_num_threads()不保证 OpenMP 运行时将使用指定数量的线程.omp_set_num_threads() 用于覆盖环境变量 OMP_NUM_THREADS 的值,它们都控制线程组大小的上限OpenMP 将为所有并行区域(在 OMP_NUM_THREADS 的情况下)或任何后续并行区域(在调用 omp_set_num_threads() 之后)生成.如果运行时系统认为它更合适,有一种叫做动态团队的东西仍然可以选择较少数量的线程.您可以通过调用 omp_set_dynamic(0) 或将环境变量 OMP_DYNAMIC 设置为 false 来禁用动态团队.

Besides calling omp_get_num_threads() outside of the parallel region in your case, calling omp_set_num_threads() still doesn't guarantee that the OpenMP runtime will use exactly the specified number of threads. omp_set_num_threads() is used to override the value of the environment variable OMP_NUM_THREADS and they both control the upper limit of the size of the thread team that OpenMP would spawn for all parallel regions (in the case of OMP_NUM_THREADS) or for any consequent parallel region (after a call to omp_set_num_threads()). There is something called dynamic teams that could still pick smaller number of threads if the run-time system deems it more appropriate. You can disable dynamic teams by calling omp_set_dynamic(0) or by setting the environment variable OMP_DYNAMIC to false.

要强制执行给定数量的线程,您应该禁用动态团队并使用 omp_set_num_threads() 指定所需的线程数:

To enforce a given number of threads you should disable dynamic teams and specify the desired number of threads with either omp_set_num_threads():

omp_set_dynamic(0);     // Explicitly disable dynamic teams
omp_set_num_threads(4); // Use 4 threads for all consecutive parallel regions
#pragma omp parallel ...
{
    ... 4 threads used here ...
}

或使用 num_threads OpenMP 子句:

or with the num_threads OpenMP clause:

omp_set_dynamic(0);     // Explicitly disable dynamic teams
// Spawn 4 threads for this parallel region only
#pragma omp parallel ... num_threads(4)
{
    ... 4 threads used here ...
}

相关文章