Teuchos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Teuchos_MatrixMarket_SymmetrizingAdder.hpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Tpetra: Templated Linear Algebra Services Package
4 //
5 // Copyright 2008 NTESS and the Tpetra contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 #ifndef __Teuchos_MatrixMarket_SymmetrizingAdder_hpp
11 #define __Teuchos_MatrixMarket_SymmetrizingAdder_hpp
12 
13 #include <Teuchos_as.hpp>
14 #include <Teuchos_ScalarTraits.hpp>
15 #include <string>
16 
17 
18 // Macro that marks a function as "possibly unused," in order to
19 // suppress build warnings.
20 #if ! defined(TRILINOS_UNUSED_FUNCTION)
21 # if defined(__GNUC__) || (defined(__INTEL_COMPILER) && !defined(_MSC_VER))
22 # define TRILINOS_UNUSED_FUNCTION __attribute__((__unused__))
23 # elif defined(__clang__)
24 # if __has_attribute(unused)
25 # define TRILINOS_UNUSED_FUNCTION __attribute__((__unused__))
26 # else
27 # define TRILINOS_UNUSED_FUNCTION
28 # endif // Clang has 'unused' attribute
29 # elif defined(__IBMCPP__)
30 // IBM's C++ compiler for Blue Gene/Q (V12.1) implements 'used' but not 'unused'.
31 //
32 // http://pic.dhe.ibm.com/infocenter/compbg/v121v141/index.jsp
33 # define TRILINOS_UNUSED_FUNCTION
34 # else // some other compiler
35 # define TRILINOS_UNUSED_FUNCTION
36 # endif
37 #endif // ! defined(TRILINOS_UNUSED_FUNCTION)
38 
39 
40 namespace Teuchos {
41  namespace MatrixMarket {
42  // Anonymous namespace for helper functions for SymmetrizingAdder.
43  namespace {
45  isSkew (const std::string& symmType) {
46  return symmType.size() >= 4 && symmType.substr(0,4) == "skew";
47  }
48 
50  isConj (const std::string& symmType) {
51  return std::string::npos != symmType.find ("hermitian");
52  }
53 
55  needsSymmetrization (const std::string& symmType) {
56  return symmType != "general";
57  }
58  } // namespace (anonymous)
59 
79  template<class AdderType>
81  public:
83  typedef typename AdderType::index_type index_type;
85  typedef typename AdderType::value_type value_type;
86 
94  const std::string& symmType) :
95  adder_ (adder),
96  symmetrize_ (needsSymmetrization (symmType)),
97  conjugate_ (isConj (symmType)),
98  skew_ (isSkew (symmType))
99  {}
100 
102  void
104  const index_type j,
105  const value_type& Aij)
106  {
107  AdderType& theAdder = *adder_;
108 
109  theAdder (i, j, Aij);
110  if (symmetrize_ && i != j) {
112  const value_type Aji = skew_ ?
113  value_type(-(conjugate_ ? STS::conjugate(Aij) : Aij)) :
114  (conjugate_ ? STS::conjugate(Aij) : Aij);
115  // The optional fourth argument (which defaults to true)
116  // specifies whether or not to count the entry against the
117  // total expected number of entries. We don't want to count
118  // this entry because it wasn't part of the original data;
119  // we inserted it because the caller doesn't want symmetric
120  // storage. The original data's total expected number of
121  // entries only counts the entries that are in the original
122  // data, not those that we insert.
123  theAdder (j, i, Aji, false);
124  }
125  }
126 
131  return adder_;
132  }
133 
134  private:
142  bool skew_;
143  };
144 
145  } // namespace MatrixMarket
146 } // namespace Teuchos
147 
148 #endif // __Teuchos_MatrixMarket_SymmetrizingAdder_hpp
Teuchos::RCP< AdderType > adder_
The wrapped AdderType instance.
Adds entries with optional symmetry to a sparse matrix.
Teuchos::RCP< AdderType > getAdder() const
Persisting non-const view of the underlying adder object.
This structure defines some basic traits for a scalar field type.
void operator()(const index_type i, const index_type j, const value_type &Aij)
Add value A_ij to entry (i,j), and optionally symmetrize.
bool conjugate_
Whether to conjugate when symmetrizing.
SymmetrizingAdder(const Teuchos::RCP< AdderType > &adder, const std::string &symmType)
Constructor.
AdderType::index_type index_type
The type of indices of the sparse matrix.
AdderType::value_type value_type
The type of entries of the sparse matrix.
Defines basic traits for the scalar field type.
Definition of Teuchos::as, for conversions between types.