/usr/include/trilinos/MueLu_Level.hpp is in libtrilinos-muelu-dev 12.4.2-2.
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 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 | // @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_LEVEL_HPP
#define MUELU_LEVEL_HPP
#include <algorithm> // for swap
#include <map> // for _Rb_tree_const_iterator, etc
#include <ostream> // for basic_ostream, etc
#include <string> // for char_traits, string, etc
#include <utility> // for pair
#include <Teuchos_Describable.hpp> // for operator<<
#include <Teuchos_FancyOStream.hpp> // for FancyOStream
#include <Teuchos_RCPDecl.hpp> // for RCP
#include <Teuchos_RCP.hpp> // for RCP::operator->, etc
#include <Teuchos_TestForException.hpp> // for TEUCHOS_TEST_FOR_EXCEPTION
#include <Xpetra_Map.hpp> // for UnderlyingLib definition
#include "MueLu_BoostGraphviz.hpp"
#include "MueLu_Exceptions.hpp" // for RuntimeError
#include "MueLu_FactoryManagerBase_fwd.hpp"
#include "MueLu_KeepType.hpp"
#include "MueLu_NoFactory.hpp"
#include "MueLu_Utilities.hpp"
#include "MueLu_VariableContainer.hpp"
#include "MueLu_VerbosityLevel.hpp" // for MsgType::Default, VerbLevel
namespace MueLu {
/*!
@class Level
@brief Class that holds all level-specific information.
All data that is stored in the <tt>Level</tt> class need a variable name
(e.g. "A", "P", ...) and a pointer to the generating factory. Only with
both the variable name and the generating factory the data can be accessed.
If no pointer to the generating factory is provided (or it is NULL) then
the Level class uses the information from a factory manager, which stores
default factories for different variable names.
We use a "two key" map for storage of data, with the pointer to the
generating factory as primary key and the variable name as secondary key.
The pointer to the generating factory is only used as primary "key". It
doesn't matter if the given factory pointer is valid or not. So the NULL
pointer can also be used.
The data itself is stored within a VariableContainer object. A reference
counter keeps track of the storage and automatically frees the memory if
the data is not needed any more. In the standard mode, the data first has
to be requested by calling the Request function. Then the data can be set
by calling Set. With Get the user can fetch data when needed. Release
decrements the reference counter for the given variable.
*/
class Level : public BaseClass {
public:
//@{
//! @name Constructors / Destructors
Level() : lib_(Xpetra::NotSpecified), levelID_(-1) { }
Level(RCP<FactoryManagerBase>& factoryManager) : lib_(Xpetra::UseTpetra), levelID_(-1), factoryManager_(factoryManager) { }
//! Destructor
virtual ~Level() { }
//@}
//@{
//! @name Build methods
//! Builds a new Level object.
RCP<Level> Build();
//@}
//@{
//! @name Level handling
//! @brief Return level number.
int GetLevelID() const;
//! @brief Set level number.
void SetLevelID(int levelID);
//! Previous level
RCP<Level>& GetPreviousLevel() { return previousLevel_; }
//! Set previous level object
//! @\param[in] const RCP<Level>& previousLevel
void SetPreviousLevel(const RCP<Level>& previousLevel);
//@}
//! @name Set/Get factory manager
//@{
//! Set default factories (used internally by Hierarchy::SetLevel()).
// Users should not use this method.
void SetFactoryManager(const RCP<const FactoryManagerBase>& factoryManager);
//! returns the current factory manager
// Users should not use this method
const RCP<const FactoryManagerBase> GetFactoryManager();
//@}
//@{
//! @name Set functions
//! Store need label and its associated data. This does not increment the storage counter.
//! - If factory is not specified or factory == NoFactory::get(), mark data as user-defined by default
//! - If factory is == NULL, use defaultFactory (FactoryManager have to be available).
template <class T>
void Set(const std::string& ename, const T& entry, const FactoryBase* factory = NoFactory::get()) {
const FactoryBase* fac = GetFactory(ename, factory);
if (fac == NoFactory::get()) {
// Any data set with a NoFactory gets UserData keep flag by default
AddKeepFlag(ename, NoFactory::get(), MueLu::UserData);
}
// Store entry only if data have been requested (or any keep flag)
if (IsRequested(ename, factory) || GetKeepFlag(ename, factory) != 0) {
TEUCHOS_TEST_FOR_EXCEPTION(!IsKey(factory, ename), Exceptions::RuntimeError, "" + ename + " not found in");
map_[factory][ename]->SetData(entry);
} else {
GetOStream(Warnings0) << "Level::Set: unable to store \"" << ename << "\" generated by factory " << factory
<< " on level " << toString(GetLevelID()) << ", as it has not been requested and no keep flags were set for it" << std::endl;
}
} // Set
//@}
//! @name Get functions
//! @brief Get functions for accessing stored data
//@{
/*! @brief Get data without decrementing associated storage counter (i.e., read-only access).
* Usage: Level->Get< RCP<Matrix> >("A", factory)
* if factory == NULL => use default factory
*
* @param[in] const std::string& ename
* @param[in] const FactoryBase* factory
* @return data (templated)
* */
template <class T>
T& Get(const std::string& ename, const FactoryBase* factory = NoFactory::get()) {
const FactoryBase* fac = GetFactory(ename, factory);
// printf("(l=%d) getting \"%20s\" generated by %10p [actually, generated by %p (%43s)]\n",
// levelID_, ename.c_str(), factory, fac, fac->description().c_str());
TEUCHOS_TEST_FOR_EXCEPTION(!IsKey(fac, ename), Exceptions::RuntimeError, "\"" + ename + "\" not found");
if (!IsAvailable(ename, fac)) {
TEUCHOS_TEST_FOR_EXCEPTION(NumRequests(fac, ename) < 1 && GetKeepFlag(ename, fac) == 0, Exceptions::RuntimeError,
"\"" << ename << "\" has not been requested (counter = " << NumRequests(fac, ename) << ", "
"KeepFlag = " << GetKeepFlag(ename, fac) << "). " << std::endl <<
"Generating factory:" << *fac << " NoFactory = " << NoFactory::get());
fac->CallBuild(*this);
Release(*fac);
}
TEUCHOS_TEST_FOR_EXCEPTION(!IsAvailable(ename, fac), Exceptions::RuntimeError, "MueLu::Level::Get(): factory did not produce expected output. "
"\"" << ename << "\" has not been generated by " << *fac);
return map_[fac][ename]->template GetData<T>();
}
/*! @brief Get data without decrementing associated storage counter (i.e., read-only access).*/
template <class T>
void Get(const std::string& ename, T& rValue, const FactoryBase* factory = NoFactory::get()) {
rValue = Get<T>(ename, factory);
}
//@}
//! @name Permanent storage
//@{
//! Request to keep variable 'ename' generated by 'factory' after the setup phase.
// This method is intented to be used by user drivers for printing, debugging or to keep some computed data for a next run of the setup phase.
//
// This method is an alias for: AddKeepFlag(ename, factory, MueLu::Keep)
// See also the description of KeepEnum for more information.
//
// To undo a keep request, one can use:
// - Delete(ename, factory) to delete the data and remove the "Keep" flag
// - or RemoveKeepFlag(ename, factory, MueLu::Keep) to go back to previous condition (data are kept only if internal MueLu logic need so).
//
// Note: Level variables tagged using this methods are also keep on the levels built using this.Build().
// This means that is you request to keep a specific variable on a fine level, all the coarser level that are created automatically during the setup phase will also retain the same variable.
void Keep(const std::string & ename, const FactoryBase* factory) { AddKeepFlag(ename, factory, MueLu::Keep); } // Note: do not add default value for input parameter 'factory'
//! Delete data that have been retained after the setup phase (using Keep(), AddKeepFlag(), or internal MueLu logic).
// Special cases:
// - If entry (ename, factory) does not exist, nothing is done.
// - If entry exists but counter !=, entry cannot be desallocated before counter set to 0 (using Release()) so an exeption is thrown.
void Delete(const std::string& ename, const FactoryBase* factory) { // Note: do not add default value for input parameter 'factory'
if (!IsKey(factory, ename))
return;
// Precondition:
// Delete() should only be called if counter == 0
// Note: It better to throw an exception rather than deleting the data if counter != 0 because users are not supposed to manipulate data with counter != 0
TEUCHOS_TEST_FOR_EXCEPTION(IsRequested(ename, factory) == true, Exceptions::RuntimeError, "MueLu::Level::Delete(): IsRequested() == true. Ref counter != 0. You are not allowed to delete data that are still in use.");
// If counter == 0 and entry exists, this means that a keep flag is set. Or there is an internal logic problem.
TEUCHOS_TEST_FOR_EXCEPTION(GetKeepFlag(ename, factory) == 0, Exceptions::RuntimeError, "MueLu::Level::Delete(), Keep flag == 0?");
RemoveKeepFlag(ename, factory, MueLu::All); // will delete the data if counter == 0
// Post condition: data must have been deleted
TEUCHOS_TEST_FOR_EXCEPTION(IsAvailable(ename, factory) == true, Exceptions::RuntimeError, "MueLu::Level::Delete(): Internal error (Post condition). Data have not been deleted.");
}
//! Delete all data that have been retained after the setup phase using Final flag
void Clear();
//! Delete all data from level that has no Keep flag set.
//! This is a function for experts only
void ExpertClear();
//! Test if a keep flag is set for variable 'ename' generated by 'factory'
//! The input parameter keep can be a combination of flags. IsKept() will then return true if at least one of the flag is set.
//! Note: There is no default parameter for IsKept() because it might be confusing (user generally wants to test IsKept with keep=MueLu::Keep but classes Level and Needs generally use keep = All)
bool IsKept(const std::string& ename, const FactoryBase* factory, KeepType keep) const { return GetKeepFlag(ename, factory) & keep; }
//! Add a keep flag for variable 'ename' generated by 'factory'
//! A variable can cumulate several keep flags (UserData+Final for example). This function just add a flag to the current flag combination.
//! By default, the flag 'Keep' is added. So Keep(ename, factory) is the same as AddKeepFlag(ename, factory).
//! See also the description of KeepEnum for more information.
void AddKeepFlag(const std::string & ename, const FactoryBase* factory = NoFactory::get(), KeepType keep = MueLu::Keep); // TODO: remove default value for input parameter 'factory'?
//! Remove a keep flag for variable 'ename' generated by 'factory'
//! A variable can cumulate several keep flags (UserData+Final for example). This function just add a flag to the current flag combination.
//! By default, all the flags are removed.
void RemoveKeepFlag(const std::string & ename, const FactoryBase* factory, KeepType keep = MueLu::All);
//! Get the flag combination set for variable 'ename' generated by 'factory'
KeepType GetKeepFlag(const std::string& ename, const FactoryBase* factory) const;
//@}
//! @name Request/Release functions
//! @brief Request and Release for incrementing/decrementing the reference count pointer for a specific variable.
//@{
//! Increment the storage counter for all the inputs of a factory
void Request(const FactoryBase& factory);
//! Decrement the storage counter for all the inputs of a factory
void Release(const FactoryBase& factory);
//! Callback from FactoryBase::CallDeclareInput() and FactoryBase::DeclareInput()
void DeclareInput(const std::string& ename, const FactoryBase* factory, const FactoryBase* requestedBy = NoFactory::get() );
//! Callback from FactoryBase::CallDeclareInput() and FactoryBase::DeclareInput() to declare factory dependencies
void DeclareDependencies(const FactoryBase* factory, bool bRequestOnly = false, bool bReleaseOnly = false);
//! Indicate that an object is needed. This increments the storage counter.
void Request(const std::string& ename, const FactoryBase* factory = NoFactory::get(), const FactoryBase* requestedBy = NoFactory::get());
//! Decrement the storage counter.
void Release(const std::string& ename, const FactoryBase* factory = NoFactory::get(), const FactoryBase* requestedBy = NoFactory::get());
//@}
//! @name Utility functions
//@{
//! Test whether a need's value has been saved.
bool IsAvailable(const std::string& ename, const FactoryBase* factory = NoFactory::get()) const {
if (!IsKey(factory, ename))
return false;
try {
return Get(factory, ename)->IsAvailable();
} catch (...) {
return false;
}
}
//! Test whether a need has been requested. Note: this tells nothing about whether the need's value exists.
bool IsRequested(const std::string& ename, const FactoryBase* factory = NoFactory::get()) const {
if (!IsKey(factory, ename))
return false;
try {
return IsRequested(Get(factory, ename));
} catch (...) {
return false;
}
}
//@}
//! @name I/O Functions
//@{
//! Return a simple one-line description of this object.
std::string description() const;
//! Printing method
// TODO: print only shows requested variables. check if we also list kept factories with ref counter=0?
void print(std::ostream& out, const VerbLevel verbLevel = Default) const;
#if defined(HAVE_MUELU_BOOST) && defined(HAVE_MUELU_BOOST_FOR_REAL) && defined(BOOST_VERSION) && (BOOST_VERSION >= 104400)
void UpdateGraph(std::map<const FactoryBase*, BoostVertex>& vindices,
std::map<std::pair<BoostVertex, BoostVertex>, std::string>& edges,
BoostProperties& dp,
BoostGraph& graph) const;
#endif
//@}
enum RequestMode { REQUEST, RELEASE, UNDEF };
RequestMode GetRequestMode() const { return requestMode_; }
void setlib(Xpetra::UnderlyingLib lib2) { lib_ = lib2; }
Xpetra::UnderlyingLib lib() { return lib_; }
void SetComm(RCP<const Teuchos::Comm<int> > const &comm) { comm_ = comm; }
RCP<const Teuchos::Comm<int> > GetComm() const { return comm_; }
private:
//! Copy constructor.
Level(const Level& source);
//! If input factory == NULL, returns the default factory. Else, return input factory.
//
// If factory == NULL, the default factory is defined as follow:
// - If user data is available, it is considered as the default and the factory manager is ignored.
// => The default factory is then NoFactory.
// - Else, the factory manager is used to get the default factory.
//
// This strategy allows to use the same factory manager on the fine and coarse level without any trouble.
// Example :
//
// FineLevel:
// ----------
// A -> User provided
// Nullspace -> User provided
//
// CoarseLevel:
// ------------
// A -> RAPFactory
// NullSpace -> NullspaceFactory
//
const FactoryBase* GetFactory(const std::string& varname, const FactoryBase* factory) const;
static RequestMode requestMode_;
Xpetra::UnderlyingLib lib_;
RCP<const Teuchos::Comm<int> > comm_;
typedef const FactoryBase* Key1;
typedef const std::string Key2;
typedef RCP<VariableContainer> Value;
typedef Teuchos::map<Key2, Value> SubMap; //! Sub-map container (Key2 -> Value)
typedef Teuchos::map<Key1, SubMap> TwoKeyMap; //! Map of a map (Key1 -> SubMap)
int levelID_; // id number associated with level
RCP<const FactoryManagerBase> factoryManager_;
RCP<Level> previousLevel_; // linked list of Level
TwoKeyMap map_;
//! @name Utility functions
//@{
//! Test whether some information about (ename, factory) are stored
bool IsKey(const FactoryBase* factory, const std::string& ename) const {
TwoKeyMap::const_iterator it = map_.find(factory);
return (it != map_.end()) ? (it->second).count(ename) : false;
}
bool IsAvailableFactory(const FactoryBase* factory) const {
TwoKeyMap::const_iterator it = map_.find(factory);
if (it == map_.end())
return false;
for (SubMap::const_iterator sit = it->second.begin(); sit != it->second.end(); sit++) {
if (sit->second->IsAvailable())
return true;
}
return false;
}
bool IsRequested(const Value& v) const {
TEUCHOS_TEST_FOR_EXCEPTION(v->NumAllRequests() == 0 && v->GetKeepFlag() == 0, Exceptions::RuntimeError,
"Internal logic error: if counter == 0, the entry in countTable_ should have been deleted");
return v->IsRequested();
}
bool IsRequestedBy(const FactoryBase* factory, const std::string& ename, const FactoryBase* requestedBy) const {
if (!IsKey(factory, ename))
return false;
return IsRequestedBy(Get(factory, ename), requestedBy);
}
bool IsRequestedBy(const Value& v, const FactoryBase* requestedBy) const {
TEUCHOS_TEST_FOR_EXCEPTION(v->NumAllRequests() == 0 && v->GetKeepFlag() == 0, Exceptions::RuntimeError,
"Internal logic error: if counter == 0, the entry in countTable_ should have been deleted");
return v->IsRequested(requestedBy);
}
bool IsRequestedFactory(const FactoryBase* factory) const {
TwoKeyMap::const_iterator it = map_.find(factory);
if (it == map_.end())
return false;
for (SubMap::const_iterator sit = it->second.begin(); sit != it->second.end(); sit++)
if (IsRequested(sit->second))
return true;
return false;
}
const Value& Get(const FactoryBase* factory, const std::string& ename) const {
TwoKeyMap::const_iterator it = map_.find(factory);
TEUCHOS_TEST_FOR_EXCEPTION(it == map_.end(), Exceptions::RuntimeError, "Key (" << factory << ", *) does not exist.");
SubMap::const_iterator sit = it->second.find(ename);
TEUCHOS_TEST_FOR_EXCEPTION(sit == it->second.end(), Exceptions::RuntimeError, "Key (" << factory << ", " << ename << ") does not exist.");
return sit->second;
}
int NumRequests(const FactoryBase* factory, const std::string & ename) const {
TEUCHOS_TEST_FOR_EXCEPTION(!IsKey(factory, ename), Exceptions::RuntimeError, "\"" + ename + "\" not found. Do a request first.");
const Teuchos::RCP<MueLu::VariableContainer>& v = Get(factory, ename);
TEUCHOS_TEST_FOR_EXCEPTION(v->NumAllRequests() == 0 && v->GetKeepFlag() == 0, Exceptions::RuntimeError,
"NumRequests(): Internal logic error: if counter == 0, the entry in countTable_ should have been deleted");
return v->NumAllRequests();
}
int CountRequestedFactory(const FactoryBase* factory) const {
TwoKeyMap::const_iterator it = map_.find(factory);
if (it == map_.end())
return 0;
int cnt = 0;
for (SubMap::const_iterator sit = it->second.begin(); sit != it->second.end(); sit++)
cnt += sit->second->NumAllRequests();
return cnt;
}
//@}
}; //class Level
} //namespace MueLu
//TODO: Caps should not matter
#endif // MUELU_LEVEL_HPP
|