我正在体验并行标量产生两个向量并测量经过的时间。我正在比较顺序与并行标量产品:
序列:double scalar(int n, double x[], double y[])
for (int i=0; i<n; i++)
{
sum += x[i]*y[i];
}
平行线:double scalar_shm(int n, double x[], double y[])
#pragma omp parallel for private(i) shared(x,y) reduction(+:sum)
for (i=0; i<n; i++)
{
sum += x[i]*y[i];
}
我一个接一个地调用它们:
//sequential loop
for (int n=0; n<loops; n++)
{ scalar(vlength,x,y); }
//measure sequential time
t1 = omp_get_wtime() - tstart;
//parallel loop
for (int n=0; n<loops; n++)
{ scalar_shm(vlength,x,y); }
//measure parallel time
t2 = omp_get_wtime() - t1 - tstart;
//print the times elapsed
cout<< "total time (sequential): " <<t1 <<" sec" <<endl;
cout<< "total time (parallel ): " <<t2 <<" sec" <<endl;
每个循环我用随机双精度填充向量,我删除了那部分,因为我认为它无关紧要。
输出是:
total time (sequential): 15.3439 sec
total time (parallel ): 24.5755 sec
我的问题是为什么并行的速度较慢?如果它更慢有什么好处?我预计它会更快,因为我有点认为像这样的计算是它的重点。
注意:我在 Intel Core i7-740QM 上运行它