Epetra Package Browser (Single Doxygen Collection)  Development
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
test/IntMultiVector/cxx_main.cpp
Go to the documentation of this file.
1 //@HEADER
2 // ************************************************************************
3 //
4 // Epetra: Linear Algebra Services Package
5 // Copyright 2011 Sandia Corporation
6 //
7 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
8 // the U.S. Government retains certain rights in this software.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
38 //
39 // ************************************************************************
40 //@HEADER
41 
42 
43 // Epetra_BlockMap Test routine
44 
45 #include "Epetra_Time.h"
46 #include "Epetra_BlockMap.h"
47 #include "Epetra_IntMultiVector.h"
48 #ifdef EPETRA_MPI
49 #include "Epetra_MpiComm.h"
50 #include <mpi.h>
51 #else
52 #include "Epetra_SerialComm.h"
53 #endif
54 #include "ExecuteTestProblems.h"
55 #include "../epetra_test_err.h"
56 #include "Epetra_Version.h"
57 
58 // Restored MultiVector tests
59 int main(int argc, char *argv[]) {
60 
61  int ierr = 0, i, j;
62 
63 #ifdef EPETRA_MPI
64 
65  // Initialize MPI
66 
67  MPI_Init(&argc,&argv);
68  int rank; // My process ID
69 
70  MPI_Comm_rank(MPI_COMM_WORLD, &rank);
71  Epetra_MpiComm Comm(MPI_COMM_WORLD);
72 
73 #else
74 
75  int rank = 0;
76  Epetra_SerialComm Comm;
77 
78 #endif
79 
80  Comm.SetTracebackMode(0); // This should shut down any error tracing
81  bool verbose = false;
82 
83  // Check if we should print results to standard out
84  if (argc>1) if (argv[1][0]=='-' && argv[1][1]=='v') verbose = true;
85 
86  // char tmp;
87  // if (rank==0) cout << "Press any key to continue..."<< endl;
88  // if (rank==0) cin >> tmp;
89  // Comm.Barrier();
90 
91  int MyPID = Comm.MyPID();
92  int NumProc = Comm.NumProc();
93 
94  if (verbose && MyPID==0)
95  cout << Epetra_Version() << endl << endl;
96 
97  if (verbose) cout << Comm <<endl;
98 
99  bool verbose1 = verbose;
100 
101  // Redefine verbose to only print on PE 0
102  if (verbose && rank!=0) verbose = false;
103 
104  int NumMyElements = 10000;
105  int NumMyElements1 = NumMyElements; // Needed for localmap
106  int NumGlobalElements = NumMyElements*NumProc+EPETRA_MIN(NumProc,3);
107  if (MyPID < 3) NumMyElements++;
108  int IndexBase = 0;
109  int ElementSize = 7;
110  int NumVectors = 4;
111 
112  // Test LocalMap constructor
113  // and Epetra-defined uniform linear distribution constructor
114 
115  if (verbose) cout << "\n*********************************************************" << endl;
116  if (verbose) cout << "Checking Epetra_LocalMap(NumMyElements1, IndexBase, Comm)" << endl;
117  if (verbose) cout << " and Epetra_BlockMap(NumGlobalElements, ElementSize, IndexBase, Comm)" << endl;
118  if (verbose) cout << "*********************************************************" << endl;
119 
120  Epetra_LocalMap *LocalMap = new Epetra_LocalMap(NumMyElements1, IndexBase,
121  Comm);
122  Epetra_BlockMap * BlockMap = new Epetra_BlockMap(NumGlobalElements, ElementSize, IndexBase, Comm);
123  EPETRA_TEST_ERR(IntMultiVectorTests(*BlockMap, NumVectors, verbose),ierr);
124 
125  delete BlockMap;
126 
127  // Test User-defined linear distribution constructor
128 
129  if (verbose) cout << "\n*********************************************************" << endl;
130  if (verbose) cout << "Checking Epetra_BlockMap(NumGlobalElements, NumMyElements, ElementSize, IndexBase, Comm)" << endl;
131  if (verbose) cout << "*********************************************************" << endl;
132 
133  BlockMap = new Epetra_BlockMap(NumGlobalElements, NumMyElements, ElementSize, IndexBase, Comm);
134 
135  EPETRA_TEST_ERR(IntMultiVectorTests(*BlockMap, NumVectors, verbose),ierr);
136 
137  delete BlockMap;
138 
139  // Test User-defined arbitrary distribution constructor
140  // Generate Global Element List. Do in reverse for fun!
141 
142  int * MyGlobalElements = new int[NumMyElements];
143  int MaxMyGID = (Comm.MyPID()+1)*NumMyElements-1+IndexBase;
144  if (Comm.MyPID()>2) MaxMyGID+=3;
145  for (i = 0; i<NumMyElements; i++) MyGlobalElements[i] = MaxMyGID-i;
146 
147  if (verbose) cout << "\n*********************************************************" << endl;
148  if (verbose) cout << "Checking Epetra_BlockMap(NumGlobalElements, NumMyElements, MyGlobalElements, ElementSize, IndexBase, Comm)" << endl;
149  if (verbose) cout << "*********************************************************" << endl;
150 
151  BlockMap = new Epetra_BlockMap(NumGlobalElements, NumMyElements, MyGlobalElements, ElementSize,
152  IndexBase, Comm);
153  EPETRA_TEST_ERR(IntMultiVectorTests(*BlockMap, NumVectors, verbose),ierr);
154 
155  delete BlockMap;
156 
157  int * ElementSizeList = new int[NumMyElements];
158  int NumMyEquations = 0;
159  int NumGlobalEquations = 0;
160  for (i = 0; i<NumMyElements; i++)
161  {
162  ElementSizeList[i] = i%6+2; // blocksizes go from 2 to 7
163  NumMyEquations += ElementSizeList[i];
164  }
165  ElementSize = 7; // Set to maximum for use in checkmap
166  NumGlobalEquations = Comm.NumProc()*NumMyEquations;
167 
168  // Adjust NumGlobalEquations based on processor ID
169  if (Comm.NumProc() > 3)
170  {
171  if (Comm.MyPID()>2)
172  NumGlobalEquations += 3*((NumMyElements)%6+2);
173  else
174  NumGlobalEquations -= (Comm.NumProc()-3)*((NumMyElements-1)%6+2);
175  }
176 
177  if (verbose) cout << "\n*********************************************************" << endl;
178  if (verbose) cout << "Checking Epetra_BlockMap(NumGlobalElements, NumMyElements, MyGlobalElements, ElementSizeList, IndexBase, Comm)" << endl;
179  if (verbose) cout << "*********************************************************" << endl;
180 
181  BlockMap = new Epetra_BlockMap(NumGlobalElements, NumMyElements, MyGlobalElements, ElementSizeList,
182  IndexBase, Comm);
183  EPETRA_TEST_ERR(IntMultiVectorTests(*BlockMap, NumVectors, verbose),ierr);
184 
185  // Test Copy constructor
186 
187  if (verbose) cout << "\n*********************************************************" << endl;
188  if (verbose) cout << "Checking Epetra_BlockMap(*BlockMap)" << endl;
189  if (verbose) cout << "*********************************************************" << endl;
190 
191  Epetra_BlockMap * BlockMap1 = new Epetra_BlockMap(*BlockMap);
192 
193  EPETRA_TEST_ERR(IntMultiVectorTests(*BlockMap, NumVectors, verbose),ierr);
194 
195  delete [] ElementSizeList;
196  delete [] MyGlobalElements;
197  delete BlockMap;
198  delete BlockMap1;
199 
200 
201  // Test Petra-defined uniform linear distribution constructor
202 
203  if (verbose) cout << "\n*********************************************************" << endl;
204  if (verbose) cout << "Checking Epetra_Map(NumGlobalElements, IndexBase, Comm)" << endl;
205  if (verbose) cout << "*********************************************************" << endl;
206 
207  Epetra_Map * Map = new Epetra_Map(NumGlobalElements, IndexBase, Comm);
208  EPETRA_TEST_ERR(IntMultiVectorTests(*Map, NumVectors, verbose),ierr);
209 
210  delete Map;
211 
212  // Test User-defined linear distribution constructor
213 
214  if (verbose) cout << "\n*********************************************************" << endl;
215  if (verbose) cout << "Checking Epetra_Map(NumGlobalElements, NumMyElements, IndexBase, Comm)" << endl;
216  if (verbose) cout << "*********************************************************" << endl;
217 
218  Map = new Epetra_Map(NumGlobalElements, NumMyElements, IndexBase, Comm);
219 
220  EPETRA_TEST_ERR(IntMultiVectorTests(*Map, NumVectors, verbose),ierr);
221 
222  delete Map;
223 
224  // Test User-defined arbitrary distribution constructor
225  // Generate Global Element List. Do in reverse for fun!
226 
227  MyGlobalElements = new int[NumMyElements];
228  MaxMyGID = (Comm.MyPID()+1)*NumMyElements-1+IndexBase;
229  if (Comm.MyPID()>2) MaxMyGID+=3;
230  for (i = 0; i<NumMyElements; i++) MyGlobalElements[i] = MaxMyGID-i;
231 
232  if (verbose) cout << "\n*********************************************************" << endl;
233  if (verbose) cout << "Checking Epetra_Map(NumGlobalElements, NumMyElements, MyGlobalElements, IndexBase, Comm)" << endl;
234  if (verbose) cout << "*********************************************************" << endl;
235 
236  Map = new Epetra_Map(NumGlobalElements, NumMyElements, MyGlobalElements,
237  IndexBase, Comm);
238  EPETRA_TEST_ERR(IntMultiVectorTests(*Map, NumVectors, verbose),ierr);
239 
240  // Test Copy constructor
241 
242  if (verbose) cout << "\n*********************************************************" << endl;
243  if (verbose) cout << "Checking Epetra_Map(*Map)" << endl;
244  if (verbose) cout << "*********************************************************" << endl;
245 
246  Epetra_Map Map1(*Map);
247 
248  EPETRA_TEST_ERR(IntMultiVectorTests(*Map, NumVectors, verbose),ierr);
249 
250  delete [] MyGlobalElements;
251  delete Map;
252  delete LocalMap;
253 
254  if (verbose1)
255  {
256  // Test MultiVector ostream operator with Petra-defined uniform linear distribution constructor
257  // and a small vector
258 
259  Epetra_Map Map4(100, IndexBase, Comm);
260  int * Ip = new int[200];
261  for (j=0; j<2; j++)
262  for (i=0; i<100; i++)
263  Ip[i+j*100] = i+j*100;
264  Epetra_IntMultiVector I(View, Map4,Ip, 100, 2);
265 
266  if (verbose) cout << "\n\nTesting ostream operator: Multivector should be 100-by-2 and print i,j indices"
267  << endl << endl;
268  cout << I << endl;
269 
270  Epetra_BlockMap Map5(-1, 25, 4, IndexBase, Comm);
271  Epetra_IntMultiVector I1(View, Map5,Ip, 100, 2);
272  if (verbose) cout << "\n\nTesting ostream operator: Same Multivector as before except using BlockMap of 25x4"
273  << endl << endl;
274  cout << I1 << endl;
275 
276  if (verbose) cout << "Traceback Mode value = " << I.GetTracebackMode() << endl;
277  delete [] Ip;
278  }
279 
280 #ifdef EPETRA_MPI
281  MPI_Finalize();
282 #endif
283 
284  return ierr;
285 }
286 
Epetra_Map: A class for partitioning vectors and matrices.
Definition: Epetra_Map.h:119
#define EPETRA_TEST_ERR(a, b)
static void SetTracebackMode(int TracebackModeValue)
Set the value of the Epetra_Object error traceback report mode.
#define EPETRA_MIN(x, y)
Epetra_IntMultiVector: A class for constructing and using dense multi-vectors, vectors and matrices i...
int MyPID() const
Return my process ID.
Epetra_MpiComm: The Epetra MPI Communication Class.
std::string Epetra_Version()
int IntMultiVectorTests(const Epetra_BlockMap &Map, int NumVectors, bool verbose)
Epetra_BlockMap: A class for partitioning block element vectors and matrices.
int NumProc() const
Returns total number of processes (always returns 1 for SerialComm).
Epetra_SerialComm: The Epetra Serial Communication Class.
static int GetTracebackMode()
Get the value of the Epetra_Object error report mode.
int main(int argc, char *argv[])
Epetra_LocalMap: A class for replicating vectors and matrices across multiple processors.