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 #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 EPETRA_LIB_DLL_EXPORT 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 #ifndef EPETRA_NO_64BIT_GLOBAL_INDICES
198 
203  static Epetra_BlockMap Create_OneToOne_BlockMap64(const Epetra_BlockMap& usermap,
204  bool high_rank_proc_owns_shared=false);
205 #endif
206 
207 
209  // For each row, sort column entries from smallest to largest.
210  // Use shell sort. Stable sort so it is fast if indices are already sorted.
211  static int SortCrsEntries(int NumRows, const int *CRS_rowptr, int *CRS_colind, double *CRS_vals);
212 
214  // For each row, sort column entries from smallest to largest.
215  // Use shell sort. Stable sort so it is fast if indices are already sorted.
216  static int SortCrsEntries(int NumRows, const size_t *CRS_rowptr, int *CRS_colind, double *CRS_vals);
217 
219  // For each row, sort column entries from smallest to largest, merging column ids that are identical by adding values.
220  // Use shell sort. Stable sort so it is fast if indices are already sorted.
221  static int SortAndMergeCrsEntries(int NumRows, int *CRS_rowptr, int *CRS_colind, double *CRS_vals);
222 
224  // For each row, sort column entries from smallest to largest, merging column ids that are identical by adding values.
225  // Use shell sort. Stable sort so it is fast if indices are already sorted.
226  static int SortAndMergeCrsEntries(int NumRows, size_t *CRS_rowptr, int *CRS_colind, double *CRS_vals);
227 
229 
236 #ifndef EPETRA_NO_32BIT_GLOBAL_INDICES
237  static int GetPidGidPairs(const Epetra_Import & Importer,std::vector< std::pair<int,int> > & gpids, bool use_minus_one_for_local);
238 #endif
239 #ifndef EPETRA_NO_64BIT_GLOBAL_INDICES
240  static int GetPidGidPairs(const Epetra_Import & Importer,std::vector< std::pair<int,long long> > & gpids, bool use_minus_one_for_local);
241 #endif
242 
243 
245 
247  static int GetPids(const Epetra_Import & Importer, std::vector<int> &pids, bool use_minus_one_for_local);
248 
250 
252  static int GetRemotePIDs(const Epetra_Import & Importer, std::vector<int> &RemotePIDs);
253 
254 
256  static double Chop(const double & Value);
257 // {
258 // if (std::abs(Value) < chopVal_) return 0;
259 // return Value;
260 // };
261 
262  static const double chopVal_;
263 
264  private:
265  unsigned int Seed_;
266 };
267 
268 
269 // Epetra_Util constructor
270 inline Epetra_Util::Epetra_Util() : Seed_(std::rand()) {}
271 // Epetra_Util constructor
272 inline Epetra_Util::Epetra_Util(const Epetra_Util& Util) : Seed_(Util.Seed_) {}
273 // Epetra_Util destructor
275 
288 template<typename T>
289 int Epetra_Util_binary_search(T item,
290  const T* list,
291  int len,
292  int& insertPoint);
293 
294 EPETRA_LIB_DLL_EXPORT int Epetra_Util_binary_search(int item,
295  const int* list,
296  int len,
297  int& insertPoint);
298 
299 EPETRA_LIB_DLL_EXPORT int Epetra_Util_binary_search(long long item,
300  const long long* list,
301  int len,
302  int& insertPoint);
303 
317 template<typename T>
319  const int* list,
320  const T* aux_list,
321  int len,
322  int& insertPoint);
323 
324 EPETRA_LIB_DLL_EXPORT int Epetra_Util_binary_search_aux(int item,
325  const int* list,
326  const int* aux_list,
327  int len,
328  int& insertPoint);
329 
330 EPETRA_LIB_DLL_EXPORT int Epetra_Util_binary_search_aux(long long item,
331  const int* list,
332  const long long* aux_list,
333  int len,
334  int& insertPoint);
335 
336 
337 
338 
339 
340 template<class T>
341 int Epetra_Util_insert_empty_positions(T*& array, int& usedLength,
342  int& allocatedLength,
343  int insertOffset, int numPositions,
344  int allocChunkSize=32)
345 {
346  if (insertOffset < 0 || insertOffset > usedLength ||
347  usedLength > allocatedLength) {
348  return(-1);
349  }
350 
351  if (((unsigned int)usedLength+(unsigned int)numPositions) < (unsigned int)allocatedLength) {
352  for(int i=usedLength-1; i>=insertOffset; --i) {
353  array[i+numPositions] = array[i];
354  }
355  usedLength += numPositions;
356  return(0);
357  }
358 
359  allocatedLength += allocChunkSize;
360  //what if allocatedLength is still not large enough?
361  //loop until it is large enough:
362  while(allocatedLength < usedLength+numPositions) {
363  allocatedLength += allocChunkSize;
364  }
365 
366  T* newlist = new T[allocatedLength];
367 
368  for(int i=0; i<insertOffset; ++i) {
369  newlist[i] = array[i];
370  }
371 
372  for(int i=insertOffset; i<usedLength; ++i) {
373  newlist[i+numPositions] = array[i];
374  }
375 
376  usedLength += numPositions;
377  delete [] array;
378  array = newlist;
379  return(0);
380 }
381 
397 template<class T>
398 int Epetra_Util_insert(T item, int offset, T*& list,
399  int& usedLength,
400  int& allocatedLength,
401  int allocChunkSize=32)
402 {
403  int code = Epetra_Util_insert_empty_positions<T>(list, usedLength,
404  allocatedLength, offset, 1,
405  allocChunkSize);
406  if (code != 0) {
407  return(code);
408  }
409 
410  list[offset] = item;
411 
412  return(0);
413 }
414 
421 template<class T>
422 T* Epetra_Util_data_ptr(std::vector<T> &vec)
423 {
424  if (!vec.empty()) {
425  return &vec.front();
426  }
427  return NULL;
428 }
429 
436 template<class T>
437 const T* Epetra_Util_data_ptr(const std::vector<T> &vec)
438 {
439  if (!vec.empty()) {
440  return &vec.front();
441  }
442  return NULL;
443 }
444 
446 
470 int EPETRA_LIB_DLL_EXPORT Epetra_Util_ExtractHbData(
472  Epetra_MultiVector * RHS,
473  int & M, int & N, int & nz, int * & ptr,
474  int * & ind, double * & val, int & Nrhs,
475  double * & rhs, int & ldrhs,
476  double * & lhs, int & ldlhs);
477 
478 
479 #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:265
static const double chopVal_
Definition: Epetra_Util.h:262
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:422
Epetra_Util()
Epetra_Util Constructor.
Definition: Epetra_Util.h:270
virtual ~Epetra_Util()
Epetra_Util Destructor.
Definition: Epetra_Util.h:274
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
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:341
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:398
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.