1
votes

I know there is same question, what I want to know is If I use "#pragma omp parallel for" inside a loop, does the program make threads every iteration? Or makes them once and reuses them?

for(i=1;i<ni;i++)
{
    #pragma omp parallel for
    for(j=1;j<nj;j++)
    {
        some codes.....
    }
    and some codes ....(with loop)
}

If this code makes threads every iteration, how can I optimize?

(EDITTED) actually, my code is not perfectly nested loop, and can not be modified nested loop.

1
See this answerKao
It makes them once and reuses them. It's call a thread pool.Z boson
@Kao, that answer you pointed to has nothing to do with the OP's question. The OP wants to know if new threads are created each iteration of the outer loop. The OP's does not want to know if it makes sense to only parallelize the inner loop.Z boson
I don't understand all the down votes. Although the OP could have worded his question better it's not that bad. Many people using OpenMP are not aware of thread pools. Perhaps it's the title that's the problem as it does not really indicate the OP's question.Z boson

1 Answers

2
votes

There are cases where it's useful to only parallelize the inner loop and not the outer loop and so it's a good question as to whether are not the threads are recreated every iteration of the outer loop.

GCC, ICC, and MSVC all create what's called a thread pool so the answer to your question is that the threads are created once and reused. As far as I know there is nothing in the OpenMP standard that says that a thread pool has to be created. Without a thread pool the inner loop would likely be much slower.