Epetra Package Browser (Single Doxygen Collection) Development
Epetra_Util.h
Go to the documentation of this file.
00001 /*
00002 //@HEADER
00003 // ************************************************************************
00004 //
00005 //               Epetra: Linear Algebra Services Package
00006 //                 Copyright 2011 Sandia Corporation
00007 //
00008 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
00009 // the U.S. Government retains certain rights in this software.
00010 //
00011 // Redistribution and use in source and binary forms, with or without
00012 // modification, are permitted provided that the following conditions are
00013 // met:
00014 //
00015 // 1. Redistributions of source code must retain the above copyright
00016 // notice, this list of conditions and the following disclaimer.
00017 //
00018 // 2. Redistributions in binary form must reproduce the above copyright
00019 // notice, this list of conditions and the following disclaimer in the
00020 // documentation and/or other materials provided with the distribution.
00021 //
00022 // 3. Neither the name of the Corporation nor the names of the
00023 // contributors may be used to endorse or promote products derived from
00024 // this software without specific prior written permission.
00025 //
00026 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00027 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00028 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00029 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00030 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00031 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00032 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00033 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00034 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00035 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00036 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00037 //
00038 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
00039 //
00040 // ************************************************************************
00041 //@HEADER
00042 */
00043 
00044 #ifndef EPETRA_UTIL_H
00045 #define EPETRA_UTIL_H
00046 
00047 #include "Epetra_ConfigDefs.h"
00048 #include "Epetra_Object.h"
00049 #include <vector>
00050 class Epetra_Map;
00051 class Epetra_BlockMap;
00052 class Epetra_CrsMatrix;
00053 class Epetra_MultiVector;
00054 class Epetra_Import;
00055 
00057 
00079 class EPETRA_LIB_DLL_EXPORT Epetra_Util {
00080 
00081   public:
00083 
00085   Epetra_Util();
00086 
00087 
00089 
00091   Epetra_Util(const Epetra_Util& Util);
00092 
00094   virtual ~Epetra_Util();
00095 
00097 
00098 
00100   unsigned int RandomInt();
00101 
00103   double RandomDouble();
00104 
00106 
00109   unsigned int Seed() const;
00110 
00112 
00118   int SetSeed(unsigned int Seed_in);
00119 
00121 
00123 
00146   template<typename T>
00147   static void Sort(bool SortAscending, int NumKeys, T * Keys,
00148        int NumDoubleCompanions,double ** DoubleCompanions,
00149        int NumIntCompanions, int ** IntCompanions,
00150        int NumLongLongCompanions, long long ** LongLongCompanions);
00151 
00152   static void Sort(bool SortAscending, int NumKeys, int * Keys,
00153        int NumDoubleCompanions,double ** DoubleCompanions,
00154        int NumIntCompanions, int ** IntCompanions,
00155        int NumLongLongCompanions, long long ** LongLongCompanions);
00156 
00157   static void Sort(bool SortAscending, int NumKeys, long long * Keys,
00158        int NumDoubleCompanions,double ** DoubleCompanions,
00159        int NumIntCompanions, int ** IntCompanions,
00160        int NumLongLongCompanions, long long ** LongLongCompanions);
00161 
00162   static void Sort(bool SortAscending, int NumKeys, int * Keys,
00163        int NumDoubleCompanions,double ** DoubleCompanions,
00164        int NumIntCompanions, int ** IntCompanions);
00165 
00166   static void Sort(bool SortAscending, int NumKeys, double * Keys,
00167        int NumDoubleCompanions,double ** DoubleCompanions,
00168        int NumIntCompanions, int ** IntCompanions,
00169        int NumLongLongCompanions, long long ** LongLongCompanions);
00170 
00172 
00178   static Epetra_Map Create_Root_Map(const Epetra_Map & usermap,
00179           int root = 0);
00180 
00182 
00186   static Epetra_Map Create_OneToOne_Map(const Epetra_Map& usermap,
00187           bool high_rank_proc_owns_shared=false);
00188 
00190 
00194   static Epetra_BlockMap Create_OneToOne_BlockMap(const Epetra_BlockMap& usermap,
00195               bool high_rank_proc_owns_shared=false);
00196 
00197 
00198 
00200   // For each row, sort column entries from smallest to largest.
00201   // Use shell sort. Stable sort so it is fast if indices are already sorted.
00202   static int SortCrsEntries(int NumRows, const int *CRS_rowptr, int *CRS_colind, double *CRS_vals);
00203 
00205   // For each row, sort column entries from smallest to largest, merging column ids that are identical by adding values.
00206   // Use shell sort. Stable sort so it is fast if indices are already sorted.
00207   static int SortAndMergeCrsEntries(int NumRows, int *CRS_rowptr, int *CRS_colind, double *CRS_vals);
00208 
00210 
00217 #ifndef EPETRA_NO_32BIT_GLOBAL_INDICES
00218   static int GetPidGidPairs(const Epetra_Import & Importer,std::vector< std::pair<int,int> > & gpids, bool use_minus_one_for_local);
00219 #endif
00220 #ifndef EPETRA_NO_64BIT_GLOBAL_INDICES
00221   static int GetPidGidPairs(const Epetra_Import & Importer,std::vector< std::pair<int,long long> > & gpids, bool use_minus_one_for_local);
00222 #endif
00223 
00224 
00226 
00228   static int GetPids(const Epetra_Import & Importer, std::vector<int> &pids, bool use_minus_one_for_local);
00229 
00231 
00233   static int GetRemotePIDs(const Epetra_Import & Importer, std::vector<int> &RemotePIDs);
00234 
00235 
00237   static double Chop(const double & Value);
00238 //  {
00239 //    if (std::abs(Value) < chopVal_) return 0;
00240 //    return Value;
00241 //  };
00242 
00243   static const double chopVal_;
00244 
00245  private:
00246   unsigned int Seed_;
00247 };
00248 
00249 
00250 // Epetra_Util constructor
00251 inline Epetra_Util::Epetra_Util() : Seed_(std::rand()) {}
00252 // Epetra_Util constructor
00253 inline Epetra_Util::Epetra_Util(const Epetra_Util& Util) : Seed_(Util.Seed_) {}
00254 // Epetra_Util destructor
00255 inline Epetra_Util::~Epetra_Util(){}
00256 
00269 template<typename T>
00270 int Epetra_Util_binary_search(T item,
00271                               const T* list,
00272                               int len,
00273                               int& insertPoint);
00274 
00275 EPETRA_LIB_DLL_EXPORT int Epetra_Util_binary_search(int item,
00276                               const int* list,
00277                               int len,
00278                               int& insertPoint);
00279 
00280 EPETRA_LIB_DLL_EXPORT int Epetra_Util_binary_search(long long item,
00281                               const long long* list,
00282                               int len,
00283                               int& insertPoint);
00284 
00298 template<typename T>
00299 int Epetra_Util_binary_search_aux(T item,
00300                               const int* list,
00301                               const T* aux_list,
00302                               int len,
00303                               int& insertPoint);
00304 
00305 EPETRA_LIB_DLL_EXPORT int Epetra_Util_binary_search_aux(int item,
00306                               const int* list,
00307                               const int* aux_list,            
00308                               int len,
00309                               int& insertPoint);
00310 
00311 EPETRA_LIB_DLL_EXPORT int Epetra_Util_binary_search_aux(long long item,
00312                               const int* list,
00313                               const long long* aux_list,            
00314                               int len,
00315                               int& insertPoint);
00316 
00317 
00318 
00319 
00320 
00321 template<class T>
00322 int Epetra_Util_insert_empty_positions(T*& array, int& usedLength,
00323                                        int& allocatedLength,
00324                                        int insertOffset, int numPositions,
00325                                        int allocChunkSize=32)
00326 {
00327   if (insertOffset < 0 || insertOffset > usedLength ||
00328       usedLength > allocatedLength) {
00329     return(-1);
00330   }
00331 
00332   if ((usedLength+numPositions) < allocatedLength) {
00333     for(int i=usedLength-1; i>=insertOffset; --i) {
00334       array[i+numPositions] = array[i];
00335     }
00336     usedLength += numPositions;
00337     return(0);
00338   }
00339 
00340   allocatedLength += allocChunkSize;
00341   //what if allocatedLength is still not large enough?
00342   //loop until it is large enough:
00343   while(allocatedLength < usedLength+numPositions) {
00344     allocatedLength += allocChunkSize;
00345   }
00346 
00347   T* newlist = new T[allocatedLength];
00348 
00349   for(int i=0; i<insertOffset; ++i) {
00350     newlist[i] = array[i];
00351   }
00352 
00353   for(int i=insertOffset; i<usedLength; ++i) {
00354     newlist[i+numPositions] = array[i];
00355   }
00356 
00357   usedLength += numPositions;
00358   delete [] array;
00359   array = newlist;
00360   return(0);
00361 }
00362 
00378 template<class T>
00379 int Epetra_Util_insert(T item, int offset, T*& list,
00380                         int& usedLength,
00381                         int& allocatedLength,
00382                         int allocChunkSize=32)
00383 {
00384   int code = Epetra_Util_insert_empty_positions<T>(list, usedLength,
00385                                                 allocatedLength, offset, 1,
00386                                                 allocChunkSize);
00387   if (code != 0) {
00388     return(code);
00389   }
00390 
00391   list[offset] = item;
00392 
00393   return(0);
00394 }
00395 
00397 
00421 int Epetra_Util_ExtractHbData(Epetra_CrsMatrix * A, Epetra_MultiVector * LHS,
00422             Epetra_MultiVector * RHS,
00423             int & M, int & N, int & nz, int * & ptr,
00424             int * & ind, double * & val, int & Nrhs,
00425             double * & rhs, int & ldrhs,
00426             double * & lhs, int & ldlhs);
00427 
00428 
00429 #endif /* EPETRA_UTIL_H */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines