ns-3分布式mpi配置及运行
一,安装openmpi
1,下载Open MPI
1.1,访问Open MPI官网,获取最新下载链接
1.2,进入命令窗口,使用wget获取安装包(或直接网页下载)
wget https://download.open-mpi.org/release/open-mpi/v4.1/openmpi-4.1.5.tar.gz
1.3,解压安装包
tar- zxvf openmpi-4.1.5.tar.gz
cd openmpi-4.1.5
2,安装
2.1,安装到用户目录
配置安装到 /usr/local/openmpi目录下,可以指定为其他目录,如,用户目录下。
./configure --prefix="/usr/local/openmpi"
2.2,编译安装
make -j8
sudo make install
3,配置环境
3.1,打开配置文件
sudo gedit ~/.bashrc
3.2,末尾添加
export PATH="$PATH:/usr/local/openmpi/bin"
export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:/usr/local/openmpi/lib/"
3.3,保存后,执行
sudo ldconfig
打开新的终端,使环境变量生效
4,测试是否安装成功
cd ~/openmpi-4.0.5/examples
make all
mpirun -np 4 hello_c
参考链接:
https://blog.csdn.net/m0_49448331/article/details/128041491?spm=1001.2014.3001.5502
二,ns-3配置MPI
ns-3 在大规模拓扑仿真中,为加速仿真速度,可以利用MPI工具,实现分布式多进程联合仿真。
1,配置ns-3的文件
sudo gedit /etc/profile
在文件最后加入一行路径为ubuntu系统安装openmpi的路径; 一行ns-3的安装路径
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/antl/zq/ns-allinone-3.30.1/ns-3.30.1
更新源
source /etc/profile
sudo /sbin/ldconfig -v
2,运行编译
根据当前版本ns-3的文档执行指令(文档位置:…/src/mpi/doc/distributed.rst)
清理目录
./waf distclean
使能MPI
./waf -d debug configure --enable-examples --enable-tests --enable-mpi
编译
./waf
MPI运行脚本
mpirun -np 2 ./waf --run simple-distributed
参考链接:
https://blog.csdn.net/m0_49448331/article/details/128043426
三,demo
撰写可MPI执行的脚本的核心是根据systemId分别运行应用部分代码,实现多核处理;
/*
· 使能:./waf configure -d debug --enable-examples --enable-tests --enable-mpi
· 单机:mpirun -np 2 ./waf --run "scratch/fattree-distributed"
· 多机:mpirun -np 2 -machinefile mpihosts ./waf --run "scratch/fattree-distributed"
· ./waf --run simple-distributed --command-template="mpiexec -np 2 %s"
· ./waf --run nms-p2p-nix-distributed --command-template="mpiexec -np 2 -machinefile mpihosts %s --nix=0"
*/
...
#include <mpi.h>
...
int main (int argc, char *argv[])
{
...
// Enable parallel simulator with the command line arguments
MpiInterface::Enable (&argc, &argv);
uint32_t systemId = MpiInterface::GetSystemId ();
uint32_t systemCount = MpiInterface::GetSize ();
...
//启动应用
PsWorkerApp (systemId);
...
// Exit the MPI execution environment
MpiInterface::Disable ();
...
}
void PsWorkerApp (uint32_t systemId){
for(uint32_t i=numofHost/10;i<numofHost;i++){
...
if (systemId == 1)
{
Address sinkLocalAddress (InetSocketAddress (Ipv4Address::GetAny (), port[desttemp]));
PacketSinkHelper sinkHelper ("ns3::UdpSocketFactory", sinkLocalAddress);
ApplicationContainer sinkApp = sinkHelper.Install (hostNodes.Get (desttemp));
sinkApp.Start (Seconds (next_time));
sinkApp.Stop (Seconds (10.0));
}
if (systemId == 0)
{
OnOffHelper clientHelper1 ("ns3::UdpSocketFactory", Address ());
clientHelper1.SetAttribute ("OnTime", StringValue (onfilename));
clientHelper1.SetAttribute ("OffTime", StringValue (offfilename));
clientHelper1.SetAttribute ("DataRate", DataRateValue (DataRate ("10Gbps")));
clientHelper1.SetAttribute ("PacketSize", UintegerValue (1024));
clientHelper1.SetAttribute ("Totaltime", DoubleValue (on));
clientHelper1.SetAttribute ("Stoptime", DoubleValue (run_time));
ApplicationContainer clientApps1;
AddressValue remoteAddress (InetSocketAddress (ipEH[desttemp].GetAddress (1), port[desttemp]));
clientHelper1.SetAttribute ("Remote", remoteAddress);
clientApps1.Add (clientHelper1.Install (hostNodes.Get (i)));
clientApps1.Start (Seconds (next_time));
clientApps1.Stop (Seconds (10.0));
}
...
}