与单独使用 OpenMP 相比,使用混合 OpenMP/MPI 的时间更慢 运行

slower running time with hybrid OpenMP/MPI vs OpenMP alone

我正在编写一段混合 openMP/MPI 代码。 我首先在 8 个线程上单独对 openMP 进行了基准测试。 然后,我像下面这样添加了 MPI 层

MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &numprocs);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Get_processor_name(processor_name, &namelen);
//the following function has OPENMP for loop embedded
parallelfunction(args);//should I add condition on rank?
MPI_finalize();

openMP 函数是一个标准的 openmp for 循环

#pragma omp parallel for schedule(dynamic,chunk) private(i)
for(i=0;i<n;i++){
//loop code here
}

我编译了混合代码,运行 它像这样 CPU

mpirun -np 1 -x OMP_NUM_THREADS=8 ./program

只意识到 运行ning 时间比单独的 openMP 慢 5 倍(必然在一个 CPU 上)。 我使用 bash time 函数对挂钟时间进行基准测试。 有什么建议吗?

我正在使用 openmp 3.1 和 mpicc

编辑

我使用 openMPI v1.10.3

您没有明确指定 MPI 实现,但使用 -x 将环境变量传递给 MPI 进程是 Open MPI 的明显标志。从版本 1.8 开始,进程 pinning/binding 默认启用,详见 MPI process launcher:

的手册页

Please note that mpirun automatically binds processes as of the start of the v1.8 series. Two binding patterns are used in the absence of any further directives:

Bind to core:
when the number of processes is <= 2
Bind to socket:
when the number of processes is > 2

If your application uses threads, then you probably want to ensure that you are either not bound at all (by specifying --bind-to none), or bound to multiple cores using an appropriate binding level or specific number of processing elements per application process.

在你的情况下,进程被绑定到一个核心,所有线程都必须在它上面分时。 --bind-to none 删除绑定并允许在所有 CPU 个核心上安排线程。