我怎么问的OpenMP在程序的每个只运行一次创建线程? [英] How do I ask OpenMP to create threads only once at each run of the program?

查看:141
本文介绍了我怎么问的OpenMP在程序的每个只运行一次创建线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想并行由第三方编写大型程序。我不能透露code,但我会尝试给什么,我希望做的最接近的例子。
基于以下的code。正如你所看到的,因为该条款水货是里面的while循环,线程的创建/销毁的(是)来完成每次迭代,它是昂贵的。
既然我不能移动Initializors ...等是,而循环之外。

I am trying to parallelize a large program that is written by a third-party. I cannot disclose the code, but I will try and give the closest example of what I wish to do. Based on the code below. As you can see, since the clause "parallel" is INSIDE the while loop, the creation/destruction of the threads are(is) done with each iteration, which is costly. Given that I cannot move the Initializors...etc to be outside the "while" loop.

- 基地code

--Base code

void funcPiece0()
{
    // many lines and branches of code
}


void funcPiece1()
{
    // also many lines and branches of code
}

void funcCore()
{
    funcInitThis();
    funcInitThat();

#pragma omp parallel
    {
#pragma omp sections
        {
#pragma omp section
            {
                funcPiece0();
            }//omp section
#pragma omp section
            {
                funcPiece1();
            }//omp section
        }//omp sections
    }//omp parallel

}

int main()
{

    funcInitThis();
    funcInitThat();
#pragma omp parallel
    {
    while(1)
    {
        funcCore();
    }
    }

}

我寻求做是为了避免每次迭代的创建/销毁,并在节目的开始/结束让它一次。我尝试了许多变化,以平行条款的位移。我基本上具有相同的本质是如下:(只有一个线程创建/销毁每次程序运行)
- 什么我试过了,但在功能初始化失败非法访问。

What I seek to do is to avoid the creation/destruction per-iteration, and make it once at the start/end of the program. I tried many variations to the displacement of the "parallel" clause. What I basically has the same essence is the below: (ONLY ONE thread creation/destruction per-program run) --What I tried, but failed "illegal access" in the initializing functions.

void funcPiece0()
{
    // many lines and branches of code
}


void funcPiece1()
{
    // also many lines and branches of code
}

void funcCore()
{
    funcInitThis();
    funcInitThat();

//#pragma omp parallel
//  {
#pragma omp sections
        {
#pragma omp section
            {
                funcPiece0();
            }//omp section
#pragma omp section
            {
                funcPiece1();
            }//omp section
        }//omp sections
//  }//omp parallel

}

int main()
{

    funcInitThis();
    funcInitThat();

    while(1)
    {
        funcCore();
    }

}

-

任何帮助将是非常美联社preciated!
谢谢!

Any help would be highly appreciated! Thanks!

推荐答案

的OpenMP仅在开始创建工作线程。并行编译不产卵线程。你如何确定线程被催生?

OpenMP only creates worker thread at start. parallel pragma does not spawn thread. How do you determine the thread are spawned?

这篇关于我怎么问的OpenMP在程序的每个只运行一次创建线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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