openmp 随机只使用一个核心
我使用的是 Ubuntu 10.04
,gcc 版本 4.4.3
。我正在使用 omp.h
和 g++ 参数 -fopenmp
编译 C++ 代码。大多数时候一切都按预期进行。我有带超线程的四核,所以实际上有 8 个核心。
随机地,当我运行我的软件(并观看系统监视器)时,所有 8 个核心都达到 100%,粉丝们开始加入,这一切都很好。半秒内,除 1 个内核外的所有内核都关闭至不活动状态,只有一个内核仍在工作。
此时,如果我取消程序(只需简单的 Ctrl+C)并重新启动它 - 它会按预期工作,所有核心都会保持 100% 的工作状态。
以下是代码片段,以防有用:
28 #include <time.h>
29 #include <omp.h>
30 #include <string>
...
713 #ifdef _OPENMP
714 if(omp_get_dynamic())
715 omp_set_dynamic(1);
716 #endif
717
718 #pragma omp parallel
719 {
720 #pragma omp for schedule(dynamic) private(node)
721 for (int c = 0; c < buffer->GetCount(); c++) {
... initialize objects pointers etc
724 #pragma omp critical(BUFFER_LOOKUP)
725 {
... perform chunk of code that is a choke
734 }
I'm on Ubuntu 10.04
, gcc version 4.4.3
. I'm compiling c++ code with omp.h
and a g++ parameter -fopenmp
. Most of the time everything works as expected. I have quad-core with hyper-threading, so virtually 8 cores.
Randomly, when I run my software (and watch system monitor) all 8 cores woof up to 100%, fans kick in, all that good deal. Within half a second all cores but 1 shut down to inactive, and one remains working.
At this time if I cancel my program (just plain Ctrl+C) and restart it - it works as expected, all cores keep working at 100%.
Here are snippets of code in case it's useful:
28 #include <time.h>
29 #include <omp.h>
30 #include <string>
...
713 #ifdef _OPENMP
714 if(omp_get_dynamic())
715 omp_set_dynamic(1);
716 #endif
717
718 #pragma omp parallel
719 {
720 #pragma omp for schedule(dynamic) private(node)
721 for (int c = 0; c < buffer->GetCount(); c++) {
... initialize objects pointers etc
724 #pragma omp critical(BUFFER_LOOKUP)
725 {
... perform chunk of code that is a choke
734 }
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从 omp_set_dynamic(n) 手册页面:
n ... 一个值,指示后续并行区域中可用的线程数是否可以由运行时调整。如果非零,则运行时可以调整线程数,如果为零,则运行时将不会动态调整线程数
尝试使用 #pragma omp parallel num_threads(n) 转动 omp_set_dynamyc 来设置所需的线程数。
From the omp_set_dynamic(n) maual page:
n ... A value that indicates if the number of threads available in subsequent parallel region can be adjusted by the runtime. If nonzero, the runtime can adjust the number of threads, if zero, the runtime will not dynamically adjust the number of threads
Try to set the number of threads you want with a #pragma omp parallel num_threads(n) turning of the omp_set_dynamyc.