| 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 |
/* |
| 11 |
* BlockedTransfer.cpp |
| 12 |
* |
| 13 |
* Created on: 01.01.2012 |
| 14 |
* Author: tobias |
| 15 |
*/ |
| 16 |
|
| 17 |
#include <unistd.h> |
| 18 |
#include <iostream> |
| 19 |
|
| 20 |
// Teuchos |
| 21 |
#include <Teuchos_RCP.hpp> |
| 22 |
#include <Teuchos_ParameterList.hpp> |
| 23 |
#include <Teuchos_CommandLineProcessor.hpp> |
| 24 |
#include <Teuchos_GlobalMPISession.hpp> |
| 25 |
#include <Teuchos_DefaultComm.hpp> |
| 26 |
#include <Teuchos_StandardCatchMacros.hpp> |
| 27 |
|
| 28 |
// Xpetra |
| 29 |
#include <Xpetra_Map.hpp> |
| 30 |
#include <Xpetra_CrsMatrixWrap.hpp> |
| 31 |
#include <Xpetra_MultiVectorFactory.hpp> |
| 32 |
#include <Xpetra_VectorFactory.hpp> |
| 33 |
#include <Xpetra_Parameters.hpp> |
| 34 |
#include <Xpetra_BlockedCrsMatrix.hpp> |
| 35 |
#include <Xpetra_MapExtractor.hpp> |
| 36 |
#include <Xpetra_MapExtractorFactory.hpp> |
| 37 |
|
| 38 |
// Galeri |
| 39 |
#include <Galeri_XpetraParameters.hpp> |
| 40 |
#include <Galeri_XpetraProblemFactory.hpp> |
| 41 |
|
| 42 |
// MueLu |
| 43 |
#include "MueLu_ConfigDefs.hpp" |
| 44 |
#include "MueLu_Memory.hpp" |
| 45 |
#include "MueLu_Hierarchy.hpp" |
| 46 |
#include "MueLu_SaPFactory.hpp" |
| 47 |
#include "MueLu_TrilinosSmoother.hpp" |
| 48 |
#include "MueLu_DirectSolver.hpp" |
| 49 |
#include "MueLu_Utilities.hpp" |
| 50 |
#include "MueLu_Exceptions.hpp" |
| 51 |
#include "MueLu_TentativePFactory.hpp" |
| 52 |
#include "MueLu_BlockedRAPFactory.hpp" |
| 53 |
#include "MueLu_PgPFactory.hpp" |
| 54 |
#include "MueLu_TransPFactory.hpp" |
| 55 |
#include "MueLu_GenericRFactory.hpp" |
| 56 |
#include "MueLu_SmootherFactory.hpp" |
| 57 |
#include "MueLu_SubBlockAFactory.hpp" |
| 58 |
#include "MueLu_BlockedPFactory.hpp" |
| 59 |
#include "MueLu_FactoryManager.hpp" |
| 60 |
#include "MueLu_BlockedGaussSeidelSmoother.hpp" |
| 61 |
|
| 62 |
// Belos |
| 63 |
#ifdef HAVE_MUELU_BELOS |
| 64 |
#include "BelosConfigDefs.hpp" |
| 65 |
#include "BelosLinearProblem.hpp" |
| 66 |
#include "BelosBlockCGSolMgr.hpp" |
| 67 |
#include "BelosBlockGmresSolMgr.hpp" |
| 68 |
#include "BelosXpetraAdapter.hpp" // this header defines Belos::XpetraOp() |
| 69 |
#include "BelosMueLuAdapter.hpp" // this header defines Belos::MueLuOp() |
| 70 |
#endif |
| 71 |
|
| 72 |
///////////////////////// |
| 73 |
// helper function |
| 74 |
|
| 75 |
namespace MueLuTests { |
| 76 |
|
| 77 |
template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node> |
| 78 |
Teuchos::RCP<Xpetra::CrsMatrixWrap<Scalar, LocalOrdinal, GlobalOrdinal, Node> > GenerateProblemMatrix(const Teuchos::RCP<const Xpetra::Map<LocalOrdinal, GlobalOrdinal, Node> > map, Scalar a = 2.0, Scalar b = -1.0, Scalar c = -1.0) { |
| 79 |
#include "MueLu_UseShortNames.hpp" |
| 80 |
Teuchos::RCP<CrsMatrixWrap> mtx = Galeri::Xpetra::MatrixTraits<Map, CrsMatrixWrap>::Build(map, 3); |
| 81 |
|
| 82 |
LocalOrdinal NumMyElements = map->getLocalNumElements(); |
| 83 |
Teuchos::ArrayView<const GlobalOrdinal> MyGlobalElements = map->getLocalElementList(); |
| 84 |
GlobalOrdinal NumGlobalElements = map->getGlobalNumElements(); |
| 85 |
GlobalOrdinal nIndexBase = map->getIndexBase(); |
| 86 |
|
| 87 |
GlobalOrdinal NumEntries; |
| 88 |
LocalOrdinal nnz = 2; |
| 89 |
std::vector<Scalar> Values(nnz); |
| 90 |
std::vector<GlobalOrdinal> Indices(nnz); |
| 91 |
|
| 92 |
for (LocalOrdinal i = 0; i < NumMyElements; ++i) { |
| 93 |
if (MyGlobalElements[i] == nIndexBase) { |
| 94 |
// off-diagonal for first row |
| 95 |
Indices[0] = nIndexBase; |
| 96 |
NumEntries = 1; |
| 97 |
Values[0] = c; |
| 98 |
} else if (MyGlobalElements[i] == nIndexBase + NumGlobalElements - 1) { |
| 99 |
// off-diagonal for last row |
| 100 |
Indices[0] = nIndexBase + NumGlobalElements - 2; |
| 101 |
NumEntries = 1; |
| 102 |
Values[0] = b; |
| 103 |
} else { |
| 104 |
// off-diagonal for internal row |
| 105 |
Indices[0] = MyGlobalElements[i] - 1; |
| 106 |
Values[1] = b; |
| 107 |
Indices[1] = MyGlobalElements[i] + 1; |
| 108 |
Values[0] = c; |
| 109 |
NumEntries = 2; |
| 110 |
} |
| 111 |
|
| 112 |
// put the off-diagonal entries |
| 113 |
// Xpetra wants ArrayViews (sigh) |
| 114 |
Teuchos::ArrayView<Scalar> av(&Values[0], NumEntries); |
| 115 |
Teuchos::ArrayView<GlobalOrdinal> iv(&Indices[0], NumEntries); |
| 116 |
mtx->insertGlobalValues(MyGlobalElements[i], iv, av); |
| 117 |
|
| 118 |
// Put in the diagonal entry |
| 119 |
mtx->insertGlobalValues(MyGlobalElements[i], |
| 120 |
Teuchos::tuple<GlobalOrdinal>(MyGlobalElements[i]), |
| 121 |
Teuchos::tuple<Scalar>(a)); |
| 122 |
|
| 123 |
} // for (LocalOrdinal i = 0; i < NumMyElements; ++i) |
| 124 |
|
| 125 |
mtx->fillComplete(map, map); |
| 126 |
|
| 127 |
return mtx; |
| 128 |
} |
| 129 |
|
| 130 |
} // namespace MueLuTests |
| 131 |
|
| 132 |
///////////////// |
| 133 |
// MAIN |
| 134 |
|
| 135 |
template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node> |
| 136 |
int main_(Teuchos::CommandLineProcessor &clp, Xpetra::UnderlyingLib lib, int argc, char *argv[]) { |
| 137 |
#include <MueLu_UseShortNames.hpp> |
| 138 |
|
| 139 |
using Teuchos::RCP; |
| 140 |
using Teuchos::rcp; |
| 141 |
using Teuchos::TimeMonitor; |
| 142 |
|
| 143 |
bool success = false; |
| 144 |
bool verbose = true; |
| 145 |
try { |
| 146 |
RCP<const Teuchos::Comm<int> > comm = Teuchos::DefaultComm<int>::getComm(); |
| 147 |
RCP<Teuchos::FancyOStream> out = Teuchos::fancyOStream(Teuchos::rcpFromRef(std::cout)); |
| 148 |
out->setOutputToRootOnly(0); |
| 149 |
*out << MueLu::MemUtils::PrintMemoryUsage() << std::endl; |
| 150 |
|
| 151 |
//#ifndef HAVE_XPETRA_INT_LONG_LONG |
| 152 |
*out << "Warning: scaling test was not compiled with long long int support" << std::endl; |
| 153 |
//#endif |
| 154 |
|
| 155 |
/**********************************************************************************/ |
| 156 |
/* SET TEST PARAMETERS */ |
| 157 |
/**********************************************************************************/ |
| 158 |
Xpetra::Parameters xpetraParameters(clp); // manage parameters of xpetra |
| 159 |
|
| 160 |
switch (clp.parse(argc, argv)) { |
| 161 |
case Teuchos::CommandLineProcessor::PARSE_HELP_PRINTED: return EXIT_SUCCESS; |
| 162 |
case Teuchos::CommandLineProcessor::PARSE_ERROR: |
| 163 |
case Teuchos::CommandLineProcessor::PARSE_UNRECOGNIZED_OPTION: return EXIT_FAILURE; |
| 164 |
case Teuchos::CommandLineProcessor::PARSE_SUCCESSFUL: break; |
| 165 |
} |
| 166 |
|
| 167 |
xpetraParameters.check(); |
| 168 |
|
| 169 |
if (comm->getRank() == 0) { |
| 170 |
std::cout << xpetraParameters; |
| 171 |
// TODO: print custom parameters // Or use paramList::print()! |
| 172 |
} |
| 173 |
|
| 174 |
/**********************************************************************************/ |
| 175 |
/* CREATE INITIAL MATRIX */ |
| 176 |
/**********************************************************************************/ |
| 177 |
RCP<const Map> bigMap; |
| 178 |
RCP<const Map> map1; |
| 179 |
RCP<const Map> map2; |
| 180 |
GO numElements = 500; |
| 181 |
GO numElements1 = 400; |
| 182 |
GO numElements2 = 100; |
| 183 |
|
| 184 |
std::vector<size_t> stridingInfo; |
| 185 |
stridingInfo.push_back(1); |
| 186 |
map1 = StridedMapFactory::Build(lib, numElements1, 0, stridingInfo, comm, -1); |
| 187 |
map2 = StridedMapFactory::Build(lib, numElements2, numElements1, stridingInfo, comm, -1); |
| 188 |
|
| 189 |
std::vector<GlobalOrdinal> localGids; // vector with all local GIDs on cur proc |
| 190 |
Teuchos::ArrayView<const GlobalOrdinal> map1eleList = map1->getLocalElementList(); // append all local gids from map1 and map2 |
| 191 |
localGids.insert(localGids.end(), map1eleList.begin(), map1eleList.end()); |
| 192 |
Teuchos::ArrayView<const GlobalOrdinal> map2eleList = map2->getLocalElementList(); |
| 193 |
localGids.insert(localGids.end(), map2eleList.begin(), map2eleList.end()); |
| 194 |
Teuchos::ArrayView<GlobalOrdinal> eleList(&localGids[0], localGids.size()); |
| 195 |
bigMap = StridedMapFactory::Build(lib, numElements, eleList, 0, stridingInfo, comm); // create full big map (concatenation of map1 and map2) |
| 196 |
|
| 197 |
std::vector<Teuchos::RCP<const Map> > maps; |
| 198 |
maps.push_back(map1); |
| 199 |
maps.push_back(map2); |
| 200 |
|
| 201 |
Teuchos::RCP<const Xpetra::MapExtractor<Scalar, LO, GO, Node> > mapExtractor = Xpetra::MapExtractorFactory<Scalar, LO, GO, Node>::Build(bigMap, maps); |
| 202 |
|
| 203 |
RCP<CrsMatrixWrap> Op11 = MueLuTests::GenerateProblemMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>(map1, 2, -1, -1); |
| 204 |
RCP<CrsMatrixWrap> Op22 = MueLuTests::GenerateProblemMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>(map2, 3, -2, -1); |
| 205 |
|
| 206 |
/*Op11->describe(*out,Teuchos::VERB_EXTREME); |
| 207 |
Op22->describe(*out,Teuchos::VERB_EXTREME);*/ |
| 208 |
|
| 209 |
// build blocked operator |
| 210 |
Teuchos::RCP<Xpetra::BlockedCrsMatrix<Scalar, LO, GO, Node> > bOp = Teuchos::rcp(new Xpetra::BlockedCrsMatrix<Scalar, LO, GO, Node>(mapExtractor, mapExtractor, 10)); |
| 211 |
|
| 212 |
bOp->setMatrix(0, 0, Op11); |
| 213 |
bOp->setMatrix(1, 1, Op22); |
| 214 |
|
| 215 |
bOp->fillComplete(); |
| 216 |
|
| 217 |
// build hierarchy |
| 218 |
Hierarchy H; |
| 219 |
H.SetMaxCoarseSize(50); |
| 220 |
RCP<Level> levelOne = H.GetLevel(); |
| 221 |
levelOne->Set("A", Teuchos::rcp_dynamic_cast<Matrix>(bOp)); // set blocked operator |
| 222 |
|
| 223 |
RCP<SubBlockAFactory> A11Fact = Teuchos::rcp(new SubBlockAFactory()); |
| 224 |
A11Fact->SetFactory("A", MueLu::NoFactory::getRCP()); |
| 225 |
A11Fact->SetParameter("block row", Teuchos::ParameterEntry(0)); |
| 226 |
A11Fact->SetParameter("block col", Teuchos::ParameterEntry(0)); |
| 227 |
RCP<SubBlockAFactory> A22Fact = Teuchos::rcp(new SubBlockAFactory()); |
| 228 |
A22Fact->SetFactory("A", MueLu::NoFactory::getRCP()); |
| 229 |
A22Fact->SetParameter("block row", Teuchos::ParameterEntry(1)); |
| 230 |
A22Fact->SetParameter("block col", Teuchos::ParameterEntry(1)); |
| 231 |
|
| 232 |
RCP<TentativePFactory> P11Fact = rcp(new TentativePFactory()); |
| 233 |
RCP<TransPFactory> R11Fact = rcp(new TransPFactory()); |
| 234 |
|
| 235 |
RCP<TentativePFactory> P22TentFact = rcp(new TentativePFactory()); |
| 236 |
RCP<PgPFactory> P22Fact = rcp(new PgPFactory()); |
| 237 |
RCP<GenericRFactory> R22Fact = rcp(new GenericRFactory()); |
| 238 |
|
| 239 |
std::string ifpackType; |
| 240 |
Teuchos::ParameterList ifpackList; |
| 241 |
ifpackList.set("relaxation: sweeps", (LO)5); |
| 242 |
ifpackList.set("relaxation: damping factor", Teuchos::ScalarTraits<SC>::one()); |
| 243 |
ifpackType = "RELAXATION"; |
| 244 |
ifpackList.set("relaxation: type", "Symmetric Gauss-Seidel"); |
| 245 |
RCP<SmootherPrototype> smoProto11 = rcp(new TrilinosSmoother(ifpackType, ifpackList, 0)); |
| 246 |
smoProto11->SetFactory("A", A11Fact); |
| 247 |
RCP<SmootherPrototype> smoProto22 = rcp(new TrilinosSmoother(ifpackType, ifpackList, 0)); |
| 248 |
smoProto22->SetFactory("A", A22Fact); |
| 249 |
|
| 250 |
// RCP<SmootherPrototype> smoProto11 = rcp( new DirectSolver("", Teuchos::ParameterList(), A11Fact) ); |
| 251 |
// RCP<SmootherPrototype> smoProto22 = rcp( new DirectSolver("", Teuchos::ParameterList(), A22Fact) ); |
| 252 |
|
| 253 |
RCP<SmootherFactory> Smoo11Fact = rcp(new SmootherFactory(smoProto11)); |
| 254 |
RCP<SmootherFactory> Smoo22Fact = rcp(new SmootherFactory(smoProto22)); |
| 255 |
|
| 256 |
RCP<FactoryManager> M11 = rcp(new FactoryManager()); |
| 257 |
M11->SetKokkosRefactor(false); |
| 258 |
M11->SetFactory("A", A11Fact); |
| 259 |
M11->SetFactory("P", P11Fact); |
| 260 |
M11->SetFactory("Ptent", P11Fact); // for Nullspace |
| 261 |
M11->SetFactory("R", R11Fact); |
| 262 |
M11->SetFactory("Smoother", Smoo11Fact); |
| 263 |
M11->SetIgnoreUserData(true); |
| 264 |
|
| 265 |
RCP<FactoryManager> M22 = rcp(new FactoryManager()); |
| 266 |
M22->SetKokkosRefactor(false); |
| 267 |
M22->SetFactory("A", A22Fact); |
| 268 |
M22->SetFactory("P", P22Fact); |
| 269 |
M22->SetFactory("R", R22Fact); |
| 270 |
M22->SetFactory("Ptent", P22TentFact); // for both P22 and Nullspace |
| 271 |
M22->SetFactory("Smoother", Smoo22Fact); |
| 272 |
M22->SetIgnoreUserData(true); |
| 273 |
|
| 274 |
RCP<BlockedPFactory> PFact = rcp(new BlockedPFactory()); |
| 275 |
PFact->AddFactoryManager(M11); |
| 276 |
PFact->AddFactoryManager(M22); |
| 277 |
|
| 278 |
RCP<GenericRFactory> RFact = rcp(new GenericRFactory()); |
| 279 |
|
| 280 |
RCP<Factory> AcFact = rcp(new BlockedRAPFactory()); |
| 281 |
|
| 282 |
// Smoothers |
| 283 |
RCP<BlockedGaussSeidelSmoother> smootherPrototype = rcp(new BlockedGaussSeidelSmoother()); |
| 284 |
smootherPrototype->SetParameter("Sweeps", Teuchos::ParameterEntry(2)); |
| 285 |
smootherPrototype->SetParameter("Damping factor", Teuchos::ParameterEntry(Teuchos::ScalarTraits<Scalar>::one())); |
| 286 |
smootherPrototype->AddFactoryManager(M11, 0); |
| 287 |
smootherPrototype->AddFactoryManager(M22, 1); |
| 288 |
RCP<SmootherFactory> smootherFact = rcp(new SmootherFactory(smootherPrototype)); |
| 289 |
|
| 290 |
// Coarse grid correction |
| 291 |
RCP<BlockedGaussSeidelSmoother> coarseSolverPrototype = rcp(new BlockedGaussSeidelSmoother()); |
| 292 |
coarseSolverPrototype->AddFactoryManager(M11, 0); |
| 293 |
coarseSolverPrototype->AddFactoryManager(M22, 1); |
| 294 |
RCP<SmootherFactory> coarseSolverFact = rcp(new SmootherFactory(coarseSolverPrototype, Teuchos::null)); |
| 295 |
|
| 296 |
// main factory manager |
| 297 |
FactoryManager M; |
| 298 |
M.SetFactory("A", AcFact); |
| 299 |
M.SetFactory("P", PFact); |
| 300 |
M.SetFactory("R", RFact); |
| 301 |
M.SetFactory("Smoother", smootherFact); // TODO fix me |
| 302 |
M.SetFactory("CoarseSolver", coarseSolverFact); |
| 303 |
|
| 304 |
H.SetVerbLevel(MueLu::Test); |
| 305 |
H.Setup(M); |
| 306 |
|
| 307 |
RCP<Level> l0 = H.GetLevel(0); |
| 308 |
RCP<Level> l1 = H.GetLevel(1); |
| 309 |
RCP<Level> l2 = H.GetLevel(2); |
| 310 |
|
| 311 |
l0->print(*out, Teuchos::VERB_EXTREME); |
| 312 |
l1->print(*out, Teuchos::VERB_EXTREME); |
| 313 |
l2->print(*out, Teuchos::VERB_EXTREME); |
| 314 |
|
| 315 |
// Define B |
| 316 |
RCP<Vector> X = VectorFactory::Build(bigMap, 1); |
| 317 |
RCP<Vector> B = VectorFactory::Build(bigMap, 1); |
| 318 |
X->setSeed(846930886); |
| 319 |
X->randomize(); |
| 320 |
bOp->apply(*X, *B, Teuchos::NO_TRANS, (SC)1.0, (SC)0.0); |
| 321 |
|
| 322 |
// X = 0 |
| 323 |
X->putScalar((SC)0.0); |
| 324 |
|
| 325 |
LO nIts = 9; |
| 326 |
H.Iterate(*B, *X, nIts); |
| 327 |
|
| 328 |
success = true; |
| 329 |
} |
| 330 |
TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success); |
| 331 |
|
| 332 |
return (success ? EXIT_SUCCESS : EXIT_FAILURE); |
| 333 |
} |
| 334 |
|
| 335 |
//- -- -------------------------------------------------------- |
| 336 |
#define MUELU_AUTOMATIC_TEST_ETI_NAME main_ |
| 337 |
#include "MueLu_Test_ETI.hpp" |
| 338 |
|
| 339 |
int main(int argc, char *argv[]) { |
| 340 |
return Automatic_Test_ETI(argc, argv); |
| 341 |
} |