A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
simple-distributed-empty-node.cc
Go to the documentation of this file.
1/*
2 * Copyright 2013. Lawrence Livermore National Security, LLC.
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Steven Smith <smith84@llnl.gov>
7 */
8
9/**
10 * \file
11 * \ingroup mpi
12 *
13 * This test is equivalent to simple-distributed but tests boundary cases
14 * when one of the ranks has no Nodes on it. When run on two tasks
15 * rank 0 will have all the Nodes and rank 1 will be empty:
16 *
17 * ------- -------
18 * RANK 0 RANK 0
19 * ------- | -------
20 * |
21 * n0 ---------| | |---------- n6
22 * | | |
23 * n1 -------\ | | | /------- n7
24 * n4 ----------|---------- n5
25 * n2 -------/ | | | \------- n8
26 * | | |
27 * n3 ---------| | |---------- n9
28 *
29 *
30 * When run on three tasks rank 1 has the left half of the Nodes and rank 2
31 * will be empty.
32 *
33 * ------- -------
34 * RANK 0 RANK 1
35 * ------- | -------
36 * |
37 * n0 ---------| | |---------- n6
38 * | | |
39 * n1 -------\ | | | /------- n7
40 * n4 ----------|---------- n5
41 * n2 -------/ | | | \------- n8
42 * | | |
43 * n3 ---------| | |---------- n9
44 *
45 * OnOff clients are placed on each left leaf node. Each right leaf node
46 * is a packet sink for a left leaf node. As a packet travels from one
47 * logical processor to another (the link between n4 and n5), MPI messages
48 * are passed containing the serialized packet. The message is then
49 * deserialized into a new packet and sent on as normal.
50 *
51 * One packet is sent from each left leaf node. The packet sinks on the
52 * right leaf nodes output logging information when they receive the packet.
53 */
54
55#include "mpi-test-fixtures.h"
56
57#include "ns3/core-module.h"
58#include "ns3/internet-stack-helper.h"
59#include "ns3/ipv4-address-helper.h"
60#include "ns3/ipv4-global-routing-helper.h"
61#include "ns3/mpi-interface.h"
62#include "ns3/network-module.h"
63#include "ns3/nix-vector-helper.h"
64#include "ns3/on-off-helper.h"
65#include "ns3/packet-sink-helper.h"
66#include "ns3/point-to-point-helper.h"
67
68#include <mpi.h>
69
70using namespace ns3;
71
72NS_LOG_COMPONENT_DEFINE("SimpleDistributedEmptyNode");
73
74int
75main(int argc, char* argv[])
76{
77 bool nix = true;
78 bool nullmsg = false;
79 bool tracing = false;
80 bool testing = false;
81 bool verbose = false;
82
83 // Parse command line
84 CommandLine cmd(__FILE__);
85 cmd.AddValue("nix", "Enable the use of nix-vector or global routing", nix);
86 cmd.AddValue("nullmsg", "Enable the use of null-message synchronization", nullmsg);
87 cmd.AddValue("tracing", "Enable pcap tracing", tracing);
88 cmd.AddValue("verbose", "verbose output", verbose);
89 cmd.AddValue("test", "Enable regression test output", testing);
90 cmd.Parse(argc, argv);
91
92 // Distributed simulation setup; by default use granted time window algorithm.
93 if (nullmsg)
94 {
95 GlobalValue::Bind("SimulatorImplementationType",
96 StringValue("ns3::NullMessageSimulatorImpl"));
97 }
98 else
99 {
100 GlobalValue::Bind("SimulatorImplementationType",
101 StringValue("ns3::DistributedSimulatorImpl"));
102 }
103
104 MpiInterface::Enable(&argc, &argv);
105
107
108 if (verbose)
109 {
110 LogComponentEnable("PacketSink",
112 }
113
115 uint32_t systemCount = MpiInterface::GetSize();
116
117 uint32_t rightHalfSystemId = 777;
118
119 // Check for valid distributed parameters.
120 // Must have 2 or 3 tasks.
121 if (systemCount == 2)
122 {
123 rightHalfSystemId = 0;
124 }
125 else if (systemCount == 3)
126 {
127 rightHalfSystemId = 1;
128 }
129 else
130 {
131 std::cout << "This simulation requires 2 or 3 logical processors." << std::endl;
132 return 1;
133 }
134
135 // Some default values
136 Config::SetDefault("ns3::OnOffApplication::PacketSize", UintegerValue(512));
137 Config::SetDefault("ns3::OnOffApplication::DataRate", StringValue("1Mbps"));
138 Config::SetDefault("ns3::OnOffApplication::MaxBytes", UintegerValue(512));
139
140 // Create leaf nodes on left with system id 0
141 NodeContainer leftLeafNodes;
142 leftLeafNodes.Create(4, 0);
143
144 // Create router nodes. Left router with system id 0, right router
145 // with system id dependent on number of processors using
146 // rightHalfSystemId
147 NodeContainer routerNodes;
148 Ptr<Node> routerNode1 = CreateObject<Node>(0);
149 Ptr<Node> routerNode2 = CreateObject<Node>(rightHalfSystemId);
150 routerNodes.Add(routerNode1);
151 routerNodes.Add(routerNode2);
152
153 // Create leaf nodes on left with system id rightHalfSystemId
154 NodeContainer rightLeafNodes;
155 rightLeafNodes.Create(4, rightHalfSystemId);
156
157 PointToPointHelper routerLink;
158 routerLink.SetDeviceAttribute("DataRate", StringValue("5Mbps"));
159 routerLink.SetChannelAttribute("Delay", StringValue("5ms"));
160
161 PointToPointHelper leafLink;
162 leafLink.SetDeviceAttribute("DataRate", StringValue("1Mbps"));
163 leafLink.SetChannelAttribute("Delay", StringValue("2ms"));
164
165 // Add link connecting routers
166 NetDeviceContainer routerDevices;
167 routerDevices = routerLink.Install(routerNodes);
168
169 // Add links for left side leaf nodes to left router
170 NetDeviceContainer leftRouterDevices;
171 NetDeviceContainer leftLeafDevices;
172 for (uint32_t i = 0; i < 4; ++i)
173 {
174 NetDeviceContainer temp = leafLink.Install(leftLeafNodes.Get(i), routerNodes.Get(0));
175 leftLeafDevices.Add(temp.Get(0));
176 leftRouterDevices.Add(temp.Get(1));
177 }
178
179 // Add links for right side leaf nodes to right router
180 NetDeviceContainer rightRouterDevices;
181 NetDeviceContainer rightLeafDevices;
182 for (uint32_t i = 0; i < 4; ++i)
183 {
184 NetDeviceContainer temp = leafLink.Install(rightLeafNodes.Get(i), routerNodes.Get(1));
185 rightLeafDevices.Add(temp.Get(0));
186 rightRouterDevices.Add(temp.Get(1));
187 }
188
190 if (nix)
191 {
193 stack.SetRoutingHelper(nixRouting); // has effect on the next Install ()
194 }
195
196 stack.InstallAll();
197
198 Ipv4InterfaceContainer routerInterfaces;
199 Ipv4InterfaceContainer leftLeafInterfaces;
200 Ipv4InterfaceContainer leftRouterInterfaces;
201 Ipv4InterfaceContainer rightLeafInterfaces;
202 Ipv4InterfaceContainer rightRouterInterfaces;
203
204 Ipv4AddressHelper leftAddress;
205 leftAddress.SetBase("10.1.1.0", "255.255.255.0");
206
207 Ipv4AddressHelper routerAddress;
208 routerAddress.SetBase("10.2.1.0", "255.255.255.0");
209
210 Ipv4AddressHelper rightAddress;
211 rightAddress.SetBase("10.3.1.0", "255.255.255.0");
212
213 // Router-to-Router interfaces
214 routerInterfaces = routerAddress.Assign(routerDevices);
215
216 // Left interfaces
217 for (uint32_t i = 0; i < 4; ++i)
218 {
220 ndc.Add(leftLeafDevices.Get(i));
221 ndc.Add(leftRouterDevices.Get(i));
222 Ipv4InterfaceContainer ifc = leftAddress.Assign(ndc);
223 leftLeafInterfaces.Add(ifc.Get(0));
224 leftRouterInterfaces.Add(ifc.Get(1));
225 leftAddress.NewNetwork();
226 }
227
228 // Right interfaces
229 for (uint32_t i = 0; i < 4; ++i)
230 {
232 ndc.Add(rightLeafDevices.Get(i));
233 ndc.Add(rightRouterDevices.Get(i));
234 Ipv4InterfaceContainer ifc = rightAddress.Assign(ndc);
235 rightLeafInterfaces.Add(ifc.Get(0));
236 rightRouterInterfaces.Add(ifc.Get(1));
237 rightAddress.NewNetwork();
238 }
239
240 if (!nix)
241 {
243 }
244
245 if (tracing)
246 {
247 if (systemId == 0)
248 {
249 routerLink.EnablePcap("router-left", routerDevices, true);
250 leafLink.EnablePcap("leaf-left", leftLeafDevices, true);
251 }
252
253 if (systemId == rightHalfSystemId)
254 {
255 routerLink.EnablePcap("router-right", routerDevices, true);
256 leafLink.EnablePcap("leaf-right", rightLeafDevices, true);
257 }
258 }
259
260 // Create a packet sink on the right leafs to receive packets from left leafs
261 uint16_t port = 50000;
262 if (systemId == rightHalfSystemId)
263 {
265 PacketSinkHelper sinkHelper("ns3::UdpSocketFactory", sinkLocalAddress);
266 ApplicationContainer sinkApp;
267 for (uint32_t i = 0; i < 4; ++i)
268 {
269 sinkApp.Add(sinkHelper.Install(rightLeafNodes.Get(i)));
270 if (testing)
271 {
272 sinkApp.Get(i)->TraceConnectWithoutContext("RxWithAddresses",
274 }
275 }
276 sinkApp.Start(Seconds(1.0));
277 sinkApp.Stop(Seconds(5));
278 }
279
280 // Create the OnOff applications to send
281 if (systemId == 0)
282 {
283 OnOffHelper clientHelper("ns3::UdpSocketFactory", Address());
284 clientHelper.SetAttribute("OnTime", StringValue("ns3::ConstantRandomVariable[Constant=1]"));
285 clientHelper.SetAttribute("OffTime",
286 StringValue("ns3::ConstantRandomVariable[Constant=0]"));
287
289 for (uint32_t i = 0; i < 4; ++i)
290 {
292 clientHelper.SetAttribute("Remote", remoteAddress);
293 clientApps.Add(clientHelper.Install(leftLeafNodes.Get(i)));
294 }
295 clientApps.Start(Seconds(1.0));
296 clientApps.Stop(Seconds(5));
297 }
298
302
303 if (testing)
304 {
306 }
307
308 // Exit the MPI execution environment
310 return 0;
311}
a polymophic address class
Definition address.h:90
holds a vector of ns3::Application pointers.
void Start(Time start) const
Start all of the Applications in this container at the start time given as a parameter.
Ptr< Application > Get(uint32_t i) const
Get the Ptr<Application> stored in this container at a given index.
void Stop(Time stop) const
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter.
void Add(ApplicationContainer other)
Append the contents of another ApplicationContainer to the end of this container.
Parse command-line arguments.
static void Bind(std::string name, const AttributeValue &value)
Iterate over the set of GlobalValues until a matching name is found and then set its value with Globa...
an Inet address class
aggregate IP/TCP/UDP functionality to existing Nodes.
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
void SetBase(Ipv4Address network, Ipv4Mask mask, Ipv4Address base="0.0.0.1")
Set the base network number, network mask and base address.
Ipv4Address NewNetwork()
Increment the network number and reset the IP address counter to the base value provided in the SetBa...
Ipv4InterfaceContainer Assign(const NetDeviceContainer &c)
Assign IP addresses to the net devices specified in the container based on the current network prefix...
static Ipv4Address GetAny()
static void PopulateRoutingTables()
Build a routing database and initialize the routing tables of the nodes in the simulation.
holds a vector of std::pair of Ptr<Ipv4> and interface index.
std::pair< Ptr< Ipv4 >, uint32_t > Get(uint32_t i) const
Get the std::pair of an Ptr<Ipv4> and interface stored at the location specified by the index.
void Add(const Ipv4InterfaceContainer &other)
Concatenate the entries in the other container with ours.
Ipv4Address GetAddress(uint32_t i, uint32_t j=0) const
static uint32_t GetSystemId()
Get the id number of this rank.
static uint32_t GetSize()
Get the number of ranks used by ns-3.
static void Disable()
Clean up the ns-3 parallel communications interface.
static void Enable(int *pargc, char ***pargv)
Setup the parallel communication interface.
holds a vector of ns3::NetDevice pointers
void Add(NetDeviceContainer other)
Append the contents of another NetDeviceContainer to the end of this container.
Ptr< NetDevice > Get(uint32_t i) const
Get the Ptr<NetDevice> stored in this container at a given index.
Helper class that adds Nix-vector routing to nodes.
keep track of a set of node pointers.
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
void Add(const NodeContainer &nc)
Append the contents of another NodeContainer to the end of this container.
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
A helper to make it easier to instantiate an ns3::OnOffApplication on a set of nodes.
A helper to make it easier to instantiate an ns3::PacketSinkApplication on a set of nodes.
void EnablePcap(std::string prefix, Ptr< NetDevice > nd, bool promiscuous=false, bool explicitFilename=false)
Enable pcap output the indicated net device.
Build a set of PointToPointNetDevice objects.
void SetDeviceAttribute(std::string name, const AttributeValue &value)
Set an attribute value to be propagated to each NetDevice created by the helper.
void SetChannelAttribute(std::string name, const AttributeValue &value)
Set an attribute value to be propagated to each Channel created by the helper.
NetDeviceContainer Install(NodeContainer c)
Smart pointer class similar to boost::intrusive_ptr.
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:131
static void Run()
Run the simulation.
Definition simulator.cc:167
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition simulator.cc:175
static void SinkTrace(const ns3::Ptr< const ns3::Packet > packet, const ns3::Address &srcAddress, const ns3::Address &destAddress)
PacketSink receive trace callback.
static void Verify(unsigned long expectedCount)
Verify the sink trace count observed matches the expected count.
static void Init()
PacketSink Init.
Hold variables of type string.
Definition string.h:45
Hold an unsigned integer type.
Definition uinteger.h:34
uint16_t port
Definition dsdv-manet.cc:33
void SetDefault(std::string name, const AttributeValue &value)
Definition config.cc:883
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:619
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1308
Common methods for MPI examples.
clientApps
Definition first.py:53
stack
Definition first.py:33
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void LogComponentEnable(const std::string &name, LogLevel level)
Enable the logging output associated with that log component.
Definition log.cc:291
Callback< R, Args... > MakeCallback(R(T::*memPtr)(Args...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition callback.h:684
LogLevel
Logging severity classes and levels.
Definition log.h:83
@ LOG_PREFIX_TIME
Prefix all trace prints with simulation time.
Definition log.h:108
@ LOG_PREFIX_NODE
Prefix all trace prints with simulation node.
Definition log.h:109
@ LOG_LEVEL_INFO
LOG_INFO and above.
Definition log.h:93
bool verbose
bool tracing
Flag to enable/disable generation of tracing files.