OpenMP set_num_threads() 不工作 [英] OpenMP set_num_threads() is not working

查看:56
本文介绍了OpenMP set_num_threads() 不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 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 ...
}

这篇关于OpenMP set_num_threads() 不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆