6

我正在编写一些计算成本高但高度可并行化的代码。一旦并行化,我打算在 HPC 上运行它,但是为了将运行时间控制在一周内,问题需要随着处理器的数量很好地扩展。

下面是我试图实现的一个简单而可笑的例子,它足够简洁,可以编译和演示我的问题;

#include <iostream>
#include <ctime>
#include "mpi.h"

using namespace std;

double int_theta(double E){
    double result = 0;
    for (int k = 0; k < 20000; k++)
        result += E*k;
    return result;
}

int main() 
{
    int n = 3500000;
    int counter = 0;
    time_t timer;
    int start_time = time(&timer);
    int myid, numprocs;
    int k;
    double integrate, result;
    double end = 0.5;
    double start = -2.;
    double E;
    double factor = (end - start)/(n*1.);
    integrate = 0;
    MPI_Init(NULL,NULL);
    MPI_Comm_size(MPI_COMM_WORLD, &numprocs);
    MPI_Comm_rank(MPI_COMM_WORLD, &myid);
    for (k = myid; k<n+1; k+=numprocs){
        E = start + k*(end-start)/n;
        if (( k == 0 ) || (k == n))
            integrate += 0.5*factor*int_theta(E);
        else
            integrate += factor*int_theta(E);
        counter++;
    }
    cout<<"process "<<myid<<" took "<<time(&timer)-start_time<<"s"<<endl;
    cout<<"process "<<myid<<" performed "<<counter<<" computations"<<endl;
    MPI_Reduce(&integrate, &result, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD);
    if (myid == 0)
        cout<<result<<endl;
    MPI_Finalize();
    return 0;
}

我已经在我的四核笔记本电脑上编译了这个问题

mpiicc test.cpp -std=c++14 -O3 -DMKL_LP64 -lmkl_intel_lp64 - lmkl_sequential -lmkl_core -lpthread -lm -ldl

我得到以下输出;

$ mpirun -np 4 ./a.out
process 3 took 14s
process 3 performed 875000 computations
process 1 took 15s
process 1 performed 875000 computations
process 2 took 16s
process 2 performed 875000 computations
process 0 took 16s
process 0 performed 875001 computations
-3.74981e+08

$ mpirun -np 3 ./a.out 
process 2 took 11s
process 2 performed 1166667 computations
process 1 took 20s
process 1 performed 1166667 computations
process 0 took 20s
process 0 performed 1166667 computations
-3.74981e+08

$ mpirun -np 2 ./a.out 
process 0 took 16s
process 0 performed 1750001 computations
process 1 took 16s
process 1 performed 1750000 computations
-3.74981e+08

对我来说,似乎一定有一个我不知道的障碍。使用 2 个处理器而不是 3 个处理器,我可以获得更好的性能。有人可以提供任何建议吗?谢谢

4

1 回答 1

1

如果我正确阅读了您给出的输出(例如在https://unix.stackexchange.com/a/218081lscpu的帮助下),您将拥有 4 个逻辑 CPU,但只有 2 个硬件核心(1 个插槽 x 每个插槽 2 个核心)。使用您可以找到 CPU 的品牌和型号,以了解更多信息。cat /proc/cpuinfo

四个逻辑 CPU 可能来自超线程,这意味着一些硬件资源(例如 FPU 单元,但我不是这方面的专家)在两个内核之间共享。因此,我不希望在两个进程之外有任何好的并行扩展。

对于可扩展性测试,您应该尝试使用可能具有 6 个或更多硬件内核的机器,以获得更好的估计。

通过查看您的代码,我期望对任意数量的内核具有完美的可扩展性 - 至少只要您不包括进程启动和最终 MPI_Reduce 所需的时间。随着涉及的进程越多,这些肯定会变得更慢。

于 2019-04-08T13:17:17.890 回答