STL并行累加函数错误
STL parallel accumulate function wrong?
有谁知道为什么下面的脚本给出错误的结果,而运行在超过1个线程?累加函数中使用的向量的长度是否有限制?当向量的长度大于999时给出错误的答案。
ksaha@cswarmfe:g++ -fopenmp test2.cc
ksaha@cswarmfe:export OMP_NUM_THREADS=1
ksaha@cswarmfe:./a.out
result 4000
Time 0.000114875
ksaha@cswarmfe:export OMP_NUM_THREADS=2
ksaha@cswarmfe:./a.out
0.000000e+00, 1.998000e+03
3.996000e+03, 1.998000e+03
result 7992
Time 0.000231437
ksaha@cswarmfe:export OMP_NUM_THREADS=4
ksaha@cswarmfe:./a.out
0.000000e+00, 9.980000e+02
1.996000e+03, 9.980000e+02
3.992000e+03, 9.980000e+02
5.988000e+03, 9.980000e+02
result 7984
Time 0.000265011
//============================================================
#include <vector>
#include <iostream>
#include <numeric>
#include <parallel/numeric>
namespace par = std::__parallel;
double myfunction (double x, double y) {
if(y!=2) printf("%e, %en",x,y);
return x+2.0*y;
}
int main(int argc, char **argv)
{
double t = omp_get_wtime();
int err = 0;
std::vector<double> vec(1000,2.0);
for (int i=0; i<1000; i++)
if(vec[i]!=2) std::cout << vec[i] << "+++" << std::endl;
double init = 0.0;
// parallel
double result = par::accumulate(vec.begin(),vec.end(),init,myfunction);
std::cout << "result " << result << std::endl;
std::cout << "Time " << omp_get_wtime()-t << std::endl;
return err;
}
为了获得一致的结果,您的myfunction需要是关联的。在串行模式下,它一次只处理一个元素,所以myfunction总是被调用,x是累积值,y是数组中的一个条目。因此,总数是所有累积值之和的2倍,即4000。
但是当并行调用时,x和y都可能是累积值,如果你的myfunction不是关联的,你会得到不同的结果,这取决于处理顺序。
例如,在你的向量中有4个元素,串行版本将得到总共16个元素,但并行版本可以按如下方式处理,得到24个元素:
0.0 2.0 0.0 2.0
/ /
4.0 2.0 4.0 2.0
/ /
8.0 8.0
/
24.0
您的二进制操作(x+2*y)不是关联的,因此结果取决于操作的顺序。
相关文章:
- "error: no matching function for call to"构造函数错误
- 为什么类中的ostringstream类型的成员会导致";调用隐含删除复制构造函数";错误
- 尝试将unique_ptrs推送到向量时使用纯虚拟函数错误
- C++ OpenCV 卡尔曼滤波器构造函数错误
- C++:用户定义的显式类型转换函数错误
- C++ 合并字符串以'system'函数错误
- C++ wmain 函数错误时使用 Unicode
- 结构数组的构造函数错误,错误消息:没有构造函数实例与参数列表匹配
- C++ 中常量属性的初始化构造函数错误
- 线程 std::调用未知类型,无法专门化函数错误
- 表单显示对话框函数错误,并且不执行下面的语句
- 函数错误 C2059:语法错误:'>'不起作用
- STL向量上出现奇怪的复制构造函数错误
- C++ Visual Studio 重载函数错误:没有重载函数的实例与指定的类型匹配
- C++引用已删除函数错误
- 为什么通过带有文字编号的引用调用会出现"无匹配函数"错误?
- C++ 乘法定义的构造函数错误消息似乎错误
- 在 c++ 代码中将数组传递给函数错误
- FFMPEG avcodec_decode_video2函数错误代码
- 为什么我会收到转换函数错误