Teuchos Package Browser (Single Doxygen Collection) Version of the Day
Teuchos_MatrixMarket_SymmetrizingAdder.hpp
Go to the documentation of this file.
00001 // @HEADER
00002 // ***********************************************************************
00003 //
00004 //          Tpetra: Templated Linear Algebra Services Package
00005 //                 Copyright (2008) Sandia Corporation
00006 //
00007 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
00008 // the U.S. Government retains certain rights in this software.
00009 //
00010 // Redistribution and use in source and binary forms, with or without
00011 // modification, are permitted provided that the following conditions are
00012 // met:
00013 //
00014 // 1. Redistributions of source code must retain the above copyright
00015 // notice, this list of conditions and the following disclaimer.
00016 //
00017 // 2. Redistributions in binary form must reproduce the above copyright
00018 // notice, this list of conditions and the following disclaimer in the
00019 // documentation and/or other materials provided with the distribution.
00020 //
00021 // 3. Neither the name of the Corporation nor the names of the
00022 // contributors may be used to endorse or promote products derived from
00023 // this software without specific prior written permission.
00024 //
00025 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00026 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00028 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00029 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00030 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00031 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00032 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00033 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00034 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00035 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00036 //
00037 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
00038 //
00039 // ************************************************************************
00040 // @HEADER
00041 
00042 #ifndef __Teuchos_MatrixMarket_SymmetrizingAdder_hpp
00043 #define __Teuchos_MatrixMarket_SymmetrizingAdder_hpp
00044 
00045 #include <Teuchos_as.hpp>
00046 #include <Teuchos_ScalarTraits.hpp>
00047 #include <string>
00048 
00049 
00050 // Macro that marks a function as "possibly unused," in order to
00051 // suppress build warnings.
00052 #if ! defined(TRILINOS_UNUSED_FUNCTION)
00053 #  if defined(__GNUC__) || defined(__INTEL_COMPILER)
00054 #    define TRILINOS_UNUSED_FUNCTION __attribute__((__unused__))
00055 #  elif defined(__clang__)
00056 #    if __has_attribute(unused)
00057 #      define TRILINOS_UNUSED_FUNCTION __attribute__((__unused__))
00058 #    else
00059 #      define TRILINOS_UNUSED_FUNCTION
00060 #    endif // Clang has 'unused' attribute
00061 #  elif defined(__IBMCPP__)
00062 // IBM's C++ compiler for Blue Gene/Q (V12.1) implements 'used' but not 'unused'.
00063 //
00064 // http://pic.dhe.ibm.com/infocenter/compbg/v121v141/index.jsp
00065 #    define TRILINOS_UNUSED_FUNCTION
00066 #  else // some other compiler
00067 #    define TRILINOS_UNUSED_FUNCTION
00068 #  endif
00069 #endif // ! defined(TRILINOS_UNUSED_FUNCTION)
00070 
00071 
00072 namespace Teuchos {
00073   namespace MatrixMarket {
00074     // Anonymous namespace for helper functions for SymmetrizingAdder.
00075     namespace {
00076       TRILINOS_UNUSED_FUNCTION bool
00077       isSkew (const std::string& symmType) {
00078         return symmType.size() >= 4 && symmType.substr(0,4) == "skew";
00079       }
00080 
00081       TRILINOS_UNUSED_FUNCTION bool
00082       isConj (const std::string& symmType) {
00083         return std::string::npos != symmType.find ("hermitian");
00084       }
00085 
00086       TRILINOS_UNUSED_FUNCTION bool
00087       needsSymmetrization (const std::string& symmType) {
00088         return symmType != "general";
00089       }
00090     } // namespace (anonymous)
00091 
00111     template<class AdderType>
00112     class SymmetrizingAdder {
00113     public:
00115       typedef typename AdderType::index_type index_type;
00117       typedef typename AdderType::value_type value_type;
00118 
00125       SymmetrizingAdder (const Teuchos::RCP<AdderType>& adder,
00126                          const std::string& symmType) :
00127         adder_ (adder),
00128         symmetrize_ (needsSymmetrization (symmType)),
00129         conjugate_ (isConj (symmType)),
00130         skew_ (isSkew (symmType))
00131       {}
00132 
00134       void
00135       operator() (const index_type i,
00136                   const index_type j,
00137                   const value_type& Aij)
00138       {
00139         AdderType& theAdder = *adder_;
00140 
00141         theAdder (i, j, Aij);
00142         if (symmetrize_ && i != j) {
00143           typedef Teuchos::ScalarTraits<value_type> STS;
00144           const value_type Aji = skew_ ?
00145             value_type(-(conjugate_ ? STS::conjugate(Aij) : Aij)) :
00146             (conjugate_ ? STS::conjugate(Aij) : Aij);
00147           // The optional fourth argument (which defaults to true)
00148           // specifies whether or not to count the entry against the
00149           // total expected number of entries.  We don't want to count
00150           // this entry because it wasn't part of the original data;
00151           // we inserted it because the caller doesn't want symmetric
00152           // storage.  The original data's total expected number of
00153           // entries only counts the entries that are in the original
00154           // data, not those that we insert.
00155           theAdder (j, i, Aji, false);
00156         }
00157       }
00158 
00162       Teuchos::RCP<AdderType> getAdder() const {
00163         return adder_;
00164       }
00165 
00166     private:
00168       Teuchos::RCP<AdderType> adder_;
00170       bool symmetrize_;
00172       bool conjugate_;
00174       bool skew_;
00175     };
00176 
00177   } // namespace MatrixMarket
00178 } // namespace Teuchos
00179 
00180 #endif // __Teuchos_MatrixMarket_SymmetrizingAdder_hpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines