A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
distributed-simulator-impl.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License version 2 as
5  * published by the Free Software Foundation;
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software
14  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15  *
16  * Author: George Riley <riley@ece.gatech.edu>
17  *
18  */
19 
22 #include "mpi-interface.h"
23 
24 #include "ns3/simulator.h"
25 #include "ns3/scheduler.h"
26 #include "ns3/event-impl.h"
27 #include "ns3/channel.h"
28 #include "ns3/node-container.h"
29 #include "ns3/ptr.h"
30 #include "ns3/pointer.h"
31 #include "ns3/assert.h"
32 #include "ns3/log.h"
33 
34 #include <cmath>
35 
36 #ifdef NS3_MPI
37 #include <mpi.h>
38 #endif
39 
40 NS_LOG_COMPONENT_DEFINE ("DistributedSimulatorImpl");
41 
42 namespace ns3 {
43 
44 NS_OBJECT_ENSURE_REGISTERED (DistributedSimulatorImpl);
45 
47 {
48 }
49 
50 Time
52 {
53  return m_smallestTime;
54 }
55 
56 uint32_t
58 {
59  return m_txCount;
60 }
61 
62 uint32_t
64 {
65  return m_rxCount;
66 }
67 uint32_t
69 {
70  return m_myId;
71 }
72 
73 bool
75 {
76  return m_isFinished;
77 }
78 
80 
81 TypeId
83 {
84  static TypeId tid = TypeId ("ns3::DistributedSimulatorImpl")
85  .SetParent<Object> ()
86  .AddConstructor<DistributedSimulatorImpl> ()
87  ;
88  return tid;
89 }
90 
92 {
93  NS_LOG_FUNCTION (this);
94 
95 #ifdef NS3_MPI
98 
99  // Allocate the LBTS message buffer
101  m_grantedTime = Seconds (0);
102 #else
104  NS_FATAL_ERROR ("Can't use distributed simulator without MPI compiled in");
105 #endif
106 
107  m_stop = false;
108  m_globalFinished = false;
109  // uids are allocated from 4.
110  // uid 0 is "invalid" events
111  // uid 1 is "now" events
112  // uid 2 is "destroy" events
113  m_uid = 4;
114  // before ::Run is entered, the m_currentUid will be zero
115  m_currentUid = 0;
116  m_currentTs = 0;
117  m_currentContext = 0xffffffff;
119  m_events = 0;
120 }
121 
123 {
124  NS_LOG_FUNCTION (this);
125 }
126 
127 void
129 {
130  NS_LOG_FUNCTION (this);
131 
132  while (!m_events->IsEmpty ())
133  {
134  Scheduler::Event next = m_events->RemoveNext ();
135  next.impl->Unref ();
136  }
137  m_events = 0;
138  delete [] m_pLBTS;
140 }
141 
142 void
144 {
145  NS_LOG_FUNCTION (this);
146 
147  while (!m_destroyEvents.empty ())
148  {
149  Ptr<EventImpl> ev = m_destroyEvents.front ().PeekEventImpl ();
150  m_destroyEvents.pop_front ();
151  NS_LOG_LOGIC ("handle destroy " << ev);
152  if (!ev->IsCancelled ())
153  {
154  ev->Invoke ();
155  }
156  }
157 
159 }
160 
161 
162 void
164 {
165  NS_LOG_FUNCTION (this);
166 
167 #ifdef NS3_MPI
168  if (MpiInterface::GetSize () <= 1)
169  {
171  m_grantedTime = Seconds (0);
172  }
173  else
174  {
176 
178 
180  for (NodeContainer::Iterator iter = c.Begin (); iter != c.End (); ++iter)
181  {
182  if ((*iter)->GetSystemId () != MpiInterface::GetSystemId ())
183  {
184  continue;
185  }
186 
187  for (uint32_t i = 0; i < (*iter)->GetNDevices (); ++i)
188  {
189  Ptr<NetDevice> localNetDevice = (*iter)->GetDevice (i);
190  // only works for p2p links currently
191  if (!localNetDevice->IsPointToPoint ())
192  {
193  continue;
194  }
195  Ptr<Channel> channel = localNetDevice->GetChannel ();
196  if (channel == 0)
197  {
198  continue;
199  }
200 
201  // grab the adjacent node
202  Ptr<Node> remoteNode;
203  if (channel->GetDevice (0) == localNetDevice)
204  {
205  remoteNode = (channel->GetDevice (1))->GetNode ();
206  }
207  else
208  {
209  remoteNode = (channel->GetDevice (0))->GetNode ();
210  }
211 
212  // if it's not remote, don't consider it
213  if (remoteNode->GetSystemId () == MpiInterface::GetSystemId ())
214  {
215  continue;
216  }
217 
218  // compare delay on the channel with current value of
219  // m_lookAhead. if delay on channel is smaller, make
220  // it the new lookAhead.
221  TimeValue delay;
222  channel->GetAttribute ("Delay", delay);
223 
225  {
227  m_grantedTime = delay.Get ();
228  }
229  }
230  }
231  }
232 
233  /*
234  * Compute the maximum inter-task latency and use that value
235  * for tasks with no inter-task links.
236  *
237  * Special processing for edge cases. For tasks that have no
238  * nodes need to determine a reasonable lookAhead value. Infinity
239  * would work correctly but introduces a performance issue; tasks
240  * with an infinite lookAhead would execute all their events
241  * before doing an AllGather resulting in very bad load balance
242  * during the first time window. Since all tasks participate in
243  * the AllGather it is desirable to have all the tasks advance in
244  * simulation time at a similar rate assuming roughly equal events
245  * per unit of simulation time in order to equalize the amount of
246  * work per time window.
247  */
248  long sendbuf;
249  long recvbuf;
250 
251  /* Tasks with no inter-task links do not contribute to max */
253  {
254  sendbuf = 0;
255  }
256  else
257  {
258  sendbuf = m_lookAhead.GetInteger ();
259  }
260 
261  MPI_Allreduce (&sendbuf, &recvbuf, 1, MPI_LONG, MPI_MAX, MPI_COMM_WORLD);
262 
263  /* For nodes that did not compute a lookahead use max from ranks
264  * that did compute a value. An edge case occurs if all nodes have
265  * no inter-task links (max will be 0 in this case). Use infinity so all tasks
266  * will proceed without synchronization until a single AllGather
267  * occurs when all tasks have finished.
268  */
269  if (m_lookAhead == GetMaximumSimulationTime () && recvbuf != 0)
270  {
271  m_lookAhead = Time (recvbuf);
273  }
274 
275 #else
276  NS_FATAL_ERROR ("Can't use distributed simulator without MPI compiled in");
277 #endif
278 }
279 
280 void
282 {
283  NS_LOG_FUNCTION (this << schedulerFactory);
284 
285  Ptr<Scheduler> scheduler = schedulerFactory.Create<Scheduler> ();
286 
287  if (m_events != 0)
288  {
289  while (!m_events->IsEmpty ())
290  {
291  Scheduler::Event next = m_events->RemoveNext ();
292  scheduler->Insert (next);
293  }
294  }
295  m_events = scheduler;
296 }
297 
298 void
300 {
301  NS_LOG_FUNCTION (this);
302 
303  Scheduler::Event next = m_events->RemoveNext ();
304 
305  NS_ASSERT (next.key.m_ts >= m_currentTs);
307 
308  NS_LOG_LOGIC ("handle " << next.key.m_ts);
309  m_currentTs = next.key.m_ts;
311  m_currentUid = next.key.m_uid;
312  next.impl->Invoke ();
313  next.impl->Unref ();
314 }
315 
316 bool
318 {
319  return m_globalFinished;
320 }
321 
322 bool
324 {
325  return m_events->IsEmpty () || m_stop;
326 }
327 
328 uint64_t
330 {
331  // If local MPI task is has no more events or stop was called
332  // next event time is infinity.
333  if (IsLocalFinished ())
334  {
336  }
337  else
338  {
339  Scheduler::Event ev = m_events->PeekNext ();
340  return ev.key.m_ts;
341  }
342 }
343 
344 Time
346 {
347  return TimeStep (NextTs ());
348 }
349 
350 void
352 {
353  NS_LOG_FUNCTION (this);
354 
355 #ifdef NS3_MPI
357  m_stop = false;
358  while (!m_globalFinished)
359  {
360  Time nextTime = Next ();
361 
362  // If local event is beyond grantedTime then need to synchronize
363  // with other tasks to determine new time window. If local task
364  // is finished then continue to participate in allgather
365  // synchronizations with other tasks until all tasks have
366  // completed.
367  if (nextTime > m_grantedTime || IsLocalFinished () )
368  {
369  // Can't process next event, calculate a new LBTS
370  // First receive any pending messages
372  // reset next time
373  nextTime = Next ();
374  // And check for send completes
376  // Finally calculate the lbts
378  m_myId, IsLocalFinished (), nextTime);
379  m_pLBTS[m_myId] = lMsg;
380  MPI_Allgather (&lMsg, sizeof (LbtsMessage), MPI_BYTE, m_pLBTS,
381  sizeof (LbtsMessage), MPI_BYTE, MPI_COMM_WORLD);
382  Time smallestTime = m_pLBTS[0].GetSmallestTime ();
383  // The totRx and totTx counts insure there are no transient
384  // messages; If totRx != totTx, there are transients,
385  // so we don't update the granted time.
386  uint32_t totRx = m_pLBTS[0].GetRxCount ();
387  uint32_t totTx = m_pLBTS[0].GetTxCount ();
389 
390  for (uint32_t i = 1; i < m_systemCount; ++i)
391  {
392  if (m_pLBTS[i].GetSmallestTime () < smallestTime)
393  {
394  smallestTime = m_pLBTS[i].GetSmallestTime ();
395  }
396  totRx += m_pLBTS[i].GetRxCount ();
397  totTx += m_pLBTS[i].GetTxCount ();
399  }
400  if (totRx == totTx)
401  {
402  // If lookahead is infinite then granted time should be as well.
403  // Covers the edge case if all the tasks have no inter tasks
404  // links, prevents overflow of granted time.
406  {
408  }
409  else
410  {
411  // Overflow is possible here if near end of representable time.
412  m_grantedTime = smallestTime + m_lookAhead;
413  }
414  }
415  }
416 
417  // Execute next event if it is within the current time window.
418  // Local task may be completed.
419  if ( (nextTime <= m_grantedTime) && (!IsLocalFinished ()) )
420  { // Safe to process
421  ProcessOneEvent ();
422  }
423  }
424 
425  // If the simulator stopped naturally by lack of events, make a
426  // consistency test to check that we didn't lose any events along the way.
427  NS_ASSERT (!m_events->IsEmpty () || m_unscheduledEvents == 0);
428 #else
429  NS_FATAL_ERROR ("Can't use distributed simulator without MPI compiled in");
430 #endif
431 }
432 
434 {
435  return m_myId;
436 }
437 
438 void
440 {
441  NS_LOG_FUNCTION (this);
442 
443  m_stop = true;
444 }
445 
446 void
448 {
449  NS_LOG_FUNCTION (this << time.GetTimeStep ());
450 
452 }
453 
454 //
455 // Schedule an event for a _relative_ time in the future.
456 //
457 EventId
459 {
460  NS_LOG_FUNCTION (this << time.GetTimeStep () << event);
461 
462  Time tAbsolute = time + TimeStep (m_currentTs);
463 
464  NS_ASSERT (tAbsolute.IsPositive ());
465  NS_ASSERT (tAbsolute >= TimeStep (m_currentTs));
466  Scheduler::Event ev;
467  ev.impl = event;
468  ev.key.m_ts = static_cast<uint64_t> (tAbsolute.GetTimeStep ());
469  ev.key.m_context = GetContext ();
470  ev.key.m_uid = m_uid;
471  m_uid++;
473  m_events->Insert (ev);
474  return EventId (event, ev.key.m_ts, ev.key.m_context, ev.key.m_uid);
475 }
476 
477 void
478 DistributedSimulatorImpl::ScheduleWithContext (uint32_t context, Time const &time, EventImpl *event)
479 {
480  NS_LOG_FUNCTION (this << context << time.GetTimeStep () << m_currentTs << event);
481 
482  Scheduler::Event ev;
483  ev.impl = event;
484  ev.key.m_ts = m_currentTs + time.GetTimeStep ();
485  ev.key.m_context = context;
486  ev.key.m_uid = m_uid;
487  m_uid++;
489  m_events->Insert (ev);
490 }
491 
492 EventId
494 {
495  NS_LOG_FUNCTION (this << event);
496 
497  Scheduler::Event ev;
498  ev.impl = event;
499  ev.key.m_ts = m_currentTs;
500  ev.key.m_context = GetContext ();
501  ev.key.m_uid = m_uid;
502  m_uid++;
504  m_events->Insert (ev);
505  return EventId (event, ev.key.m_ts, ev.key.m_context, ev.key.m_uid);
506 }
507 
508 EventId
510 {
511  NS_LOG_FUNCTION (this << event);
512 
513  EventId id (Ptr<EventImpl> (event, false), m_currentTs, 0xffffffff, 2);
514  m_destroyEvents.push_back (id);
515  m_uid++;
516  return id;
517 }
518 
519 Time
521 {
522  return TimeStep (m_currentTs);
523 }
524 
525 Time
527 {
528  if (IsExpired (id))
529  {
530  return TimeStep (0);
531  }
532  else
533  {
534  return TimeStep (id.GetTs () - m_currentTs);
535  }
536 }
537 
538 void
540 {
541  if (id.GetUid () == 2)
542  {
543  // destroy events.
544  for (DestroyEvents::iterator i = m_destroyEvents.begin (); i != m_destroyEvents.end (); i++)
545  {
546  if (*i == id)
547  {
548  m_destroyEvents.erase (i);
549  break;
550  }
551  }
552  return;
553  }
554  if (IsExpired (id))
555  {
556  return;
557  }
558  Scheduler::Event event;
559  event.impl = id.PeekEventImpl ();
560  event.key.m_ts = id.GetTs ();
561  event.key.m_context = id.GetContext ();
562  event.key.m_uid = id.GetUid ();
563  m_events->Remove (event);
564  event.impl->Cancel ();
565  // whenever we remove an event from the event list, we have to unref it.
566  event.impl->Unref ();
567 
569 }
570 
571 void
573 {
574  if (!IsExpired (id))
575  {
576  id.PeekEventImpl ()->Cancel ();
577  }
578 }
579 
580 bool
582 {
583  if (ev.GetUid () == 2)
584  {
585  if (ev.PeekEventImpl () == 0
586  || ev.PeekEventImpl ()->IsCancelled ())
587  {
588  return true;
589  }
590  // destroy events.
591  for (DestroyEvents::const_iterator i = m_destroyEvents.begin (); i != m_destroyEvents.end (); i++)
592  {
593  if (*i == ev)
594  {
595  return false;
596  }
597  }
598  return true;
599  }
600  if (ev.PeekEventImpl () == 0
601  || ev.GetTs () < m_currentTs
602  || (ev.GetTs () == m_currentTs
603  && ev.GetUid () <= m_currentUid)
604  || ev.PeekEventImpl ()->IsCancelled ())
605  {
606  return true;
607  }
608  else
609  {
610  return false;
611  }
612 }
613 
614 Time
616 {
619  return TimeStep (0x7fffffffffffffffLL);
620 }
621 
622 uint32_t
624 {
625  return m_currentContext;
626 }
627 
628 } // namespace ns3
Time Get(void) const
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:79
smart pointer class similar to boost::intrusive_ptr
Definition: ptr.h:60
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
virtual void SetScheduler(ObjectFactory schedulerFactory)
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register the class in the ns-3 factory.
Definition: object-base.h:38
std::vector< Ptr< Node > >::const_iterator Iterator
Node container iterator.
Time TimeStep(uint64_t ts)
Definition: nstime.h:997
EventImpl * impl
Definition: scheduler.h:73
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file...
Definition: assert.h:61
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:170
virtual EventId ScheduleDestroy(EventImpl *event)
virtual Time GetMaximumSimulationTime(void) const
Iterator End(void) const
Get an iterator which indicates past-the-last Node in the container.
bool IsCancelled(void)
Definition: event-impl.cc:57
virtual void DoDispose(void)
This method is called by Object::Dispose or by the object's destructor, whichever comes first...
Definition: object.cc:335
#define NS_FATAL_ERROR(msg)
fatal error handling
Definition: fatal-error.h:95
static EventId Schedule(Time const &time, MEM mem_ptr, OBJ obj)
Schedule an event to expire at the relative time "time" is reached.
Definition: simulator.h:825
EventImpl * PeekEventImpl(void) const
Definition: event-id.cc:65
uint32_t GetSystemId(void) const
Definition: node.cc:113
virtual Time Now(void) const
Return the "current simulation time".
virtual void DoDispose(void)
This method is called by Object::Dispose or by the object's destructor, whichever comes first...
void Invoke(void)
Called by the simulation engine to notify the event that it has expired.
Definition: event-impl.cc:40
static void TestSendComplete()
Check for completed sends.
double GetSeconds(void) const
Definition: nstime.h:272
virtual Time GetDelayLeft(const EventId &id) const
static void Destroy()
Deletes storage used by the parallel environment.
hold objects of type ns3::Time
Definition: nstime.h:1008
Ptr< Object > Create(void) const
virtual EventId ScheduleNow(EventImpl *event)
void Unref(void) const
Decrement the reference count.
uint32_t GetUid(void) const
Definition: event-id.cc:83
Maintain the event list.
Definition: scheduler.h:58
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition: log.h:233
virtual void Remove(const EventId &ev)
Remove an event from the event list.
keep track of a set of node pointers.
virtual void Destroy()
This method is typically invoked at the end of a simulation to avoid false-positive reports by a leak...
Iterator Begin(void) const
Get an iterator which refers to the first Node in the container.
virtual void ScheduleWithContext(uint32_t context, Time const &time, EventImpl *event)
int64_t GetTimeStep(void) const
Definition: nstime.h:354
int64_t GetInteger(void) const
Definition: nstime.h:362
virtual uint32_t GetSystemId(void) const
virtual uint32_t GetContext(void) const
Structure used for all-reduce LBTS computation.
static NodeContainer GetGlobal(void)
Create a NodeContainer that contains a list of all nodes created through NodeContainer::Create() and ...
virtual void Cancel(const EventId &ev)
Set the cancel bit on this event: the event's associated function will not be invoked when it expires...
instantiate subclasses of ns3::Object.
a simulation event
Definition: event-impl.h:39
virtual EventId Schedule(Time const &time, EventImpl *event)
static void ReceiveMessages()
Check for received messages complete.
an identifier for simulation events.
Definition: event-id.h:46
static uint32_t GetSystemId()
static void Stop(void)
If an event invokes this method, it will be the last event scheduled by the Simulator::run method bef...
Definition: simulator.cc:165
#define NS_UNUSED(x)
Definition: unused.h:5
a base class which provides memory management and object aggregation
Definition: object.h:64
virtual void Run(void)
Run the simulation until one of:
virtual void Stop(void)
If an event invokes this method, it will be the last event scheduled by the Simulator::Run method bef...
virtual bool IsFinished(void) const
If there are no more events lefts to be scheduled, or if simulation time has already reached the "sto...
a unique identifier for an interface.
Definition: type-id.h:49
TypeId SetParent(TypeId tid)
Definition: type-id.cc:610
static uint32_t GetSize()
uint64_t GetTs(void) const
Definition: event-id.cc:71
virtual bool IsExpired(const EventId &ev) const
This method has O(1) complexity.