author | Peter D. Barnes, Jr. <barnes26@llnl.gov> |
Fri, 26 Sep 2014 15:51:00 -0700 | |
changeset 10968 | 2d29fee2b7b8 |
parent 10632 | e5e6f104f627 |
permissions | -rw-r--r-- |
10476 | 1 |
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
2 |
/* |
|
3 |
* Copyright 2013. Lawrence Livermore National Security, LLC. |
|
4 |
* |
|
5 |
* This program is free software; you can redistribute it and/or modify |
|
6 |
* it under the terms of the GNU General Public License version 2 as |
|
7 |
* published by the Free Software Foundation; |
|
8 |
* |
|
9 |
* This program is distributed in the hope that it will be useful, |
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 |
* GNU General Public License for more details. |
|
13 |
* |
|
14 |
* You should have received a copy of the GNU General Public License |
|
15 |
* along with this program; if not, write to the Free Software |
|
16 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
17 |
* |
|
18 |
* Author: Steven Smith <smith84@llnl.gov> |
|
19 |
* |
|
20 |
*/ |
|
21 |
||
22 |
#include "null-message-mpi-interface.h" |
|
23 |
||
24 |
#include "null-message-simulator-impl.h" |
|
25 |
#include "remote-channel-bundle-manager.h" |
|
26 |
#include "remote-channel-bundle.h" |
|
27 |
||
28 |
#include "ns3/mpi-receiver.h" |
|
29 |
#include "ns3/node.h" |
|
30 |
#include "ns3/node-list.h" |
|
31 |
#include "ns3/net-device.h" |
|
32 |
#include "ns3/nstime.h" |
|
33 |
#include "ns3/simulator.h" |
|
34 |
#include "ns3/log.h" |
|
35 |
||
36 |
#ifdef NS3_MPI |
|
37 |
#include <mpi.h> |
|
38 |
#endif |
|
39 |
||
40 |
#include <iostream> |
|
41 |
#include <iomanip> |
|
42 |
#include <list> |
|
43 |
||
10968
2d29fee2b7b8
[Bug 1551] Redux: NS_LOG_COMPONENT_DEFINE inside or outside of ns3 namespace?
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
10632
diff
changeset
|
44 |
namespace ns3 { |
10476 | 45 |
|
10968
2d29fee2b7b8
[Bug 1551] Redux: NS_LOG_COMPONENT_DEFINE inside or outside of ns3 namespace?
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
10632
diff
changeset
|
46 |
NS_LOG_COMPONENT_DEFINE ("NullMessageMpiInterface"); |
10476 | 47 |
|
48 |
/** |
|
49 |
* maximum MPI message size for easy |
|
50 |
* buffer creation |
|
51 |
*/ |
|
10632
e5e6f104f627
Bug 1859 clang unused variable
Brian Swenson <bswenson3@gatech.edu>
parents:
10594
diff
changeset
|
52 |
#ifdef NS3_MPI |
10476 | 53 |
const uint32_t NULL_MESSAGE_MAX_MPI_MSG_SIZE = 2000; |
10632
e5e6f104f627
Bug 1859 clang unused variable
Brian Swenson <bswenson3@gatech.edu>
parents:
10594
diff
changeset
|
54 |
#endif |
10476 | 55 |
|
56 |
NullMessageSentBuffer::NullMessageSentBuffer () |
|
57 |
{ |
|
58 |
m_buffer = 0; |
|
59 |
m_request = 0; |
|
60 |
} |
|
61 |
||
62 |
NullMessageSentBuffer::~NullMessageSentBuffer () |
|
63 |
{ |
|
64 |
delete [] m_buffer; |
|
65 |
} |
|
66 |
||
67 |
uint8_t* |
|
68 |
NullMessageSentBuffer::GetBuffer () |
|
69 |
{ |
|
70 |
return m_buffer; |
|
71 |
} |
|
72 |
||
73 |
void |
|
74 |
NullMessageSentBuffer::SetBuffer (uint8_t* buffer) |
|
75 |
{ |
|
76 |
m_buffer = buffer; |
|
77 |
} |
|
78 |
||
79 |
MPI_Request* |
|
80 |
NullMessageSentBuffer::GetRequest () |
|
81 |
{ |
|
82 |
return &m_request; |
|
83 |
} |
|
84 |
||
85 |
uint32_t NullMessageMpiInterface::g_sid = 0; |
|
86 |
uint32_t NullMessageMpiInterface::g_size = 1; |
|
87 |
uint32_t NullMessageMpiInterface::g_numNeighbors = 0; |
|
88 |
bool NullMessageMpiInterface::g_initialized = false; |
|
89 |
bool NullMessageMpiInterface::g_enabled = false; |
|
90 |
std::list<NullMessageSentBuffer> NullMessageMpiInterface::g_pendingTx; |
|
91 |
||
92 |
MPI_Request* NullMessageMpiInterface::g_requests; |
|
93 |
char** NullMessageMpiInterface::g_pRxBuffers; |
|
94 |
||
95 |
NullMessageMpiInterface::NullMessageMpiInterface () |
|
96 |
{ |
|
97 |
NS_LOG_FUNCTION (this); |
|
98 |
||
99 |
#ifndef NS3_MPI |
|
100 |
/* |
|
101 |
* This class can only be constructed if MPI is available. Fail if an |
|
102 |
* attempt is made to instantiate this class without MPI. |
|
103 |
*/ |
|
104 |
NS_FATAL_ERROR ("Must compile with MPI if Null Message simulator is used, see --enable-mpi option for waf"); |
|
105 |
#endif |
|
106 |
} |
|
107 |
||
108 |
NullMessageMpiInterface::~NullMessageMpiInterface () |
|
109 |
{ |
|
110 |
NS_LOG_FUNCTION (this); |
|
111 |
} |
|
112 |
||
113 |
void |
|
114 |
NullMessageMpiInterface::Destroy () |
|
115 |
{ |
|
116 |
NS_LOG_FUNCTION (this); |
|
117 |
} |
|
118 |
||
119 |
uint32_t |
|
120 |
NullMessageMpiInterface::GetSystemId () |
|
121 |
{ |
|
122 |
NS_ASSERT (g_enabled); |
|
123 |
return g_sid; |
|
124 |
} |
|
125 |
||
126 |
uint32_t |
|
127 |
NullMessageMpiInterface::GetSize () |
|
128 |
{ |
|
129 |
NS_ASSERT (g_enabled); |
|
130 |
return g_size; |
|
131 |
} |
|
132 |
||
133 |
bool |
|
134 |
NullMessageMpiInterface::IsEnabled () |
|
135 |
{ |
|
136 |
if (!g_initialized) |
|
137 |
{ |
|
138 |
Simulator::GetImplementation (); |
|
139 |
g_initialized = true; |
|
140 |
} |
|
141 |
return g_enabled; |
|
142 |
} |
|
143 |
||
144 |
void |
|
145 |
NullMessageMpiInterface::Enable (int* pargc, char*** pargv) |
|
146 |
{ |
|
147 |
NS_LOG_FUNCTION (this << *pargc); |
|
148 |
#ifdef NS3_MPI |
|
149 |
||
150 |
// Initialize the MPI interface |
|
151 |
MPI_Init (pargc, pargv); |
|
152 |
MPI_Barrier (MPI_COMM_WORLD); |
|
153 |
||
154 |
// SystemId and Size are unit32_t in interface but MPI uses int so convert. |
|
155 |
int mpiSystemId; |
|
156 |
int mpiSize; |
|
157 |
MPI_Comm_rank (MPI_COMM_WORLD, &mpiSystemId); |
|
158 |
MPI_Comm_size (MPI_COMM_WORLD, &mpiSize); |
|
159 |
||
160 |
g_sid = mpiSystemId; |
|
161 |
g_size = mpiSize; |
|
162 |
||
163 |
g_enabled = true; |
|
164 |
g_initialized = true; |
|
165 |
||
166 |
#endif |
|
167 |
} |
|
168 |
||
169 |
void |
|
170 |
NullMessageMpiInterface::InitializeSendReceiveBuffers(void) |
|
171 |
{ |
|
172 |
NS_LOG_FUNCTION_NOARGS (); |
|
173 |
#ifdef NS3_MPI |
|
174 |
NS_ASSERT (g_enabled); |
|
175 |
||
176 |
g_numNeighbors = RemoteChannelBundleManager::Size(); |
|
177 |
||
178 |
// Post a non-blocking receive for all peers |
|
179 |
g_requests = new MPI_Request[g_numNeighbors]; |
|
180 |
g_pRxBuffers = new char*[g_numNeighbors]; |
|
181 |
int index = 0; |
|
182 |
for (uint32_t rank = 0; rank < g_size; ++rank) |
|
183 |
{ |
|
184 |
Ptr<RemoteChannelBundle> bundle = RemoteChannelBundleManager::Find(rank); |
|
185 |
if (bundle) |
|
186 |
{ |
|
187 |
g_pRxBuffers[index] = new char[NULL_MESSAGE_MAX_MPI_MSG_SIZE]; |
|
188 |
MPI_Irecv (g_pRxBuffers[index], NULL_MESSAGE_MAX_MPI_MSG_SIZE, MPI_CHAR, rank, 0, |
|
189 |
MPI_COMM_WORLD, &g_requests[index]); |
|
190 |
++index; |
|
191 |
} |
|
192 |
} |
|
193 |
#endif |
|
194 |
} |
|
195 |
||
196 |
void |
|
197 |
NullMessageMpiInterface::SendPacket (Ptr<Packet> p, const Time& rxTime, uint32_t node, uint32_t dev) |
|
198 |
{ |
|
199 |
NS_LOG_FUNCTION (this << p << rxTime.GetTimeStep () << node << dev); |
|
200 |
||
201 |
NS_ASSERT (g_enabled); |
|
202 |
||
203 |
#ifdef NS3_MPI |
|
204 |
||
205 |
// Find the system id for the destination node |
|
206 |
Ptr<Node> destNode = NodeList::GetNode (node); |
|
207 |
uint32_t nodeSysId = destNode->GetSystemId (); |
|
208 |
||
209 |
NullMessageSentBuffer sendBuf; |
|
210 |
g_pendingTx.push_back (sendBuf); |
|
211 |
std::list<NullMessageSentBuffer>::reverse_iterator iter = g_pendingTx.rbegin (); // Points to the last element |
|
212 |
||
213 |
uint32_t serializedSize = p->GetSerializedSize (); |
|
214 |
uint32_t bufferSize = serializedSize + ( 2 * sizeof (uint64_t) ) + ( 2 * sizeof (uint32_t) ); |
|
215 |
uint8_t* buffer = new uint8_t[bufferSize]; |
|
216 |
iter->SetBuffer (buffer); |
|
217 |
// Add the time, dest node and dest device |
|
218 |
uint64_t t = rxTime.GetInteger (); |
|
219 |
uint64_t* pTime = reinterpret_cast <uint64_t *> (buffer); |
|
220 |
*pTime++ = t; |
|
221 |
||
222 |
Time guarantee_update = NullMessageSimulatorImpl::GetInstance ()->CalculateGuaranteeTime (nodeSysId); |
|
223 |
*pTime++ = guarantee_update.GetTimeStep (); |
|
224 |
||
225 |
uint32_t* pData = reinterpret_cast<uint32_t *> (pTime); |
|
226 |
*pData++ = node; |
|
227 |
*pData++ = dev; |
|
228 |
// Serialize the packet |
|
229 |
p->Serialize (reinterpret_cast<uint8_t *> (pData), serializedSize); |
|
230 |
||
231 |
MPI_Isend (reinterpret_cast<void *> (iter->GetBuffer ()), bufferSize, MPI_CHAR, nodeSysId, |
|
232 |
0, MPI_COMM_WORLD, (iter->GetRequest ())); |
|
233 |
||
234 |
NullMessageSimulatorImpl::GetInstance ()->RescheduleNullMessageEvent (nodeSysId); |
|
235 |
||
236 |
#endif |
|
237 |
} |
|
238 |
||
239 |
void |
|
240 |
NullMessageMpiInterface::SendNullMessage (const Time& guarantee_update, Ptr<RemoteChannelBundle> bundle) |
|
241 |
{ |
|
242 |
NS_LOG_FUNCTION (guarantee_update.GetTimeStep () << bundle); |
|
243 |
||
244 |
NS_ASSERT (g_enabled); |
|
245 |
||
246 |
#ifdef NS3_MPI |
|
247 |
||
248 |
NullMessageSentBuffer sendBuf; |
|
249 |
g_pendingTx.push_back (sendBuf); |
|
250 |
std::list<NullMessageSentBuffer>::reverse_iterator iter = g_pendingTx.rbegin (); // Points to the last element |
|
251 |
||
252 |
uint32_t bufferSize = 2 * sizeof (uint64_t) + 2 * sizeof (uint32_t); |
|
253 |
uint8_t* buffer = new uint8_t[bufferSize]; |
|
254 |
iter->SetBuffer (buffer); |
|
255 |
// Add the time, dest node and dest device |
|
256 |
uint64_t* pTime = reinterpret_cast <uint64_t *> (buffer); |
|
257 |
*pTime++ = 0; |
|
258 |
*pTime++ = guarantee_update.GetInteger (); |
|
259 |
uint32_t* pData = reinterpret_cast<uint32_t *> (pTime); |
|
260 |
*pData++ = 0; |
|
261 |
*pData++ = 0; |
|
262 |
||
263 |
// Find the system id for the destination MPI rank |
|
264 |
uint32_t nodeSysId = bundle->GetSystemId (); |
|
265 |
||
266 |
MPI_Isend (reinterpret_cast<void *> (iter->GetBuffer ()), bufferSize, MPI_CHAR, nodeSysId, |
|
267 |
0, MPI_COMM_WORLD, (iter->GetRequest ())); |
|
268 |
#endif |
|
269 |
} |
|
270 |
||
271 |
void |
|
272 |
NullMessageMpiInterface::ReceiveMessagesBlocking () |
|
273 |
{ |
|
274 |
NS_LOG_FUNCTION_NOARGS (); |
|
275 |
||
276 |
ReceiveMessages(true); |
|
277 |
} |
|
278 |
||
279 |
||
280 |
void |
|
281 |
NullMessageMpiInterface::ReceiveMessagesNonBlocking () |
|
282 |
{ |
|
283 |
NS_LOG_FUNCTION_NOARGS (); |
|
284 |
||
285 |
ReceiveMessages(false); |
|
286 |
} |
|
287 |
||
288 |
||
289 |
void |
|
290 |
NullMessageMpiInterface::ReceiveMessages (bool blocking) |
|
291 |
{ |
|
292 |
NS_LOG_FUNCTION (blocking); |
|
293 |
||
294 |
NS_ASSERT (g_enabled); |
|
295 |
||
296 |
#ifdef NS3_MPI |
|
297 |
||
298 |
// stop flag set to true when no more messages are found to |
|
299 |
// process. |
|
300 |
bool stop = false; |
|
301 |
||
302 |
||
303 |
if (!g_numNeighbors) { |
|
304 |
// Not communicating with anyone. |
|
305 |
return; |
|
306 |
} |
|
307 |
||
308 |
do |
|
309 |
{ |
|
310 |
int messageReceived = 0; |
|
311 |
int index = 0; |
|
312 |
MPI_Status status; |
|
313 |
||
314 |
if (blocking) |
|
315 |
{ |
|
316 |
MPI_Waitany (g_numNeighbors, g_requests, &index, &status); |
|
317 |
messageReceived = 1; /* Wait always implies message was received */ |
|
318 |
stop = true; |
|
319 |
} |
|
320 |
else |
|
321 |
{ |
|
322 |
MPI_Testany (g_numNeighbors, g_requests, &index, &messageReceived, &status); |
|
323 |
} |
|
324 |
||
325 |
if (messageReceived) |
|
326 |
{ |
|
327 |
int count; |
|
328 |
MPI_Get_count (&status, MPI_CHAR, &count); |
|
329 |
||
330 |
// Get the meta data first |
|
331 |
uint64_t* pTime = reinterpret_cast<uint64_t *> (g_pRxBuffers[index]); |
|
332 |
uint64_t time = *pTime++; |
|
333 |
uint64_t guaranteeUpdate = *pTime++; |
|
334 |
||
335 |
uint32_t* pData = reinterpret_cast<uint32_t *> (pTime); |
|
336 |
uint32_t node = *pData++; |
|
337 |
uint32_t dev = *pData++; |
|
338 |
||
339 |
Time rxTime (time); |
|
340 |
||
341 |
// rxtime == 0 means this is a Null Message |
|
10594
147765b4725b
Update Time scaling usage to use new functions.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
10476
diff
changeset
|
342 |
if (rxTime > Time (0)) |
10476 | 343 |
{ |
344 |
count -= sizeof (time) + sizeof (guaranteeUpdate) + sizeof (node) + sizeof (dev); |
|
345 |
||
346 |
Ptr<Packet> p = Create<Packet> (reinterpret_cast<uint8_t *> (pData), count, true); |
|
347 |
||
348 |
// Find the correct node/device to schedule receive event |
|
349 |
Ptr<Node> pNode = NodeList::GetNode (node); |
|
350 |
Ptr<MpiReceiver> pMpiRec = 0; |
|
351 |
uint32_t nDevices = pNode->GetNDevices (); |
|
352 |
for (uint32_t i = 0; i < nDevices; ++i) |
|
353 |
{ |
|
354 |
Ptr<NetDevice> pThisDev = pNode->GetDevice (i); |
|
355 |
if (pThisDev->GetIfIndex () == dev) |
|
356 |
{ |
|
357 |
pMpiRec = pThisDev->GetObject<MpiReceiver> (); |
|
358 |
break; |
|
359 |
} |
|
360 |
} |
|
361 |
NS_ASSERT (pNode && pMpiRec); |
|
362 |
||
363 |
// Schedule the rx event |
|
364 |
Simulator::ScheduleWithContext (pNode->GetId (), rxTime - Simulator::Now (), |
|
365 |
&MpiReceiver::Receive, pMpiRec, p); |
|
366 |
||
367 |
} |
|
368 |
||
369 |
// Update guarantee time for both packet receives and Null Messages. |
|
370 |
Ptr<RemoteChannelBundle> bundle = RemoteChannelBundleManager::Find (status.MPI_SOURCE); |
|
371 |
NS_ASSERT (bundle); |
|
372 |
||
373 |
bundle->SetGuaranteeTime (Time (guaranteeUpdate)); |
|
374 |
||
375 |
// Re-queue the next read |
|
376 |
MPI_Irecv (g_pRxBuffers[index], NULL_MESSAGE_MAX_MPI_MSG_SIZE, MPI_CHAR, status.MPI_SOURCE, 0, |
|
377 |
MPI_COMM_WORLD, &g_requests[index]); |
|
378 |
||
379 |
} |
|
380 |
else |
|
381 |
{ |
|
382 |
// if non-blocking and no message received in testany then stop message loop |
|
383 |
stop = true; |
|
384 |
} |
|
385 |
} |
|
386 |
while (!stop); |
|
387 |
#endif |
|
388 |
} |
|
389 |
||
390 |
void |
|
391 |
NullMessageMpiInterface::TestSendComplete () |
|
392 |
{ |
|
393 |
NS_LOG_FUNCTION_NOARGS (); |
|
394 |
||
395 |
NS_ASSERT (g_enabled); |
|
396 |
||
397 |
#ifdef NS3_MPI |
|
398 |
std::list<NullMessageSentBuffer>::iterator iter = g_pendingTx.begin (); |
|
399 |
while (iter != g_pendingTx.end ()) |
|
400 |
{ |
|
401 |
MPI_Status status; |
|
402 |
int flag = 0; |
|
403 |
MPI_Test (iter->GetRequest (), &flag, &status); |
|
404 |
std::list<NullMessageSentBuffer>::iterator current = iter; // Save current for erasing |
|
405 |
++iter; // Advance to next |
|
406 |
if (flag) |
|
407 |
{ // This message is complete |
|
408 |
g_pendingTx.erase (current); |
|
409 |
} |
|
410 |
} |
|
411 |
#endif |
|
412 |
} |
|
413 |
||
414 |
void |
|
415 |
NullMessageMpiInterface::Disable () |
|
416 |
{ |
|
417 |
NS_LOG_FUNCTION (this); |
|
418 |
||
419 |
#ifdef NS3_MPI |
|
420 |
int flag = 0; |
|
421 |
MPI_Initialized (&flag); |
|
422 |
if (flag) |
|
423 |
{ |
|
424 |
||
425 |
for (std::list<NullMessageSentBuffer>::iterator iter = g_pendingTx.begin (); |
|
426 |
iter != g_pendingTx.end (); |
|
427 |
++iter) |
|
428 |
{ |
|
429 |
MPI_Cancel (iter->GetRequest ()); |
|
430 |
MPI_Request_free (iter->GetRequest ()); |
|
431 |
} |
|
432 |
||
433 |
for (uint32_t i = 0; i < g_numNeighbors; ++i) |
|
434 |
{ |
|
435 |
MPI_Cancel (&g_requests[i]); |
|
436 |
MPI_Request_free (&g_requests[i]); |
|
437 |
} |
|
438 |
||
439 |
MPI_Finalize (); |
|
440 |
||
441 |
for (uint32_t i = 0; i < g_numNeighbors; ++i) |
|
442 |
{ |
|
443 |
delete [] g_pRxBuffers[i]; |
|
444 |
} |
|
445 |
delete [] g_pRxBuffers; |
|
446 |
delete [] g_requests; |
|
447 |
||
448 |
g_pendingTx.clear (); |
|
449 |
||
450 |
g_enabled = false; |
|
451 |
g_initialized = false; |
|
452 |
||
453 |
} |
|
454 |
else |
|
455 |
{ |
|
456 |
NS_FATAL_ERROR ("Cannot disable MPI environment without Initializing it first"); |
|
457 |
} |
|
458 |
#endif |
|
459 |
} |
|
460 |
||
461 |
} // namespace ns3 |