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.
206  // Use shell sort. Stable sort so it is fast if indices are already sorted.
207  static int SortCrsEntries(int NumRows, const size_t *CRS_rowptr, int *CRS_colind, double *CRS_vals);
208 
210  // For each row, sort column entries from smallest to largest, merging column ids that are identical by adding values.
211  // Use shell sort. Stable sort so it is fast if indices are already sorted.
212  static int SortAndMergeCrsEntries(int NumRows, int *CRS_rowptr, int *CRS_colind, double *CRS_vals);
213 
215  // For each row, sort column entries from smallest to largest, merging column ids that are identical by adding values.
216  // Use shell sort. Stable sort so it is fast if indices are already sorted.
217  static int SortAndMergeCrsEntries(int NumRows, size_t *CRS_rowptr, int *CRS_colind, double *CRS_vals);
218 
220 
227 #ifndef EPETRA_NO_32BIT_GLOBAL_INDICES
228  static int GetPidGidPairs(const Epetra_Import & Importer,std::vector< std::pair<int,int> > & gpids, bool use_minus_one_for_local);
229 #endif
230 #ifndef EPETRA_NO_64BIT_GLOBAL_INDICES
231  static int GetPidGidPairs(const Epetra_Import & Importer,std::vector< std::pair<int,long long> > & gpids, bool use_minus_one_for_local);
232 #endif
233 
234 
236 
238  static int GetPids(const Epetra_Import & Importer, std::vector<int> &pids, bool use_minus_one_for_local);
239 
241 
243  static int GetRemotePIDs(const Epetra_Import & Importer, std::vector<int> &RemotePIDs);
244 
245 
247  static double Chop(const double & Value);
248 // {
249 // if (std::abs(Value) < chopVal_) return 0;
250 // return Value;
251 // };
252 
253  static const double chopVal_;
254 
255  private:
256  unsigned int Seed_;
257 };
258 
259 
260 // Epetra_Util constructor
261 inline Epetra_Util::Epetra_Util() : Seed_(std::rand()) {}
262 // Epetra_Util constructor
263 inline Epetra_Util::Epetra_Util(const Epetra_Util& Util) : Seed_(Util.Seed_) {}
264 // Epetra_Util destructor
266 
279 template<typename T>
280 int Epetra_Util_binary_search(T item,
281  const T* list,
282  int len,
283  int& insertPoint);
284 
285 EPETRA_LIB_DLL_EXPORT int Epetra_Util_binary_search(int item,
286  const int* list,
287  int len,
288  int& insertPoint);
289 
290 EPETRA_LIB_DLL_EXPORT int Epetra_Util_binary_search(long long item,
291  const long long* list,
292  int len,
293  int& insertPoint);
294 
308 template<typename T>
310  const int* list,
311  const T* aux_list,
312  int len,
313  int& insertPoint);
314 
315 EPETRA_LIB_DLL_EXPORT int Epetra_Util_binary_search_aux(int item,
316  const int* list,
317  const int* aux_list,
318  int len,
319  int& insertPoint);
320 
321 EPETRA_LIB_DLL_EXPORT int Epetra_Util_binary_search_aux(long long item,
322  const int* list,
323  const long long* aux_list,
324  int len,
325  int& insertPoint);
326 
327 
328 
329 
330 
331 template<class T>
332 int Epetra_Util_insert_empty_positions(T*& array, int& usedLength,
333  int& allocatedLength,
334  int insertOffset, int numPositions,
335  int allocChunkSize=32)
336 {
337  if (insertOffset < 0 || insertOffset > usedLength ||
338  usedLength > allocatedLength) {
339  return(-1);
340  }
341 
342  if ((usedLength+numPositions) < allocatedLength) {
343  for(int i=usedLength-1; i>=insertOffset; --i) {
344  array[i+numPositions] = array[i];
345  }
346  usedLength += numPositions;
347  return(0);
348  }
349 
350  allocatedLength += allocChunkSize;
351  //what if allocatedLength is still not large enough?
352  //loop until it is large enough:
353  while(allocatedLength < usedLength+numPositions) {
354  allocatedLength += allocChunkSize;
355  }
356 
357  T* newlist = new T[allocatedLength];
358 
359  for(int i=0; i<insertOffset; ++i) {
360  newlist[i] = array[i];
361  }
362 
363  for(int i=insertOffset; i<usedLength; ++i) {
364  newlist[i+numPositions] = array[i];
365  }
366 
367  usedLength += numPositions;
368  delete [] array;
369  array = newlist;
370  return(0);
371 }
372 
388 template<class T>
389 int Epetra_Util_insert(T item, int offset, T*& list,
390  int& usedLength,
391  int& allocatedLength,
392  int allocChunkSize=32)
393 {
394  int code = Epetra_Util_insert_empty_positions<T>(list, usedLength,
395  allocatedLength, offset, 1,
396  allocChunkSize);
397  if (code != 0) {
398  return(code);
399  }
400 
401  list[offset] = item;
402 
403  return(0);
404 }
405 
412 template<class T>
413 T* Epetra_Util_data_ptr(std::vector<T> &vec)
414 {
415  if (!vec.empty()) {
416  return &vec.front();
417  }
418  return NULL;
419 }
420 
427 template<class T>
428 const T* Epetra_Util_data_ptr(const std::vector<T> &vec)
429 {
430  if (!vec.empty()) {
431  return &vec.front();
432  }
433  return NULL;
434 }
435 
437 
462  Epetra_MultiVector * RHS,
463  int & M, int & N, int & nz, int * & ptr,
464  int * & ind, double * & val, int & Nrhs,
465  double * & rhs, int & ldrhs,
466  double * & lhs, int & ldlhs);
467 
468 
469 #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:256
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:253
T * Epetra_Util_data_ptr(std::vector< T > &vec)
Function that returns either a pointer to the first entry in the vector or, if the vector is empty...
Definition: Epetra_Util.h:413
Epetra_Util()
Epetra_Util Constructor.
Definition: Epetra_Util.h:261
virtual ~Epetra_Util()
Epetra_Util Destructor.
Definition: Epetra_Util.h:265
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:332
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:389