| 1 |
// @HEADER |
| 2 |
// ***************************************************************************** |
| 3 |
// Tpetra: Templated Linear Algebra Services Package |
| 4 |
// |
| 5 |
// Copyright 2008 NTESS and the Tpetra contributors. |
| 6 |
// SPDX-License-Identifier: BSD-3-Clause |
| 7 |
// ***************************************************************************** |
| 8 |
// @HEADER |
| 9 |
|
| 10 |
// Test for github issue #3101 -- test Tpetra::FEMultiVector::doOwnedToOwnedPlusShared |
| 11 |
|
| 12 |
#include "Tpetra_Core.hpp" |
| 13 |
#include "Tpetra_FEMultiVector.hpp" |
| 14 |
#include "Teuchos_CommHelpers.hpp" |
| 15 |
|
| 16 |
#include <string> |
| 17 |
#include <sstream> |
| 18 |
#include <iostream> |
| 19 |
|
| 20 |
///////////////////////////////////////////////////////////////////////////// |
| 21 |
// Each processor owns ten vertices and copies five from another processor. |
| 22 |
// Each processor contributes values to all fifteen vertices it stores. |
| 23 |
// Tpetra::FEMultiVector pushes the copies to their owning processors, and |
| 24 |
// ADDs them to the owned values. |
| 25 |
// Tpetra::FEMultiVector updates copies from their owning processors. |
| 26 |
|
| 27 |
class FEMultiVectorTest { |
| 28 |
public: |
| 29 |
|
| 30 |
typedef Tpetra::Map<> map_t; |
| 31 |
typedef map_t::local_ordinal_type lno_t; |
| 32 |
typedef map_t::global_ordinal_type gno_t; |
| 33 |
typedef int scalar_t; |
| 34 |
typedef Tpetra::FEMultiVector<scalar_t, lno_t, gno_t> femv_t; |
| 35 |
|
| 36 |
FEMultiVectorTest(Teuchos::RCP<const Teuchos::Comm<int> > &comm_); |
| 37 |
|
| 38 |
int intTest(); |
| 39 |
|
| 40 |
void printFEMV(const char *msg) ; |
| 41 |
|
| 42 |
private: |
| 43 |
int me; // my processor rank |
| 44 |
int np; // number of processors |
| 45 |
int nLocalOwned; // number of vertices owned by this processor |
| 46 |
int nLocalCopy; // number of copies of off-processor vertices on this proc |
| 47 |
int nVec; // number of vectors in multivector |
| 48 |
|
| 49 |
Teuchos::RCP<const Teuchos::Comm<int> > comm; // MPI communicator |
| 50 |
|
| 51 |
Teuchos::RCP<const map_t> mapWithCopies; // Tpetra::Map including owned |
| 52 |
// vertices and copies |
| 53 |
Teuchos::RCP<const map_t> mapOwned; // Tpetra::Map including only owned |
| 54 |
Teuchos::RCP<femv_t> femv; // Tpetra::FEMultiVector for test |
| 55 |
}; |
| 56 |
|
| 57 |
////////////////////////////////////////////////////////////////////////////// |
| 58 |
// Constructor |
| 59 |
// - assigns vertices to processors |
| 60 |
// - builds map with owned vertices and |
| 61 |
// map with owned+ghosted (copied) vertices |
| 62 |
// - then builds a Tpetra::FEMultiVector with two vectors using the maps |
| 63 |
FEMultiVectorTest::FEMultiVectorTest( |
| 64 |
Teuchos::RCP<const Teuchos::Comm<int> > &comm_ |
| 65 |
) : |
| 66 |
me(comm_->getRank()), |
| 67 |
np(comm_->getSize()), |
| 68 |
nLocalOwned(10), |
| 69 |
nLocalCopy( np > 1 ? 5 : 0), |
| 70 |
nVec(2), |
| 71 |
comm(comm_) |
| 72 |
{ |
| 73 |
// Each rank has 15 IDs, the last five of which overlap with the next rank. |
| 74 |
// (IDs and owning processors wrap-around from processor np-1 to 0.) |
| 75 |
const Tpetra::global_size_t nGlobal = np * nLocalOwned; |
| 76 |
lno_t offset = me * nLocalOwned; |
| 77 |
|
| 78 |
Teuchos::Array<gno_t> gids(nLocalOwned + nLocalCopy); |
| 79 |
for (lno_t i = 0 ; i < nLocalOwned + nLocalCopy; i++) |
| 80 |
gids[i] = static_cast<gno_t> ((offset + i) % nGlobal); |
| 81 |
|
| 82 |
// Create Map of owned + copies (a.k.a. overlap map); analagous to ColumnMap |
| 83 |
Tpetra::global_size_t dummy = |
| 84 |
Teuchos::OrdinalTraits<Tpetra::global_size_t>::invalid(); |
| 85 |
mapWithCopies = rcp(new map_t(dummy, gids(), 0, comm)); |
| 86 |
|
| 87 |
// Create Map of owned only (a.k.a. one-to-one map); analagous to RowMap |
| 88 |
mapOwned = rcp(new map_t(dummy, gids(0, nLocalOwned), 0, comm)); |
| 89 |
|
| 90 |
// Print the entries of each map |
| 91 |
std::cout << me << " MAP WITH COPIES (" |
| 92 |
<< mapWithCopies->getGlobalNumElements() << "): "; |
| 93 |
lno_t nlocal = lno_t(mapWithCopies->getLocalNumElements()); |
| 94 |
for (lno_t i = 0; i < nlocal; i++) |
| 95 |
std::cout << mapWithCopies->getGlobalElement(i) << " "; |
| 96 |
std::cout << std::endl; |
| 97 |
|
| 98 |
std::cout << me << " ONE TO ONE MAP (" |
| 99 |
<< mapOwned->getGlobalNumElements() << "): "; |
| 100 |
nlocal = lno_t(mapOwned->getLocalNumElements()); |
| 101 |
for (lno_t i = 0; i < nlocal; i++) |
| 102 |
std::cout << mapOwned->getGlobalElement(i) << " "; |
| 103 |
std::cout << std::endl; |
| 104 |
|
| 105 |
// Create FEMultiVector |
| 106 |
typedef Tpetra::Import<lno_t, gno_t> import_t; |
| 107 |
Teuchos::RCP<import_t> importer = rcp(new import_t(mapOwned, |
| 108 |
mapWithCopies)); |
| 109 |
femv = rcp(new femv_t(mapOwned, importer, nVec, true)); |
| 110 |
} |
| 111 |
|
| 112 |
////////////////////////////////////////////////////////////////////////////// |
| 113 |
// Test FEMultiVector |
| 114 |
// Fill first vector with GIDs of vertices |
| 115 |
// Fill second vector with processor rank (me) |
| 116 |
// Perform communication to add copies' contributions to their owned vertices |
| 117 |
// Perform communication to send owned vertices' values back to copies. |
| 118 |
int FEMultiVectorTest::intTest() |
| 119 |
{ |
| 120 |
int ierr = 0; |
| 121 |
|
| 122 |
|
| 123 |
// Add contributions to owned vertices and copies of off-processor vertices |
| 124 |
try { |
| 125 |
femv->beginAssembly(); |
| 126 |
for (lno_t i = 0; i < nLocalOwned + nLocalCopy; i++) { |
| 127 |
gno_t gid = mapWithCopies->getGlobalElement(i); |
| 128 |
femv->replaceGlobalValue(gid, 0, gid); |
| 129 |
femv->replaceGlobalValue(gid, 1, me); |
| 130 |
} |
| 131 |
femv->endAssembly(); |
| 132 |
} |
| 133 |
catch (std::exception &e) { |
| 134 |
std::cout << "FAIL: Exception thrown in Fill: " << e.what() << std::endl; |
| 135 |
throw e; |
| 136 |
} |
| 137 |
|
| 138 |
printFEMV("After Fill "); |
| 139 |
|
| 140 |
// Update copied vertices from their owners |
| 141 |
try { |
| 142 |
femv->doOwnedToOwnedPlusShared(Tpetra::REPLACE); |
| 143 |
} |
| 144 |
catch (std::exception &e) { |
| 145 |
std::cout << "FAIL: Exception thrown in doOwnedToOwnedPlusShared: " |
| 146 |
<< e.what() << std::endl; |
| 147 |
throw e; |
| 148 |
} |
| 149 |
|
| 150 |
printFEMV("After doOwnedToOwnedPlusShared "); |
| 151 |
|
| 152 |
// Check results: after ADD in endAssembly, |
| 153 |
// - overlapping entries of vec 0 should be 2 * gid |
| 154 |
// nonoverlapping entries of vec 0 should be gid |
| 155 |
// - overlapping entries of vec 1 should be me + (np + me-1) % np; |
| 156 |
// nonoverlapping entries of vec 1 should be me |
| 157 |
|
| 158 |
// Check vector 0: SOURCE |
| 159 |
auto value = femv->getData(0); |
| 160 |
for (lno_t i = 0; i < nLocalCopy; i++){ |
| 161 |
gno_t gid = femv->getMap()->getGlobalElement(i); |
| 162 |
if (value[i] != 2*gid) { |
| 163 |
std::cout << me << " Error in SOURCE vec 0 overlap: gid=" << gid |
| 164 |
<< " value= " << value[i] << " should be " << 2*gid |
| 165 |
<< std::endl; |
| 166 |
ierr++; |
| 167 |
} |
| 168 |
} |
| 169 |
for (lno_t i = nLocalCopy; i < nLocalOwned; i++) { |
| 170 |
gno_t gid = femv->getMap()->getGlobalElement(i); |
| 171 |
if (value[i] != gid) { |
| 172 |
std::cout << me << " Error in SOURCE vec 0: gid=" << gid |
| 173 |
<< " value= " << value[i] << " should be " << gid |
| 174 |
<< std::endl; |
| 175 |
ierr++; |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
// Check vector 0: TARGET |
| 180 |
femv->switchActiveMultiVector(); // needed to allow testing copies |
| 181 |
value = femv->getData(0); |
| 182 |
for (lno_t i = 0; i < nLocalCopy; i++){ |
| 183 |
gno_t gid = femv->getMap()->getGlobalElement(i); |
| 184 |
if (value[i] != 2*gid) { |
| 185 |
std::cout << me << " Error in TARGET vec 0 overlap: gid=" << gid |
| 186 |
<< " value= " << value[i] << " should be " << 2*gid |
| 187 |
<< std::endl; |
| 188 |
ierr++; |
| 189 |
} |
| 190 |
} |
| 191 |
for (lno_t i = nLocalCopy; i < nLocalOwned; i++) { |
| 192 |
gno_t gid = femv->getMap()->getGlobalElement(i); |
| 193 |
if (value[i] != gid) { |
| 194 |
std::cout << me << " Error in TARGET vec 0: gid=" << gid |
| 195 |
<< " value= " << value[i] << " should be " << gid |
| 196 |
<< std::endl; |
| 197 |
ierr++; |
| 198 |
} |
| 199 |
} |
| 200 |
for (lno_t i = nLocalOwned; i < nLocalCopy + nLocalOwned; i++) { |
| 201 |
gno_t gid = femv->getMap()->getGlobalElement(i); |
| 202 |
if (value[i] != 2*gid) { |
| 203 |
std::cout << me << " Error in TARGET vec 0 copies: gid=" << gid |
| 204 |
<< " value= " << value[i] << " should be " << gid |
| 205 |
<< std::endl; |
| 206 |
ierr++; |
| 207 |
} |
| 208 |
} |
| 209 |
femv->switchActiveMultiVector(); // restore state |
| 210 |
|
| 211 |
// Check vector 1: SOURCE |
| 212 |
value = femv->getData(1); |
| 213 |
int tmp = me + (np + me - 1) % np; |
| 214 |
for (lno_t i = 0; i < nLocalCopy; i++) { |
| 215 |
gno_t gid = mapWithCopies->getGlobalElement(i); |
| 216 |
if (value[i] != tmp) { |
| 217 |
std::cout << me << " Error in SOURCE vec 1 overlap: gid=" << gid |
| 218 |
<< " value= " << value[i] << " should be " << tmp |
| 219 |
<< std::endl; |
| 220 |
ierr++; |
| 221 |
} |
| 222 |
} |
| 223 |
for (lno_t i = nLocalCopy; i < nLocalOwned; i++) { |
| 224 |
gno_t gid = mapWithCopies->getGlobalElement(i); |
| 225 |
if (value[i] != me) { |
| 226 |
std::cout << me << " Error in SOURCE vec 1: gid=" << gid |
| 227 |
<< " value= " << value[i] << " should be " << me |
| 228 |
<< std::endl; |
| 229 |
ierr++; |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
// Check vector 1: TARGET |
| 234 |
femv->switchActiveMultiVector(); // needed to allow testing copies |
| 235 |
value = femv->getData(1); |
| 236 |
for (lno_t i = 0; i < nLocalCopy; i++) { |
| 237 |
gno_t gid = mapWithCopies->getGlobalElement(i); |
| 238 |
if (value[i] != tmp) { |
| 239 |
std::cout << me << " Error in TARGET vec 1 overlap: gid=" << gid |
| 240 |
<< " value= " << value[i] << " should be " << tmp |
| 241 |
<< std::endl; |
| 242 |
ierr++; |
| 243 |
} |
| 244 |
} |
| 245 |
for (lno_t i = nLocalCopy; i < nLocalOwned; i++) { |
| 246 |
gno_t gid = mapWithCopies->getGlobalElement(i); |
| 247 |
if (value[i] != me) { |
| 248 |
std::cout << me << " Error in TARGET vec 1: gid=" << gid |
| 249 |
<< " value= " << value[i] << " should be " << me |
| 250 |
<< std::endl; |
| 251 |
ierr++; |
| 252 |
} |
| 253 |
} |
| 254 |
tmp = me + (me + 1) % np; |
| 255 |
for (lno_t i = nLocalOwned; i < nLocalCopy + nLocalOwned; i++) { |
| 256 |
gno_t gid = mapWithCopies->getGlobalElement(i); |
| 257 |
if (value[i] != tmp) { |
| 258 |
std::cout << me << " Error in TARGET vec 1 copies: gid=" << gid |
| 259 |
<< " value= " << value[i] << " should be " << tmp |
| 260 |
<< std::endl; |
| 261 |
ierr++; |
| 262 |
} |
| 263 |
} |
| 264 |
femv->switchActiveMultiVector(); // restore state |
| 265 |
|
| 266 |
return ierr; |
| 267 |
} |
| 268 |
|
| 269 |
////////////////////////////////////////////////////////////////////////////// |
| 270 |
// Print the SOURCE and TARGET multivector entries |
| 271 |
void FEMultiVectorTest::printFEMV(const char *msg) |
| 272 |
{ |
| 273 |
// print Source MV (Owned only) |
| 274 |
for (int v = 0; v < nVec; v++) { |
| 275 |
std::cout << me << " SOURCE " << msg << " FEMV[" << v << "] Owned: "; |
| 276 |
auto value = femv->getData(v); |
| 277 |
for (lno_t i = 0; i < nLocalOwned; i++) std::cout << value[i] << " "; |
| 278 |
std::cout << std::endl; |
| 279 |
} |
| 280 |
|
| 281 |
// print Target MV (Owned + copies) |
| 282 |
femv->switchActiveMultiVector(); // needed to allow printing copies |
| 283 |
for (int v = 0; v < nVec; v++) { |
| 284 |
std::cout << me << " TARGET " << msg << " FEMV[" << v << "] Owned: "; |
| 285 |
auto value = femv->getData(v); |
| 286 |
for (lno_t i = 0; i < nLocalOwned; i++) std::cout << value[i] << " "; |
| 287 |
std::cout << " Copies: "; |
| 288 |
for (lno_t i = nLocalOwned; i < nLocalOwned+nLocalCopy; i++) |
| 289 |
std::cout << value[i] << " "; |
| 290 |
std::cout << std::endl; |
| 291 |
} |
| 292 |
femv->switchActiveMultiVector(); // restore state |
| 293 |
} |
| 294 |
|
| 295 |
/////////////////////////////////////////////////////////////////////////////// |
| 296 |
|
| 297 |
int main(int narg, char **arg) |
| 298 |
{ |
| 299 |
Tpetra::ScopeGuard scope(&narg, &arg); |
| 300 |
Teuchos::RCP<const Teuchos::Comm<int> > comm = Tpetra::getDefaultComm(); |
| 301 |
int me = comm->getRank(); |
| 302 |
int ierr = 0; |
| 303 |
|
| 304 |
FEMultiVectorTest femvTest(comm); |
| 305 |
|
| 306 |
try { |
| 307 |
ierr = femvTest.intTest(); |
| 308 |
} |
| 309 |
catch (std::exception &e) { |
| 310 |
std::cout << "FAIL: Exception thrown in intTest " << e.what() << std::endl; |
| 311 |
return 0; |
| 312 |
} |
| 313 |
|
| 314 |
int gerr = 0; |
| 315 |
Teuchos::reduceAll<int, int>(*comm, Teuchos::REDUCE_SUM, 1, &ierr, &gerr); |
| 316 |
|
| 317 |
if (me == 0) { |
| 318 |
if (gerr == 0) std::cout << "PASS" << std::endl; |
| 319 |
else std::cout << "FAIL: " << gerr << " failures" << std::endl; |
| 320 |
} |
| 321 |
|
| 322 |
return 0; |
| 323 |
} |