【ns-3】OpenMPI安装与ns-3的分布式配置及运行

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));
      }
    ...
}


### ns3 网络仿真集群配置部署 #### 准备工作 为了在集群环境中成功设置并运行 NS-3 (Network Simulator 3),需先确保所有计算节点上均正确安装了必要的依赖项以及NS-3本身。这通常涉及Linux操作系统下的软件包管理工具,比如`apt-get`用于Ubuntu系统。 对于Java环境的需求,在某些特定场景下可能也需要考虑,例如当模拟涉及到CloudSim或其他基于Java的应用程序集成时[^1]。然而,标准的NS-3并不强制要求JRE的存在。 #### 安装NS-3 建议从官方源码库获取最新版本的NS-3,并按照官方文档中的指导完成编译和安装过程。此过程中需要注意的是要保持各个节点上的NS-3版本一致,以避免因不同步而导致的问题。 #### 配置MPI支持 为了让NS-3能够在分布式环境下高效运作,推荐启用Message Passing Interface(MPI)功能。MPI是一种广泛应用于高性能计算领域的通信协议,能够极大地提升大规模网络仿真的效率。可以通过如下命令来确认MPI是否已经正确安装: ```bash mpirun --version ``` 如果尚未安装,则应依据目标平台选择合适的MPI实现进行安装,如Open MPIMPICH等。 #### 编写适合集群执行的脚本 编写专门针对集群架构优化过的NS-3脚本至关重要。这些脚本不仅要定义好待研究的具体网络拓扑结构及其行为模式,还需特别注意如何利用MPI接口实现在多个处理器间的有效协作。下面是一个简单的例子,展示了怎样创建一个多主机间相互通信的基础框架: ```cpp #include "ns3/core-module.h" #include "ns3/network-module.h" #include "ns3/internet-module.h" #include "ns3/point-to-point-module.h" #include "ns3/applications-module.h" using namespace ns3; int main(int argc, char *argv[]) { CommandLine cmd(__FILE__); cmd.Parse(argc, argv); NodeContainer nodes; nodes.Create(2); // 创建两个节点 PointToPointHelper pointToPoint; NetDeviceContainer devices = pointToPoint.Install(nodes); InternetStackHelper stack; stack.InstallAll(); Ipv4AddressHelper address; address.SetBase("10.1.1.0", "255.255.255.0"); Ipv4InterfaceContainer interfaces = address.Assign(devices); UdpEchoServerHelper echoServer(9); ApplicationContainer serverApps = echoServer.Install(nodes.Get(1)); serverApps.Start(Seconds(1.0)); serverApps.Stop(Seconds(10.0)); UdpEchoClientHelper echoClient(interfaces.GetAddress(1), 9); echoClient.SetAttribute("MaxPackets", UintegerValue(1)); echoClient.SetAttribute("Interval", TimeValue(Seconds(1.0))); echoClient.SetAttribute("PacketSize", UintegerValue(1024)); ApplicationContainer clientApps = echoClient.Install(nodes.Get(0)); clientApps.Start(Seconds(2.0)); clientApps.Stop(Seconds(10.0)); Simulator::Run(); Simulator::Destroy(); return 0; } ``` 这段代码构建了一个非常基础的客户端-服务器模型,其中包含了两台设备之间的简单UDP回声请求响应机制。实际项目中可能会更加复杂,具体取决于所关注的研究方向。 #### 提交作业至集群管理系统 最后一步就是将上述准备工作打包成可以提交给集群调度系统的任务文件。大多数情况下这意味着撰写SLURM、PBS Pro或是SGE这样的批处理指令集。这里给出一个适用于SLURM的例子: ```slurm #!/bin/bash #SBATCH --job-name=ns3_simulation # Job name #SBATCH --output=output.log # Log file path #SBATCH --error=error.log # Error log file path #SBATCH --ntasks=4 # Number of tasks to run #SBATCH --time=01:00:00 # Wall clock limit HH:MM:SS #SBATCH --mem-per-cpu=2G # Memory per CPU core module load mpi/openmpi-x86_64 # Load required modules srun mpirun ./waf --run "scratch/simulation-script" # Run simulation script with MPI support ``` 以上即为一套完整的关于如何在一个典型的HPC(High Performance Computing)平台上布置NS-3仿真实验的方法论介绍。当然,具体的实施细节会根据不同的硬件设施和个人需求有所调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值