Tpetra_Util.hpp

Go to the documentation of this file.
00001 // @HEADER
00002 // ***********************************************************************
00003 // 
00004 //          Tpetra: Templated Linear Algebra Services Package
00005 //                 Copyright (2004) Sandia Corporation
00006 // 
00007 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
00008 // license for use of this work by or on behalf of the U.S. Government.
00009 // 
00010 // This library is free software; you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as
00012 // published by the Free Software Foundation; either version 2.1 of the
00013 // License, or (at your option) any later version.
00014 //  
00015 // This library is distributed in the hope that it will be useful, but
00016 // WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018 // Lesser General Public License for more details.
00019 //  
00020 // You should have received a copy of the GNU Lesser General Public
00021 // License along with this library; if not, write to the Free Software
00022 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00023 // USA
00024 // Questions? Contact Michael A. Heroux (maherou@sandia.gov) 
00025 // 
00026 // ***********************************************************************
00027 // @HEADER
00028 
00029 #ifndef TPETRA_UTIL_HPP
00030 #define TPETRA_UTIL_HPP
00031 
00032 #include "Tpetra_ConfigDefs.hpp" // for map, vector, string, and iostream 
00033 #include <Teuchos_Utils.hpp>
00034 #include <Teuchos_TestForException.hpp>
00035 #include <Teuchos_ArrayView.hpp>
00036 
00037 namespace Tpetra {
00038 
00039 // shared test for exception
00040 // just like Teuchos TEST_FOR_EXCEPTION, but with the assurance 
00041 // that all nodes test and throw the exception together
00042 #define SHARED_TEST_FOR_EXCEPTION(throw_exception_test,Exception,msg,comm) \
00043 { \
00044     const Ordinal lcl_throw_exception = (throw_exception_test) ? Teuchos::rank(comm)+1 : 0; \
00045     Ordinal gbl_throw; \
00046     Teuchos::reduceAll(comm,Teuchos::REDUCE_MAX,lcl_throw_exception,&gbl_throw); \
00047     TEST_FOR_EXCEPTION(gbl_throw,Exception,  \
00048                        msg << " Failure on node " << gbl_throw-1 << "." << std::endl); \
00049 }
00050 
00051 // if TEUCHOS_DEBUG is defined, then it calls SHARED_TEST_FOR_EXCEPTION
00052 // otherwise, it calls TEST_FOR_EXCEPTION
00053 #ifdef TEUCHOS_DEBUG
00054 #define SWITCHED_TEST_FOR_EXCEPTION(throw_exception_test,Exception,msg,comm) \
00055 { \
00056     SHARED_TEST_FOR_EXCEPTION(throw_exception_test,Exception,msg,comm); \
00057 }
00058 #else 
00059 #define SWITCHED_TEST_FOR_EXCEPTION(throw_exception_test,Exception,msg,comm) \
00060 { \
00061     TEST_FOR_EXCEPTION(throw_exception_test,Exception,msg); \
00062 }
00063 #endif
00064 
00065 
00088   // efficientAddOrUpdate is taken from Scott Meyers' "Effective STL", Item 24.
00089   // if m already contains an entry with key k, use operator [].
00090   // if it doesn't, insert is used.
00091   template<typename MapType, typename KeyArgType, typename ValueArgType>
00092   typename MapType::iterator efficientAddOrUpdate(MapType& m, 
00093                           const KeyArgType & k, 
00094                           const ValueArgType & v) 
00095   {
00096     typename MapType::iterator lb = m.lower_bound(k);
00097     if(lb != m.end() && !(m.key_comp()(k, lb->first))) {
00098       lb->second = v;
00099       return(lb);
00100     }
00101     else {
00102       typedef typename MapType::value_type MVT;
00103       return(m.insert(lb, MVT(k, v)));
00104     }
00105   }
00106 
00107   // this function works much the way Teuchos::Array::toString works.
00108   // it allows std::vector to be used with an ostream.
00109   // The contents of the vector are printed in the following format:
00110   // "{4, 7, 18, 23, 6, 2}"
00111   template <typename T>
00112   std::string toString(const std::vector<T> & x) {
00113     std::ostringstream os;
00114     os << "{";
00115     typename std::vector<T>::const_iterator i = x.begin();
00116     if (i != x.end()) {
00117       os << *i;
00118       i++;
00119       for(; i != x.end(); i++) {
00120         os << "," << *i;
00121       }
00122     }
00123     os << "}";
00124     return(os.str());
00125   }
00126 
00127   template <typename T>
00128   std::string toString(const std::complex<T> & x) {
00129     return("(" + Teuchos::toString(x.real()) + "," + Teuchos::toString(x.imag()) + ")");
00130   }
00131  
00132   // sort function for multiple arrays
00133   // The values in sortVals will be sorted in ascending order.
00134   // The same permutation required to sort sortVals will be applied
00135   // to otherVals.
00136 
00137   // for two arrays
00138   template<typename T1, typename T2>
00139   void sortArrays(const Teuchos::ArrayView<T1> &sortVals, const Teuchos::ArrayView<T2> &otherVals) {
00140     // if sortVals and otherVals are not the same length, throw exception
00141     TEST_FOR_EXCEPTION(sortVals.size() != otherVals.size(), std::runtime_error,
00142         "Error in Tpetra_Util::sortArrays(sortVals,otherVals): sortVals and otherVals are not equally sized");
00143     
00144     // copy sortVals and otherVals into a multimap
00145     // (using a multimap instead of a map because values in sortVals may be duplicated)
00146     std::multimap<T1,T2> tempMap;
00147     typename Teuchos::ArrayView<T1>::iterator keyIter   = sortVals.begin();
00148     typename Teuchos::ArrayView<T2>::iterator valueIter = otherVals.begin();
00149     for(; keyIter != sortVals.end(); ++keyIter, ++valueIter) {
00150       tempMap.insert(std::pair<T1,T2>(*keyIter, *valueIter));
00151     }
00152 
00153     // multimap will automatically sort them, we just need to pull them out in order
00154     // and write them back to the original arrays
00155     keyIter   = sortVals.begin();
00156     valueIter = otherVals.begin();
00157     for(typename std::multimap<T1,T2>::iterator i = tempMap.begin(); 
00158         i != tempMap.end(); ++i, ++keyIter, ++valueIter) 
00159     {
00160       *keyIter   = i->first;
00161       *valueIter = i->second;
00162     }
00163   }
00164 
00165   // for three arrays
00166   template<typename T1, typename T2, typename T3>
00167   void sortArrays(const Teuchos::ArrayView<T1> &sortVals, 
00168                   const Teuchos::ArrayView<T2> &otherVals1, 
00169                   const Teuchos::ArrayView<T2> &otherVals2) 
00170   {
00171     // if sortVals and otherVals are not the same length, throw exception
00172     TEST_FOR_EXCEPTION((sortVals.size() != otherVals1.size()) || (sortVals.size() != otherVals2.size()), std::runtime_error,
00173         "Error in Tpetra_Util::sortArrays(sortVals,otherVals1,otherVals2): sortVals and otherVals are not equally sized");
00174     
00175     // copy sortVals and otherVals into a multimap
00176     // (using a multimap instead of a map because values in sortVals may be duplicated)
00177     typedef typename std::pair<T2, T3> ValuePair;
00178     std::multimap<T1, ValuePair> tempMap;
00179     typename Teuchos::ArrayView<T1>::iterator keyIter    = sortVals.begin();
00180     typename Teuchos::ArrayView<T2>::iterator valueIter1 = otherVals1.begin();
00181     typename Teuchos::ArrayView<T3>::iterator valueIter2 = otherVals2.begin();
00182     for(; keyIter != sortVals.end(); ++keyIter, ++valueIter1, ++valueIter2) {
00183       tempMap.insert(std::pair<T1, ValuePair>(*keyIter, ValuePair(*valueIter1, *valueIter2)));
00184     }
00185 
00186     // multimap will automatically sort them, we just need to pull them out in order
00187     // and write them back to the original arrays
00188     keyIter    = sortVals.begin();
00189     valueIter1 = otherVals1.begin();
00190     valueIter2 = otherVals2.begin();
00191     for(typename std::multimap<T1,ValuePair>::iterator i = tempMap.begin(); 
00192         i != tempMap.end(); i++, keyIter++, valueIter1++, valueIter2++) 
00193     {
00194       *keyIter    = i->first;
00195       *valueIter1 = i->second.first;
00196       *valueIter2 = i->second.second;
00197     }
00198   }
00199 
00200 } // namespace Tpetra
00201 
00202 #endif // TPETRA_UTIL_HPP

Generated on Wed May 12 21:59:41 2010 for Tpetra Matrix/Vector Services by  doxygen 1.4.7