A Discrete-Event Network Simulator
API
null-message-simulator-impl.cc
Go to the documentation of this file.
1 /*
2  * Copyright 2013. Lawrence Livermore National Security, LLC.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation;
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  *
17  * Author: Steven Smith <smith84@llnl.gov>
18  */
19 
27 
28 #include "mpi-interface.h"
31 #include "remote-channel-bundle.h"
32 
33 #include <ns3/assert.h>
34 #include <ns3/channel.h>
35 #include <ns3/double.h>
36 #include <ns3/event-impl.h>
37 #include <ns3/log.h>
38 #include <ns3/node-container.h>
39 #include <ns3/pointer.h>
40 #include <ns3/ptr.h>
41 #include <ns3/scheduler.h>
42 #include <ns3/simulator.h>
43 
44 #include <cmath>
45 #include <fstream>
46 #include <iomanip>
47 #include <iostream>
48 
49 namespace ns3
50 {
51 
52 NS_LOG_COMPONENT_DEFINE("NullMessageSimulatorImpl");
53 
54 NS_OBJECT_ENSURE_REGISTERED(NullMessageSimulatorImpl);
55 
56 NullMessageSimulatorImpl* NullMessageSimulatorImpl::g_instance = nullptr;
57 
58 TypeId
60 {
61  static TypeId tid =
62  TypeId("ns3::NullMessageSimulatorImpl")
64  .SetGroupName("Mpi")
65  .AddConstructor<NullMessageSimulatorImpl>()
66  .AddAttribute("SchedulerTune",
67  "Null Message scheduler tuning parameter",
68  DoubleValue(1.0),
70  MakeDoubleChecker<double>(0.01, 1.0));
71  return tid;
72 }
73 
75 {
76  NS_LOG_FUNCTION(this);
77 
80 
81  m_stop = false;
84  m_currentTs = 0;
87  m_eventCount = 0;
88  m_events = nullptr;
89 
90  m_safeTime = Seconds(0);
91 
92  NS_ASSERT(g_instance == nullptr);
93  g_instance = this;
94 }
95 
97 {
98  NS_LOG_FUNCTION(this);
99 }
100 
101 void
103 {
104  NS_LOG_FUNCTION(this);
105 
106  while (!m_events->IsEmpty())
107  {
108  Scheduler::Event next = m_events->RemoveNext();
109  next.impl->Unref();
110  }
111  m_events = nullptr;
113 }
114 
115 void
117 {
118  NS_LOG_FUNCTION(this);
119 
120  while (!m_destroyEvents.empty())
121  {
122  Ptr<EventImpl> ev = m_destroyEvents.front().PeekEventImpl();
123  m_destroyEvents.pop_front();
124  NS_LOG_LOGIC("handle destroy " << ev);
125  if (!ev->IsCancelled())
126  {
127  ev->Invoke();
128  }
129  }
130 
133 }
134 
135 void
137 {
138  NS_LOG_FUNCTION(this);
139 
140  if (MpiInterface::GetSize() > 1)
141  {
143  for (NodeContainer::Iterator iter = c.Begin(); iter != c.End(); ++iter)
144  {
145  if ((*iter)->GetSystemId() != MpiInterface::GetSystemId())
146  {
147  continue;
148  }
149 
150  for (uint32_t i = 0; i < (*iter)->GetNDevices(); ++i)
151  {
152  Ptr<NetDevice> localNetDevice = (*iter)->GetDevice(i);
153  // only works for p2p links currently
154  if (!localNetDevice->IsPointToPoint())
155  {
156  continue;
157  }
158  Ptr<Channel> channel = localNetDevice->GetChannel();
159  if (!channel)
160  {
161  continue;
162  }
163 
164  // grab the adjacent node
165  Ptr<Node> remoteNode;
166  if (channel->GetDevice(0) == localNetDevice)
167  {
168  remoteNode = (channel->GetDevice(1))->GetNode();
169  }
170  else
171  {
172  remoteNode = (channel->GetDevice(0))->GetNode();
173  }
174 
175  // if it's not remote, don't consider it
176  if (remoteNode->GetSystemId() == MpiInterface::GetSystemId())
177  {
178  continue;
179  }
180 
185  Ptr<RemoteChannelBundle> remoteChannelBundle =
187  if (!remoteChannelBundle)
188  {
189  remoteChannelBundle =
191  }
192 
193  TimeValue delay;
194  channel->GetAttribute("Delay", delay);
195  remoteChannelBundle->AddChannel(channel, delay.Get());
196  }
197  }
198  }
199 
200  // Completed setup of remote channel bundles. Setup send and receive buffers.
202 
203  // Initialized to 0 as we don't have a simulation start time.
204  m_safeTime = Time(0);
205 }
206 
207 void
209 {
210  NS_LOG_FUNCTION(this << schedulerFactory);
211 
212  Ptr<Scheduler> scheduler = schedulerFactory.Create<Scheduler>();
213 
214  if (m_events)
215  {
216  while (!m_events->IsEmpty())
217  {
218  Scheduler::Event next = m_events->RemoveNext();
219  scheduler->Insert(next);
220  }
221  }
222  m_events = scheduler;
223 }
224 
225 void
227 {
228  NS_LOG_FUNCTION(this);
229 
230  Scheduler::Event next = m_events->RemoveNext();
231 
232  PreEventHook(EventId(next.impl, next.key.m_ts, next.key.m_context, next.key.m_uid));
233 
234  NS_ASSERT(next.key.m_ts >= m_currentTs);
236  m_eventCount++;
237 
238  NS_LOG_LOGIC("handle " << next.key.m_ts);
239  m_currentTs = next.key.m_ts;
241  m_currentUid = next.key.m_uid;
242  next.impl->Invoke();
243  next.impl->Unref();
244 }
245 
246 bool
248 {
249  return m_events->IsEmpty() || m_stop;
250 }
251 
252 Time
254 {
255  NS_LOG_FUNCTION(this);
256 
257  NS_ASSERT(!m_events->IsEmpty());
258 
259  Scheduler::Event ev = m_events->PeekNext();
260  return TimeStep(ev.key.m_ts);
261 }
262 
263 void
265 {
266  NS_LOG_FUNCTION(this << bundle);
267 
268  Time delay(m_schedulerTune * bundle->GetDelay().GetTimeStep());
269 
270  bundle->SetEventId(Simulator::Schedule(delay,
272  this,
273  PeekPointer(bundle)));
274 }
275 
276 void
278 {
279  NS_LOG_FUNCTION(this << bundle);
280 
281  Simulator::Cancel(bundle->GetEventId());
282 
283  Time delay(m_schedulerTune * bundle->GetDelay().GetTimeStep());
284 
285  bundle->SetEventId(Simulator::Schedule(delay,
287  this,
288  PeekPointer(bundle)));
289 }
290 
291 void
293 {
294  NS_LOG_FUNCTION(this << nodeSysId);
295 
297  NS_ASSERT(bundle);
298 
300 }
301 
302 void
304 {
305  NS_LOG_FUNCTION(this);
306 
308 
310 
311  // Stop will be set if stop is called by simulation.
312  m_stop = false;
313  while (!IsFinished())
314  {
315  Time nextTime = Next();
316 
317  if (nextTime <= GetSafeTime())
318  {
319  ProcessOneEvent();
321  }
322  else
323  {
324  // Block until packet or Null Message has been received.
326  }
327  }
328 }
329 
330 void
332 {
333  NS_LOG_FUNCTION(this);
334 
336 
338 
339  // Check for send completes
341 }
342 
343 void
345 {
346  NS_LOG_FUNCTION(this);
347 
349 
351 
352  // Check for send completes
354 }
355 
356 void
358 {
359  NS_LOG_FUNCTION(this);
360 
363 }
364 
365 Time
367 {
368  return m_safeTime;
369 }
370 
371 uint32_t
373 {
374  return m_myId;
375 }
376 
377 void
379 {
380  NS_LOG_FUNCTION(this);
381 
382  m_stop = true;
383 }
384 
385 void
387 {
388  NS_LOG_FUNCTION(this << delay.GetTimeStep());
389 
391 }
392 
393 //
394 // Schedule an event for a _relative_ time in the future.
395 //
396 EventId
398 {
399  NS_LOG_FUNCTION(this << delay.GetTimeStep() << event);
400 
401  Time tAbsolute = delay + TimeStep(m_currentTs);
402 
403  NS_ASSERT(tAbsolute.IsPositive());
404  NS_ASSERT(tAbsolute >= TimeStep(m_currentTs));
405  Scheduler::Event ev;
406  ev.impl = event;
407  ev.key.m_ts = static_cast<uint64_t>(tAbsolute.GetTimeStep());
408  ev.key.m_context = GetContext();
409  ev.key.m_uid = m_uid;
410  m_uid++;
412  m_events->Insert(ev);
413  return EventId(event, ev.key.m_ts, ev.key.m_context, ev.key.m_uid);
414 }
415 
416 void
417 NullMessageSimulatorImpl::ScheduleWithContext(uint32_t context, const Time& delay, EventImpl* event)
418 {
419  NS_LOG_FUNCTION(this << context << delay.GetTimeStep() << m_currentTs << event);
420 
421  Time tAbsolute(m_currentTs + delay.GetTimeStep());
422 
423  NS_ASSERT(tAbsolute.IsPositive());
424  NS_ASSERT(tAbsolute >= TimeStep(m_currentTs));
425 
426  Scheduler::Event ev;
427  ev.impl = event;
428  ev.key.m_ts = tAbsolute.GetTimeStep();
429  ev.key.m_context = context;
430  ev.key.m_uid = m_uid;
431  m_uid++;
433  m_events->Insert(ev);
434 }
435 
436 EventId
438 {
439  NS_LOG_FUNCTION(this << event);
440  return Schedule(Time(0), event);
441 }
442 
443 EventId
445 {
446  NS_LOG_FUNCTION(this << event);
447 
448  EventId id(Ptr<EventImpl>(event, false), m_currentTs, 0xffffffff, 2);
449  m_destroyEvents.push_back(id);
450  m_uid++;
451  return id;
452 }
453 
454 Time
456 {
457  return TimeStep(m_currentTs);
458 }
459 
460 Time
462 {
463  if (IsExpired(id))
464  {
465  return TimeStep(0);
466  }
467  else
468  {
469  return TimeStep(id.GetTs() - m_currentTs);
470  }
471 }
472 
473 void
475 {
476  if (id.GetUid() == EventId::UID::DESTROY)
477  {
478  // destroy events.
479  for (DestroyEvents::iterator i = m_destroyEvents.begin(); i != m_destroyEvents.end(); i++)
480  {
481  if (*i == id)
482  {
483  m_destroyEvents.erase(i);
484  break;
485  }
486  }
487  return;
488  }
489  if (IsExpired(id))
490  {
491  return;
492  }
493  Scheduler::Event event;
494  event.impl = id.PeekEventImpl();
495  event.key.m_ts = id.GetTs();
496  event.key.m_context = id.GetContext();
497  event.key.m_uid = id.GetUid();
498  m_events->Remove(event);
499  event.impl->Cancel();
500  // whenever we remove an event from the event list, we have to unref it.
501  event.impl->Unref();
502 
504 }
505 
506 void
508 {
509  if (!IsExpired(id))
510  {
511  id.PeekEventImpl()->Cancel();
512  }
513 }
514 
515 bool
517 {
518  if (id.GetUid() == EventId::UID::DESTROY)
519  {
520  if (id.PeekEventImpl() == nullptr || id.PeekEventImpl()->IsCancelled())
521  {
522  return true;
523  }
524  // destroy events.
525  for (DestroyEvents::const_iterator i = m_destroyEvents.begin(); i != m_destroyEvents.end();
526  i++)
527  {
528  if (*i == id)
529  {
530  return false;
531  }
532  }
533  return true;
534  }
535  if (id.PeekEventImpl() == nullptr || id.GetTs() < m_currentTs ||
536  (id.GetTs() == m_currentTs && id.GetUid() <= m_currentUid) ||
537  id.PeekEventImpl()->IsCancelled())
538  {
539  return true;
540  }
541  else
542  {
543  return false;
544  }
545 }
546 
547 Time
549 {
550  // XXX: I am fairly certain other compilers use other non-standard
551  // post-fixes to indicate 64 bit constants.
552  return TimeStep(0x7fffffffffffffffLL);
553 }
554 
555 uint32_t
557 {
558  return m_currentContext;
559 }
560 
561 uint64_t
563 {
564  return m_eventCount;
565 }
566 
567 Time
569 {
571  NS_ASSERT(bundle);
572 
573  return Min(NullMessageSimulatorImpl::GetInstance()->Next(), GetSafeTime()) + bundle->GetDelay();
574 }
575 
576 void
578 {
579  NS_LOG_FUNCTION(this << bundle);
580 
581  Time time = Min(Next(), GetSafeTime()) + bundle->GetDelay();
583 
584  ScheduleNullMessageEvent(bundle);
585 }
586 
589 {
590  NS_ASSERT(g_instance != nullptr);
591  return g_instance;
592 }
593 } // namespace ns3
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition: double.h:42
An identifier for simulation events.
Definition: event-id.h:55
A simulation event.
Definition: event-impl.h:46
void Invoke()
Called by the simulation engine to notify the event that it is time to execute.
Definition: event-impl.cc:47
static void Destroy()
Deletes storage used by the parallel environment.
static uint32_t GetSystemId()
Get the id number of this rank.
static uint32_t GetSize()
Get the number of ranks used by ns-3.
keep track of a set of node pointers.
Iterator End() const
Get an iterator which indicates past-the-last Node in the container.
static NodeContainer GetGlobal()
Create a NodeContainer that contains a list of all nodes created through NodeContainer::Create() and ...
std::vector< Ptr< Node > >::const_iterator Iterator
Node container iterator.
Iterator Begin() const
Get an iterator which refers to the first Node in the container.
uint32_t GetSystemId() const
Definition: node.cc:131
static void ReceiveMessagesBlocking()
Blocking message receive.
static void ReceiveMessagesNonBlocking()
Non-blocking check for received messages complete.
static void SendNullMessage(const Time &guaranteeUpdate, Ptr< RemoteChannelBundle > bundle)
Send a Null Message to across the specified bundle.
static void TestSendComplete()
Check for completed sends.
static void InitializeSendReceiveBuffers()
Initialize send and receive buffers.
Simulator implementation using MPI and a Null Message algorithm.
void CalculateLookAhead()
Calculate the lookahead allowable for this MPI task.
double m_schedulerTune
Null Message performance tuning parameter.
Ptr< Scheduler > m_events
The event priority queue.
void Remove(const EventId &id) override
Remove an event from the event list.
uint32_t m_currentUid
Unique id of the current event.
Time GetDelayLeft(const EventId &id) const override
Get the remaining time until this event will execute.
void HandleArrivingMessagesBlocking()
Blocking receive of arriving messages.
uint32_t m_systemCount
MPI communicator size.
void Run() override
Run the simulation.
void HandleArrivingMessagesNonBlocking()
Non blocking receive of pending messages.
uint32_t m_currentContext
Execution context of the current event.
void DoDispose() override
Destructor implementation.
uint32_t GetSystemId() const override
Get the system id of this simulator.
static NullMessageSimulatorImpl * GetInstance()
Time m_safeTime
The time for which it is safe for this task to execute events without danger of out-of-order events.
bool IsExpired(const EventId &id) const override
Check if an event has already run or been cancelled.
EventId ScheduleDestroy(EventImpl *event) override
Schedule an event to run at the end of the simulation, after the Stop() time or condition has been re...
Time CalculateGuaranteeTime(uint32_t systemId)
static NullMessageSimulatorImpl * g_instance
Singleton instance.
void ProcessOneEvent()
Process the next event on the queue.
uint32_t m_uid
Next event unique id.
static TypeId GetTypeId()
Register this type.
uint64_t GetEventCount() const override
Get the number of events executed.
void SetScheduler(ObjectFactory schedulerFactory) override
Set the Scheduler to be used to manage the event list.
uint64_t m_currentTs
Timestamp of the current event.
Time GetMaximumSimulationTime() const override
Get the maximum representable simulation time.
void ScheduleWithContext(uint32_t context, const Time &delay, EventImpl *event) override
Schedule a future event execution (in a different context).
void NullMessageEventHandler(RemoteChannelBundle *bundle)
DestroyEvents m_destroyEvents
The container of events to run at Destroy()
void Cancel(const EventId &id) override
Set the cancel bit on this event: the event's associated function will not be invoked when it expires...
EventId Schedule(const Time &delay, EventImpl *event) override
Schedule a future event execution (in the same context).
uint32_t GetContext() const override
Get the current simulation context.
Time GetSafeTime()
Get the current SafeTime; the maximum time that events can be processed based on information received...
int m_unscheduledEvents
Number of events that have been inserted but not yet scheduled, not counting the "destroy" events; th...
bool IsFinished() const override
Check if the simulation should finish.
void Stop() override
Tell the Simulator the calling event should be the last one executed.
void RescheduleNullMessageEvent(Ptr< RemoteChannelBundle > bundle)
bool m_stop
Flag calling for the end of the simulation.
Time Now() const override
Return the current simulation virtual time.
void Destroy() override
Execute the events scheduled with ScheduleDestroy().
void CalculateSafeTime()
Calculate the SafeTime.
void ScheduleNullMessageEvent(Ptr< RemoteChannelBundle > bundle)
EventId ScheduleNow(EventImpl *event) override
Schedule an event to run at the current virtual time.
Instantiate subclasses of ns3::Object.
Ptr< Object > Create() const
Create an Object instance of the configured TypeId.
virtual void DoDispose()
Destructor implementation.
Definition: object.cc:353
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:78
Collection of ns-3 channels between local and remote nodes.
Time GetDelay() const
Get the minimum delay along any channel in this bundle.
static Time GetSafeTime()
Get the safe time across all channels in this bundle.
static Ptr< RemoteChannelBundle > Find(uint32_t systemId)
Get the bundle corresponding to a remote rank.
static void InitializeNullMessageEvents()
Setup initial Null Message events for every RemoteChannelBundle.
static Ptr< RemoteChannelBundle > Add(uint32_t systemId)
Add RemoteChannelBundle from this task to MPI task on other side of the link.
static void Destroy()
Destroy the singleton.
Maintain the event list.
Definition: scheduler.h:157
void Unref() const
Decrement the reference count.
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:568
static void Cancel(const EventId &id)
Set the cancel bit on this event: the event's associated function will not be invoked when it expires...
Definition: simulator.cc:276
@ NO_CONTEXT
Flag for events not associated with any particular context.
Definition: simulator.h:202
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:184
The SimulatorImpl base class.
virtual void PreEventHook(const EventId &id)
Hook called before processing each event.
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
bool IsPositive() const
Exactly equivalent to t >= 0.
Definition: nstime.h:332
int64_t GetTimeStep() const
Get the raw time value, in the current resolution unit.
Definition: nstime.h:444
AttributeValue implementation for Time.
Definition: nstime.h:1423
Time Get() const
Definition: time.cc:532
a unique identifier for an interface.
Definition: type-id.h:60
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:935
@ INVALID
INVALID.
Definition: aodv-rtable.h:53
@ VALID
VALID.
Definition: aodv-rtable.h:52
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition: assert.h:66
Ptr< const AttributeAccessor > MakeDoubleAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method.
Definition: double.h:43
int64x64_t Min(const int64x64_t &a, const int64x64_t &b)
Minimum.
Definition: int64x64.h:229
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition: log.h:282
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:46
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1336
Declaration of class ns3::MpiInterface.
void(* Time)(Time oldValue, Time newValue)
TracedValue callback signature for Time.
Definition: nstime.h:848
Every class exported by the ns3 library is enclosed in the ns3 namespace.
U * PeekPointer(const Ptr< U > &p)
Definition: ptr.h:488
channel
Definition: third.py:81
Declaration of classes ns3::NullMessageSentBuffer and ns3::NullMessageMpiInterface.
Declaration of class ns3::NullMessageSimulatorImpl.
Declaration of class ns3::RemoteChannelBundleManager.
Declaration of class ns3::RemoteChannelBundle.
Scheduler event.
Definition: scheduler.h:184
EventKey key
Key for sorting and ordering Events.
Definition: scheduler.h:186
EventImpl * impl
Pointer to the event implementation.
Definition: scheduler.h:185
uint32_t m_context
Event context.
Definition: scheduler.h:173
uint64_t m_ts
Event time stamp.
Definition: scheduler.h:171
uint32_t m_uid
Event unique id.
Definition: scheduler.h:172