| 1 |
// Copyright 2002 - 2008, 2010, 2011 National Technology Engineering |
| 2 |
// Solutions of Sandia, LLC (NTESS). Under the terms of Contract |
| 3 |
// DE-NA0003525 with NTESS, the U.S. Government retains certain rights |
| 4 |
// in this software. |
| 5 |
// |
| 6 |
// Redistribution and use in source and binary forms, with or without |
| 7 |
// modification, are permitted provided that the following conditions are |
| 8 |
// met: |
| 9 |
// |
| 10 |
// * Redistributions of source code must retain the above copyright |
| 11 |
// notice, this list of conditions and the following disclaimer. |
| 12 |
// |
| 13 |
// * Redistributions in binary form must reproduce the above |
| 14 |
// copyright notice, this list of conditions and the following |
| 15 |
// disclaimer in the documentation and/or other materials provided |
| 16 |
// with the distribution. |
| 17 |
// |
| 18 |
// * Neither the name of NTESS nor the names of its contributors |
| 19 |
// may be used to endorse or promote products derived from this |
| 20 |
// software without specific prior written permission. |
| 21 |
// |
| 22 |
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 23 |
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 24 |
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 25 |
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 26 |
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 27 |
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 28 |
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 29 |
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 30 |
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 31 |
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 32 |
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 33 |
// |
| 34 |
|
| 35 |
#include "gtest/gtest.h" |
| 36 |
#include "stk_util/parallel/Parallel.hpp" // for parallel_machine_rank, parallel_machine_... |
| 37 |
#include "stk_util/parallel/ParallelReduce.hpp" // for all_reduce_max, all_reduce_maxloc, all_r... |
| 38 |
#include "stk_util/stk_config.h" // for STK_HAS_MPI |
| 39 |
#include <cstdint> // for uint64_t, uint32_t |
| 40 |
#include <limits> // for numeric_limits |
| 41 |
#include <vector> // for vector |
| 42 |
|
| 43 |
|
| 44 |
#if defined ( STK_HAS_MPI ) |
| 45 |
TEST(ParallelComm, AllReduceLoc) |
| 46 |
{ |
| 47 |
stk::ParallelMachine comm = MPI_COMM_WORLD; |
| 48 |
|
| 49 |
int myProcId = stk::parallel_machine_rank(comm); |
| 50 |
int numProcs = stk::parallel_machine_size(comm); |
| 51 |
|
| 52 |
int nvalues=5; |
| 53 |
uint64_t limit_32bit_integer = std::numeric_limits<uint32_t>::max(); |
| 54 |
|
| 55 |
std::vector<double> values(nvalues); |
| 56 |
std::vector<uint64_t> locations(nvalues); |
| 57 |
std::vector<double> globalValues(nvalues); |
| 58 |
std::vector<uint64_t> globalLocations(nvalues); |
| 59 |
|
| 60 |
for(int n = 0; n < nvalues; n++) { |
| 61 |
values[n] = myProcId*numProcs+n; |
| 62 |
locations[n] = limit_32bit_integer + myProcId*numProcs*numProcs+n; //Want to test when outside 32-bit range for index. |
| 63 |
} |
| 64 |
|
| 65 |
stk::all_reduce_maxloc(comm, values.data(), locations.data(), globalValues.data(), globalLocations.data(), nvalues); |
| 66 |
|
| 67 |
for(int n = 0; n < nvalues; n++) { |
| 68 |
EXPECT_EQ((numProcs-1)*numProcs+n, globalValues[n]); |
| 69 |
EXPECT_EQ(limit_32bit_integer + (numProcs-1)*numProcs*numProcs+n, globalLocations[n]); |
| 70 |
} |
| 71 |
|
| 72 |
stk::all_reduce_minloc(comm, values.data(), locations.data(), globalValues.data(), globalLocations.data(), nvalues); |
| 73 |
|
| 74 |
for(int n = 0; n < nvalues; n++) { |
| 75 |
EXPECT_EQ(n, globalValues[n]); |
| 76 |
EXPECT_EQ(limit_32bit_integer+n, globalLocations[n]); |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
TEST(ParallelComm, AllReduce) |
| 81 |
{ |
| 82 |
stk::ParallelMachine comm = MPI_COMM_WORLD; |
| 83 |
|
| 84 |
int myProcId = stk::parallel_machine_rank(comm); |
| 85 |
int numProcs = stk::parallel_machine_size(comm); |
| 86 |
|
| 87 |
int nvalues=5; |
| 88 |
|
| 89 |
std::vector<double> values(nvalues); |
| 90 |
std::vector<double> globalValues(nvalues); |
| 91 |
|
| 92 |
for(int n = 0; n < nvalues; n++) { |
| 93 |
values[n] = myProcId*numProcs+n; |
| 94 |
} |
| 95 |
|
| 96 |
stk::all_reduce_max(comm, values.data(), globalValues.data(), nvalues); |
| 97 |
for(int n = 0; n < nvalues; n++) { |
| 98 |
EXPECT_EQ((numProcs-1)*numProcs+n, globalValues[n]); |
| 99 |
} |
| 100 |
|
| 101 |
stk::all_reduce_min(comm, values.data(), globalValues.data(), nvalues); |
| 102 |
for(int n = 0; n < nvalues; n++) { |
| 103 |
EXPECT_EQ(n, globalValues[n]); |
| 104 |
} |
| 105 |
|
| 106 |
int alpha = 0; |
| 107 |
for(int n = 0; n < numProcs; ++n) { |
| 108 |
alpha += n*numProcs; |
| 109 |
} |
| 110 |
|
| 111 |
stk::all_reduce_sum(comm, values.data(), globalValues.data(), nvalues); |
| 112 |
for(int n = 0; n < nvalues; n++) { |
| 113 |
EXPECT_EQ(n*numProcs + alpha, globalValues[n]); |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
TEST(ParallelComm, GetGlobal) |
| 118 |
{ |
| 119 |
stk::ParallelMachine comm = MPI_COMM_WORLD; |
| 120 |
|
| 121 |
int myProcId = stk::parallel_machine_rank(comm); |
| 122 |
int numProcs = stk::parallel_machine_size(comm); |
| 123 |
|
| 124 |
double localValue = myProcId * numProcs; |
| 125 |
|
| 126 |
double globalMax = stk::get_global_max(comm, localValue); |
| 127 |
EXPECT_EQ(globalMax, (numProcs-1)*numProcs); |
| 128 |
|
| 129 |
double globalMin = stk::get_global_min(comm, localValue); |
| 130 |
EXPECT_EQ(globalMin, 0); |
| 131 |
|
| 132 |
int expectedSum = 0; |
| 133 |
for(int n = 0; n < numProcs; ++n) { |
| 134 |
expectedSum += n*numProcs; |
| 135 |
} |
| 136 |
|
| 137 |
double globalSum = stk::get_global_sum(comm, localValue); |
| 138 |
EXPECT_EQ(globalSum, expectedSum); |
| 139 |
} |
| 140 |
|
| 141 |
TEST(ParallelComm, uint64_reduce_min) |
| 142 |
{ |
| 143 |
// Test coverage of openmpi-4.1.4 bug with unsigned integer min reduction |
| 144 |
stk::ParallelMachine comm = MPI_COMM_WORLD; |
| 145 |
|
| 146 |
int myProcId = stk::parallel_machine_rank(comm); |
| 147 |
|
| 148 |
const uint64_t goldValue = 10; |
| 149 |
|
| 150 |
uint64_t id = (myProcId == 0) ? goldValue : std::numeric_limits<uint64_t>::max(); |
| 151 |
uint64_t gid; |
| 152 |
|
| 153 |
stk::all_reduce_min(comm, &id, &gid, 1u); |
| 154 |
|
| 155 |
EXPECT_EQ(goldValue, gid); |
| 156 |
} |
| 157 |
|
| 158 |
#endif |
| 159 |
|