| 1 |
// @HEADER |
| 2 |
// ***************************************************************************** |
| 3 |
// MueLu: A package for multigrid based preconditioning |
| 4 |
// |
| 5 |
// Copyright 2012 NTESS and the MueLu contributors. |
| 6 |
// SPDX-License-Identifier: BSD-3-Clause |
| 7 |
// ***************************************************************************** |
| 8 |
// @HEADER |
| 9 |
|
| 10 |
#include <cstdio> |
| 11 |
#include <iomanip> |
| 12 |
#include <iostream> |
| 13 |
#include <unistd.h> |
| 14 |
|
| 15 |
#include <Teuchos_XMLParameterListHelpers.hpp> |
| 16 |
#include <Teuchos_StandardCatchMacros.hpp> |
| 17 |
|
| 18 |
// Xpetra |
| 19 |
#include <Xpetra_MultiVectorFactory.hpp> |
| 20 |
#include <Xpetra_ImportFactory.hpp> |
| 21 |
#include <Xpetra_IO.hpp> |
| 22 |
|
| 23 |
// Galeri |
| 24 |
#include <Galeri_XpetraParameters.hpp> |
| 25 |
#include <Galeri_XpetraProblemFactory.hpp> |
| 26 |
#include <Galeri_XpetraUtils.hpp> |
| 27 |
#include <Galeri_XpetraMaps.hpp> |
| 28 |
|
| 29 |
#include <MueLu.hpp> |
| 30 |
|
| 31 |
#include <MueLu_BaseClass.hpp> |
| 32 |
#ifdef HAVE_MUELU_EXPLICIT_INSTANTIATION |
| 33 |
#include <MueLu_ExplicitInstantiation.hpp> |
| 34 |
#endif |
| 35 |
#include <MueLu_Level.hpp> |
| 36 |
#include <MueLu_MutuallyExclusiveTime.hpp> |
| 37 |
#include <MueLu_ParameterListInterpreter.hpp> |
| 38 |
#include <MueLu_Utilities.hpp> |
| 39 |
|
| 40 |
#ifdef HAVE_MUELU_EPETRA |
| 41 |
#include <EpetraExt_MMHelpers.h> |
| 42 |
#include <EpetraExt_RowMatrixOut.h> |
| 43 |
#endif |
| 44 |
|
| 45 |
#include <TpetraExt_MatrixMatrix.hpp> |
| 46 |
|
| 47 |
#include <MueLu_CreateXpetraPreconditioner.hpp> |
| 48 |
|
| 49 |
using Teuchos::RCP; |
| 50 |
using Teuchos::rcp; |
| 51 |
using Teuchos::TimeMonitor; |
| 52 |
|
| 53 |
// ========================================================================= |
| 54 |
// ========================================================================= |
| 55 |
// ========================================================================= |
| 56 |
// Provide a "resize" operation for double*'s. |
| 57 |
inline void resize_doubles(int nold, int nnew, double*& d) { |
| 58 |
if (nnew > nold) { |
| 59 |
double* tmp = new double[nnew]; |
| 60 |
for (int i = 0; i < nold; i++) |
| 61 |
tmp[i] = d[i]; |
| 62 |
delete[] d; |
| 63 |
d = tmp; |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
// ========================================================================= |
| 68 |
// ========================================================================= |
| 69 |
// ========================================================================= |
| 70 |
#if defined(HAVE_MUELU_EPETRA) |
| 71 |
|
| 72 |
extern void MakeColMapAndReindexSort(int& NumRemoteColGIDs, int*& RemoteColindices, |
| 73 |
std::vector<int>& RemotePermuteIDs, std::vector<int>& RemoteOwningPIDs); |
| 74 |
|
| 75 |
extern void MakeColMapAndReindexSort(int& NumRemoteColGIDs, long long*& RemoteColindices, |
| 76 |
std::vector<int>& RemotePermuteIDs, std::vector<int>& RemoteOwningPIDs); |
| 77 |
|
| 78 |
void build_remote_pids(int MyPID, const std::vector<int>& ColMapOwningPIDs, std::vector<int>& RemotePIDs) { |
| 79 |
// Presume the column map has Aztec ordering |
| 80 |
int N = (int)ColMapOwningPIDs.size(); |
| 81 |
int first_idx; |
| 82 |
for (first_idx = 0; first_idx < N; first_idx++) |
| 83 |
if (ColMapOwningPIDs[first_idx] != MyPID) |
| 84 |
break; |
| 85 |
|
| 86 |
/* printf("[%d] ColMapOwningPIDs(%d) =",MyPID,(int)ColMapOwningPIDs.size()); |
| 87 |
for(int i=0;i<(int)ColMapOwningPIDs.size(); i++) |
| 88 |
printf("%d ",ColMapOwningPIDs[i]); |
| 89 |
printf("\n");*/ |
| 90 |
|
| 91 |
// Make sure there are some non-local unknowns |
| 92 |
if (first_idx == N) { |
| 93 |
printf("[%d] No remotes\n", MyPID); |
| 94 |
return; |
| 95 |
} |
| 96 |
|
| 97 |
RemotePIDs.resize(ColMapOwningPIDs.size() - first_idx); |
| 98 |
for (int i = first_idx; i < N; i++) |
| 99 |
RemotePIDs[i - first_idx] = ColMapOwningPIDs[i]; |
| 100 |
|
| 101 |
/* printf("[%d] RemotePIDs(%d) =",MyPID,(int)RemotePIDs.size()); |
| 102 |
for(int i=0;i<(int)RemotePIDs.size(); i++) |
| 103 |
printf("%d ",RemotePIDs[i]); |
| 104 |
printf("\n");*/ |
| 105 |
} |
| 106 |
|
| 107 |
Epetra_Map* convert_lightweightmap_to_map(const EpetraExt::LightweightMap& A, const Epetra_Comm& Comm) { |
| 108 |
Epetra_Map* Aout = 0; |
| 109 |
if (A.GlobalIndicesInt()) { |
| 110 |
#ifndef EPETRA_NO_32BIT_GLOBAL_INDICES |
| 111 |
Aout = new Epetra_Map(-1, A.NumMyElements(), A.MyGlobalElements(), 0, Comm); |
| 112 |
#endif |
| 113 |
} else if (A.GlobalIndicesLongLong()) { |
| 114 |
#ifndef EPETRA_NO_64BIT_GLOBAL_INDICES |
| 115 |
Aout = new Epetra_Map((long long)-1, A.NumMyElements(), A.MyGlobalElements64(), (long long)0, Comm); |
| 116 |
#endif |
| 117 |
} |
| 118 |
|
| 119 |
return Aout; |
| 120 |
} |
| 121 |
|
| 122 |
Epetra_CrsMatrix* convert_lightweightcrsmatrix_to_crsmatrix(const EpetraExt::LightweightCrsMatrix& A) { |
| 123 |
auto tm = TimeMonitor::getNewTimer("OptimizedTransfer: Convert: MapConstructor"); |
| 124 |
const Epetra_Comm& Comm = A.DomainMap_.Comm(); |
| 125 |
|
| 126 |
// Build Maps |
| 127 |
Epetra_Map *RowMap, *ColMap; |
| 128 |
if (A.use_lw) |
| 129 |
RowMap = convert_lightweightmap_to_map(*A.RowMapLW_, Comm); |
| 130 |
else |
| 131 |
throw std::runtime_error("Only works in LW mode"); |
| 132 |
ColMap = convert_lightweightmap_to_map(A.ColMap_, Comm); |
| 133 |
Epetra_CrsMatrix* Aout = new Epetra_CrsMatrix(Copy, *RowMap, *ColMap, 0); |
| 134 |
int N = RowMap->NumMyElements(); |
| 135 |
int nnz = A.colind_.size(); |
| 136 |
|
| 137 |
tm = Teuchos::null; |
| 138 |
// Copy pointers over |
| 139 |
auto tm2 = TimeMonitor::getNewTimer("OptimizedTransfer: Convert: Data Copy"); |
| 140 |
Epetra_IntSerialDenseVector& rowptr = Aout->ExpertExtractIndexOffset(); |
| 141 |
Epetra_IntSerialDenseVector& colind = Aout->ExpertExtractIndices(); |
| 142 |
double*& vals = Aout->ExpertExtractValues(); |
| 143 |
rowptr.Resize(N + 1); |
| 144 |
colind.Resize(nnz); |
| 145 |
resize_doubles(0, nnz, vals); |
| 146 |
|
| 147 |
for (int i = 0; i < N + 1; i++) |
| 148 |
rowptr[i] = A.rowptr_[i]; |
| 149 |
|
| 150 |
for (int i = 0; i < nnz; i++) { |
| 151 |
colind[i] = A.colind_[i]; |
| 152 |
vals[i] = A.vals_[i]; |
| 153 |
} |
| 154 |
tm2 = Teuchos::null; |
| 155 |
auto tm3 = TimeMonitor::getNewTimer("OptimizedTransfer: Convert: BuildRemote"); |
| 156 |
|
| 157 |
// Get RemotePIDs |
| 158 |
std::vector<int> RemotePIDs_; |
| 159 |
build_remote_pids(Comm.MyPID(), A.ColMapOwningPIDs_, RemotePIDs_); |
| 160 |
|
| 161 |
tm3 = Teuchos::null; |
| 162 |
auto tm4 = TimeMonitor::getNewTimer("OptimizedTransfer: Convert: BuildImport"); |
| 163 |
|
| 164 |
// Importer build |
| 165 |
const int* ExportLIDs = A.ExportLIDs_.size() ? &A.ExportLIDs_[0] : 0; |
| 166 |
const int* ExportPIDs = A.ExportPIDs_.size() ? &A.ExportPIDs_[0] : 0; |
| 167 |
const int* RemotePIDs = RemotePIDs_.size() ? &RemotePIDs_[0] : 0; |
| 168 |
Epetra_Import* Importer = new Epetra_Import(*ColMap, A.DomainMap_, RemotePIDs_.size(), RemotePIDs, A.ExportLIDs_.size(), ExportLIDs, ExportPIDs); |
| 169 |
|
| 170 |
tm4 = Teuchos::null; |
| 171 |
auto tm5 = TimeMonitor::getNewTimer("OptimizedTransfer: Convert: ESFC"); |
| 172 |
|
| 173 |
// ESFC |
| 174 |
Aout->ExpertStaticFillComplete(A.DomainMap_, *RowMap, Importer, 0); |
| 175 |
|
| 176 |
// Cleanup |
| 177 |
if (A.use_lw) delete RowMap; |
| 178 |
delete ColMap; |
| 179 |
|
| 180 |
return Aout; |
| 181 |
} |
| 182 |
|
| 183 |
#endif |
| 184 |
|
| 185 |
// ========================================================================= |
| 186 |
// ========================================================================= |
| 187 |
// ========================================================================= |
| 188 |
#if defined(HAVE_MUELU_EPETRA) |
| 189 |
bool epetra_check_importer_correctness(const Epetra_Import& A, const Epetra_Import& B) { |
| 190 |
int MyPID = A.SourceMap().Comm().MyPID(); |
| 191 |
bool is_correct = true; |
| 192 |
|
| 193 |
// Same |
| 194 |
if (A.NumSameIDs() != B.NumSameIDs()) { |
| 195 |
printf("[%d] NumSameIDs %d vs. %d\n", MyPID, A.NumSameIDs(), B.NumSameIDs()); |
| 196 |
is_correct = false; |
| 197 |
} |
| 198 |
// Permutes |
| 199 |
if (A.NumPermuteIDs() != B.NumPermuteIDs()) { |
| 200 |
printf("[%d] NumPermuteIDs %d vs. %d\n", MyPID, A.NumPermuteIDs(), B.NumPermuteIDs()); |
| 201 |
is_correct = false; |
| 202 |
} else { |
| 203 |
int N = A.NumPermuteIDs(); |
| 204 |
bool error_detected = false; |
| 205 |
for (int i = 0; !error_detected && i < N; i++) |
| 206 |
error_detected = (A.PermuteFromLIDs()[i] != B.PermuteFromLIDs()[i]) || (A.PermuteToLIDs()[i] != B.PermuteToLIDs()[i]); |
| 207 |
|
| 208 |
if (error_detected) { |
| 209 |
printf("[%d] A Permutes = ", MyPID); |
| 210 |
for (int i = 0; i < N; i++) |
| 211 |
printf("%d->%d ", A.PermuteFromLIDs()[i], A.PermuteToLIDs()[i]); |
| 212 |
printf("\n[%d] B Permutes = ", MyPID); |
| 213 |
for (int i = 0; i < N; i++) |
| 214 |
printf("%d->%d ", B.PermuteFromLIDs()[i], B.PermuteToLIDs()[i]); |
| 215 |
printf("\n"); |
| 216 |
is_correct = false; |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
// Remotes |
| 221 |
if (A.NumRemoteIDs() != B.NumRemoteIDs()) { |
| 222 |
printf("[%d] NumRemoteIDs %d vs. %d\n", MyPID, A.NumRemoteIDs(), B.NumRemoteIDs()); |
| 223 |
is_correct = false; |
| 224 |
} else { |
| 225 |
int N = A.NumRemoteIDs(); |
| 226 |
bool error_detected = false; |
| 227 |
for (int i = 0; !error_detected && i < N; i++) |
| 228 |
error_detected = A.RemoteLIDs()[i] != B.RemoteLIDs()[i]; |
| 229 |
|
| 230 |
if (error_detected) { |
| 231 |
printf("[%d] A RemoteLIDs = ", MyPID); |
| 232 |
for (int i = 0; i < N; i++) |
| 233 |
printf("%d ", A.RemoteLIDs()[i]); |
| 234 |
printf("\n[%d] B RemoteLIDs = ", MyPID); |
| 235 |
for (int i = 0; i < N; i++) |
| 236 |
printf("%d ", B.RemoteLIDs()[i]); |
| 237 |
printf("\n"); |
| 238 |
is_correct = false; |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
// Exports |
| 243 |
if (A.NumExportIDs() != B.NumExportIDs()) { |
| 244 |
printf("[%d] NumExportIDs %d vs. %d\n", MyPID, A.NumExportIDs(), B.NumExportIDs()); |
| 245 |
is_correct = false; |
| 246 |
} else { |
| 247 |
int N = A.NumExportIDs(); |
| 248 |
bool error_detected = false; |
| 249 |
for (int i = 0; !error_detected && i < N; i++) |
| 250 |
error_detected = (A.ExportLIDs()[i] != B.ExportLIDs()[i]) || (A.ExportPIDs()[i] != B.ExportPIDs()[i]); |
| 251 |
|
| 252 |
if (error_detected) { |
| 253 |
printf("[%d] A Exports(%d) = ", MyPID, A.NumExportIDs()); |
| 254 |
for (int i = 0; i < N; i++) |
| 255 |
printf("%d(%d)->%d ", A.ExportLIDs()[i], A.SourceMap().GID(A.ExportLIDs()[i]), A.ExportPIDs()[i]); |
| 256 |
printf("\n[%d] B Exports(%d) = ", MyPID, B.NumExportIDs()); |
| 257 |
for (int i = 0; i < N; i++) |
| 258 |
printf("%d(%d)->%d ", B.ExportLIDs()[i], B.SourceMap().GID(A.ExportLIDs()[i]), B.ExportPIDs()[i]); |
| 259 |
printf("\n"); |
| 260 |
is_correct = false; |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
// Message Counts |
| 265 |
if (A.NumSend() != B.NumSend()) { |
| 266 |
printf("[%d] NumSend %d vs. %d\n", MyPID, A.NumSend(), B.NumSend()); |
| 267 |
is_correct = false; |
| 268 |
} |
| 269 |
if (A.NumRecv() != B.NumRecv()) { |
| 270 |
printf("[%d] NumRecv %d vs. %d\n", MyPID, A.NumRecv(), B.NumRecv()); |
| 271 |
is_correct = false; |
| 272 |
} |
| 273 |
|
| 274 |
#ifdef HAVE_MPI |
| 275 |
const Epetra_MpiDistributor& Ad = *dynamic_cast<Epetra_MpiDistributor*>(&A.Distributor()); |
| 276 |
const Epetra_MpiDistributor& Bd = *dynamic_cast<Epetra_MpiDistributor*>(&B.Distributor()); |
| 277 |
|
| 278 |
if (Ad.MaxSendLength() != Bd.MaxSendLength()) { |
| 279 |
printf("[%d] Distor.MaxSendLength %d vs. %d\n", MyPID, Ad.MaxSendLength(), Bd.MaxSendLength()); |
| 280 |
is_correct = false; |
| 281 |
} |
| 282 |
if (Ad.TotalReceiveLength() != Bd.TotalReceiveLength()) { |
| 283 |
printf("[%d] Distor.TotalReceiveLength %d vs. %d\n", MyPID, Ad.TotalReceiveLength(), Bd.TotalReceiveLength()); |
| 284 |
is_correct = false; |
| 285 |
} |
| 286 |
|
| 287 |
if (Ad.NumSends() != Bd.NumSends()) { |
| 288 |
printf("[%d] Distor.NumSends %d vs. %d\n", MyPID, Ad.NumSends(), Bd.NumSends()); |
| 289 |
is_correct = false; |
| 290 |
} else { |
| 291 |
int N = Ad.NumSends(); |
| 292 |
bool error_detected = false; |
| 293 |
for (int i = 0; !error_detected && i < N; i++) |
| 294 |
error_detected = (Ad.ProcsTo()[i] != Bd.ProcsTo()[i]) || (Ad.LengthsTo()[i] != Bd.LengthsTo()[i]); |
| 295 |
|
| 296 |
if (error_detected) { |
| 297 |
printf("[%d] Ad Sends = ", MyPID); |
| 298 |
for (int i = 0; i < N; i++) |
| 299 |
printf("%d->%d ", Ad.LengthsTo()[i], Ad.ProcsTo()[i]); |
| 300 |
printf("\n[%d] Bd Sends = ", MyPID); |
| 301 |
for (int i = 0; i < N; i++) |
| 302 |
printf("%d->%d ", Bd.LengthsTo()[i], Bd.ProcsTo()[i]); |
| 303 |
printf("\n"); |
| 304 |
is_correct = false; |
| 305 |
} |
| 306 |
} |
| 307 |
|
| 308 |
if (Ad.NumReceives() != Bd.NumReceives()) { |
| 309 |
printf("[%d] Distor.NumReceives %d vs. %d\n", MyPID, Ad.NumReceives(), Bd.NumReceives()); |
| 310 |
is_correct = false; |
| 311 |
} else { |
| 312 |
int N = Ad.NumReceives(); |
| 313 |
bool error_detected = false; |
| 314 |
for (int i = 0; !error_detected && i < N; i++) |
| 315 |
error_detected = (Ad.ProcsFrom()[i] != Bd.ProcsFrom()[i]) || (Ad.LengthsFrom()[i] != Bd.LengthsFrom()[i]); |
| 316 |
|
| 317 |
if (error_detected) { |
| 318 |
printf("[%d] Ad Receives = ", MyPID); |
| 319 |
for (int i = 0; i < N; i++) |
| 320 |
printf("%d->%d ", Ad.LengthsFrom()[i], Ad.ProcsFrom()[i]); |
| 321 |
printf("\n[%d] Bd Receives = ", MyPID); |
| 322 |
for (int i = 0; i < N; i++) |
| 323 |
printf("%d->%d ", Bd.LengthsFrom()[i], Bd.ProcsFrom()[i]); |
| 324 |
printf("\n"); |
| 325 |
is_correct = false; |
| 326 |
} |
| 327 |
} |
| 328 |
#endif |
| 329 |
|
| 330 |
return is_correct; |
| 331 |
} |
| 332 |
#endif // if defined(HAVE_MUELU_EPETRA) |
| 333 |
|
| 334 |
// ========================================================================= |
| 335 |
// ========================================================================= |
| 336 |
// ========================================================================= |
| 337 |
template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node> |
| 338 |
void TestTransfer(Teuchos::RCP<Xpetra::Matrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> > A, Teuchos::RCP<Xpetra::Matrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> > P) { |
| 339 |
#include <MueLu_UseShortNames.hpp> |
| 340 |
Xpetra::UnderlyingLib lib = A->getRowMap()->lib(); |
| 341 |
|
| 342 |
if (lib == Xpetra::UseTpetra) { |
| 343 |
typedef Tpetra::CrsMatrixStruct<SC, LO, GO, NO> crs_matrix_struct_type; |
| 344 |
typedef Tpetra::CrsMatrix<SC, LO, GO, NO> crs_matrix_type; |
| 345 |
typedef Tpetra::Import<LO, GO, NO> import_type; |
| 346 |
|
| 347 |
RCP<const crs_matrix_type> Au = toTpetra(A); |
| 348 |
RCP<const crs_matrix_type> Pu = toTpetra(P); |
| 349 |
if (Au->getComm()->getSize() == 1) return; |
| 350 |
|
| 351 |
// ================== |
| 352 |
// Optimized Transfer |
| 353 |
// ================== |
| 354 |
auto tm = TimeMonitor::getNewTimer("OptimizedTransfer: Import"); |
| 355 |
crs_matrix_struct_type Pview; |
| 356 |
Tpetra::MMdetails::import_and_extract_views(*Pu, Au->getColMap(), Pview, Au->getGraph()->getImporter(), false, "ImportPerf: "); |
| 357 |
|
| 358 |
Au->getComm()->barrier(); |
| 359 |
|
| 360 |
// ================== |
| 361 |
// Naive Transfer |
| 362 |
// ================== |
| 363 |
// Use the columnmap from Aopt and build an importer ex nihilo |
| 364 |
|
| 365 |
tm = Teuchos::null; |
| 366 |
auto tm2 = TimeMonitor::getNewTimer("NaiveTransfer: BuildImport"); |
| 367 |
import_type NaiveImport(Pview.importMatrix->getColMap(), Pu->getDomainMap()); |
| 368 |
Au->getComm()->barrier(); |
| 369 |
} else if (lib == Xpetra::UseEpetra) { |
| 370 |
#if defined(HAVE_MUELU_EPETRA) |
| 371 |
RCP<const Epetra_CrsMatrix> Au = Utilities::Op2EpetraCrs(A); |
| 372 |
RCP<const Epetra_CrsMatrix> Pu = Utilities::Op2EpetraCrs(P); |
| 373 |
if (Au->Comm().NumProc() == 1) return; |
| 374 |
|
| 375 |
// ================== |
| 376 |
// Optimized Transfer |
| 377 |
// ================== |
| 378 |
// Build the LightweightCrsMatrix |
| 379 |
|
| 380 |
auto tm3 = TimeMonitor::getNewTimer("OptimizedTransfer: Import"); |
| 381 |
EpetraExt::CrsMatrixStruct Pview; |
| 382 |
bool SortGhosts = true; |
| 383 |
|
| 384 |
if (Au->RowMap().GlobalIndicesInt()) { |
| 385 |
#ifndef EPETRA_NO_32BIT_GLOBAL_INDICES |
| 386 |
EpetraExt::import_only<int>(*Pu, Au->ColMap(), Pview, Au->Importer(), SortGhosts, "ImportPerf: "); |
| 387 |
#endif |
| 388 |
} else if (Au->RowMap().GlobalIndicesInt()) { |
| 389 |
#ifndef EPETRA_NO_64BIT_GLOBAL_INDICES |
| 390 |
EpetraExt::import_only<long long>(*Pu, Au->ColMap(), Pview, Au->Importer(), SortGhosts, "ImportPerf: "); |
| 391 |
#endif |
| 392 |
} |
| 393 |
tm3 = Teuchos::null; |
| 394 |
auto tm4 = TimeMonitor::getNewTimer("OptimizedTransfer: Convert"); |
| 395 |
Epetra_CrsMatrix* Aopt = convert_lightweightcrsmatrix_to_crsmatrix(*Pview.importMatrix); |
| 396 |
|
| 397 |
Au->Comm().Barrier(); |
| 398 |
// ================== |
| 399 |
// Naive Transfer |
| 400 |
// ================== |
| 401 |
// Use the columnmap from Aopt and build an importer ex nihilo |
| 402 |
tm4 = Teuchos::null; |
| 403 |
auto tm5 = TimeMonitor::getNewTimer("NaiveTransfer: BuildImport"); |
| 404 |
const Epetra_Map& NaiveColMap = Aopt->ColMap(); |
| 405 |
Epetra_Import NaiveImport(NaiveColMap, Pu->DomainMap()); |
| 406 |
|
| 407 |
Au->Comm().Barrier(); |
| 408 |
|
| 409 |
// Check importer for correctness |
| 410 |
fflush(stdout); |
| 411 |
const Epetra_Import* OptImport = Aopt->Importer(); |
| 412 |
bool is_correct = epetra_check_importer_correctness(NaiveImport, *OptImport); |
| 413 |
fflush(stdout); |
| 414 |
int is_OK_local = is_correct, is_OK_global; |
| 415 |
Au->Comm().MinAll(&is_OK_local, &is_OK_global, 1); |
| 416 |
if (!is_OK_global) throw std::runtime_error("Importer correctness test failed."); |
| 417 |
|
| 418 |
// Cleanup |
| 419 |
delete Aopt; |
| 420 |
#endif // defined(HAVE_MUELU_EPETRA) |
| 421 |
} |
| 422 |
} |
| 423 |
|
| 424 |
// ========================================================================= |
| 425 |
// ========================================================================= |
| 426 |
// ========================================================================= |
| 427 |
template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node> |
| 428 |
int main_(Teuchos::CommandLineProcessor& clp, Xpetra::UnderlyingLib& lib, int argc, char* argv[]) { |
| 429 |
#include <MueLu_UseShortNames.hpp> |
| 430 |
using Teuchos::ArrayRCP; |
| 431 |
using Teuchos::ParameterList; |
| 432 |
using Teuchos::RCP; |
| 433 |
using Teuchos::rcp; |
| 434 |
|
| 435 |
// ========================================================================= |
| 436 |
// MPI initialization using Teuchos |
| 437 |
// ========================================================================= |
| 438 |
RCP<const Teuchos::Comm<int> > comm = Teuchos::DefaultComm<int>::getComm(); |
| 439 |
|
| 440 |
// ========================================================================= |
| 441 |
// Convenient definitions |
| 442 |
// ========================================================================= |
| 443 |
typedef Teuchos::ScalarTraits<SC> STS; |
| 444 |
SC one = STS::one(); |
| 445 |
typedef typename STS::coordinateType real_type; |
| 446 |
typedef Xpetra::MultiVector<real_type, LO, GO, NO> RealValuedMultiVector; |
| 447 |
|
| 448 |
// ========================================================================= |
| 449 |
// Parameters initialization |
| 450 |
// ========================================================================= |
| 451 |
GO nx = 100, ny = 100, nz = 100; |
| 452 |
Galeri::Xpetra::Parameters<GO> galeriParameters(clp, nx, ny, nz, "Laplace2D"); // manage parameters of the test case |
| 453 |
Xpetra::Parameters xpetraParameters(clp); // manage parameters of Xpetra |
| 454 |
|
| 455 |
std::string xmlFileName = "import.xml"; |
| 456 |
clp.setOption("xml", &xmlFileName, "read parameters from a file"); |
| 457 |
bool printTimings = true; |
| 458 |
clp.setOption("timings", "notimings", &printTimings, "print timings to screen"); |
| 459 |
std::string timingsFormat = "table-fixed"; |
| 460 |
clp.setOption("time-format", &timingsFormat, "timings format (table-fixed | table-scientific | yaml)"); |
| 461 |
int numImports = 100; |
| 462 |
clp.setOption("numImport", &numImports, "#times to test"); |
| 463 |
int MM_TAFC_OptCoreCnt = 3000; |
| 464 |
clp.setOption("MM_TAFC_OptimizationCoreCount", &MM_TAFC_OptCoreCnt, "Num Cores above which Optimized MatrixMatrix transferAndFillComplete is used"); |
| 465 |
|
| 466 |
switch (clp.parse(argc, argv)) { |
| 467 |
case Teuchos::CommandLineProcessor::PARSE_HELP_PRINTED: return EXIT_SUCCESS; |
| 468 |
case Teuchos::CommandLineProcessor::PARSE_ERROR: |
| 469 |
case Teuchos::CommandLineProcessor::PARSE_UNRECOGNIZED_OPTION: return EXIT_FAILURE; |
| 470 |
case Teuchos::CommandLineProcessor::PARSE_SUCCESSFUL: break; |
| 471 |
} |
| 472 |
|
| 473 |
ParameterList paramList; |
| 474 |
Teuchos::updateParametersFromXmlFileAndBroadcast(xmlFileName, Teuchos::Ptr<ParameterList>(¶mList), *comm); |
| 475 |
|
| 476 |
ParameterList& mmlist = paramList.sublist("matrixmatrix: kernel params", false); |
| 477 |
int commandcc = mmlist.get("MM_TAFC_OptimizationCoreCount", MM_TAFC_OptCoreCnt); |
| 478 |
commandcc = paramList.get("MM_TAFC_OptimizationCoreCount", commandcc); |
| 479 |
paramList.remove("MM_TAFC_OptimizationCoreCount", false); |
| 480 |
mmlist.set("MM_TAFC_OptimizationCoreCount", commandcc); |
| 481 |
|
| 482 |
bool isDriver = paramList.isSublist("Run1"); |
| 483 |
if (isDriver) { |
| 484 |
// update galeriParameters with the values from the XML file |
| 485 |
ParameterList& realParams = galeriParameters.GetParameterList(); |
| 486 |
|
| 487 |
for (ParameterList::ConstIterator it = realParams.begin(); it != realParams.end(); it++) { |
| 488 |
const std::string& name = realParams.name(it); |
| 489 |
if (paramList.isParameter(name)) |
| 490 |
realParams.setEntry(name, paramList.getEntry(name)); |
| 491 |
} |
| 492 |
} |
| 493 |
|
| 494 |
// Retrieve matrix parameters (they may have been changed on the command line) |
| 495 |
// [for instance, if we changed matrix type from 2D to 3D we need to update nz] |
| 496 |
ParameterList galeriList = galeriParameters.GetParameterList(); |
| 497 |
|
| 498 |
// ========================================================================= |
| 499 |
// Problem construction |
| 500 |
// ========================================================================= |
| 501 |
std::ostringstream galeriStream; |
| 502 |
comm->barrier(); |
| 503 |
|
| 504 |
RCP<Matrix> A; |
| 505 |
RCP<const Map> map; |
| 506 |
RCP<RealValuedMultiVector> coordinates; |
| 507 |
typedef typename RealValuedMultiVector::scalar_type Real; |
| 508 |
RCP<MultiVector> nullspace; |
| 509 |
std::string matrixType = galeriParameters.GetMatrixType(); |
| 510 |
|
| 511 |
RCP<TimeMonitor> globalTimeMonitor = rcp(new TimeMonitor(*TimeMonitor::getNewTimer("Driver: S - Global Time"))); |
| 512 |
{ |
| 513 |
auto tm = rcp(new TimeMonitor(*TimeMonitor::getNewTimer("Driver: 1 - Matrix Build"))); |
| 514 |
|
| 515 |
galeriStream << "========================================================\n" |
| 516 |
<< xpetraParameters << galeriParameters; |
| 517 |
|
| 518 |
// Galeri will attempt to create a square-as-possible distribution of subdomains di, e.g., |
| 519 |
// d1 d2 d3 |
| 520 |
// d4 d5 d6 |
| 521 |
// d7 d8 d9 |
| 522 |
// d10 d11 d12 |
| 523 |
// A perfect distribution is only possible when the #processors is a perfect square. |
| 524 |
// This *will* result in "strip" distribution if the #processors is a prime number or if the factors are very different in |
| 525 |
// size. For example, np=14 will give a 7-by-2 distribution. |
| 526 |
// If you don't want Galeri to do this, specify mx or my on the galeriList. |
| 527 |
|
| 528 |
// Create map and coordinates |
| 529 |
// In the future, we hope to be able to first create a Galeri problem, and then request map and coordinates from it |
| 530 |
// At the moment, however, things are fragile as we hope that the Problem uses same map and coordinates inside |
| 531 |
if (matrixType == "Laplace1D") { |
| 532 |
map = Galeri::Xpetra::CreateMap<LO, GO, Node>(xpetraParameters.GetLib(), "Cartesian1D", comm, galeriList); |
| 533 |
coordinates = Galeri::Xpetra::Utils::CreateCartesianCoordinates<Real, LO, GO, Map, RealValuedMultiVector>("1D", map, galeriList); |
| 534 |
|
| 535 |
} else if (matrixType == "Laplace2D" || matrixType == "Star2D" || |
| 536 |
matrixType == "BigStar2D" || matrixType == "Elasticity2D") { |
| 537 |
map = Galeri::Xpetra::CreateMap<LO, GO, Node>(xpetraParameters.GetLib(), "Cartesian2D", comm, galeriList); |
| 538 |
coordinates = Galeri::Xpetra::Utils::CreateCartesianCoordinates<Real, LO, GO, Map, RealValuedMultiVector>("2D", map, galeriList); |
| 539 |
|
| 540 |
} else if (matrixType == "Laplace3D" || matrixType == "Brick3D" || matrixType == "Elasticity3D") { |
| 541 |
map = Galeri::Xpetra::CreateMap<LO, GO, Node>(xpetraParameters.GetLib(), "Cartesian3D", comm, galeriList); |
| 542 |
coordinates = Galeri::Xpetra::Utils::CreateCartesianCoordinates<Real, LO, GO, Map, RealValuedMultiVector>("3D", map, galeriList); |
| 543 |
} |
| 544 |
|
| 545 |
// Expand map to do multiple DOF per node for block problems |
| 546 |
if (matrixType == "Elasticity2D") |
| 547 |
map = Xpetra::MapFactory<LO, GO, Node>::Build(map, 2); |
| 548 |
if (matrixType == "Elasticity3D") |
| 549 |
map = Xpetra::MapFactory<LO, GO, Node>::Build(map, 3); |
| 550 |
|
| 551 |
galeriStream << "Processor subdomains in x direction: " << galeriList.get<GO>("mx") << std::endl |
| 552 |
<< "Processor subdomains in y direction: " << galeriList.get<GO>("my") << std::endl |
| 553 |
<< "Processor subdomains in z direction: " << galeriList.get<GO>("mz") << std::endl |
| 554 |
<< "========================================================" << std::endl; |
| 555 |
|
| 556 |
if (matrixType == "Elasticity2D" || matrixType == "Elasticity3D") { |
| 557 |
// Our default test case for elasticity: all boundaries of a square/cube have Neumann b.c. except left which has Dirichlet |
| 558 |
galeriList.set("right boundary", "Neumann"); |
| 559 |
galeriList.set("bottom boundary", "Neumann"); |
| 560 |
galeriList.set("top boundary", "Neumann"); |
| 561 |
galeriList.set("front boundary", "Neumann"); |
| 562 |
galeriList.set("back boundary", "Neumann"); |
| 563 |
} |
| 564 |
|
| 565 |
RCP<Galeri::Xpetra::Problem<Map, CrsMatrixWrap, MultiVector> > Pr = |
| 566 |
Galeri::Xpetra::BuildProblem<SC, LO, GO, Map, CrsMatrixWrap, MultiVector>(galeriParameters.GetMatrixType(), map, galeriList); |
| 567 |
A = Pr->BuildMatrix(); |
| 568 |
|
| 569 |
if (matrixType == "Elasticity2D" || |
| 570 |
matrixType == "Elasticity3D") { |
| 571 |
nullspace = Pr->BuildNullspace(); |
| 572 |
A->SetFixedBlockSize((galeriParameters.GetMatrixType() == "Elasticity2D") ? 2 : 3); |
| 573 |
} |
| 574 |
|
| 575 |
comm->barrier(); |
| 576 |
tm = Teuchos::null; |
| 577 |
} |
| 578 |
|
| 579 |
galeriStream << "Galeri complete.\n========================================================" << std::endl; |
| 580 |
|
| 581 |
int numReruns = 1; |
| 582 |
if (paramList.isParameter("number of reruns")) |
| 583 |
numReruns = paramList.get<int>("number of reruns"); |
| 584 |
|
| 585 |
const bool mustAlreadyExist = true; |
| 586 |
for (int rerunCount = 1; rerunCount <= numReruns; rerunCount++) { |
| 587 |
ParameterList mueluList, runList; |
| 588 |
|
| 589 |
bool stop = false; |
| 590 |
if (isDriver) { |
| 591 |
runList = paramList.sublist("Run1", mustAlreadyExist); |
| 592 |
mueluList = runList.sublist("MueLu", mustAlreadyExist); |
| 593 |
} else { |
| 594 |
mueluList = paramList; |
| 595 |
stop = true; |
| 596 |
} |
| 597 |
|
| 598 |
if (nullspace.is_null()) { |
| 599 |
int blkSize = 1; |
| 600 |
if (mueluList.isSublist("Matrix")) { |
| 601 |
// Factory style parameter list |
| 602 |
const Teuchos::ParameterList& operatorList = paramList.sublist("Matrix"); |
| 603 |
if (operatorList.isParameter("PDE equations")) |
| 604 |
blkSize = operatorList.get<int>("PDE equations"); |
| 605 |
|
| 606 |
} else if (paramList.isParameter("number of equations")) { |
| 607 |
// Easy style parameter list |
| 608 |
blkSize = paramList.get<int>("number of equations"); |
| 609 |
} |
| 610 |
|
| 611 |
nullspace = MultiVectorFactory::Build(map, blkSize); |
| 612 |
for (int i = 0; i < blkSize; i++) { |
| 613 |
RCP<const Map> domainMap = A->getDomainMap(); |
| 614 |
GO indexBase = domainMap->getIndexBase(); |
| 615 |
|
| 616 |
ArrayRCP<SC> nsData = nullspace->getDataNonConst(i); |
| 617 |
for (int j = 0; j < nsData.size(); j++) { |
| 618 |
GO GID = domainMap->getGlobalElement(j) - indexBase; |
| 619 |
|
| 620 |
if ((GID - i) % blkSize == 0) |
| 621 |
nsData[j] = one; |
| 622 |
} |
| 623 |
} |
| 624 |
} |
| 625 |
|
| 626 |
int runCount = 1; |
| 627 |
do { |
| 628 |
int savedOut = -1; |
| 629 |
FILE* openedOut = NULL; |
| 630 |
if (isDriver) { |
| 631 |
if (runList.isParameter("filename")) { |
| 632 |
// Redirect all output into a filename We have to redirect all output, |
| 633 |
// including printf's, therefore we cannot simply replace C++ cout |
| 634 |
// buffers, and have to use heavy machinary (dup2) |
| 635 |
std::string filename = runList.get<std::string>("filename"); |
| 636 |
if (numReruns > 1) |
| 637 |
filename += "_run" + MueLu::toString(rerunCount); |
| 638 |
filename += (lib == Xpetra::UseEpetra ? ".epetra" : ".tpetra"); |
| 639 |
|
| 640 |
savedOut = dup(STDOUT_FILENO); |
| 641 |
openedOut = fopen(filename.c_str(), "w"); |
| 642 |
dup2(fileno(openedOut), STDOUT_FILENO); |
| 643 |
} |
| 644 |
} |
| 645 |
|
| 646 |
// Instead of checking each time for rank, create a rank 0 stream |
| 647 |
RCP<Teuchos::FancyOStream> fancy = Teuchos::fancyOStream(Teuchos::rcpFromRef(std::cout)); |
| 648 |
Teuchos::FancyOStream& out = *fancy; |
| 649 |
out.setOutputToRootOnly(0); |
| 650 |
|
| 651 |
out << galeriStream.str(); |
| 652 |
|
| 653 |
// ========================================================================= |
| 654 |
// Preconditioner construction |
| 655 |
// ========================================================================= |
| 656 |
comm->barrier(); |
| 657 |
|
| 658 |
RCP<Hierarchy> H; |
| 659 |
{ |
| 660 |
auto MueLuSU_D2 = TimeMonitor(*TimeMonitor::getNewTimer("Driver: 2 - MueLu Setup")); |
| 661 |
|
| 662 |
A->SetMaxEigenvalueEstimate(-one); |
| 663 |
if (lib == Xpetra::UseEpetra) { |
| 664 |
mueluList.set("use kokkos refactor", false); |
| 665 |
} |
| 666 |
Teuchos::ParameterList& userParamList = mueluList.sublist("user data"); |
| 667 |
userParamList.set<RCP<RealValuedMultiVector> >("Coordinates", coordinates); |
| 668 |
H = MueLu::CreateXpetraPreconditioner(A, mueluList); |
| 669 |
comm->barrier(); |
| 670 |
} |
| 671 |
|
| 672 |
// ========================================================================= |
| 673 |
// Grab useful pieces |
| 674 |
// ========================================================================= |
| 675 |
|
| 676 |
RCP<Matrix> P; |
| 677 |
{ |
| 678 |
auto MueLuES_D3 = TimeMonitor(*TimeMonitor::getNewTimer("Driver: 3 - Extract Stuff")); |
| 679 |
H->GetLevel(1)->Get("P", P); |
| 680 |
comm->barrier(); |
| 681 |
} |
| 682 |
|
| 683 |
RCP<Teuchos::ParameterList> opt_list = rcp(new Teuchos::ParameterList); |
| 684 |
opt_list->set("Timer Label", "OptTAFC"); |
| 685 |
|
| 686 |
for (int i = 0; i < numImports; i++) { |
| 687 |
// ========================================================================= |
| 688 |
// Optimized transfer & fill complete loop for P_1 |
| 689 |
// ========================================================================= |
| 690 |
auto D4 = TimeMonitor(*TimeMonitor::getNewTimer("Driver: 4 - TransferAndFillComplete")); |
| 691 |
TestTransfer(A, P); |
| 692 |
comm->barrier(); |
| 693 |
} |
| 694 |
|
| 695 |
globalTimeMonitor = Teuchos::null; |
| 696 |
|
| 697 |
// ========================================================================= |
| 698 |
// Timing stuff |
| 699 |
// ========================================================================= |
| 700 |
if (printTimings) { |
| 701 |
RCP<ParameterList> reportParams = rcp(new ParameterList); |
| 702 |
if (timingsFormat == "yaml") { |
| 703 |
reportParams->set("Report format", "YAML"); // "Table" or "YAML" |
| 704 |
reportParams->set("YAML style", "compact"); // "spacious" or "compact" |
| 705 |
} |
| 706 |
reportParams->set("How to merge timer sets", "Union"); |
| 707 |
reportParams->set("alwaysWriteLocal", false); |
| 708 |
reportParams->set("writeGlobalStats", true); |
| 709 |
reportParams->set("writeZeroTimers", false); |
| 710 |
// FIXME: no "ignoreZeroTimers" |
| 711 |
|
| 712 |
const std::string filter = ""; |
| 713 |
|
| 714 |
std::ios_base::fmtflags ff(out.flags()); |
| 715 |
if (timingsFormat == "table-fixed") |
| 716 |
out << std::fixed; |
| 717 |
else |
| 718 |
out << std::scientific; |
| 719 |
TimeMonitor::report(comm.ptr(), out, filter, reportParams); |
| 720 |
out << std::setiosflags(ff); |
| 721 |
} |
| 722 |
|
| 723 |
TimeMonitor::clearCounters(); |
| 724 |
|
| 725 |
if (isDriver) { |
| 726 |
if (openedOut != NULL) { |
| 727 |
TEUCHOS_ASSERT(savedOut >= 0); |
| 728 |
dup2(savedOut, STDOUT_FILENO); |
| 729 |
fclose(openedOut); |
| 730 |
openedOut = NULL; |
| 731 |
} |
| 732 |
try { |
| 733 |
runList = paramList.sublist("Run" + MueLu::toString(++runCount), mustAlreadyExist); |
| 734 |
mueluList = runList.sublist("MueLu", mustAlreadyExist); |
| 735 |
} catch (Teuchos::Exceptions::InvalidParameterName&) { |
| 736 |
stop = true; |
| 737 |
} |
| 738 |
} |
| 739 |
|
| 740 |
} while (!stop); |
| 741 |
} |
| 742 |
|
| 743 |
comm->barrier(); |
| 744 |
|
| 745 |
return EXIT_SUCCESS; |
| 746 |
} |
| 747 |
|
| 748 |
//- -- -------------------------------------------------------- |
| 749 |
#define MUELU_AUTOMATIC_TEST_ETI_NAME main_ |
| 750 |
#include "MueLu_Test_ETI.hpp" |
| 751 |
|
| 752 |
int main(int argc, char* argv[]) { |
| 753 |
auto val = Automatic_Test_ETI(argc, argv); |
| 754 |
return val; |
| 755 |
} |