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 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 //  {
00177 //    if (std::abs(Value) < chopVal_) return 0;
00178 //    return Value;
00179 //  };
00180 
00181   static const double chopVal_;
00182 
00183  private:
00184   unsigned int Seed_;
00185 };
00186 
00187 
00188 // Epetra_Util constructor
00189 inline Epetra_Util::Epetra_Util() : Seed_(std::rand()) {}
00190 // Epetra_Util constructor
00191 inline Epetra_Util::Epetra_Util(const Epetra_Util& Util) : Seed_(Util.Seed_) {}
00192 // Epetra_Util destructor
00193 inline Epetra_Util::~Epetra_Util(){}
00194 
00207 EPETRA_LIB_DLL_EXPORT int Epetra_Util_binary_search(int item,
00208                               const int* list,
00209                               int len,
00210                               int& insertPoint);
00211 
00212 template<class T>
00213 int Epetra_Util_insert_empty_positions(T*& array, int& usedLength,
00214                                        int& allocatedLength,
00215                                        int insertOffset, int numPositions,
00216                                        int allocChunkSize=32)
00217 {
00218   if (insertOffset < 0 || insertOffset > usedLength ||
00219       usedLength > allocatedLength) {
00220     return(-1);
00221   }
00222 
00223   if ((usedLength+numPositions) < allocatedLength) {
00224     for(int i=usedLength-1; i>=insertOffset; --i) {
00225       array[i+numPositions] = array[i];
00226     }
00227     usedLength += numPositions;
00228     return(0);
00229   }
00230 
00231   allocatedLength += allocChunkSize;
00232   //what if allocatedLength is still not large enough?
00233   //loop until it is large enough:
00234   while(allocatedLength < usedLength+numPositions) {
00235     allocatedLength += allocChunkSize;
00236   }
00237 
00238   T* newlist = new T[allocatedLength];
00239 
00240   for(int i=0; i<insertOffset; ++i) {
00241     newlist[i] = array[i];
00242   }
00243 
00244   for(int i=insertOffset; i<usedLength; ++i) {
00245     newlist[i+numPositions] = array[i];
00246   }
00247 
00248   usedLength += numPositions;
00249   delete [] array;
00250   array = newlist;
00251   return(0);
00252 }
00253 
00269 template<class T>
00270 int Epetra_Util_insert(T item, int offset, T*& list,
00271                         int& usedLength,
00272                         int& allocatedLength,
00273                         int allocChunkSize=32)
00274 {
00275   int code = Epetra_Util_insert_empty_positions<T>(list, usedLength,
00276                                                 allocatedLength, offset, 1,
00277                                                 allocChunkSize);
00278   if (code != 0) {
00279     return(code);
00280   }
00281 
00282   list[offset] = item;
00283 
00284   return(0);
00285 }
00286 
00288 
00312 int Epetra_Util_ExtractHbData(Epetra_CrsMatrix * A, Epetra_MultiVector * LHS,
00313             Epetra_MultiVector * RHS,
00314             int & M, int & N, int & nz, int * & ptr,
00315             int * & ind, double * & val, int & Nrhs,
00316             double * & rhs, int & ldrhs,
00317             double * & lhs, int & ldlhs);
00318 
00319 
00320 #endif /* EPETRA_UTIL_H */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines