|
Tpetra Matrix/Vector Services Version of the Day
|
00001 // HAVE_@HEADER 00002 // *********************************************************************** 00003 // 00004 // Tpetra: Templated Linear Algebra Services Package 00005 // Copyright (2008) 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 <iterator> 00034 #include <Teuchos_Utils.hpp> 00035 #include <Teuchos_TestForException.hpp> 00036 00037 #if defined(HAVE_TPETRA_THROW_EFFICIENCY_WARNINGS) || defined(HAVE_TPETRA_PRINT_EFFICIENCY_WARNINGS) 00038 00039 #define TPETRA_EFFICIENCY_WARNING(throw_exception_test,Exception,msg) \ 00040 { \ 00041 std::string err = Teuchos::typeName(*this) + msg; \ 00042 if (TPETRA_PRINTS_EFFICIENCY_WARNINGS && (throw_exception_test)) { \ 00043 std::cerr << err << std::endl; \ 00044 } \ 00045 TEST_FOR_EXCEPTION(TPETRA_THROWS_EFFICIENCY_WARNINGS && (throw_exception_test), Exception, err); \ 00046 } 00047 #else 00048 00049 #define TPETRA_EFFICIENCY_WARNING(throw_exception_test,Exception,msg) 00050 #endif 00051 00052 // handle an abuse warning, according to HAVE_TPETRA_THROW_ABUSE_WARNINGS and HAVE_TPETRA_PRINT_ABUSE_WARNINGS 00053 #if defined(HAVE_TPETRA_THROW_ABUSE_WARNINGS) || defined(HAVE_TPETRA_PRINT_ABUSE_WARNINGS) 00054 00055 #define TPETRA_ABUSE_WARNING(throw_exception_test,Exception,msg) \ 00056 { \ 00057 std::string err = Teuchos::typeName(*this) + msg; \ 00058 if (TPETRA_PRINTS_ABUSE_WARNINGS && (throw_exception_test)) { \ 00059 std::cerr << err << std::endl; \ 00060 } \ 00061 TEST_FOR_EXCEPTION(TPETRA_THROWS_ABUSE_WARNINGS && (throw_exception_test), Exception, err); \ 00062 } 00063 #else 00064 00065 #define TPETRA_ABUSE_WARNING(throw_exception_test,Exception,msg) 00066 #endif 00067 00068 00072 #define SHARED_TEST_FOR_EXCEPTION(throw_exception_test,Exception,msg,comm) \ 00073 { \ 00074 using Teuchos::outArg; \ 00075 const int lcl_throw_exception = (throw_exception_test) ? Teuchos::rank(comm)+1 : 0; \ 00076 int gbl_throw; \ 00077 Teuchos::reduceAll(comm,Teuchos::REDUCE_MAX,lcl_throw_exception,outArg(gbl_throw)); \ 00078 TEST_FOR_EXCEPTION(gbl_throw,Exception, \ 00079 msg << " Failure on node " << gbl_throw-1 << "." << std::endl); \ 00080 } 00081 00082 #ifdef HAVE_TEUCHOS_DEBUG 00083 00084 #define SWITCHED_TEST_FOR_EXCEPTION(throw_exception_test,Exception,msg,comm) \ 00085 { \ 00086 SHARED_TEST_FOR_EXCEPTION(throw_exception_test,Exception,msg,comm); \ 00087 } 00088 #else 00089 00090 #define SWITCHED_TEST_FOR_EXCEPTION(throw_exception_test,Exception,msg,comm) \ 00091 { \ 00092 TEST_FOR_EXCEPTION(throw_exception_test,Exception,msg); \ 00093 } 00094 #endif 00095 00096 namespace Tpetra { 00097 00124 template<typename MapType, typename KeyArgType, typename ValueArgType> 00125 typename MapType::iterator efficientAddOrUpdate(MapType& m, 00126 const KeyArgType & k, 00127 const ValueArgType & v) 00128 { 00129 typename MapType::iterator lb = m.lower_bound(k); 00130 if(lb != m.end() && !(m.key_comp()(k, lb->first))) { 00131 lb->second = v; 00132 return(lb); 00133 } 00134 else { 00135 typedef typename MapType::value_type MVT; 00136 return(m.insert(lb, MVT(k, v))); 00137 } 00138 } 00139 00140 00146 template<class IT1, class IT2> 00147 void sort2(const IT1 &first1, const IT1 &last1, const IT2 &first2) { 00148 typedef typename std::iterator_traits<IT1>::value_type KT; 00149 typedef typename std::iterator_traits<IT2>::value_type VT; 00150 // copy values into a multimap 00151 // (using a multimap instead of a map because values may be duplicated) 00152 std::multimap<KT,VT> tempMap; 00153 IT1 keyIter = first1; 00154 IT2 valueIter = first2; 00155 for (; keyIter != last1; ++keyIter, ++valueIter) { 00156 tempMap.insert(std::pair<KT,VT>(*keyIter, *valueIter)); 00157 } 00158 // multimap will automatically sort them, we just need to pull them out in order 00159 // and write them back to the original arrays 00160 keyIter = first1; 00161 valueIter = first2; 00162 for(typename std::multimap<KT,VT>::iterator i = tempMap.begin(); 00163 i != tempMap.end(); 00164 ++i, ++keyIter, ++valueIter) 00165 { 00166 *keyIter = i->first; 00167 *valueIter = i->second; 00168 } 00169 } 00170 00176 template<class IT1, class IT2, class IT3> 00177 void sort3(const IT1 &first1, const IT1 &last1, const IT2 &first2, const IT3 &first3) 00178 { 00179 typedef typename std::iterator_traits<IT1>::value_type KT; 00180 typedef typename std::iterator_traits<IT2>::value_type VT1; 00181 typedef typename std::iterator_traits<IT3>::value_type VT2; 00182 // copy values into a multimap 00183 // (using a multimap instead of a map because values may be duplicated) 00184 typedef typename std::pair<VT1,VT2> ValuePair; 00185 std::multimap<KT,ValuePair> tempMap; 00186 IT1 keyIter = first1; 00187 IT2 valueIter1 = first2; 00188 IT3 valueIter2 = first3; 00189 for(; keyIter != last1; ++keyIter, ++valueIter1, ++valueIter2) { 00190 tempMap.insert(std::pair<KT,ValuePair>(*keyIter,ValuePair(*valueIter1,*valueIter2))); 00191 } 00192 // multimap will automatically sort them, we just need to pull them out in order 00193 // and write them back to the original arrays 00194 keyIter = first1; 00195 valueIter1 = first2; 00196 valueIter2 = first3; 00197 for(typename std::multimap<KT,ValuePair>::iterator i = tempMap.begin(); 00198 i != tempMap.end(); 00199 i++, keyIter++, valueIter1++, valueIter2++) 00200 { 00201 *keyIter = i->first; 00202 *valueIter1 = i->second.first; 00203 *valueIter2 = i->second.second; 00204 } 00205 } 00206 00207 } // namespace Tpetra 00208 00209 00210 #endif // TPETRA_UTIL_HPP
1.7.4