跨多个函数/对象跨越OpenMP并行区域

时间:2017-06-23 20:47:10

标签: c++ multithreading for-loop parallel-processing openmp

有没有办法跨多个函数跨越OpenMP并行区域?

void run()
{
    omp_set_num_threads(2);
    #pragma omp parallel
    {
        foo();
        #pragma omp for
        for(int i = 0; i < 10; ++i)
        {
           //Do stuff here
        }

    }
}

void foo()
{
    #pragma omp for
    for(int j = 0; j < 10; ++j)
    {
        // Have this code be run as a worksharing loop by the OMP threads
        // spawned in run
    }
}

在这个例子中,我希望在run函数的omp并行区域中启动的线程进入foo,并将其作为工作共享循环运行,就像它们在运行中运行for循环一样。这是默认情况下发生的情况还是每个线程独立运行循环?你如何测试每一个?

在我的例子中,函数foo和run是成员函数是单独的类。

谢谢!

1 个答案:

答案 0 :(得分:2)

您所描述的是您的愿望是OpenMP的工作方式。

相关问题