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 2001 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 class Epetra_Map;
00050 class Epetra_BlockMap;
00051 class Epetra_CrsMatrix;
00052 class Epetra_MultiVector;
00053 
00055 
00077 class EPETRA_LIB_DLL_EXPORT Epetra_Util {
00078     
00079   public:
00081 
00083   Epetra_Util();
00084 
00085 
00087 
00089   Epetra_Util(const Epetra_Util& Util);
00090 
00092   virtual ~Epetra_Util();
00093 
00095 
00096 
00098   unsigned int RandomInt();
00099 
00101   double RandomDouble();
00102 
00104 
00107   unsigned int Seed() const;
00108 
00110 
00116   int SetSeed(unsigned int Seed_in);
00117 
00119   
00121 
00144   static void Sort(bool SortAscending, int NumKeys, int * Keys, 
00145        int NumDoubleCompanions,double ** DoubleCompanions, 
00146        int NumIntCompanions, int ** IntCompanions);
00147 
00149 
00155   static Epetra_Map Create_Root_Map(const Epetra_Map & usermap,
00156           int root = 0);
00157 
00159 
00163   static Epetra_Map Create_OneToOne_Map(const Epetra_Map& usermap,
00164           bool high_rank_proc_owns_shared=false);
00165 
00167 
00171   static Epetra_BlockMap Create_OneToOne_BlockMap(const Epetra_BlockMap& usermap,
00172               bool high_rank_proc_owns_shared=false);
00173 
00175   static double Chop(const double & Value){
00176     if (std::abs(Value) < chopVal_) return 0;
00177     return Value;
00178   };
00179 
00180   static const double chopVal_;
00181 
00182  private:
00183   unsigned int Seed_;
00184 };
00185 
00186 
00187 // Epetra_Util constructor
00188 inline Epetra_Util::Epetra_Util() : Seed_(std::rand()) {}
00189 // Epetra_Util constructor
00190 inline Epetra_Util::Epetra_Util(const Epetra_Util& Util) : Seed_(Util.Seed_) {}
00191 // Epetra_Util destructor
00192 inline Epetra_Util::~Epetra_Util(){}
00193 
00206 int Epetra_Util_binary_search(int item,
00207                               const int* list,
00208                               int len,
00209                               int& insertPoint);
00210 
00211 template<class T>
00212 int Epetra_Util_insert_empty_positions(T*& array, int& usedLength,
00213                                        int& allocatedLength,
00214                                        int insertOffset, int numPositions,
00215                                        int allocChunkSize=32)
00216 {
00217   if (insertOffset < 0 || insertOffset > usedLength ||
00218       usedLength > allocatedLength) {
00219     return(-1);
00220   }
00221 
00222   if ((usedLength+numPositions) < allocatedLength) {
00223     for(int i=usedLength-1; i>=insertOffset; --i) {
00224       array[i+numPositions] = array[i];
00225     }
00226     usedLength += numPositions;
00227     return(0);
00228   }
00229 
00230   allocatedLength += allocChunkSize;
00231   //what if allocatedLength is still not large enough?
00232   //loop until it is large enough:
00233   while(allocatedLength < usedLength+numPositions) {
00234     allocatedLength += allocChunkSize;
00235   }
00236 
00237   T* newlist = new T[allocatedLength];
00238 
00239   for(int i=0; i<insertOffset; ++i) {
00240     newlist[i] = array[i];
00241   }
00242 
00243   for(int i=insertOffset; i<usedLength; ++i) {
00244     newlist[i+numPositions] = array[i];
00245   }
00246 
00247   usedLength += numPositions;
00248   delete [] array;
00249   array = newlist;
00250   return(0);
00251 }
00252 
00268 template<class T>
00269 int Epetra_Util_insert(T item, int offset, T*& list,
00270                         int& usedLength,
00271                         int& allocatedLength,
00272                         int allocChunkSize=32)
00273 {
00274   int code = Epetra_Util_insert_empty_positions<T>(list, usedLength,
00275                                                 allocatedLength, offset, 1,
00276                                                 allocChunkSize);
00277   if (code != 0) {
00278     return(code);
00279   }
00280 
00281   list[offset] = item;
00282 
00283   return(0);
00284 }
00285 
00287 
00311 int Epetra_Util_ExtractHbData(Epetra_CrsMatrix * A, Epetra_MultiVector * LHS,
00312             Epetra_MultiVector * RHS,
00313             int & M, int & N, int & nz, int * & ptr,
00314             int * & ind, double * & val, int & Nrhs,
00315             double * & rhs, int & ldrhs,
00316             double * & lhs, int & ldlhs);
00317 
00318 
00319 #endif /* EPETRA_UTIL_H */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines