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 
00212 #ifndef EPETRA_NO_32BIT_GLOBAL_INDICES
00213   static int GetPidGidPairs(const Epetra_Import & Importer,std::vector< std::pair<int,int> > & gpids, bool use_minus_one_for_local);
00214 #endif
00215 #ifndef EPETRA_NO_64BIT_GLOBAL_INDICES
00216   static int GetPidGidPairs(const Epetra_Import & Importer,std::vector< std::pair<int,long long> > & gpids, bool use_minus_one_for_local);
00217 #endif
00218   
00219 
00221 
00223   static int GetPids(const Epetra_Import & Importer, std::vector<int> &pids, bool use_minus_one_for_local);
00224 
00226 
00228   static int GetRemotePIDs(const Epetra_Import & Importer, std::vector<int> &RemotePIDs);
00229   
00230 
00232   static double Chop(const double & Value);
00233 //  {
00234 //    if (std::abs(Value) < chopVal_) return 0;
00235 //    return Value;
00236 //  };
00237 
00238   static const double chopVal_;
00239 
00240  private:
00241   unsigned int Seed_;
00242 };
00243 
00244 
00245 // Epetra_Util constructor
00246 inline Epetra_Util::Epetra_Util() : Seed_(std::rand()) {}
00247 // Epetra_Util constructor
00248 inline Epetra_Util::Epetra_Util(const Epetra_Util& Util) : Seed_(Util.Seed_) {}
00249 // Epetra_Util destructor
00250 inline Epetra_Util::~Epetra_Util(){}
00251 
00264 template<typename T>
00265 int Epetra_Util_binary_search(T item,
00266                               const T* list,
00267                               int len,
00268                               int& insertPoint);
00269 
00270 EPETRA_LIB_DLL_EXPORT int Epetra_Util_binary_search(int item,
00271                               const int* list,
00272                               int len,
00273                               int& insertPoint);
00274 
00275 EPETRA_LIB_DLL_EXPORT int Epetra_Util_binary_search(long long item,
00276                               const long long* list,
00277                               int len,
00278                               int& insertPoint);
00279 
00293 template<typename T>
00294 int Epetra_Util_binary_search_aux(T item,
00295                               const int* list,
00296                               const T* aux_list,
00297                               int len,
00298                               int& insertPoint);
00299 
00300 EPETRA_LIB_DLL_EXPORT int Epetra_Util_binary_search_aux(int item,
00301                               const int* list,
00302                               const int* aux_list,                   
00303                               int len,
00304                               int& insertPoint);
00305 
00306 EPETRA_LIB_DLL_EXPORT int Epetra_Util_binary_search_aux(long long item,
00307                               const int* list,
00308                               const long long* aux_list,                   
00309                               int len,
00310                               int& insertPoint);
00311 
00312 
00313 
00314 
00315 
00316 template<class T>
00317 int Epetra_Util_insert_empty_positions(T*& array, int& usedLength,
00318                                        int& allocatedLength,
00319                                        int insertOffset, int numPositions,
00320                                        int allocChunkSize=32)
00321 {
00322   if (insertOffset < 0 || insertOffset > usedLength ||
00323       usedLength > allocatedLength) {
00324     return(-1);
00325   }
00326 
00327   if ((usedLength+numPositions) < allocatedLength) {
00328     for(int i=usedLength-1; i>=insertOffset; --i) {
00329       array[i+numPositions] = array[i];
00330     }
00331     usedLength += numPositions;
00332     return(0);
00333   }
00334 
00335   allocatedLength += allocChunkSize;
00336   //what if allocatedLength is still not large enough?
00337   //loop until it is large enough:
00338   while(allocatedLength < usedLength+numPositions) {
00339     allocatedLength += allocChunkSize;
00340   }
00341 
00342   T* newlist = new T[allocatedLength];
00343 
00344   for(int i=0; i<insertOffset; ++i) {
00345     newlist[i] = array[i];
00346   }
00347 
00348   for(int i=insertOffset; i<usedLength; ++i) {
00349     newlist[i+numPositions] = array[i];
00350   }
00351 
00352   usedLength += numPositions;
00353   delete [] array;
00354   array = newlist;
00355   return(0);
00356 }
00357 
00373 template<class T>
00374 int Epetra_Util_insert(T item, int offset, T*& list,
00375                         int& usedLength,
00376                         int& allocatedLength,
00377                         int allocChunkSize=32)
00378 {
00379   int code = Epetra_Util_insert_empty_positions<T>(list, usedLength,
00380                                                 allocatedLength, offset, 1,
00381                                                 allocChunkSize);
00382   if (code != 0) {
00383     return(code);
00384   }
00385 
00386   list[offset] = item;
00387 
00388   return(0);
00389 }
00390 
00392 
00416 int Epetra_Util_ExtractHbData(Epetra_CrsMatrix * A, Epetra_MultiVector * LHS,
00417             Epetra_MultiVector * RHS,
00418             int & M, int & N, int & nz, int * & ptr,
00419             int * & ind, double * & val, int & Nrhs,
00420             double * & rhs, int & ldrhs,
00421             double * & lhs, int & ldlhs);
00422 
00423 
00424 #endif /* EPETRA_UTIL_H */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines