A Discrete-Event Network Simulator
API
granted-time-window-mpi-interface.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 
20 // This object contains static methods that provide an easy interface
21 // to the necessary MPI information.
22 
23 #include <iostream>
24 #include <iomanip>
25 #include <list>
26 
28 #include "mpi-receiver.h"
29 #include "mpi-interface.h"
30 
31 #include "ns3/node.h"
32 #include "ns3/node-list.h"
33 #include "ns3/net-device.h"
34 #include "ns3/simulator.h"
35 #include "ns3/simulator-impl.h"
36 #include "ns3/nstime.h"
37 #include "ns3/log.h"
38 
39 #include <mpi.h>
40 
41 namespace ns3 {
42 
43 NS_LOG_COMPONENT_DEFINE ("GrantedTimeWindowMpiInterface");
44 
46 {
47  m_buffer = 0;
48  m_request = 0;
49 }
50 
52 {
53  delete [] m_buffer;
54 }
55 
56 uint8_t*
58 {
59  return m_buffer;
60 }
61 
62 void
63 SentBuffer::SetBuffer (uint8_t* buffer)
64 {
65  m_buffer = buffer;
66 }
67 
68 MPI_Request*
70 {
71  return &m_request;
72 }
73 
80 std::list<SentBuffer> GrantedTimeWindowMpiInterface::m_pendingTx;
81 
84 
85 TypeId
87 {
88  static TypeId tid = TypeId ("ns3::GrantedTimeWindowMpiInterface")
89  .SetParent<Object> ()
90  .SetGroupName ("Mpi")
91  ;
92  return tid;
93 }
94 
95 void
97 {
98  NS_LOG_FUNCTION (this);
99 
100  for (uint32_t i = 0; i < GetSize (); ++i)
101  {
102  delete [] m_pRxBuffers[i];
103  }
104  delete [] m_pRxBuffers;
105  delete [] m_requests;
106 
107  m_pendingTx.clear ();
108 }
109 
110 uint32_t
112 {
113  return m_rxCount;
114 }
115 
116 uint32_t
118 {
119  return m_txCount;
120 }
121 
122 uint32_t
124 {
125  if (!m_initialized)
126  {
128  m_initialized = true;
129  }
130  return m_sid;
131 }
132 
133 uint32_t
135 {
136  if (!m_initialized)
137  {
139  m_initialized = true;
140  }
141  return m_size;
142 }
143 
144 bool
146 {
147  if (!m_initialized)
148  {
150  m_initialized = true;
151  }
152  return m_enabled;
153 }
154 
155 void
156 GrantedTimeWindowMpiInterface::Enable (int* pargc, char*** pargv)
157 {
158  NS_LOG_FUNCTION (this << pargc << pargv);
159 
160  // Initialize the MPI interface
161  MPI_Init (pargc, pargv);
162  MPI_Barrier (MPI_COMM_WORLD);
163  MPI_Comm_rank (MPI_COMM_WORLD, reinterpret_cast <int *> (&m_sid));
164  MPI_Comm_size (MPI_COMM_WORLD, reinterpret_cast <int *> (&m_size));
165  m_enabled = true;
166  m_initialized = true;
167  // Post a non-blocking receive for all peers
168  m_pRxBuffers = new char*[m_size];
169  m_requests = new MPI_Request[m_size];
170  for (uint32_t i = 0; i < GetSize (); ++i)
171  {
172  m_pRxBuffers[i] = new char[MAX_MPI_MSG_SIZE];
173  MPI_Irecv (m_pRxBuffers[i], MAX_MPI_MSG_SIZE, MPI_CHAR, MPI_ANY_SOURCE, 0,
174  MPI_COMM_WORLD, &m_requests[i]);
175  }
176 }
177 
178 void
179 GrantedTimeWindowMpiInterface::SendPacket (Ptr<Packet> p, const Time& rxTime, uint32_t node, uint32_t dev)
180 {
181  NS_LOG_FUNCTION (this << p << rxTime.GetTimeStep () << node << dev);
182 
183  SentBuffer sendBuf;
184  m_pendingTx.push_back (sendBuf);
185  std::list<SentBuffer>::reverse_iterator i = m_pendingTx.rbegin (); // Points to the last element
186 
187  uint32_t serializedSize = p->GetSerializedSize ();
188  uint8_t* buffer = new uint8_t[serializedSize + 16];
189  i->SetBuffer (buffer);
190  // Add the time, dest node and dest device
191  uint64_t t = rxTime.GetInteger ();
192  uint64_t* pTime = reinterpret_cast <uint64_t *> (buffer);
193  *pTime++ = t;
194  uint32_t* pData = reinterpret_cast<uint32_t *> (pTime);
195  *pData++ = node;
196  *pData++ = dev;
197  // Serialize the packet
198  p->Serialize (reinterpret_cast<uint8_t *> (pData), serializedSize);
199 
200  // Find the system id for the destination node
201  Ptr<Node> destNode = NodeList::GetNode (node);
202  uint32_t nodeSysId = destNode->GetSystemId ();
203 
204  MPI_Isend (reinterpret_cast<void *> (i->GetBuffer ()), serializedSize + 16, MPI_CHAR, nodeSysId,
205  0, MPI_COMM_WORLD, (i->GetRequest ()));
206  m_txCount++;
207 }
208 
209 void
211 {
213 
214  // Poll the non-block reads to see if data arrived
215  while (true)
216  {
217  int flag = 0;
218  int index = 0;
219  MPI_Status status;
220 
221  MPI_Testany (MpiInterface::GetSize (), m_requests, &index, &flag, &status);
222  if (!flag)
223  {
224  break; // No more messages
225  }
226  int count;
227  MPI_Get_count (&status, MPI_CHAR, &count);
228  m_rxCount++; // Count this receive
229 
230  // Get the meta data first
231  uint64_t* pTime = reinterpret_cast<uint64_t *> (m_pRxBuffers[index]);
232  uint64_t time = *pTime++;
233  uint32_t* pData = reinterpret_cast<uint32_t *> (pTime);
234  uint32_t node = *pData++;
235  uint32_t dev = *pData++;
236 
237  Time rxTime (time);
238 
239  count -= sizeof (time) + sizeof (node) + sizeof (dev);
240 
241  Ptr<Packet> p = Create<Packet> (reinterpret_cast<uint8_t *> (pData), count, true);
242 
243  // Find the correct node/device to schedule receive event
244  Ptr<Node> pNode = NodeList::GetNode (node);
245  Ptr<MpiReceiver> pMpiRec = 0;
246  uint32_t nDevices = pNode->GetNDevices ();
247  for (uint32_t i = 0; i < nDevices; ++i)
248  {
249  Ptr<NetDevice> pThisDev = pNode->GetDevice (i);
250  if (pThisDev->GetIfIndex () == dev)
251  {
252  pMpiRec = pThisDev->GetObject<MpiReceiver> ();
253  break;
254  }
255  }
256 
257  NS_ASSERT (pNode && pMpiRec);
258 
259  // Schedule the rx event
260  Simulator::ScheduleWithContext (pNode->GetId (), rxTime - Simulator::Now (),
261  &MpiReceiver::Receive, pMpiRec, p);
262 
263  // Re-queue the next read
264  MPI_Irecv (m_pRxBuffers[index], MAX_MPI_MSG_SIZE, MPI_CHAR, MPI_ANY_SOURCE, 0,
265  MPI_COMM_WORLD, &m_requests[index]);
266  }
267 }
268 
269 void
271 {
273 
274  std::list<SentBuffer>::iterator i = m_pendingTx.begin ();
275  while (i != m_pendingTx.end ())
276  {
277  MPI_Status status;
278  int flag = 0;
279  MPI_Test (i->GetRequest (), &flag, &status);
280  std::list<SentBuffer>::iterator current = i; // Save current for erasing
281  i++; // Advance to next
282  if (flag)
283  { // This message is complete
284  m_pendingTx.erase (current);
285  }
286  }
287 }
288 
289 void
291 {
293 
294  int flag = 0;
295  MPI_Initialized (&flag);
296  if (flag)
297  {
298  MPI_Finalize ();
299  m_enabled = false;
300  m_initialized = false;
301  }
302  else
303  {
304  NS_FATAL_ERROR ("Cannot disable MPI environment without Initializing it first");
305  }
306 }
307 
308 
309 } // namespace ns3
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:103
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
uint32_t GetId(void) const
Definition: node.cc:109
int64_t GetInteger(void) const
Get the raw time value, in the current resolution unit.
Definition: nstime.h:423
static Ptr< SimulatorImpl > GetImplementation(void)
Get the SimulatorImpl singleton.
Definition: simulator.cc:353
static Ptr< Node > GetNode(uint32_t n)
Definition: node-list.cc:241
Ptr< NetDevice > GetDevice(uint32_t index) const
Retrieve the index-th NetDevice associated to this node.
Definition: node.cc:144
uint32_t GetSerializedSize(void) const
Returns number of bytes required for packet serialization.
Definition: packet.cc:585
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file...
Definition: assert.h:67
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition: fatal-error.h:165
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
virtual void Enable(int *pargc, char ***pargv)
static void TestSendComplete()
Check for completed sends.
void Receive(Ptr< Packet > p)
Direct an incoming packet to the device Receive() method.
Definition: mpi-receiver.cc:44
Tracks non-blocking sends.
Class to aggregate to a NetDevice if it supports MPI capability.
Definition: mpi-receiver.h:42
static void ScheduleWithContext(uint32_t context, Time const &delay, FUNC f, Ts &&... args)
Schedule an event with the given context.
Definition: simulator.h:572
Every class exported by the ns3 library is enclosed in the ns3 namespace.
uint32_t GetSystemId(void) const
Definition: node.cc:123
static Time Now(void)
Return the current simulation virtual time.
Definition: simulator.cc:195
virtual void Disable()
Terminates the MPI environment by calling MPI_Finalize This function must be called after Destroy () ...
virtual void SendPacket(Ptr< Packet > p, const Time &rxTime, uint32_t node, uint32_t dev)
static void ReceiveMessages()
Check for received messages complete.
const uint32_t MAX_MPI_MSG_SIZE
maximum MPI message size for easy buffer creation
A base class which provides memory management and object aggregation.
Definition: object.h:87
a unique identifier for an interface.
Definition: type-id.h:58
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:923
static uint32_t GetSize()
uint32_t GetNDevices(void) const
Definition: node.cc:152
uint32_t Serialize(uint8_t *buffer, uint32_t maxSize) const
Serialize a packet, tags, and metadata into a byte buffer.
Definition: packet.cc:638
int64_t GetTimeStep(void) const
Get the raw time value, in the current resolution unit.
Definition: nstime.h:415