Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
937 views
in Technique[技术] by (71.8m points)

c - How do I ask OpenMP to create threads only once at each run of the program?

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.

--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();
    }

}

--

Any help would be highly appreciated! Thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...