MPI跨节点运行配置
注:以下教程未特别注明均为在mpich3下测试
mpi_hello_world.c源码
//mpi_hello_world.c
#include <mpi.h>
#include <stdio.h>
int main(int argc, char** argv) {
// Initialize the MPI environment. The two arguments to MPI Init are not
// currently used by MPI implementations, but are there in case future
// implementations might need the arguments.
MPI_Init(NULL, NULL);
// Get the number of processes,得到总进程数,由运行时参数传入
int world_size;
MPI_Comm_size(MPI_COMM_WORLD, &world_size);
// Get the rank of the process,得到进程号
int world_rank;
MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
// Get the name of the processor,得到进程名
char processor_name[MPI_MAX_PROCESSOR_NAME];
int name_len;
MPI_Get_processor_name(processor_name, &name_len);
// Print off a hello world message
printf("Hello world from processor %s, rank %d out of %d processors\n",
processor_name, world_rank, world_size);
// Finalize the MPI environment. No more MPI calls can be made after this
MPI_Finalize();
}
编译
mpicc -o mpi_hello_world mpi_hello_world.c
单节点运行
mpirun -np 4 ./mpi_hello_world
多节点运行
注:先确保可以跨节点免密登陆,见另一篇教程“linux各节点免密登陆”
准备节点名称文件
mpich3节点文件
//host_file
n3
n4
n5
n2
openmpi3节点文件
//host_file
n1
n2
n3
n4
注:实际节点文件需将此处第一行去掉
运行
mpich3
在各个节点单核起进程
mpirun -np 4 -f host_file ./mpi_hello_world
在各个节点多核起进程
在四个节点双核上起进程
mpirun -np 4 -f host_file ./mpi_hello_world
注:根据配置文件共可起8个进程,此时只用n3,n4节点进程
//host_file
n3:2
n4:2
n5:2
n2:2
openmpi3
将各个节点所有进程耗完在其他节点起进程
mpirun -machinefile hostfile -np 72 ./mpi_hello_world
//hostfile
n1
n2
n3
n4
指定每个节点起进程数目
mpirun -machinefile hostfile -np 9 ./mpi_hello_world
注:
①最大进程数需要与“hostfile”中指定总进程数一致
②文件中"n1"为节点名称,不同机器可能因人而异
//hostfile
n1 slots=1
n2 slots=2
n3 slots=2
n4 slots=4
本文详细介绍了如何使用MPI和OpenMPI进行跨节点程序编译与运行,涉及单节点和多节点配置、节点命名文件、进程启动控制,包括单核和多核部署,以及指定每个节点的进程数量。适合理解和实践分布式计算环境的开发者。
6529

被折叠的 条评论
为什么被折叠?



