/usr/include/trilinos/MueLu_VariableContainer.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 | // @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_VARIABLECONTAINER_HPP
#define MUELU_VARIABLECONTAINER_HPP
#include <map>
#include <Teuchos_TypeNameTraits.hpp>
#include <Xpetra_Matrix.hpp>
#include <Xpetra_Operator.hpp>
#include "MueLu_ConfigDefs.hpp"
#include "MueLu_BaseClass.hpp"
#include "MueLu_Exceptions.hpp"
#include "MueLu_FactoryBase_fwd.hpp"
#include "MueLu_KeepType.hpp"
namespace MueLu {
/*!
@class VariableContainer
@brief Class that stores all relevant data for a variable
Maintains all data for a variable, that is, the data itself, a boolean flag for the "Keep" status,
a boolean flag for the "Available" status, a reference counter for all requests and a list with
all requesting factories.
*/
class VariableContainer : public BaseClass {
private:
// Motivated by Teuchos_any.hpp
class DataBase {
public:
virtual ~DataBase() {}
virtual const std::type_info& type() const = 0;
virtual std::string typeName() const = 0;
};
template<typename T>
class Data : public DataBase {
public:
Data(const T& data) : data_(data) {}
const std::type_info& type() const { return typeid(T); }
std::string typeName() const { return Teuchos::TypeNameTraits<T>::name(); }
T data_;
};
template<typename T>
struct Getter {
static T& get(DataBase* data_, DataBase*& datah_) {
if ((data_ == NULL) || (data_->type() != typeid(T))) // NVR added guard to avoid determining typeName unless we will use it
{
const std::string typeName = Teuchos::TypeNameTraits<T>::name(); // calls Teuchos::demangleName(), which can be expensive
TEUCHOS_TEST_FOR_EXCEPTION(data_ == NULL, Teuchos::bad_any_cast,
"Error, cast to type Data<" << typeName << "> failed since the content is NULL");
TEUCHOS_TEST_FOR_EXCEPTION(data_->type() != typeid(T), Teuchos::bad_any_cast,
"Error, cast to type Data<" << typeName << "> failed since the actual underlying type is "
"\'" << data_->typeName() << "!");
}
Data<T>* data = dynamic_cast<Data<T>*>(data_);
if (!data) // NVR added guard to avoid determining typeName unless we will use it
{
const std::string typeName = Teuchos::TypeNameTraits<T>::name(); // calls Teuchos::demangleName(), which can be expensive
TEUCHOS_TEST_FOR_EXCEPTION(!data, std::logic_error,
"Error, cast to type Data<" << typeName << "> failed but should not have and the actual underlying type is "
"\'" << data_->typeName() << "! The problem might be related to incompatible RTTI systems in static and shared libraries!");
}
return data->data_;
}
};
public:
typedef std::map<const FactoryBase*,int> request_container;
private:
DataBase* data_; ///< the data itself
mutable
DataBase* datah_; ///< temporary data storage (need to get a reference
///< to RCP to a base class (like Operator)
bool available_; ///< is data available?
KeepType keep_; ///< keep flag
int count_; ///< number of requests by all factories
request_container requests_; ///< requesting factories
public:
//! @name Constructors/Destructors.
//@{
//! Default constructor.
VariableContainer() : data_(NULL), datah_(NULL), available_(false), keep_(false), count_(0) { }
~VariableContainer() {
delete data_; data_ = NULL;
delete datah_; datah_ = NULL;
}
//@}
//! @name Data access
//@{
//! Store data in container class and set the "Available" status true.
template<typename T>
void SetData(const T& entry) {
delete data_;
delete datah_;
data_ = new Data<T>(entry);
datah_ = NULL;
available_ = true;
}
//! Return const reference to data stored in container
//! NOTE: we do not check if data is available
template<typename T>
const T& GetData() const {
return Getter<T>::get(data_, datah_);
}
//! Return reference to data stored in container
//! NOTE: we do not check if data is available
template<typename T>
T& GetData() {
return Getter<T>::get(data_, datah_);
}
std::string GetTypeName() {
if (data_ == NULL)
return std::string("");
return data_->typeName();
}
//! Returns true if data is available, i.e.
// if SetData has been called before
bool IsAvailable() const { return available_; }
//@}
//! @name Request/Release
//@{
//! Request data
void Request(const FactoryBase* reqFactory) {
request_container::iterator it = requests_.find(reqFactory);
if (it == requests_.end())
requests_[reqFactory] = 1;
else
(it->second)++;
count_++; // increment request counter
}
//! Release data
void Release(const FactoryBase* reqFactory) {
request_container::iterator it = requests_.find(reqFactory);
TEUCHOS_TEST_FOR_EXCEPTION(it == requests_.end(), Exceptions::RuntimeError, "MueLu::VariableContainer::Release(): "
"cannot call Release if factory has not been requested before by factory " << reqFactory);
if (--(it->second) == 0)
requests_.erase(it);
count_--;
}
//! Return the number of times the data has been requested by a specific factory
int NumRequests(const FactoryBase* reqFactory) const {
request_container::const_iterator it = requests_.find(reqFactory);
return (it != requests_.end()) ? it->second : 0;
}
//! Returns the number of times the data has been requested
int NumAllRequests() const { return count_; }
//! Returns true, if data is requested by reqFactory
bool IsRequested(const FactoryBase* reqFactory) const { return (NumRequests(reqFactory) > 0); }
//! Returns true, if data is requested by at least one factory
bool IsRequested() const { return (count_ > 0); }
const request_container& Requests() const { return requests_; }
//@}
//! @name Keep status
//@{
//! Returns true if at least one keep flag is set
bool IsKept(KeepType keep) const { return keep_ & keep; }
//! Adds a keep flag to the flag combination
void AddKeepFlag(KeepType keep = UserData) { keep_ |= keep; }
//! Removes a keep flag to the flag combination
void RemoveKeepFlag(KeepType keep = UserData) { keep_ = keep_ & (keep_ ^ keep); }
//! Returns the keep flag combination
KeepType GetKeepFlag() const { return keep_; }
//@}
};
template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
struct VariableContainer::Getter<Teuchos::RCP<Xpetra::Operator<Scalar,LocalOrdinal,GlobalOrdinal,Node> > > {
typedef Xpetra::Operator<Scalar,LocalOrdinal,GlobalOrdinal,Node> Operator;
typedef Xpetra::Matrix <Scalar,LocalOrdinal,GlobalOrdinal,Node> Matrix;
static Teuchos::RCP<Operator>& get(DataBase* data_, DataBase*& datah_) {
typedef Teuchos::RCP<Operator> TO;
typedef Teuchos::RCP<Matrix> TM;
if (data_ == NULL) // NVR added guard to avoid unnecessary call to TypeNameTraits<TO>::name(), which calls Teuchos::demangleName(), which can be expensive
{
TEUCHOS_TEST_FOR_EXCEPTION(data_ == NULL, Teuchos::bad_any_cast,
"Error, cast to type Data<" << Teuchos::TypeNameTraits<TO>::name() << "> failed since the content is NULL");
}
if (data_->type() == typeid(TO)) {
Data<TO>* data = dynamic_cast<Data<TO>*>(data_);
if (!data) // NVR added guard to avoid unnecessary call to TypeNameTraits<TO>::name(), which calls Teuchos::demangleName(), which can be expensive
{
TEUCHOS_TEST_FOR_EXCEPTION(!data, std::logic_error,
"Error, cast to type Data<" << Teuchos::TypeNameTraits<TO>::name() << "> failed but should not have and the actual underlying type is "
"\'" << data_->typeName() << "! The problem might be related to incompatible RTTI systems in static and shared libraries!");
}
return data->data_;
}
if (data_->type() != typeid(TM)) // NVR added guard to avoid unnecessary call to TypeNameTraits<TO>::name(), which calls Teuchos::demangleName(), which can be expensive
{
TEUCHOS_TEST_FOR_EXCEPTION(data_->type() != typeid(TM), Teuchos::bad_any_cast,
"Error, cast to type Data<" << Teuchos::TypeNameTraits<TM>::name() << "> failed since the actual underlying type is "
"\'" << data_->typeName() << "!");
}
Data<TM>* data = dynamic_cast<Data<TM>*>(data_);
if (!data) // NVR added guard to avoid unnecessary call to TypeNameTraits<TO>::name(), which calls Teuchos::demangleName(), which can be expensive
{
TEUCHOS_TEST_FOR_EXCEPTION(!data, std::logic_error,
"Error, cast to type Data<" << Teuchos::TypeNameTraits<TM>::name() << "> failed but should not have and the actual underlying type is "
"\'" << data_->typeName() << "! The problem might be related to incompatible RTTI systems in static and shared libraries!");
}
if (datah_ == NULL)
datah_ = new Data<TO>(Teuchos::rcp_dynamic_cast<Operator>(data->data_));
Data<TO>* datah = dynamic_cast<Data<TO>*>(datah_);
if (!datah) // NVR added guard to avoid unnecessary call to TypeNameTraits<TO>::name(), which calls Teuchos::demangleName(), which can be expensive
{
TEUCHOS_TEST_FOR_EXCEPTION(!datah, std::logic_error,
"Error, cast to type Data<" << Teuchos::TypeNameTraits<TO>::name() << "> failed but should not have and the actual underlying type is "
"\'" << datah_->typeName() << "! The problem might be related to incompatible RTTI systems in static and shared libraries!");
}
return datah->data_;
}
};
}
#endif /* MUELU_VARIABLECONTAINER_HPP */
|