2013-05-08 115 views
2

我在Windows 7机器上使用Visual Studio 2010和OpenMP。我已经写了下面的代码snipplet:C++ OpenMP退出并行循环内部对于

#pragma omp parallel for 
for(int jj=0;jj<2;jj++){ 
MSG msg; 
// do something in parallel for jj 
int i_loopcounter = 0; 
bool b_continue = true; 
while(GetMessage(&msg, NULL, 0, 0)&&b_continue){ 
    TranslateMessage(&msg); 
    DispatchMessage(&msg); 
    i_loopcounter++;  
    if(i_loopcounter==50){ 
     b_continue = false; 
     //abort(); <- ugly, since a window appears 
     //exit(0); <- rest of the code is not executed 
     //break; <- not allowed 
     //goto end; <- not allowed 
    } 
} 
end:; 
// do some other stuff in parallel for jj 
} //end of parallel for 

我的问题是:

如果我使用b_continue变量,第一while循环到达中止条件导致程序挂起(这是一个竞争条件,我没有看到?),结果程序不执行其余的代码。

那么,我该如何得到这个工作?

我试过breaking out of structured block in openmp中建议的解决方案,但这并没有改变这种情况。

需要selfdestuction while循环来触发从一个硬件下载文件。完成后,程序应该对该文件做一些工作。在开始平行化代码之前,它使用while循环中的break语句运行良好。

感谢您的任何意见,这给我一个提示来解决这个问题。

更新:我现在改变了代码

int i_loopcounter = 0; 
    while(_i_NoDownloads!=2){ 
     GetMessage(&msg, NULL, 0, 0); 
     TranslateMessage(&msg); 
     DispatchMessage(&msg); 
     i_loopcounter++;  
     if(_b_control){ 
      cout<<i_loopcounter <<" of Thread "<<omp_get_thread_num()<<endl; 
     }  
    } 

全局变量_i_NoDownloads计数的下载文件的数量,如果达到2个的消息循环不再需要。但是,即使发生这种变化,程序的行为也不会改变,尽管事实是,while循环现在不一定是50次迭代。我认为问题是同时降级消息循环。

更新2:在阅读了很多关于Windows消息的概念之后,我发现了一个可行的解决方案。并行部分没有完成的原因是GetMessage的行为。

再次,非常感谢任何对此问题作出贡献的人。

干杯 TL

+0

在不同的OpenMP线程中运行相同的消息循环?我无法想象这会如何表现。消息循环只属于一个线程。你需要完全重新设计你的代码。 – 2013-05-08 08:50:28

+0

从一些显示i_loopcounter和omp_get_thread_num()的cout命令中,似乎有两个消息循环,因为我同时获得了两个线程号(0和1) – TLkuebler 2013-05-08 08:59:38

回答

1
+0

谢谢您的链接,但最终得到的结果与在问题描述中。通常我不想中止并行区域。我想退出while循环,这样parallel的for可以完成,如果我没有为while循环设置中止连接,那么程序永远不会终止。目前我正在考虑使用部分的解决方案。在这里,我将有三个线程,其中2个用于发送消息给第三个线程,并处理它们并在一段时间后终止。但我不知道它是否有效。目前我的信息处理并不清楚。 – TLkuebler 2013-05-08 10:10:24

0

你似乎用一个小而硬编码的迭代次数:

#pragma omp parallel for 
for(int jj = 0; jj < 2; jj++){ 
/* ... */ 
} //end of parallel for 

你可能会t探索基于sections指令的解决方案:

#pragma omp parallel 
{ 
#pragma omp sections 
    { 
#pragma omp section 
    { 
     /* jj = 0 */ 
    }   
#pragma omp section 
    { 
     /* jj = 1 */ 
    }    
    } // End sections 
#pragma omp single 
    { 
    /* While loop */ 
    } 
    /* ... */  
} // End parallel