Epetra Package Browser (Single Doxygen Collection)  Development
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
test/IntMultiVectorDistributed/cxx_main.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 
44 #include "Epetra_ConfigDefs.h"
45 #include "Epetra_IntMultiVector.h"
46 #include "Epetra_BlockMap.h"
47 #include "Epetra_CombineMode.h"
48 #include "Epetra_Import.h"
49 #ifdef EPETRA_MPI
50 #include "Epetra_MpiComm.h"
51 #include <mpi.h>
52 #else
53 #include "Epetra_SerialComm.h"
54 #endif
55 #include "../epetra_test_err.h"
56 #include "Epetra_Version.h"
57 
58 int main(int argc, char *argv[]) {
59  int ierr = 0;
60  int global_ierr = 0;
61  int numErr = 0;
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  bool verbose = false;
81 
82  // Check if we should print results to standard out
83  if(argc > 1) {
84  if(argv[1][0]=='-' && argv[1][1]=='v') {
85  verbose = true;
86  }
87  }
88 
89  Comm.SetTracebackMode(0); // This should shut down any error traceback reporting
90  int MyPID = Comm.MyPID();
91  int NumProc = Comm.NumProc();
92 
93  if(verbose && MyPID==0)
94  cout << Epetra_Version() << endl << endl;
95 
96  if(verbose) cout << "Processor " << MyPID << " of " << NumProc << " is alive." << endl;
97 
98  // Redefine verbose to only print on PE 0
99  if(verbose && rank != 0)
100  verbose = false;
101 
102  int NumMyEquations = 6;
103  int NumGlobalEquations = NumMyEquations*NumProc+EPETRA_MIN(NumProc,3);
104  if(MyPID < 3)
105  NumMyEquations++;
106 
107  // Construct a Map that puts approximately the same Number of equations on each processor
108 
109  Epetra_BlockMap Map(NumGlobalEquations, NumMyEquations, 1, 0, Comm);
110 
111  // Get update list and number of local equations from newly created Map
112  std::vector<int> MyGlobalElements(Map.NumMyElements());
113  Map.MyGlobalElements(MyGlobalElements.data());
114 
115  // Construct the IntMultiVector
116  const int numVecs = 3;
117  Epetra_IntMultiVector myIMV(Map, numVecs);
118 
119  // Extract a view to populate the IntMultiVector
120  int myLDA;
121  int* myVals;
122  ierr = myIMV.ExtractView(&myVals, &myLDA);
123  Comm.MaxAll(&ierr, &global_ierr, 1);
124  EPETRA_TEST_ERR(global_ierr, numErr);
125  for(int i = 0; i < myIMV.MyLength(); ++i) {
126  for(int j = 0; j < numVecs; ++j) {
127  myVals[j*myLDA + i] = numVecs*i + j;
128  }
129  }
130 
131  // Test: replace local values
132  myIMV.ReplaceMyValue(3, 2, 42);
133  ierr = (myIMV[2][3] == 42 ? 0 : 1);
134  Comm.MaxAll(&ierr, &global_ierr, 1);
135  EPETRA_TEST_ERR(global_ierr, numErr);
136 
137  // Test: sum into local values
138  myIMV.SumIntoMyValue(3, 1, 42);
139  ierr = (myIMV[1][3] == 3*numVecs + 1 + 42 ? 0 : 1);
140  Comm.MaxAll(&ierr, &global_ierr, 1);
141  EPETRA_TEST_ERR(global_ierr, numErr);
142 
143  // Test: replace and sum global values on rank 2
144  if(MyPID == 2) {
145  myIMV.ReplaceGlobalValue(Map.GID(3), 2, 48);
146  ierr = (myIMV[2][3] == 48 ? 0 : 1);
147  EPETRA_TEST_ERR(ierr, numErr);
148  myIMV.SumIntoGlobalValue(Map.GID(3), 1, 48);
149  ierr = (myIMV[1][3] == 3*numVecs + 1 + 42 + 48 ? 0 : 1);
150  EPETRA_TEST_ERR(ierr, numErr);
151  }
152  Comm.MaxAll(&ierr, &global_ierr, 1);
153  EPETRA_TEST_ERR(global_ierr, numErr);
154 
155  // Construct a Map that puts all the elements on proc 0
156  Epetra_BlockMap Map0(NumGlobalEquations, (MyPID == 0 ? NumGlobalEquations : 0), 1, 0, Comm);
157  Epetra_IntMultiVector myIMV0(Map0, numVecs);
158 
159  Epetra_Import myImporter(Map0, Map);
160  ierr = myIMV0.Import(myIMV, myImporter, Epetra_CombineMode::Insert);
161  Comm.MaxAll(&ierr, &global_ierr, 1);
162  EPETRA_TEST_ERR(global_ierr, numErr);
163 
164  if(MyPID == 0) {
165  ierr = (myIMV0[1][ 3] == 3*numVecs + 1 + 42 ? 0 : 1);
166  EPETRA_TEST_ERR(ierr, numErr);
167  ierr = (myIMV0[1][10] == 3*numVecs + 1 + 42 ? 0 : 1);
168  EPETRA_TEST_ERR(ierr, numErr);
169  ierr = (myIMV0[1][17] == 3*numVecs + 1 + 42 + 48 ? 0 : 1);
170  EPETRA_TEST_ERR(ierr, numErr);
171  ierr = (myIMV0[1][24] == 3*numVecs + 1 + 42 ? 0 : 1);
172  EPETRA_TEST_ERR(ierr, numErr);
173  }
174 
175 
176 #ifdef EPETRA_MPI
177  MPI_Finalize() ;
178 #endif
179 /* end main */
180  return(ierr);
181 }
#define EPETRA_TEST_ERR(a, b)
int MyGlobalElements(int *MyGlobalElementList) const
Puts list of global elements on this processor into the user-provided array.
int MyLength() const
Returns the local vector length on the calling processor of vectors in the multi-vector.
static void SetTracebackMode(int TracebackModeValue)
Set the value of the Epetra_Object error traceback report mode.
int ExtractView(int **A, int *MyLDA) const
Set user-provided addresses of A and MyLDA.
#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()
Epetra_Import: This class builds an import object for efficient importing of off-processor elements...
Definition: Epetra_Import.h:71
int ReplaceGlobalValue(int GlobalRow, int VectorIndex, int OrdinalValue)
Replace current value at the specified (GlobalRow, VectorIndex) location with OrdinalValue.
Epetra_Combine Mode enumerable type.
int NumMyElements() const
Number of elements on the calling processor.
int GID(int LID) const
Returns global ID of local ID, return IndexBase-1 if not found on this processor. ...
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.
int MaxAll(double *PartialMaxs, double *GlobalMaxs, int Count) const
Epetra_SerialComm Global Max function.
int SumIntoGlobalValue(int GlobalRow, int VectorIndex, int OrdinalValue)
Adds OrdinalValue to existing value at the specified (GlobalRow, VectorIndex) location.
int main(int argc, char *argv[])
int SumIntoMyValue(int MyRow, int VectorIndex, int OrdinalValue)
Adds OrdinalValue to existing value at the specified (MyRow, VectorIndex) location.
int ReplaceMyValue(int MyRow, int VectorIndex, int OrdinalValue)
Replace current value at the specified (MyRow, VectorIndex) location with OrdinalValue.
int Import(const Epetra_SrcDistObject &A, const Epetra_Import &Importer, Epetra_CombineMode CombineMode, const Epetra_OffsetIndex *Indexor=0)
Imports an Epetra_DistObject using the Epetra_Import object.