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
//======================================================================================================================
//
// 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 SetReductionTest.cpp
//! \ingroup core
//! \author Christian Godenschwager <christian.godenschwager@fau.de>
//
//======================================================================================================================
#include "core/DataTypes.h"
#include "core/debug/TestSubsystem.h"
#include "core/logging/Logging.h"
#include "core/mpi/MPIManager.h"
#include "core/mpi/SetReduction.h"
#include "core/mpi/BufferDataTypeExtensions.h"
#include <algorithm>
#include <string>
#include <vector>
namespace set_reduction_test {
using namespace walberla;
void testRankSet()
{
int rank = mpi::MPIManager::instance()->rank();
int numberOfRanks = mpi::MPIManager::instance()->numProcesses();
std::vector<int> value;
value.push_back( rank );
std::vector<int> reducedSetUnion = mpi::allReduceSet( value, mpi::UNION );
std::vector<int> reducedSetIntersection = mpi::allReduceSet( value, mpi::INTERSECTION );
WALBERLA_CHECK( reducedSetIntersection.empty() || ( numberOfRanks == 1 && reducedSetIntersection.size() == 1 ) );
//std::ostringstream oss;
//for( auto it = reducedSetUnion.begin(); it != reducedSetUnion.end(); ++it )
//{
// oss << *it << ' ';
//}
//WALBERLA_LOG_DEVEL( oss.str() );
WALBERLA_CHECK_EQUAL( reducedSetUnion.size(), numberOfRanks );
int ctr = 0;
for( auto it = reducedSetUnion.begin(); it != reducedSetUnion.end(); ++it )
{
WALBERLA_CHECK_EQUAL( *it, ctr );
++ctr;
}
value.clear();
for( int i = 0; i < numberOfRanks; ++i )
{
value.push_back( i );
value.push_back( i );
value.push_back( i );
}
reducedSetUnion = mpi::allReduceSet( value, mpi::UNION );
reducedSetIntersection = mpi::allReduceSet( value, mpi::INTERSECTION );
WALBERLA_CHECK_EQUAL( reducedSetUnion.size(), numberOfRanks );
ctr = 0;
auto it2 = reducedSetIntersection.begin();
for( auto it = reducedSetUnion.begin(); it != reducedSetUnion.end(); ++it, ++it2 )
{
WALBERLA_CHECK_EQUAL( *it, ctr );
WALBERLA_CHECK_EQUAL( *it2, ctr );
++ctr;
}
}
static const int NUM_FRUITS = 5;
static const std::string FRUITS[] = { "apple", "banana", "pear", "melon", "grapefruit" };
void testStrings()
{
int rank = mpi::MPIManager::instance()->rank();
int numProcesses = mpi::MPIManager::instance()->numProcesses();
std::vector< std::string > values;
values.push_back( FRUITS[rank % NUM_FRUITS] );
std::vector< std::string > reducedValuesUnion = mpi::allReduceSet( values, mpi::UNION );
values.emplace_back("GRAPES" );

Christian Godenschwager
committed
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
std::vector< std::string > reducedValuesIntersection = mpi::allReduceSet( values, mpi::INTERSECTION );
if( numProcesses == 1 )
{
WALBERLA_CHECK_EQUAL( reducedValuesIntersection.size(), values.size() );
}
else
{
WALBERLA_CHECK_EQUAL( reducedValuesIntersection.size(), 1 );
WALBERLA_CHECK_EQUAL( reducedValuesIntersection.front(), "GRAPES" );
}
//std::ostringstream oss;
//for( auto it = reducedValuesUnion.begin(); it != reducedValuesUnion.end(); ++it )
//{
// oss << *it << ' ';
//}
//WALBERLA_LOG_DEVEL( oss.str() );
int numberOfProcesses = mpi::MPIManager::instance()->numProcesses();
std::vector< std::string > expectedSet( FRUITS, FRUITS + std::min( numberOfProcesses, NUM_FRUITS ) );
std::sort( expectedSet.begin(), expectedSet.end() );
WALBERLA_CHECK_EQUAL( reducedValuesUnion.size(), expectedSet.size() );
for( size_t i = 0; i < expectedSet.size(); ++i )
{
WALBERLA_CHECK_EQUAL( reducedValuesUnion[i], expectedSet[i] );
}
}
} // namespace set_reduction_test
int main( int argc, char * argv[] )
{
walberla::debug::enterTestMode();
walberla::MPIManager::instance()->initializeMPI( &argc, &argv );
WALBERLA_MPI_SECTION() { walberla::MPIManager::instance()->useWorldComm(); }
set_reduction_test::testRankSet();
set_reduction_test::testStrings();
}