3 Parallel regions
How do I ensure that 3 pieces of code execute concurrently with OpenMP? In the following toy problem, sections A & B generate some data and section C polls the data and acts on it.
int main(int argc, char* argv[])
{
int G = -1,S = -1;
#pragm开发者_开发问答a omp parallel sections default(none) shared(G,S,cout)
{
// Section A
#pragma omp section
{
for(;;)
{
G = G_Generator();
if(G == 0) break;
}
}
// Section B
#pragma omp section
{
for(;;)
{
S = S_Generator();
if(S == 0) break;
}
}
// Section C
#pragma omp section
{
for(;;)
{
if(G == 1 || S == 1) Do_1();
if(G == 2 || S == 2) Do_2();
if(G == 0 || S == 0) break;
}
}
}
return 0;
}
This doesn't work and I can't debug it. Is it possible that the polling section C can "miss" a G
or S
value of 1 or 2? The code just doesn't seem to achieve the desired results --- is this the right way to code in OpenMP? I've only parallelized loops before.
What you want to do is pipeline parallelism, and it will be difficult to synchronize correctly. If you want any parallelism at all, you will need a queue (probably a circular buffer) to store the data coming out of sections A and B until section C is ready for it. Look at page 147 of http://www.openmp.org/mp-documents/omp-hands-on-SC08.pdf for one example, but that is one-producer, one-consumer.
精彩评论