MPI跨节点小结

本文详细介绍了如何使用MPI和OpenMPI进行跨节点程序编译与运行,涉及单节点和多节点配置、节点命名文件、进程启动控制,包括单核和多核部署,以及指定每个节点的进程数量。适合理解和实践分布式计算环境的开发者。


注:以下教程未特别注明均为在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
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值