到目前为止,我已经将OpenMP与Visual Studio 2010一起使用了一段时间,但是今天我又遇到了另一个令人困惑的VS怪癖。在切断所有可能的嫌疑人之后,我剩下了下面的程序。
它只是简单地计算一个周期,有时需要进行一些计算并淘汰计数器。
#include "stdafx.h"
#include "omp.h"
#include <string>
#include <iostream>
#include <time.h>
int _tmain(int argc, _TCHAR* argv[])
{
int count = 0;
double a = 1;
double b = 2;
double c = 3, mean_tau = 1, r_w = 1, weights = 1, r0 = 1, tau = 1, sq_tau = 1,
r_sw = 1;
#pragma omp parallel num_threads(3) shared(count)
{
int tid = omp_get_thread_num();
int pers_count = 0;
std::string someline;
for (int i = 0; i < 100000; i++)
{
pers_count++;
#pragma omp critical
{
count++;
if ((count%10000 == 0))
{
sq_tau = (r_sw / weights) * pow( 1/ r0 * tau, 2);
std::cout << count << " " << pers_count << std::endl;
}
}
}
}
std::getchar();
return 0;
}
现在,如果我在禁用优化(/ Od)的情况下对其进行编译,则它会按预期工作,将其共享计数器与专用计数器(大约小三倍)一起吐出,类似于
10000 3890
20000 6523
...
300000 100000
但是,如果我启用了优化功能(我尝试了所有选项,但为清楚起见,请说/ O2),由于某种原因,共享计数似乎变得私有(private)了,因为我开始得到类似
10000 10000
10000 10000
10000 10000
...
60000 60000
50000 50000
...
100000 100000
现在,我遇到了这个奇怪的问题,即使我不做任何更改,以某种方式,以前可以正常使用的所有内容也会重新构建为错误的版本。这可能是什么原因,我该怎么办?谢谢。
最佳答案
我不知道为什么共享的count
表现为这种方式。我可以提供一种解决方法(假设您仅对共享变量使用原子操作):
#pragma omp critical
{
#pragma omp atomic
count++;
if ((count%10000 == 0))
{
sq_tau = (r_sw / weights) * pow( 1/ r0 * tau, 2);
std::cout << count << " " << pers_count << std::endl;
}
}