[MPI学习笔记]mpi并行实现方法

本文深入解析了MPI(Message Passing Interface)与RSH(Remote Shell)在并行计算中的作用及关系。详细介绍了如何使用mpif77、mpirun等工具进行MPI程序的编译和执行,以及它们如何通过RSH在多个节点上并行运行程序,实现进程间通信和数据交换。同时,文章强调了mpirun相较于RSH的额外功能——进程rank的分配,这对于理解并行计算的底层机制至关重要。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

首先介绍下rsh命令功能:

rsh


NAME
rsh - remote shell

SYNOPSIS
rsh [-Kdnx] [-l username] host [command]

DESCRIPTION
Rsh executes command on host.
Rsh copies its standard input to the remote command, the standard output of the remote command to its standard output, and the standard error of the remote command to its standard error. Interrupt, quit and terminate signals are propagated to the remote command; rsh normally terminates when the remote command does. The options are as follows:

-d The -d option turns on socket debugging (using setsockopt (2)) on
the TCP sockets used for communication with the remote host.

-l By default, the remote username is the same as the local username
The -l option allows the remote name to be specified.

-n The -n option redirects input from the special device /dev/null
(see the BUGS section of this manual page).

If no command is specified, you will be logged in on the remote host
using rlogin(1).

FILES
/etc/hosts

通过执行mpirun命令和do.sh脚本分析mpirun与rsh命令关系:
Script:do.sh

1 #! /bin/bash
2 cat hosts$1 | sed '/#/d' >idotemp
3 for i in `cat idotemp`
4 do
5 rsh $i $2
6 done
7 rm -f idotemp

其中hoststemp文件内容为:

data
node100
node101
node102
node104


执行编译命令

1 mpif77 -O2 -o 02 02_mpi.f

(1)执行rsh命令

1 rsh temp ./02

得到输出结果:
 

 Hello! My rank is   0.  My hostname is "data.gucas.ac.cn"
Hello! My rank is 0. My hostname is "node100.gucas.ac.cn"
Hello! My rank is 0. My hostname is "node101.gucas.ac.cn"
Hello! My rank is 0. My hostname is "node102.gucas.ac.cn"
Hello! My rank is 0. My hostname is "node104.gucas.ac.cn"

(2)或执行mpirun命令

1 mpirun -np 5 -machinefile hoststemp 02

得到输出结果:
 

 Hello! My rank is   2.  My hostname is "node101.gucas.ac.cn"
Hello! My rank is 3. My hostname is "node102.gucas.ac.cn"
Hello! My rank is 1. My hostname is "node100.gucas.ac.cn"
Hello! My rank is 4. My hostname is "node104.gucas.ac.cn"
Hello! My rank is 0. My hostname is "data.gucas.ac.cn"


通过对以上命令输出结果分析发现其中my rank不同,而my hostname相同。
(1)my hostname相同说明mpirun通过rsh在各个节点上并行执行命令,这点可以从mpirun脚本中得到验证

1 which mpirun
2 vi /opt/mpichintel/bin/mpirun

mpirun脚本中有一行:

RSHCOMMAND="/usr/bin/rsh"


(2)而my rank不同,说明mpirun相对于rsh还有另外一个功能:对进程rank赋值。

 

总结:

(1)mpi程序编译执行一般流程是:

1 mpif77 -O2 -o 02 02_mpi.f    

     ===> 编译mpi程序,将mpi.f头文件加载到程序中,形成可执行并行程序。

1 mpirun -np 5 -machinefile hoststemp 02     

    ===> 通过mpirun脚本建立并行环境,分配节点/进程rank,通过rsh/ssh

              在各个节点上加载程序并并行执行。根据指定不同进程rank号执行

              不同并行子任务。并通过rsh/ssh将结果输出到执行节点。

(2)重要概念:mpif77、mpif90、mpirun都是shell脚本文件。

 

参考资料:根据张怀老师讲课内容整理所得。

转载于:https://www.cnblogs.com/Parallel-Life/archive/2012/03/11/2390674.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值