Newer
Older

Christian Godenschwager
committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
//======================================================================================================================
//
// This file is part of waLBerla. waLBerla is free software: you can
// redistribute it and/or modify it under the terms of the GNU General Public
// License as published by the Free Software Foundation, either version 3 of
// the License, or (at your option) any later version.
//
// waLBerla is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
// for more details.
//
// You should have received a copy of the GNU General Public License along
// with waLBerla (see COPYING.txt). If not, see <http://www.gnu.org/licenses/>.
//
//! \file BufferSystemTest.cpp
//! \ingroup core
//! \author Martin Bauer <martin.bauer@fau.de>
//! \brief Tests for BufferSystem: symmetric and asymmetric MPI communication tests
//
//======================================================================================================================
#include "core/Abort.h"
#include "core/debug/TestSubsystem.h"
#include "core/logging/Logging.h"
#include "core/mpi/BufferSystem.h"
#include "core/mpi/Environment.h"
#include <boost/random/mersenne_twister.hpp>
#include <boost/random/uniform_int.hpp>
#include <boost/random/variate_generator.hpp>
#include <boost/thread/thread.hpp>
#include <cmath>
#include <iostream>
#include <set>
using namespace walberla;
using mpi::BufferSystem;
typedef boost::mt19937 base_generator_type;
/**
* Utility function for sleeping a random time
* used to simulate a variable process load
*/
void randomSleep( int maxTimeInMs = 20 )
{
static base_generator_type generator(42u);
static unsigned int counter =0;
counter += 100;
int rank = MPIManager::instance()->worldRank();
unsigned int seed = static_cast<unsigned int>(std::time(0)) + static_cast<unsigned int>(rank*1000) + counter;
generator.seed(seed);
boost::uniform_int<> uni_dist(0,maxTimeInMs);
boost::variate_generator<base_generator_type&, boost::uniform_int<> > uni(generator, uni_dist);
int sleepTime = uni();
boost::this_thread::sleep( boost::posix_time::milliseconds( sleepTime ) );
}
/**
* Every process sends a message containing its own rank
* to the neighboring processes (1D , periodic boundary)
*/
void symmetricCommunication()
{
const int MSG_SIZE = 10;
auto mpiManager = MPIManager::instance();
int numProcesses = mpiManager->numProcesses();
int rank = mpiManager->worldRank();
int leftNeighbor = (rank-1+numProcesses) % numProcesses;
int rightNeighbor = (rank+1) % numProcesses;
WALBERLA_CHECK_GREATER_EQUAL( numProcesses, 3 );
BufferSystem bs ( MPI_COMM_WORLD );
// Pack Message to left neighbor containing own rank
for( int i=0; i< MSG_SIZE; ++i )
bs.sendBuffer( leftNeighbor ) << rank;
// Pack Message to right neighbor containing own rank
for( int i=0; i< MSG_SIZE; ++i )
bs.sendBuffer( rightNeighbor ) << rank;
bs.setReceiverInfoFromSendBufferState( true, false );
randomSleep();
bs.sendAll();
// In between we could do some computation
randomSleep();
for( auto it = bs.begin(); it != bs.end(); ++it )
{
WALBERLA_CHECK ( it.rank() == leftNeighbor || it.rank() == rightNeighbor );
WALBERLA_CHECK_EQUAL( it.buffer().size(), MSG_SIZE * sizeof(int) + MSG_SIZE * mpi::BUFFER_DEBUG_OVERHEAD );
int receivedVal = -1;
it.buffer() >> receivedVal;
WALBERLA_CHECK_EQUAL( receivedVal, it.rank() );
}
}
/**
* Every process sends a message as big as his rank number
* to the neighboring processes (1D , periodic boundary)
*/
void asymmetricCommunication()
{
auto mpiManager = MPIManager::instance();
int numProcesses = mpiManager->numProcesses();
int rank = mpiManager->worldRank();
int leftNeighbor = (rank-1+numProcesses) % numProcesses;
int rightNeighbor = (rank+1) % numProcesses;
WALBERLA_CHECK_GREATER_EQUAL( numProcesses, 3 );
BufferSystem bs ( MPI_COMM_WORLD );
// Set receiver information
std::set<int> receiveFrom;
if ( leftNeighbor > 0 ) receiveFrom.insert( leftNeighbor );
if ( rightNeighbor > 0 ) receiveFrom.insert( rightNeighbor );
bs.setReceiverInfo( receiveFrom, false );
const uint_t NUM_STEPS = 3;
for ( uint_t step = 0; step < NUM_STEPS; ++step )
{
// Pack Messages to neighbors containing rank times rank value
for( int i=0; i< rank; ++i ) bs.sendBuffer( leftNeighbor ) << rank;
for( int i=0; i< rank; ++i ) bs.sendBuffer( rightNeighbor ) << rank;
randomSleep();
bs.sendAll();
// In between we could do some computation
randomSleep();
for( auto it = bs.begin(); it != bs.end(); ++it )
{
if ( it.rank() == leftNeighbor )
{
for( int i=0; i < leftNeighbor; ++i ) {
int value = -1;
it.buffer() >> value;
WALBERLA_CHECK_EQUAL( value, leftNeighbor );
}
}
else if ( it.rank() == rightNeighbor )
{
for( int i=0; i < rightNeighbor; ++i ) {
int value = -1;
it.buffer() >> value;
WALBERLA_CHECK_EQUAL( value, rightNeighbor );
}
}
else
WALBERLA_CHECK( false ); // unexpected sender
WALBERLA_CHECK( it.buffer().isEmpty() );
}
}
}
// like asymmetricCommunication, but the message size is a random value
// that changes every communication step
void timeVaryingCommunication()
{
auto mpiManager = MPIManager::instance();
int numProcesses = mpiManager->numProcesses();
int rank = mpiManager->worldRank();
int leftNeighbor = (rank-1+numProcesses) % numProcesses;
int rightNeighbor = (rank+1) % numProcesses;
WALBERLA_CHECK_GREATER_EQUAL( numProcesses, 3 );
BufferSystem bs ( MPI_COMM_WORLD );
// artificial special case: no message from root
bs.sendBuffer( rightNeighbor );
bs.sendBuffer( leftNeighbor );
bs.setReceiverInfoFromSendBufferState( false, true );
const uint_t NUM_STEPS = 5;
for ( uint_t step = 1; step <= NUM_STEPS; ++step )
{
for( uint_t i=0; i < std::max<uint_t>( uint_c(rank * leftNeighbor) * step % 17, 1ul); ++i )
bs.sendBuffer( leftNeighbor ) << i;
bs.send( leftNeighbor );
for( uint_t i=0; i < std::max<uint_t>( uint_c(rank * rightNeighbor) * step % 17, 1ul); ++i )
bs.sendBuffer( rightNeighbor ) << i;
bs.send( rightNeighbor );
WALBERLA_CHECK( bs.isCommunciationRunning() );
for( auto it = bs.begin(); it != bs.end(); ++it )
{
if ( it.rank() == leftNeighbor )
{
for( uint_t i=0; i < std::max<uint_t>( uint_c(rank * leftNeighbor) * step % 17, 1ul); ++i ) {
uint_t value = 0;
it.buffer() >> value;
WALBERLA_CHECK_EQUAL( value, i );
}
}
else if ( it.rank() == rightNeighbor )
{
for( uint_t i=0; i < std::max<uint_t>( uint_c(rank * rightNeighbor) * step % 17,1ul); ++i ) {
uint_t value = 0;
it.buffer() >> value;
WALBERLA_CHECK_EQUAL( value, i );
}
}
else
WALBERLA_CHECK( false ); // unexpected sender
WALBERLA_CHECK( it.buffer().isEmpty() );
}
WALBERLA_CHECK( ! bs.isCommunciationRunning() );
}
}
/**
* Gathering using asymmetric communication
* every process sends a message of size rank*sizeof(int) containing only its own rank to root process
* i.e. rank 1 sends a "1" once, rank 2 sends a message containing two "2"'s ...
*/
void gatherUsingAsymmetricCommunication()
{
int rank = MPIManager::instance()->worldRank();
int numProcesses = MPIManager::instance()->numProcesses();
WALBERLA_CHECK_GREATER_EQUAL( numProcesses, 3 );
const int TAG=42;
BufferSystem bs (MPI_COMM_WORLD, TAG );
if ( rank ==0 )
bs.setReceiverInfo( BufferSystem::allRanksButRoot(), true );
else
bs.setReceiverInfo( std::set<mpi::MPIRank>(), true );
if(rank > 0)
{
for( int i=0; i < rank; ++i )
bs.sendBuffer(0) << rank;
}
bs.sendAll();
randomSleep();
for( auto it = bs.begin(); it != bs.end(); ++it )
{
WALBERLA_CHECK( rank == 0); // only root should receive something
for( int i=0; i < it.rank(); ++i )
{
int received = -1;
it.buffer() >> received;
WALBERLA_CHECK_EQUAL( received, it.rank() );
}
}
}
void selfSend()
{
int rank = MPIManager::instance()->worldRank();
int numProcesses = MPIManager::instance()->numProcesses();
WALBERLA_CHECK_GREATER_EQUAL( numProcesses, 3 );
const int TAG=42;
BufferSystem bs (MPI_COMM_WORLD, TAG );
if ( rank ==0 )
bs.setReceiverInfo( BufferSystem::allRanks(), true );
else
bs.setReceiverInfo( std::set<mpi::MPIRank>(), true );
bs.sendBuffer(0) << rank;
bs.sendAll();
randomSleep();
for( auto it = bs.begin(); it != bs.end(); ++it )
{
WALBERLA_CHECK( rank == 0); // only root should receive something
int received = -1;
it.buffer() >> received;
WALBERLA_CHECK_EQUAL( received, it.rank() );
}
}
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
void copyTest()
{
int rank = MPIManager::instance()->worldRank();
BufferSystem bs1( MPI_COMM_WORLD, 3 );
{
BufferSystem bs2( MPI_COMM_WORLD, 7 );
bs2.sendBuffer(rank) << int(42);
bs2.setReceiverInfoFromSendBufferState( true, false );
bs2.sendAll();
for ( auto i = bs2.begin(); i != bs2.end(); ++i )
{
int messageContent;
i.buffer() >> messageContent;
WALBERLA_CHECK_EQUAL(messageContent, 42);
}
bs1 = bs2;
}
bs1.sendBuffer(rank) << int(42);
bs1.sendAll();
for ( auto i = bs1.begin(); i != bs1.end(); ++i )
{
int messageContent;
i.buffer() >> messageContent;
WALBERLA_CHECK_EQUAL(messageContent, 42);
}
}

Christian Godenschwager
committed
int main(int argc, char**argv)
{
mpi::Environment mpiEnv( argc, argv );
debug::enterTestMode();
auto mpiManager = MPIManager::instance();
int numProcesses = mpiManager->numProcesses();
if(numProcesses <= 2)
{
WALBERLA_ABORT("This test has to be executed on at least 3 processes. Executed on " << numProcesses);
return 1;
}
WALBERLA_LOG_INFO_ON_ROOT("Testing Symmetric Communication...");

Christian Godenschwager
committed
symmetricCommunication();
WALBERLA_LOG_INFO_ON_ROOT("Testing Asymmetric Communication...");

Christian Godenschwager
committed
asymmetricCommunication();
WALBERLA_LOG_INFO_ON_ROOT("Testing time-varying Communication...");

Christian Godenschwager
committed
timeVaryingCommunication();
WALBERLA_LOG_INFO_ON_ROOT("Testing Gather Operation...");

Christian Godenschwager
committed
gatherUsingAsymmetricCommunication();
WALBERLA_LOG_INFO_ON_ROOT("Testing self-send...");

Christian Godenschwager
committed
selfSend();
WALBERLA_LOG_INFO_ON_ROOT("Testing Buffer System copy...");
copyTest();

Christian Godenschwager
committed
return EXIT_SUCCESS;
}