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 
00167 
00173   static Epetra_Map Create_Root_Map(const Epetra_Map & usermap,
00174           int root = 0);
00175 
00177 
00181   static Epetra_Map Create_OneToOne_Map(const Epetra_Map& usermap,
00182           bool high_rank_proc_owns_shared=false);
00183 
00185 
00189   static Epetra_BlockMap Create_OneToOne_BlockMap(const Epetra_BlockMap& usermap,
00190               bool high_rank_proc_owns_shared=false);
00191 
00192 
00193 
00195   // For each row, sort column entries from smallest to largest.
00196   // Use shell sort. Stable sort so it is fast if indices are already sorted.
00197   static int SortCrsEntries(int NumRows, const int *CRS_rowptr, int *CRS_colind, double *CRS_vals);
00198 
00200 
00207 #ifndef EPETRA_NO_32BIT_GLOBAL_INDICES
00208   static int GetPidGidPairs(const Epetra_Import & Importer,std::vector< std::pair<int,int> > & gpids, bool use_minus_one_for_local);
00209 #endif
00210 #ifndef EPETRA_NO_64BIT_GLOBAL_INDICES
00211   static int GetPidGidPairs(const Epetra_Import & Importer,std::vector< std::pair<int,long long> > & gpids, bool use_minus_one_for_local);
00212 #endif
00213   
00214 
00216 
00218   static int GetPids(const Epetra_Import & Importer, std::vector<int> &pids, bool use_minus_one_for_local);
00219 
00221 
00223   static int GetRemotePIDs(const Epetra_Import & Importer, std::vector<int> &RemotePIDs);
00224   
00225 
00227   static double Chop(const double & Value);
00228 //  {
00229 //    if (std::abs(Value) < chopVal_) return 0;
00230 //    return Value;
00231 //  };
00232 
00233   static const double chopVal_;
00234 
00235  private:
00236   unsigned int Seed_;
00237 };
00238 
00239 
00240 // Epetra_Util constructor
00241 inline Epetra_Util::Epetra_Util() : Seed_(std::rand()) {}
00242 // Epetra_Util constructor
00243 inline Epetra_Util::Epetra_Util(const Epetra_Util& Util) : Seed_(Util.Seed_) {}
00244 // Epetra_Util destructor
00245 inline Epetra_Util::~Epetra_Util(){}
00246 
00259 template<typename T>
00260 int Epetra_Util_binary_search(T item,
00261                               const T* list,
00262                               int len,
00263                               int& insertPoint);
00264 
00265 EPETRA_LIB_DLL_EXPORT int Epetra_Util_binary_search(int item,
00266                               const int* list,
00267                               int len,
00268                               int& insertPoint);
00269 
00270 EPETRA_LIB_DLL_EXPORT int Epetra_Util_binary_search(long long item,
00271                               const long long* list,
00272                               int len,
00273                               int& insertPoint);
00274 
00288 template<typename T>
00289 int Epetra_Util_binary_search_aux(T item,
00290                               const int* list,
00291                               const T* aux_list,
00292                               int len,
00293                               int& insertPoint);
00294 
00295 EPETRA_LIB_DLL_EXPORT int Epetra_Util_binary_search_aux(int item,
00296                               const int* list,
00297                               const int* aux_list,                   
00298                               int len,
00299                               int& insertPoint);
00300 
00301 EPETRA_LIB_DLL_EXPORT int Epetra_Util_binary_search_aux(long long item,
00302                               const int* list,
00303                               const long long* aux_list,                   
00304                               int len,
00305                               int& insertPoint);
00306 
00307 
00308 
00309 
00310 
00311 template<class T>
00312 int Epetra_Util_insert_empty_positions(T*& array, int& usedLength,
00313                                        int& allocatedLength,
00314                                        int insertOffset, int numPositions,
00315                                        int allocChunkSize=32)
00316 {
00317   if (insertOffset < 0 || insertOffset > usedLength ||
00318       usedLength > allocatedLength) {
00319     return(-1);
00320   }
00321 
00322   if ((usedLength+numPositions) < allocatedLength) {
00323     for(int i=usedLength-1; i>=insertOffset; --i) {
00324       array[i+numPositions] = array[i];
00325     }
00326     usedLength += numPositions;
00327     return(0);
00328   }
00329 
00330   allocatedLength += allocChunkSize;
00331   //what if allocatedLength is still not large enough?
00332   //loop until it is large enough:
00333   while(allocatedLength < usedLength+numPositions) {
00334     allocatedLength += allocChunkSize;
00335   }
00336 
00337   T* newlist = new T[allocatedLength];
00338 
00339   for(int i=0; i<insertOffset; ++i) {
00340     newlist[i] = array[i];
00341   }
00342 
00343   for(int i=insertOffset; i<usedLength; ++i) {
00344     newlist[i+numPositions] = array[i];
00345   }
00346 
00347   usedLength += numPositions;
00348   delete [] array;
00349   array = newlist;
00350   return(0);
00351 }
00352 
00368 template<class T>
00369 int Epetra_Util_insert(T item, int offset, T*& list,
00370                         int& usedLength,
00371                         int& allocatedLength,
00372                         int allocChunkSize=32)
00373 {
00374   int code = Epetra_Util_insert_empty_positions<T>(list, usedLength,
00375                                                 allocatedLength, offset, 1,
00376                                                 allocChunkSize);
00377   if (code != 0) {
00378     return(code);
00379   }
00380 
00381   list[offset] = item;
00382 
00383   return(0);
00384 }
00385 
00387 
00411 int Epetra_Util_ExtractHbData(Epetra_CrsMatrix * A, Epetra_MultiVector * LHS,
00412             Epetra_MultiVector * RHS,
00413             int & M, int & N, int & nz, int * & ptr,
00414             int * & ind, double * & val, int & Nrhs,
00415             double * & rhs, int & ldrhs,
00416             double * & lhs, int & ldlhs);
00417 
00418 
00419 #endif /* EPETRA_UTIL_H */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines