BufferSystemTest.cpp 14.9 KB
Newer Older
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
//======================================================================================================================
//
//  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"

29
#include <random>
30
31
32
33

#include <cmath>
#include <iostream>
#include <set>
34
35
#include <thread>
#include <chrono>
36
37
38
39


using namespace walberla;
using mpi::BufferSystem;
40
using namespace std::literals::chrono_literals;
41
42
43



44
using base_generator_type = std::mt19937;
45
46
47
48
49
50
51
52
53
54
55
56
57

/**
 * 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();
58
   unsigned int seed = static_cast<unsigned int>(std::time(nullptr)) + static_cast<unsigned int>(rank*1000) + counter;
59
60
   generator.seed(seed);

61
   std::uniform_int_distribution<> uni_dist(0,maxTimeInMs);
62

63
   int sleepTime = uni_dist(generator);
64
   std::this_thread::sleep_for( sleepTime * 1ms );
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
}


/**
 * 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() );
   }
113
114
115

   WALBERLA_CHECK_EQUAL( bs.getBytesSent(), (MSG_SIZE * sizeof(int) + MSG_SIZE * mpi::BUFFER_DEBUG_OVERHEAD) * 2 );
   WALBERLA_CHECK_EQUAL( bs.getBytesReceived(), (MSG_SIZE * sizeof(int) + MSG_SIZE * mpi::BUFFER_DEBUG_OVERHEAD) * 2 );
116
117
118
119
120
121
}

/**
 * Every process sends a message as big as his rank number
 * to the neighboring processes (1D , periodic boundary)
 */
122
void asymmetricCommunication(const bool useIProbe)
123
124
125
126
127
128
129
130
131
132
133
134
{
   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 );
135
   bs.useIProbe(useIProbe);
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

   // 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() );
      }
   }
182
183
184

   WALBERLA_CHECK_EQUAL( bs.getBytesSent(), int64_c(sizeof(int) + mpi::BUFFER_DEBUG_OVERHEAD) * int64_c(rank + rank) );
   WALBERLA_CHECK_EQUAL( bs.getBytesReceived(), int64_c(sizeof(int) + mpi::BUFFER_DEBUG_OVERHEAD) * int64_c(leftNeighbor + rightNeighbor) );
185
186
187
188
189
}


// like asymmetricCommunication, but the message size is a random value
// that changes every communication step
190
void timeVaryingCommunication(const bool useIProbe)
191
192
193
194
195
196
197
198
199
200
201
{
   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 );
202
   bs.useIProbe(useIProbe);
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221

   // 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 );


Sebastian Eibl's avatar
Sebastian Eibl committed
222
      WALBERLA_CHECK( bs.isCommunicationRunning()  );
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

      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() );

      }
Sebastian Eibl's avatar
Sebastian Eibl committed
248
      WALBERLA_CHECK( ! bs.isCommunicationRunning()  );
249
250
251
252
253
254
255
256
257
258
259
260
261
   }
}





/**
 * 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 ...
 */

262
void gatherUsingAsymmetricCommunication(const bool useIProbe)
263
264
265
266
267
268
269
270
271
{
   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 );
272
   bs.useIProbe(useIProbe);
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


   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() );
      }
   }

}


304
305
306
307
308
309
/**
 * Test for communication when only the number of receives is known but not the ranks.
 */

void unknownRanksAllToAll()
{
310
311
312
313
314
315
316
317
318
319
320
   // The unknown-sender communication is more vulnerable when
   // tags of other MPI communication routines are equal since
   // it processes messages with MPI_ANY_SOURCE
   //
   // Therefore this barrier makes sure that previous tests
   // do not impose race-conditions.
   //
   // This is safer than specifying "unique" tags, since
   // tags are never "unique" :)
   WALBERLA_MPI_BARRIER();

321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
   const int rank         = MPIManager::instance()->worldRank();
   const int numProcesses = MPIManager::instance()->numProcesses();

   WALBERLA_CHECK_GREATER_EQUAL(numProcesses, 3);

   BufferSystem bs(MPI_COMM_WORLD, 42);

   for (auto targetRank = 0; targetRank < numProcesses; ++targetRank)
   {
      auto& sb = bs.sendBuffer(targetRank);
      for (int i = 0; i < rank + 1; ++i)
         sb << rank;
   }

   //we await numProcesses messages on every rank
   bs.setReceiverInfo(numProcesses);
   //equivalent to
   //bs.setReceiverInfoFromSendBufferState(false, true);
   
   bs.sendAll();

   auto numReceives = 0;
   for (auto it = bs.begin(); it != bs.end(); ++it)
   {
345
      WALBERLA_LOG_INFO( "Sender rank: " << it.rank() << ", msg size: " << it.buffer().size() );
346
347
348
349
350
351
352
353
354
355
356
357
358
      WALBERLA_CHECK_EQUAL(it.buffer().size(), (it.rank() + 1) * 4);
      for (int i = 0; i < it.rank() + 1; ++i)
      {
         int received = -1;
         it.buffer() >> received;
         WALBERLA_CHECK_EQUAL(received, it.rank());
      }
      ++numReceives;
   }
   WALBERLA_CHECK_EQUAL(numReceives, numProcesses);
}
void unknownRanksAllToLower()
{
359
360
361
362
363
364
365
366
367
368
369
   // The unknown-sender communication is more vulnerable when
   // tags of other MPI communication routines are equal since
   // it processes messages with MPI_ANY_SOURCE
   //
   // Therefore this barrier makes sure that previous tests
   // do not impose race-conditions.
   //
   // This is safer than specifying "unique" tags, since
   // tags are never "unique" :)
   WALBERLA_MPI_BARRIER();

370
371
372
373
374
375
376
377
378
379
380
381
382
383
   const int rank         = MPIManager::instance()->worldRank();
   const int numProcesses = MPIManager::instance()->numProcesses();

   WALBERLA_CHECK_GREATER_EQUAL(numProcesses, 3);

   BufferSystem bs(MPI_COMM_WORLD, 42);

   for (auto targetRank = 0; targetRank < rank + 1; ++targetRank)
   {
      auto& sb = bs.sendBuffer(targetRank);
      for (int i = 0; i < rank + 1; ++i)
         sb << rank;
   }

384
385
   //we await numProcesses - rank messages on every rank
   bs.setReceiverInfo(numProcesses - rank);
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
   //equivalent to
   //std::set<mpi::MPIRank> recvs;
   //for (auto targetRank = numProcesses - 1; targetRank >=rank; --targetRank)
   //{
   //   recvs.emplace(targetRank);
   //}
   //bs.setReceiverInfo(recvs, true);
   
   bs.sendAll();

   auto numReceives = 0;
   for (auto it = bs.begin(); it != bs.end(); ++it)
   {
      WALBERLA_CHECK_EQUAL(it.buffer().size(), (it.rank() + 1) * 4);
      for (int i = 0; i < it.rank() + 1; ++i)
      {
         int received = -1;
         it.buffer() >> received;
         WALBERLA_CHECK_EQUAL(received, it.rank());
      }
      ++numReceives;
   }
   WALBERLA_CHECK_EQUAL(numReceives, numProcesses - rank);
}

411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
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() );
   }
}

443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
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);
   }

}
475
476
477
478
479
480
481

int main(int argc, char**argv)
{
   mpi::Environment mpiEnv( argc, argv );
   debug::enterTestMode();

   auto mpiManager = MPIManager::instance();
482
   mpiManager->useWorldComm();
483
484
485
486
487
488
489
490
   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;
   }

491
   WALBERLA_LOG_INFO_ON_ROOT("Testing Symmetric Communication...");
492
493
   symmetricCommunication();

494
   WALBERLA_LOG_INFO_ON_ROOT("Testing Asymmetric Communication...");
495
496
   asymmetricCommunication(false);
   asymmetricCommunication(true);
497

498
   WALBERLA_LOG_INFO_ON_ROOT("Testing time-varying Communication...");
499
500
   timeVaryingCommunication(false);
   timeVaryingCommunication(true);
501

502
   WALBERLA_LOG_INFO_ON_ROOT("Testing Gather Operation...");
503
504
   gatherUsingAsymmetricCommunication(false);
   gatherUsingAsymmetricCommunication(true);
505

506
507
508
509
510
511
   WALBERLA_LOG_INFO_ON_ROOT("Testing Unknown Sender Ranks...");
   WALBERLA_LOG_INFO_ON_ROOT("AllToAll...");
   unknownRanksAllToAll();
   WALBERLA_LOG_INFO_ON_ROOT("AllToLower...");
   unknownRanksAllToLower();

512
   WALBERLA_LOG_INFO_ON_ROOT("Testing self-send...");
513
514
   selfSend();

515
516
517
   WALBERLA_LOG_INFO_ON_ROOT("Testing Buffer System copy...");
   copyTest();

518
519
   return EXIT_SUCCESS;
}