/usr/include/trilinos/MueLu_HierarchyManager.hpp is in libtrilinos-muelu-dev 12.12.1-5.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 106 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 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 | // @HEADER
//
// ***********************************************************************
//
// MueLu: A package for multigrid based preconditioning
// Copyright 2012 Sandia Corporation
//
// Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
// the U.S. Government retains certain rights in this software.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// 3. Neither the name of the Corporation nor the names of the
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// Questions? Contact
// Jonathan Hu (jhu@sandia.gov)
// Andrey Prokopenko (aprokop@sandia.gov)
// Ray Tuminaro (rstumin@sandia.gov)
//
// ***********************************************************************
//
// @HEADER
#ifndef MUELU_HIERARCHYMANAGER_DECL_HPP
#define MUELU_HIERARCHYMANAGER_DECL_HPP
#include <string>
#include <map>
#include <Teuchos_Array.hpp>
#include <Xpetra_Operator.hpp>
#include <Xpetra_IO.hpp>
#include "MueLu_ConfigDefs.hpp"
#include "MueLu_Exceptions.hpp"
#include "MueLu_Hierarchy.hpp"
#include "MueLu_HierarchyFactory.hpp"
#include "MueLu_Level.hpp"
#include "MueLu_MasterList.hpp"
#include "MueLu_PerfUtils.hpp"
#ifdef HAVE_MUELU_INTREPID2
#ifdef HAVE_MUELU_INTREPID2_REFACTOR
#include "Kokkos_DynRankView.hpp"
#else
#include "Intrepid2_FieldContainer.hpp"
#endif
#endif
namespace MueLu {
// This class stores the configuration of a Hierarchy.
// The class also provides an algorithm to build a Hierarchy from the configuration.
//
// See also: FactoryManager
//
template <class Scalar = double, class LocalOrdinal = int, class GlobalOrdinal = LocalOrdinal, class Node = KokkosClassic::DefaultNode::DefaultNodeType>
class HierarchyManager : public HierarchyFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node> {
#undef MUELU_HIERARCHYMANAGER_SHORT
#include "MueLu_UseShortNames.hpp"
typedef std::pair<std::string, const FactoryBase*> keep_pair;
public:
//!
HierarchyManager(int numDesiredLevel = MasterList::getDefault<int>("max levels")) :
numDesiredLevel_ (numDesiredLevel),
maxCoarseSize_ (MasterList::getDefault<int>("coarse: max size")),
verbosity_ (Medium),
doPRrebalance_ (MasterList::getDefault<bool>("repartition: rebalance P and R")),
implicitTranspose_ (MasterList::getDefault<bool>("transpose: use implicit")),
graphOutputLevel_(-1) { }
//!
virtual ~HierarchyManager() { }
//!
void AddFactoryManager(int startLevel, int numDesiredLevel, RCP<FactoryManagerBase> manager) {
const int lastLevel = startLevel + numDesiredLevel - 1;
if (levelManagers_.size() < lastLevel + 1)
levelManagers_.resize(lastLevel + 1);
for (int iLevel = startLevel; iLevel <= lastLevel; iLevel++)
levelManagers_[iLevel] = manager;
}
//!
RCP<FactoryManagerBase> GetFactoryManager(int levelID) const {
// NOTE: last levelManager is used for all the remaining levels
return (levelID >= levelManagers_.size() ? levelManagers_[levelManagers_.size()-1] : levelManagers_[levelID]);
}
//! returns number of factory managers stored in levelManagers_ vector.
size_t getNumFactoryManagers() const {
return levelManagers_.size();
}
//!
void CheckConfig() {
for (int i = 0; i < levelManagers_.size(); i++)
TEUCHOS_TEST_FOR_EXCEPTION(levelManagers_[i] == Teuchos::null, Exceptions::RuntimeError, "MueLu:HierarchyConfig::CheckConfig(): Undefined configuration for level:");
}
//@{
virtual RCP<Hierarchy> CreateHierarchy() const {
return rcp(new Hierarchy());
}
//! Setup Hierarchy object
virtual void SetupHierarchy(Hierarchy& H) const {
TEUCHOS_TEST_FOR_EXCEPTION(!H.GetLevel(0)->IsAvailable("A"), Exceptions::RuntimeError, "No fine level operator");
#ifdef HAVE_MUELU_DEBUG
// Reset factories' data used for debugging
for (int i = 0; i < levelManagers_.size(); i++)
levelManagers_[i]->ResetDebugData();
#endif
// Setup Matrix
// TODO: I should certainly undo this somewhere...
RCP<Level> l0 = H.GetLevel(0);
RCP<Operator> Op = l0->Get<RCP<Operator> >("A");
Xpetra::UnderlyingLib lib = Op->getDomainMap()->lib();
H.setlib(lib);
SetupOperator(*Op);
SetupExtra(H);
// Setup Hierarchy
H.SetMaxCoarseSize(maxCoarseSize_);
VerboseObject::SetDefaultVerbLevel(verbosity_);
if (graphOutputLevel_ >= 0)
H.EnableGraphDumping("dep_graph.dot", graphOutputLevel_);
if (VerboseObject::IsPrint(Statistics2)) {
RCP<Matrix> Amat = rcp_dynamic_cast<Matrix>(Op);
if (!Amat.is_null()) {
RCP<ParameterList> params = rcp(new ParameterList());
params->set("printLoadBalancingInfo", true);
params->set("printCommInfo", true);
VerboseObject::GetOStream(Statistics2) << PerfUtils::PrintMatrixInfo(*Amat, "A0", params);
} else {
VerboseObject::GetOStream(Warnings1) << "Fine level operator is not a matrix, statistics are not available" << std::endl;
}
}
H.SetPRrebalance(doPRrebalance_);
H.SetImplicitTranspose(implicitTranspose_);
H.Clear();
// There are few issues with using Keep in the interpreter:
// 1. Hierarchy::Keep interface takes a name and a factory. If
// factories are different on different levels, the AddNewLevel() call
// in Hierarchy does not work properly, as it assume that factories are
// the same.
// 2. FactoryManager does not have a Keep option, only Hierarchy and
// Level have it
// 3. Interpreter constructs factory managers, but not levels. So we
// cannot set up Keep flags there.
//
// The solution implemented here does the following:
// 1. Construct hierarchy with dummy levels. This avoids
// Hierarchy::AddNewLevel() calls which will propagate wrong
// inheritance.
// 2. Interpreter constructs keep_ array with names and factories for
// that level
// 3. For each level, we call Keep(name, factory) for each keep_
for (int i = 0; i < numDesiredLevel_; i++) {
std::map<int, std::vector<keep_pair> >::const_iterator it = keep_.find(i);
if (it != keep_.end()) {
RCP<Level> l = H.GetLevel(i);
const std::vector<keep_pair>& keeps = it->second;
for (size_t j = 0; j < keeps.size(); j++)
l->Keep(keeps[j].first, keeps[j].second);
}
if (i < numDesiredLevel_-1) {
RCP<Level> newLevel = rcp(new Level());
H.AddLevel(newLevel);
}
}
ExportDataSetKeepFlags(H, matricesToPrint_,"A");
ExportDataSetKeepFlags(H, prolongatorsToPrint_, "P");
ExportDataSetKeepFlags(H, restrictorsToPrint_, "R");
ExportDataSetKeepFlags(H, nullspaceToPrint_, "Nullspace");
ExportDataSetKeepFlags(H, coordinatesToPrint_, "Coordinates");
#ifdef HAVE_MUELU_INTREPID2
ExportDataSetKeepFlags(H,elementToNodeMapsToPrint_, "pcoarsen: element to node map");
#endif
int levelID = 0;
int lastLevelID = numDesiredLevel_ - 1;
bool isLastLevel = false;
while (!isLastLevel) {
bool r = H.Setup(levelID,
LvlMngr(levelID-1, lastLevelID),
LvlMngr(levelID, lastLevelID),
LvlMngr(levelID+1, lastLevelID));
isLastLevel = r || (levelID == lastLevelID);
levelID++;
}
RCP<Teuchos::FancyOStream> fos = this->getOStream();
fos->setOutputToRootOnly(0);
H.describe(*fos, verbosity_);
// When we reuse hierarchy, it is necessary that we don't
// change the number of levels. We also cannot make requests
// for coarser levels, because we don't construct all the
// data on previous levels. For instance, let's say our first
// run constructed three levels. If we try to do requests during
// next setup for the fourth level, it would need Aggregates
// which we didn't construct for level 3 because we reused P.
// To fix this situation, we change the number of desired levels
// here.
numDesiredLevel_ = levelID;
WriteData<Matrix>(H, matricesToPrint_, "A");
WriteData<Matrix>(H, prolongatorsToPrint_, "P");
WriteData<Matrix>(H, restrictorsToPrint_, "R");
WriteData<MultiVector>(H, nullspaceToPrint_, "Nullspace");
WriteData<MultiVector>(H, coordinatesToPrint_, "Coordinates");
#ifdef HAVE_MUELU_INTREPID2
#ifdef HAVE_MUELU_INTREPID2_REFACTOR
typedef Kokkos::DynRankView<LocalOrdinal,typename Node::device_type> FCi;
#else
typedef Intrepid2::FieldContainer<LocalOrdinal> FCi;
#endif
WriteDataFC<FCi>(H,elementToNodeMapsToPrint_, "pcoarsen: element to node map","el2node");
#endif
} //SetupHierarchy
//@}
typedef std::map<std::string, RCP<const FactoryBase> > FactoryMap;
protected: //TODO: access function
//! Setup Matrix object
virtual void SetupOperator(Operator& Op) const { }
//! Setup extra data
// TODO: merge with SetupMatrix ?
virtual void SetupExtra(Hierarchy& H) const { }
// TODO this was private
// Used in SetupHierarchy() to access levelManagers_
// Inputs i=-1 and i=size() are allowed to simplify calls to hierarchy->Setup()
Teuchos::RCP<FactoryManagerBase> LvlMngr(int levelID, int lastLevelID) const {
// NOTE: the order of 'if' statements is important
if (levelID == -1) // levelID = -1 corresponds to the finest level
return Teuchos::null;
if (levelID == lastLevelID+1) // levelID = 'lastLevelID+1' corresponds to the last level (i.e., no nextLevel)
return Teuchos::null;
if (levelManagers_.size() == 0) { // default factory manager.
// The default manager is shared across levels, initialized only if needed and deleted with the HierarchyManager
static RCP<FactoryManagerBase> defaultMngr = rcp(new FactoryManager());
return defaultMngr;
}
return GetFactoryManager(levelID);
}
// Hierarchy parameters
mutable int numDesiredLevel_;
Xpetra::global_size_t maxCoarseSize_;
MsgType verbosity_;
bool doPRrebalance_;
bool implicitTranspose_;
int graphOutputLevel_;
Teuchos::Array<int> matricesToPrint_;
Teuchos::Array<int> prolongatorsToPrint_;
Teuchos::Array<int> restrictorsToPrint_;
Teuchos::Array<int> nullspaceToPrint_;
Teuchos::Array<int> coordinatesToPrint_;
Teuchos::Array<int> elementToNodeMapsToPrint_;
std::map<int, std::vector<keep_pair> > keep_;
private:
// Set the keep flags for Export Data
void ExportDataSetKeepFlags(Hierarchy& H, const Teuchos::Array<int>& data, const std::string& name) const {
for (int i = 0; i < data.size(); ++i) {
if (data[i] < H.GetNumLevels()) {
RCP<Level> L = H.GetLevel(data[i]);
L->AddKeepFlag(name, &*levelManagers_[data[i]]->GetFactory(name));
}
}
}
template<class T>
void WriteData(Hierarchy& H, const Teuchos::Array<int>& data, const std::string& name) const {
for (int i = 0; i < data.size(); ++i) {
std::string fileName = name + "_" + Teuchos::toString(data[i]) + ".m";
if (data[i] < H.GetNumLevels()) {
RCP<Level> L = H.GetLevel(data[i]);
if (L->IsAvailable(name,&*levelManagers_[i]->GetFactory(name))) {
// Try generating factory
RCP<T> M = L->template Get< RCP<T> >(name,&*levelManagers_[i]->GetFactory(name));
if (!M.is_null()) {
Xpetra::IO<Scalar, LocalOrdinal, GlobalOrdinal, Node>::Write(fileName,* M);
}
}
else if (L->IsAvailable(name)) {
// Try nofactory
RCP<T> M = L->template Get< RCP<T> >(name);
if (!M.is_null()) {
Xpetra::IO<Scalar, LocalOrdinal, GlobalOrdinal, Node>::Write(fileName,* M);
}
}
}
}
}
template<class T>
void WriteDataFC(Hierarchy& H, const Teuchos::Array<int>& data, const std::string& name, const std::string & ofname) const {
for (int i = 0; i < data.size(); ++i) {
const std::string fileName = ofname + "_" + Teuchos::toString(data[i]) + ".m";
if (data[i] < H.GetNumLevels()) {
RCP<Level> L = H.GetLevel(data[i]);
if (L->IsAvailable(name)) {
RCP<T> M = L->template Get< RCP<T> >(name);
if (!M.is_null()) {
RCP<Matrix> A = L->template Get<RCP<Matrix> >("A");
RCP<const CrsGraph> AG = A->getCrsGraph();
WriteFieldContainer<T>(fileName,*M,*AG->getColMap());
}
}
}
}
}
// For dumping an IntrepidPCoarsening element-to-node map to disk
template<class T>
void WriteFieldContainer(const std::string& fileName, T & fcont,const Map &colMap) const {
typedef LocalOrdinal LO;
typedef GlobalOrdinal GO;
typedef Node NO;
typedef Xpetra::MultiVector<GO,LO,GO,NO> GOMultiVector;
size_t num_els = (size_t) fcont.dimension(0);
size_t num_vecs =(size_t) fcont.dimension(1);
// Generate rowMap
Teuchos::RCP<const Map> rowMap = Xpetra::MapFactory<LO,GO,NO>::Build(colMap.lib(),Teuchos::OrdinalTraits<Xpetra::global_size_t>::invalid(),fcont.dimension(0),colMap.getIndexBase(),colMap.getComm());
// Fill multivector to use *petra dump routines
RCP<GOMultiVector> vec = Xpetra::MultiVectorFactory<GO, LO, GO, NO>::Build(rowMap,num_vecs);
for(size_t j=0; j<num_vecs; j++) {
Teuchos::ArrayRCP<GO> v = vec->getDataNonConst(j);
for(size_t i=0; i<num_els; i++)
v[i] = colMap.getGlobalElement(fcont(i,j));
}
Xpetra::IO<GO,LO,GO,NO>::Write(fileName,*vec);
}
// Levels
Array<RCP<FactoryManagerBase> > levelManagers_; // one FactoryManager per level (the last levelManager is used for all the remaining levels)
}; // class HierarchyManager
} // namespace MueLu
#define MUELU_HIERARCHYMANAGER_SHORT
#endif // MUELU_HIERARCHYMANAGER_HPP
//TODO: split into _decl/_def
// TODO: default value for first param (FactoryManager()) should not be duplicated (code maintainability)
|