为什么串行执行比并行执行花费的时间少?

Why is serial execution taking less time than parallel?

我必须添加两个向量并将串行性能与并行性能进行比较。 但是,我的并行代码似乎比串行代码执行时间更长。

能否请您提出修改建议以使并行代码更快?

#include <iostream>
#include <time.h>
#include "omp.h"
#define ull unsigned long long

using namespace std;

void parallelAddition (ull N, const double *A, const double *B, double *C)
{
    ull i;

    #pragma omp parallel for shared (A,B,C,N) private(i) schedule(static)
    for (i = 0; i < N; ++i)
    {
        C[i] = A[i] + B[i];
    }
}

int main(){

ull n = 100000000;
double* A = new double[n];
double* B = new double[n];
double* C = new double[n];

double time_spent = 0.0;


for(ull i = 0; i<n; i++)
{
    A[i] = 1;
    B[i] = 1;
}

//PARALLEL
clock_t begin = clock();
parallelAddition(n, &A[0], &B[0], &C[0]);
clock_t end = clock();
time_spent += (double)(end - begin) / CLOCKS_PER_SEC;

cout<<"time elapsed in parallel : "<<time_spent<<endl;


//SERIAL 
time_spent = 0.0;
for(ull i = 0; i<n; i++)
{
    A[i] = 1;
    B[i] = 1;
}
begin = clock();
for (ull i = 0; i < n; ++i)
{
    C[i] = A[i] + B[i];
}
end = clock();
time_spent += (double)(end - begin) / CLOCKS_PER_SEC;
cout<<"time elapsed in serial : "<<time_spent;
return 0;
}

这些是结果:

并行时间: 0.824808

序列流逝的时间: 0.351246

我在另一个线程上读到,有线程生成、资源分配等因素。但是我不知道该怎么做才能得到预期的结果。


编辑:

谢谢! @zulan 和@Daniel Langr 的回答确实有帮助!

我用了 omp_get_wtime() 而不是 clock()。 碰巧 clock() 测量所有线程的累积时间,而 omp_get_wtime() 可用于测量从任意点到其他任意点的时间

这个答案也很好地回答了这个问题:

固定代码如下:

void parallelAddition (ull N, const double *A, const double *B, double *C)
{
    ....
}

int main(){

    ....


    //PARALLEL
    double begin = omp_get_wtime();
    parallelAddition(n, &A[0], &B[0], &C[0]);
    double end = omp_get_wtime();
    time_spent += (double)(end - begin);

    cout<<"time elapsed in parallel : "<<time_spent<<endl;



    ....

    //SERIAL
    begin = omp_get_wtime();
    for (ull i = 0; i < n; ++i)
    {
        C[i] = A[i] + B[i];
    }
    end = omp_get_wtime();
    time_spent += (double)(end - begin);
    cout<<"time elapsed in serial : "<<time_spent;
return 0;
}

更改后的结果:

并行时间: 0.204763

连续播放的时间: 0.351711

有多种因素会影响您的测量结果:

  1. 按照@zulan的建议使用omp_get_wtime(),否则,您实际上可能会计算组合CPU时间,而不是墙时间.

  2. 线程有一些开销,通常不会为短期计算带来回报。您可能希望使用更高的 n.

  3. "Touch" C 数组中的数据在 运行 parallelAddition 之前。否则,内存页面实际上是从 parallelAddition 内部的 OS 分配的。自 C++11 以来的轻松修复:double* C = new double[n]{};.

我试过你的程序 n 为 1G,最后一次更改将 2 个线程的 parallelAddition 运行时间从 1.54 减少到 0.94 [s]。串行版本耗时 1.83 [s],因此,2 线程的加速为 1.95,非常接近理想。

其他注意事项:

  • 通常,如果您对某些内容进行概要分析,确保该程序具有一些可观察到的效果。否则,编译器可能会优化掉很多代码。您的数组添加没有明显的效果。

  • 将某种形式的 restrict 关键字 添加到 C 参数。没有它,编译器可能无法应用矢量化。

  • 如果您使用的是多插槽系统,请注意线程的关联性和 NUMA 效果。在我的双插槽系统上,当将线程限制为单个 NUMA 节点 (numactl -N 0 -m 0) 时,2 线程并行版本的运行时间为 0.94 [s](如上所述)。没有 numactl,它花费了 1.35 [s],因此增加了 1.44 倍。