Epetra Package Browser (Single Doxygen Collection)  Development
Epetra_Util.h
Go to the documentation of this file.
1 /*
2 //@HEADER
3 // ************************************************************************
4 //
5 // Epetra: Linear Algebra Services Package
6 // Copyright 2011 Sandia Corporation
7 //
8 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
9 // the U.S. Government retains certain rights in this software.
10 //
11 // Redistribution and use in source and binary forms, with or without
12 // modification, are permitted provided that the following conditions are
13 // met:
14 //
15 // 1. Redistributions of source code must retain the above copyright
16 // notice, this list of conditions and the following disclaimer.
17 //
18 // 2. Redistributions in binary form must reproduce the above copyright
19 // notice, this list of conditions and the following disclaimer in the
20 // documentation and/or other materials provided with the distribution.
21 //
22 // 3. Neither the name of the Corporation nor the names of the
23 // contributors may be used to endorse or promote products derived from
24 // this software without specific prior written permission.
25 //
26 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 //
38 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
39 //
40 // ************************************************************************
41 //@HEADER
42 */
43 
44 #ifndef EPETRA_UTIL_H
45 #define EPETRA_UTIL_H
46 
47 #include "Epetra_ConfigDefs.h"
48 #include "Epetra_Object.h"
49 #include <vector>
50 class Epetra_Map;
51 class Epetra_BlockMap;
52 class Epetra_CrsMatrix;
53 class Epetra_MultiVector;
54 class Epetra_Import;
55 
57 
79 class EPETRA_LIB_DLL_EXPORT Epetra_Util {
80 
81  public:
83 
85  Epetra_Util();
86 
87 
89 
91  Epetra_Util(const Epetra_Util& Util);
92 
94  virtual ~Epetra_Util();
95 
97 
98 
100  unsigned int RandomInt();
101 
103  double RandomDouble();
104 
106 
109  unsigned int Seed() const;
110 
112 
118  int SetSeed(unsigned int Seed_in);
119 
121 
123 
146  template<typename T>
147  static void Sort(bool SortAscending, int NumKeys, T * Keys,
148  int NumDoubleCompanions,double ** DoubleCompanions,
149  int NumIntCompanions, int ** IntCompanions,
150  int NumLongLongCompanions, long long ** LongLongCompanions);
151 
152  static void Sort(bool SortAscending, int NumKeys, int * Keys,
153  int NumDoubleCompanions,double ** DoubleCompanions,
154  int NumIntCompanions, int ** IntCompanions,
155  int NumLongLongCompanions, long long ** LongLongCompanions);
156 
157  static void Sort(bool SortAscending, int NumKeys, long long * Keys,
158  int NumDoubleCompanions,double ** DoubleCompanions,
159  int NumIntCompanions, int ** IntCompanions,
160  int NumLongLongCompanions, long long ** LongLongCompanions);
161 
162  static void Sort(bool SortAscending, int NumKeys, int * Keys,
163  int NumDoubleCompanions,double ** DoubleCompanions,
164  int NumIntCompanions, int ** IntCompanions);
165 
166  static void Sort(bool SortAscending, int NumKeys, double * Keys,
167  int NumDoubleCompanions,double ** DoubleCompanions,
168  int NumIntCompanions, int ** IntCompanions,
169  int NumLongLongCompanions, long long ** LongLongCompanions);
170 
172 
178  static Epetra_Map Create_Root_Map(const Epetra_Map & usermap,
179  int root = 0);
180 
182 
186  static Epetra_Map Create_OneToOne_Map(const Epetra_Map& usermap,
187  bool high_rank_proc_owns_shared=false);
188 
190 
194  static Epetra_BlockMap Create_OneToOne_BlockMap(const Epetra_BlockMap& usermap,
195  bool high_rank_proc_owns_shared=false);
196 
197 
198 
200  // For each row, sort column entries from smallest to largest.
201  // Use shell sort. Stable sort so it is fast if indices are already sorted.
202  static int SortCrsEntries(int NumRows, const int *CRS_rowptr, int *CRS_colind, double *CRS_vals);
203 
205  // For each row, sort column entries from smallest to largest, merging column ids that are identical by adding values.
206  // Use shell sort. Stable sort so it is fast if indices are already sorted.
207  static int SortAndMergeCrsEntries(int NumRows, int *CRS_rowptr, int *CRS_colind, double *CRS_vals);
208 
210 
217 #ifndef EPETRA_NO_32BIT_GLOBAL_INDICES
218  static int GetPidGidPairs(const Epetra_Import & Importer,std::vector< std::pair<int,int> > & gpids, bool use_minus_one_for_local);
219 #endif
220 #ifndef EPETRA_NO_64BIT_GLOBAL_INDICES
221  static int GetPidGidPairs(const Epetra_Import & Importer,std::vector< std::pair<int,long long> > & gpids, bool use_minus_one_for_local);
222 #endif
223 
224 
226 
228  static int GetPids(const Epetra_Import & Importer, std::vector<int> &pids, bool use_minus_one_for_local);
229 
231 
233  static int GetRemotePIDs(const Epetra_Import & Importer, std::vector<int> &RemotePIDs);
234 
235 
237  static double Chop(const double & Value);
238 // {
239 // if (std::abs(Value) < chopVal_) return 0;
240 // return Value;
241 // };
242 
243  static const double chopVal_;
244 
245  private:
246  unsigned int Seed_;
247 };
248 
249 
250 // Epetra_Util constructor
251 inline Epetra_Util::Epetra_Util() : Seed_(std::rand()) {}
252 // Epetra_Util constructor
253 inline Epetra_Util::Epetra_Util(const Epetra_Util& Util) : Seed_(Util.Seed_) {}
254 // Epetra_Util destructor
256 
269 template<typename T>
270 int Epetra_Util_binary_search(T item,
271  const T* list,
272  int len,
273  int& insertPoint);
274 
275 EPETRA_LIB_DLL_EXPORT int Epetra_Util_binary_search(int item,
276  const int* list,
277  int len,
278  int& insertPoint);
279 
280 EPETRA_LIB_DLL_EXPORT int Epetra_Util_binary_search(long long item,
281  const long long* list,
282  int len,
283  int& insertPoint);
284 
298 template<typename T>
300  const int* list,
301  const T* aux_list,
302  int len,
303  int& insertPoint);
304 
305 EPETRA_LIB_DLL_EXPORT int Epetra_Util_binary_search_aux(int item,
306  const int* list,
307  const int* aux_list,
308  int len,
309  int& insertPoint);
310 
311 EPETRA_LIB_DLL_EXPORT int Epetra_Util_binary_search_aux(long long item,
312  const int* list,
313  const long long* aux_list,
314  int len,
315  int& insertPoint);
316 
317 
318 
319 
320 
321 template<class T>
322 int Epetra_Util_insert_empty_positions(T*& array, int& usedLength,
323  int& allocatedLength,
324  int insertOffset, int numPositions,
325  int allocChunkSize=32)
326 {
327  if (insertOffset < 0 || insertOffset > usedLength ||
328  usedLength > allocatedLength) {
329  return(-1);
330  }
331 
332  if ((usedLength+numPositions) < allocatedLength) {
333  for(int i=usedLength-1; i>=insertOffset; --i) {
334  array[i+numPositions] = array[i];
335  }
336  usedLength += numPositions;
337  return(0);
338  }
339 
340  allocatedLength += allocChunkSize;
341  //what if allocatedLength is still not large enough?
342  //loop until it is large enough:
343  while(allocatedLength < usedLength+numPositions) {
344  allocatedLength += allocChunkSize;
345  }
346 
347  T* newlist = new T[allocatedLength];
348 
349  for(int i=0; i<insertOffset; ++i) {
350  newlist[i] = array[i];
351  }
352 
353  for(int i=insertOffset; i<usedLength; ++i) {
354  newlist[i+numPositions] = array[i];
355  }
356 
357  usedLength += numPositions;
358  delete [] array;
359  array = newlist;
360  return(0);
361 }
362 
378 template<class T>
379 int Epetra_Util_insert(T item, int offset, T*& list,
380  int& usedLength,
381  int& allocatedLength,
382  int allocChunkSize=32)
383 {
384  int code = Epetra_Util_insert_empty_positions<T>(list, usedLength,
385  allocatedLength, offset, 1,
386  allocChunkSize);
387  if (code != 0) {
388  return(code);
389  }
390 
391  list[offset] = item;
392 
393  return(0);
394 }
395 
397 
422  Epetra_MultiVector * RHS,
423  int & M, int & N, int & nz, int * & ptr,
424  int * & ind, double * & val, int & Nrhs,
425  double * & rhs, int & ldrhs,
426  double * & lhs, int & ldlhs);
427 
428 
429 #endif /* EPETRA_UTIL_H */
Epetra_MultiVector: A class for constructing and using dense multi-vectors, vectors and matrices in p...
Epetra_Map: A class for partitioning vectors and matrices.
Definition: Epetra_Map.h:119
unsigned int Seed_
Definition: Epetra_Util.h:246
int Epetra_Util_binary_search_aux(T item, const int *list, const T *aux_list, int len, int &insertPoint)
Utility function to perform a binary-search on a list of data.
static const double chopVal_
Definition: Epetra_Util.h:243
Epetra_Util()
Epetra_Util Constructor.
Definition: Epetra_Util.h:251
virtual ~Epetra_Util()
Epetra_Util Destructor.
Definition: Epetra_Util.h:255
Epetra_Import: This class builds an import object for efficient importing of off-processor elements...
Definition: Epetra_Import.h:63
Epetra_Util: The Epetra Util Wrapper Class.
Definition: Epetra_Util.h:79
int Epetra_Util_ExtractHbData(Epetra_CrsMatrix *A, Epetra_MultiVector *LHS, Epetra_MultiVector *RHS, int &M, int &N, int &nz, int *&ptr, int *&ind, double *&val, int &Nrhs, double *&rhs, int &ldrhs, double *&lhs, int &ldlhs)
Harwell-Boeing data extraction routine.
Epetra_BlockMap: A class for partitioning block element vectors and matrices.
int Epetra_Util_binary_search(T item, const T *list, int len, int &insertPoint)
Utility function to perform a binary-search on a list of data.
Epetra_CrsMatrix: A class for constructing and using real-valued double-precision sparse compressed r...
int Epetra_Util_insert_empty_positions(T *&array, int &usedLength, int &allocatedLength, int insertOffset, int numPositions, int allocChunkSize=32)
Definition: Epetra_Util.h:322
int Epetra_Util_insert(T item, int offset, T *&list, int &usedLength, int &allocatedLength, int allocChunkSize=32)
Function to insert an item in a list, at a specified offset.
Definition: Epetra_Util.h:379