Epetra Package Browser (Single Doxygen Collection)  Development
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Epetra_test_functions.cpp
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 #include <Epetra_Map.h>
44 #include <Epetra_CrsMatrix.h>
45 #include <Epetra_SerialComm.h>
46 #include <Epetra_Util.h>
47 
48 #ifdef EPETRA_MPI
49 #include <Epetra_MpiComm.h>
50 #endif
51 
52 namespace epetra_test {
53 
54 bool global_check_for_flag_on_proc_0(const char* flag,
55  int numargs,
56  char** strargs,
57  const Epetra_Comm& comm)
58 {
59  int mypid = comm.MyPID();
60  int numprocs = comm.NumProc();
61 
62  int flag_found = 0;
63  if (mypid==0) {
64  for(int i=0; i<numargs; ++i) {
65  if (strargs[i]==0) continue;
66 
67  if (strcmp(flag, strargs[i]) == 0) {
68  flag_found = 1; break;
69  }
70  }
71  }
72 
73  if (numprocs > 1) {
74  comm.Broadcast(&flag_found, 1, 0);
75  }
76 
77  bool return_value = flag_found==1 ? true : false;
78 
79  return( return_value );
80 }
81 
82 Epetra_Comm* create_comm(int argc, char** argv)
83 {
84 #ifdef EPETRA_MPI
85  MPI_Init(&argc, &argv);
86 
87  return( new Epetra_MpiComm(MPI_COMM_WORLD) );
88 #else
89  return( new Epetra_SerialComm );
90 #endif
91 }
92 
94 {
95  const Epetra_Map& Amap = A.RowMap();
96  const Epetra_Map& Bmap = B.RowMap();
97 
98  if (!Amap.PointSameAs(Bmap)) {
99  return(false);
100  }
101 
102  int numRows = Amap.NumMyElements();
103 
104  if(Amap.GlobalIndicesInt())
105  {
106 #ifndef EPETRA_NO_32BIT_GLOBAL_INDICES
107  int* rows = Amap.MyGlobalElements();
108 
109  Epetra_Util util;
110 
111  for(int i=0; i<numRows; ++i) {
112  int row = rows[i];
113  int rowLen = A.NumGlobalEntries(row);
114  if (rowLen != B.NumGlobalEntries(row)) {
115  return(false);
116  }
117 
118  int* indices = new int[rowLen*2];
119  int* Bindices = indices+rowLen;
120 
121  double* values = new double[rowLen*2];
122  double* Bvalues = values+rowLen;
123 
124  A.ExtractGlobalRowCopy(row, rowLen, rowLen, values, indices);
125  B.ExtractGlobalRowCopy(row, rowLen, rowLen, Bvalues, Bindices);
126 
127  util.Sort(true, rowLen, indices, 1, &values, 0, 0, 0, 0);
128  util.Sort(true, rowLen, Bindices, 1, &Bvalues, 0, 0, 0, 0);
129 
130  bool same = true;
131  for(int j=0; j<rowLen; ++j) {
132  if (indices[j] != Bindices[j]) {
133  same = false; break;
134  }
135  if (values[j] != Bvalues[j]) {
136  same = false; break;
137  }
138  }
139 
140  delete [] indices;
141  delete [] values;
142 
143  if (!same) {
144  return(false);
145  }
146  }
147 #else
148  throw "compare_matrices: GlobalIndices int but not API for it.";
149 #endif
150  }
151  else if(Amap.GlobalIndicesLongLong()) {
152 #ifndef EPETRA_NO_64BIT_GLOBAL_INDICES
153 
154  long long* rows = Amap.MyGlobalElements64();
155 
156  Epetra_Util util;
157 
158  for(int i=0; i<numRows; ++i) {
159  long long row = rows[i];
160  int rowLen = A.NumGlobalEntries(row);
161  if (rowLen != B.NumGlobalEntries(row)) {
162  return(false);
163  }
164 
165  long long* indices = new long long[rowLen*2];
166  long long* Bindices = indices+rowLen;
167 
168  double* values = new double[rowLen*2];
169  double* Bvalues = values+rowLen;
170 
171  A.ExtractGlobalRowCopy(row, rowLen, rowLen, values, indices);
172  B.ExtractGlobalRowCopy(row, rowLen, rowLen, Bvalues, Bindices);
173 
174  util.Sort(true, rowLen, indices, 1, &values, 0, 0, 0, 0);
175  util.Sort(true, rowLen, Bindices, 1, &Bvalues, 0, 0, 0, 0);
176 
177  bool same = true;
178  for(int j=0; j<rowLen; ++j) {
179  if (indices[j] != Bindices[j]) {
180  same = false; break;
181  }
182  if (values[j] != Bvalues[j]) {
183  same = false; break;
184  }
185  }
186 
187  delete [] indices;
188  delete [] values;
189 
190  if (!same) {
191  return(false);
192  }
193  }
194 #else
195  throw "compare_matrices: GlobalIndices long long but not API for it.";
196 #endif
197  }
198  else {
199  return(false);
200  }
201 
202 
203  return(true);
204 }
205 
207 {
208  const Epetra_Map& Amap = A.RowMap();
209  const Epetra_Map& Bmap = B.RowMap();
210 
211  if (!Amap.PointSameAs(Bmap)) {
212  return(false);
213  }
214 
215  // FIXME (mfh 18 Apr 2014) Does the commented-out line below have
216  // any side effects? In any case, it was causing a build warning
217  // due to numRows never being used. btw, shouldn't a function that
218  // claims to compare matrices actually look at the matrices???
219 
220  //int numRows = Amap.NumMyElements();
221  (void) Amap.NumMyElements();
222  return true;
223 }
224 
225 }//namespace epetra_test
226 
bool compare_matrices_LL(const Epetra_CrsMatrix &A, const Epetra_CrsMatrix &B)
bool PointSameAs(const Epetra_BlockMap &Map) const
Returns true if this and Map have identical point-wise structure.
Epetra_Map: A class for partitioning vectors and matrices.
Definition: Epetra_Map.h:119
int NumGlobalEntries(long long Row) const
Returns the current number of nonzero entries in specified global row on this processor.
bool GlobalIndicesLongLong() const
Returns true if map create with long long NumGlobalElements.
Epetra_Comm * create_comm(int argc, char **argv)
If macro EPETRA_MPI is defined, call MPI_Init and then return new Epetra_MpiComm. ...
int MyGlobalElements(int *MyGlobalElementList) const
Puts list of global elements on this processor into the user-provided array.
bool GlobalIndicesInt() const
Returns true if map create with int NumGlobalElements.
bool global_check_for_flag_on_proc_0(const char *flag, int numargs, char **strargs, const Epetra_Comm &comm)
Check through a list of C-style string arguments searching for a specified flag on proc 0...
Epetra_MpiComm: The Epetra MPI Communication Class.
virtual int MyPID() const =0
Return my process ID.
Epetra_Util: The Epetra Util Wrapper Class.
Definition: Epetra_Util.h:79
const Epetra_Map & RowMap() const
Returns the Epetra_Map object associated with the rows of this matrix.
int NumMyElements() const
Number of elements on the calling processor.
Epetra_Comm: The Epetra Communication Abstract Base Class.
Definition: Epetra_Comm.h:73
int ExtractGlobalRowCopy(int GlobalRow, int Length, int &NumEntries, double *Values, int *Indices) const
Returns a copy of the specified global row in user-provided arrays.
bool compare_matrices(const Epetra_CrsMatrix &A, const Epetra_CrsMatrix &B)
Check whether the two CrsMatrix arguments have the same size, structure and coefs.
virtual int Broadcast(double *MyVals, int Count, int Root) const =0
Epetra_Comm Broadcast function.
Epetra_SerialComm: The Epetra Serial Communication Class.
virtual int NumProc() const =0
Returns total number of processes.
Epetra_CrsMatrix: A class for constructing and using real-valued double-precision sparse compressed r...
static void Sort(bool SortAscending, int NumKeys, T *Keys, int NumDoubleCompanions, double **DoubleCompanions, int NumIntCompanions, int **IntCompanions, int NumLongLongCompanions, long long **LongLongCompanions)
Epetra_Util Sort Routine (Shell sort)
long long * MyGlobalElements64() const