Epetra Package Browser (Single Doxygen Collection)  Development
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 #if defined(Epetra_SHOW_DEPRECATED_WARNINGS)
48 #ifdef __GNUC__
49 #warning "The Epetra package is deprecated"
50 #endif
51 #endif
52 
53 
54 
55 #include "Epetra_ConfigDefs.h"
56 #include "Epetra_Object.h"
57 #include <vector>
58 class Epetra_Map;
59 class Epetra_BlockMap;
60 class Epetra_CrsMatrix;
61 class Epetra_MultiVector;
62 class Epetra_Import;
63 
65 
87 class EPETRA_LIB_DLL_EXPORT Epetra_Util {
88 
89  public:
91 
93  Epetra_Util();
94 
95 
97 
99  Epetra_Util(const Epetra_Util& Util);
100 
102  virtual ~Epetra_Util();
103 
105 
106 
108  unsigned int RandomInt();
109 
111  double RandomDouble();
112 
114 
117  unsigned int Seed() const;
118 
120 
126  int SetSeed(unsigned int Seed_in);
127 
129 
131 
154  template<typename T>
155  static void EPETRA_LIB_DLL_EXPORT Sort(bool SortAscending, int NumKeys, T * Keys,
156  int NumDoubleCompanions,double ** DoubleCompanions,
157  int NumIntCompanions, int ** IntCompanions,
158  int NumLongLongCompanions, long long ** LongLongCompanions);
159 
160  static void Sort(bool SortAscending, int NumKeys, int * Keys,
161  int NumDoubleCompanions,double ** DoubleCompanions,
162  int NumIntCompanions, int ** IntCompanions,
163  int NumLongLongCompanions, long long ** LongLongCompanions);
164 
165  static void Sort(bool SortAscending, int NumKeys, long long * Keys,
166  int NumDoubleCompanions,double ** DoubleCompanions,
167  int NumIntCompanions, int ** IntCompanions,
168  int NumLongLongCompanions, long long ** LongLongCompanions);
169 
170  static void Sort(bool SortAscending, int NumKeys, int * Keys,
171  int NumDoubleCompanions,double ** DoubleCompanions,
172  int NumIntCompanions, int ** IntCompanions);
173 
174  static void Sort(bool SortAscending, int NumKeys, double * Keys,
175  int NumDoubleCompanions,double ** DoubleCompanions,
176  int NumIntCompanions, int ** IntCompanions,
177  int NumLongLongCompanions, long long ** LongLongCompanions);
178 
180 
186  static Epetra_Map Create_Root_Map(const Epetra_Map & usermap,
187  int root = 0);
188 
190 
194  static Epetra_Map Create_OneToOne_Map(const Epetra_Map& usermap,
195  bool high_rank_proc_owns_shared=false);
196 
198 
202  static Epetra_BlockMap Create_OneToOne_BlockMap(const Epetra_BlockMap& usermap,
203  bool high_rank_proc_owns_shared=false);
204 
205 #ifndef EPETRA_NO_64BIT_GLOBAL_INDICES
206 
211  static Epetra_BlockMap Create_OneToOne_BlockMap64(const Epetra_BlockMap& usermap,
212  bool high_rank_proc_owns_shared=false);
213 #endif
214 
215 
217  // For each row, sort column entries from smallest to largest.
218  // Use shell sort. Stable sort so it is fast if indices are already sorted.
219  static int SortCrsEntries(int NumRows, const int *CRS_rowptr, int *CRS_colind, double *CRS_vals);
220 
222  // For each row, sort column entries from smallest to largest.
223  // Use shell sort. Stable sort so it is fast if indices are already sorted.
224  static int SortCrsEntries(int NumRows, const size_t *CRS_rowptr, int *CRS_colind, double *CRS_vals);
225 
227  // For each row, sort column entries from smallest to largest, merging column ids that are identical by adding values.
228  // Use shell sort. Stable sort so it is fast if indices are already sorted.
229  static int SortAndMergeCrsEntries(int NumRows, int *CRS_rowptr, int *CRS_colind, double *CRS_vals);
230 
232  // For each row, sort column entries from smallest to largest, merging column ids that are identical by adding values.
233  // Use shell sort. Stable sort so it is fast if indices are already sorted.
234  static int SortAndMergeCrsEntries(int NumRows, size_t *CRS_rowptr, int *CRS_colind, double *CRS_vals);
235 
237 
244 #ifndef EPETRA_NO_32BIT_GLOBAL_INDICES
245  static int GetPidGidPairs(const Epetra_Import & Importer,std::vector< std::pair<int,int> > & gpids, bool use_minus_one_for_local);
246 #endif
247 #ifndef EPETRA_NO_64BIT_GLOBAL_INDICES
248  static int GetPidGidPairs(const Epetra_Import & Importer,std::vector< std::pair<int,long long> > & gpids, bool use_minus_one_for_local);
249 #endif
250 
251 
253 
255  static int GetPids(const Epetra_Import & Importer, std::vector<int> &pids, bool use_minus_one_for_local);
256 
258 
260  static int GetRemotePIDs(const Epetra_Import & Importer, std::vector<int> &RemotePIDs);
261 
262 
264  static double Chop(const double & Value);
265 // {
266 // if (std::abs(Value) < chopVal_) return 0;
267 // return Value;
268 // };
269 
270  static const double chopVal_;
271 
272  private:
273  unsigned int Seed_;
274 };
275 
276 
277 // Epetra_Util constructor
278 inline Epetra_Util::Epetra_Util() : Seed_(std::rand()) {}
279 // Epetra_Util constructor
280 inline Epetra_Util::Epetra_Util(const Epetra_Util& Util) : Seed_(Util.Seed_) {}
281 // Epetra_Util destructor
283 
296 template<typename T>
297 int Epetra_Util_binary_search(T item,
298  const T* list,
299  int len,
300  int& insertPoint);
301 
302 EPETRA_LIB_DLL_EXPORT int Epetra_Util_binary_search(int item,
303  const int* list,
304  int len,
305  int& insertPoint);
306 
307 EPETRA_LIB_DLL_EXPORT int Epetra_Util_binary_search(long long item,
308  const long long* list,
309  int len,
310  int& insertPoint);
311 
325 template<typename T>
327  const int* list,
328  const T* aux_list,
329  int len,
330  int& insertPoint);
331 
332 EPETRA_LIB_DLL_EXPORT int Epetra_Util_binary_search_aux(int item,
333  const int* list,
334  const int* aux_list,
335  int len,
336  int& insertPoint);
337 
338 EPETRA_LIB_DLL_EXPORT int Epetra_Util_binary_search_aux(long long item,
339  const int* list,
340  const long long* aux_list,
341  int len,
342  int& insertPoint);
343 
344 
345 
346 
347 
348 template<class T>
349 int Epetra_Util_insert_empty_positions(T*& array, int& usedLength,
350  int& allocatedLength,
351  int insertOffset, int numPositions,
352  int allocChunkSize=32)
353 {
354  if (insertOffset < 0 || insertOffset > usedLength ||
355  usedLength > allocatedLength) {
356  return(-1);
357  }
358 
359  if (((unsigned int)usedLength+(unsigned int)numPositions) < (unsigned int)allocatedLength) {
360  for(int i=usedLength-1; i>=insertOffset; --i) {
361  array[i+numPositions] = array[i];
362  }
363  usedLength += numPositions;
364  return(0);
365  }
366 
367  allocatedLength += allocChunkSize;
368  //what if allocatedLength is still not large enough?
369  //loop until it is large enough:
370  while(allocatedLength < usedLength+numPositions) {
371  allocatedLength += allocChunkSize;
372  }
373 
374  T* newlist = new T[allocatedLength];
375 
376  for(int i=0; i<insertOffset; ++i) {
377  newlist[i] = array[i];
378  }
379 
380  for(int i=insertOffset; i<usedLength; ++i) {
381  newlist[i+numPositions] = array[i];
382  }
383 
384  usedLength += numPositions;
385  delete [] array;
386  array = newlist;
387  return(0);
388 }
389 
405 template<class T>
406 int Epetra_Util_insert(T item, int offset, T*& list,
407  int& usedLength,
408  int& allocatedLength,
409  int allocChunkSize=32)
410 {
411  int code = Epetra_Util_insert_empty_positions<T>(list, usedLength,
412  allocatedLength, offset, 1,
413  allocChunkSize);
414  if (code != 0) {
415  return(code);
416  }
417 
418  list[offset] = item;
419 
420  return(0);
421 }
422 
429 template<class T>
430 T* Epetra_Util_data_ptr(std::vector<T> &vec)
431 {
432  if (!vec.empty()) {
433  return &vec.front();
434  }
435  return NULL;
436 }
437 
444 template<class T>
445 const T* Epetra_Util_data_ptr(const std::vector<T> &vec)
446 {
447  if (!vec.empty()) {
448  return &vec.front();
449  }
450  return NULL;
451 }
452 
454 
478 int EPETRA_LIB_DLL_EXPORT Epetra_Util_ExtractHbData(
480  Epetra_MultiVector * RHS,
481  int & M, int & N, int & nz, int * & ptr,
482  int * & ind, double * & val, int & Nrhs,
483  double * & rhs, int & ldrhs,
484  double * & lhs, int & ldlhs);
485 
486 
487 #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:127
unsigned int Seed_
Definition: Epetra_Util.h:273
static const double chopVal_
Definition: Epetra_Util.h:270
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:430
Epetra_Util()
Epetra_Util Constructor.
Definition: Epetra_Util.h:278
virtual ~Epetra_Util()
Epetra_Util Destructor.
Definition: Epetra_Util.h:282
Epetra_Import: This class builds an import object for efficient importing of off-processor elements...
Definition: Epetra_Import.h:71
Epetra_Util: The Epetra Util Wrapper Class.
Definition: Epetra_Util.h:87
Epetra_BlockMap: A class for partitioning block element vectors and matrices.
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.
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:349
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:406
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.