// @HEADER // ***************************************************************************** // MueLu: A package for multigrid based preconditioning // // Copyright 2012 NTESS and the MueLu contributors. // SPDX-License-Identifier: BSD-3-Clause // ***************************************************************************** // @HEADER #ifndef MUELU_LEVEL_HPP #define MUELU_LEVEL_HPP #include // for swap #include // for _Rb_tree_const_iterator, etc #include // for basic_ostream, etc #include // for char_traits, string, etc #include // for pair #include // for operator<< #include // for FancyOStream #include // for RCP #include // for RCP::operator->, etc #include // for TEUCHOS_TEST_FOR_EXCEPTION #include // 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 Level 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(); Level(RCP& factoryManager); //! Destructor virtual ~Level(); //@} //@{ //! @name Build methods //! Builds a new Level object. RCP Build(); //@} //@{ //! @name Level handling //! @brief Return level number. int GetLevelID() const; //! @brief Set level number. void SetLevelID(int levelID); //! Previous level RCP& GetPreviousLevel(); //! Set previous level object //! @\param[in] const RCP& previousLevel void SetPreviousLevel(const RCP& previousLevel); //@} //! @name Set/Get factory manager //@{ //! Set default factories (used internally by Hierarchy::SetLevel()). // Users should not use this method. void SetFactoryManager(const RCP& factoryManager); //! returns the current factory manager // Users should not use this method const RCP 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 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(Runtime1) << "Level::Set: Not storing \"" << ename << "\" generated by factory " << factory->ShortClassName() << "[" << factory->GetID() << "]" << " 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 >("A", factory) * if factory == NULL => use default factory * * @param[in] const std::string& ename * @param[in] const FactoryBase* factory * @return data (templated) * */ template 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 + "\" generated by factory \"" + fac->description() + "\" not found on level " + toString(GetLevelID()) + "."); 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 on level " << GetLevelID() << ". The data \"" << ename << "\" has not been generated by " << *fac); return map_[fac][ename]->template GetData(); } /*! @brief Get data without decrementing associated storage counter (i.e., read-only access).*/ template void Get(const std::string& ename, T& rValue, const FactoryBase* factory = NoFactory::get()) { rValue = Get(ename, factory); } template bool IsType(const std::string& ename, const FactoryBase* factory = NoFactory::get()) { const FactoryBase* fac = GetFactory(ename, factory); TEUCHOS_TEST_FOR_EXCEPTION(!IsKey(fac, ename), Exceptions::RuntimeError, "\"" + ename + "\" generated by factory \"" + fac->description() + "\" not found on level " + toString(GetLevelID()) + "."); 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 on level " << GetLevelID() << ". The data \"" << ename << "\" has not been generated by " << *fac); return map_[fac][ename]->template CheckType(); } /*! @brief GetTypeName returns type string of variable stored using ename and factory * * @param[in] const std::string& ename * @param[in] const FactoryBase* factory * @return std::string with type information (e.g., "int") * */ std::string GetTypeName(const std::string& ename, const FactoryBase* factory = NoFactory::get()); //@} //! @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); //! 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; //! 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; //@} //! @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& vindices, std::map, 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& comm) { comm_ = comm; } RCP > 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 > comm_; using Key1 = const FactoryBase*; using Key2 = const std::string; using Value = RCP; using SubMap = Teuchos::map; //! Sub-map container (Key2 -> Value) using TwoKeyMap = Teuchos::map; //! Map of a map (Key1 -> SubMap) int levelID_; // id number associated with level RCP factoryManager_; RCP 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; bool IsAvailableFactory(const FactoryBase* factory) const; bool IsRequested(const Value& v) const; bool IsRequestedBy(const FactoryBase* factory, const std::string& ename, const FactoryBase* requestedBy) const; bool IsRequestedBy(const Value& v, const FactoryBase* requestedBy) const; bool IsRequestedFactory(const FactoryBase* factory) const; const Value& Get(const FactoryBase* factory, const std::string& ename) const; int NumRequests(const FactoryBase* factory, const std::string& ename) const; int CountRequestedFactory(const FactoryBase* factory) const; //@} }; // class Level } // namespace MueLu // TODO: Caps should not matter #endif // MUELU_LEVEL_HPP