3 平行区域
如何确保 3 段代码与 OpenMP 同时执行?在下面的玩具问题中,A 部分和 A 部分是: B 生成一些数据,C 部分轮询数据并对其进行操作。
int main(int argc, char* argv[])
{
int G = -1,S = -1;
#pragma 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;
}
这不起作用,我无法调试它。轮询部分 C 是否有可能“错过”值为 1 或 2 的 G
或 S
值?代码似乎没有达到预期的结果 -- 这是在 OpenMP 中编码的正确方法吗?我之前只并行化过循环。
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;
#pragma 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你想做的是管道并行,并且很难正确同步。如果您想要任何并行性,您将需要一个队列(可能是循环缓冲区)来存储来自 A 和 B 部分的数据,直到 C 部分准备好为止。请参阅 http://www.openmp.org/ 的第 147 页mp-documents/omp-hands-on-SC08.pdf 为例,但这是一个生产者,一个消费者。
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.