Epetra Package Browser (Single Doxygen Collection)  Development
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
example/ImportExport/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 #include <stdio.h>
43 #include <stdlib.h>
44 #include <ctype.h>
45 #include <assert.h>
46 #include <string.h>
47 #include <math.h>
48 #include <iostream>
49 #include "mpi.h"
50 #include "Epetra_MpiComm.h"
51 #include "Epetra_Map.h"
52 #include "Epetra_LocalMap.h"
53 #include "Epetra_Vector.h"
54 #include "Epetra_Import.h"
55 #include "Epetra_Export.h"
56 int main(int argc, char *argv[])
57 {
58  using namespace std;
59  MPI_Init(&argc,&argv);
60  Epetra_MpiComm Comm( MPI_COMM_WORLD );
61 
62  cout << Comm <<endl;
63 
64  if (Comm.NumProc()!=3) cout << "Must be run on 3 MPI processes." << endl;
65 
66  char tmp;
67  if (Comm.MyPID()==0) cout << "Press any key to continue..."<< endl;
68  if (Comm.MyPID()==0) cin >> tmp;
69  Comm.Barrier();
70  int NumMyElements = 2;
71  Epetra_Map SourceMap(-1, NumMyElements, 0, Comm);
72  int NumGlobalElements = SourceMap.NumGlobalElements();
73 
74  Epetra_Vector SourceX(SourceMap);
75 
76  // Define SourceX = [0, 1, 2, 3, 4, 5]' with 2 elements per process
77 
78  SourceX[0] = (double) SourceMap.GID(0);
79  SourceX[1] = (double) SourceMap.GID(1);
80 
81  cout << SourceX; // Print it to stdout
82 
83  // Construct first target map: PE0 gets all elements of SourceX
84 
85  int TargetOneNumMyElements;
86  if (Comm.MyPID()==0) TargetOneNumMyElements = NumGlobalElements;
87  else TargetOneNumMyElements = 0;
88 
89  Epetra_Map TargetOneMap(-1, TargetOneNumMyElements, 0, Comm);
90 
91  Epetra_Import ImporterOne(TargetOneMap, SourceMap);
92 
93  Epetra_Vector TargetOneX(TargetOneMap);
94 
95  TargetOneX.Import(SourceX, ImporterOne, Insert);
96 
97  cout << TargetOneX << endl;
98 
99  int * GlobalElementList = new int[2];
100  GlobalElementList[0] = 5 - SourceMap.GID(0);
101  GlobalElementList[1] = 5 - SourceMap.GID(1);
102 
103  Epetra_Map TargetTwoMap(-1, 2, GlobalElementList, 0, Comm);
104 
105  Epetra_Import ImporterTwo(TargetTwoMap, TargetOneMap);
106 
107  Epetra_Vector TargetTwoX(TargetTwoMap);
108 
109  TargetTwoX.Import(TargetOneX, ImporterTwo, Insert);
110 
111  cout << TargetTwoX << endl;
112 
113  Epetra_LocalMap TargetThreeMap(6, 0, Comm);
114 
115  Epetra_Import ImporterThree(TargetThreeMap, TargetOneMap);
116 
117  Epetra_Vector TargetThreeX(TargetThreeMap);
118 
119  TargetThreeX.Import(TargetOneX, ImporterThree, Insert);
120 
121  cout << TargetThreeX << endl;
122 
123  Epetra_Export ExporterOne(TargetThreeMap, SourceMap);
124 
125  Epetra_Vector TargetFourX(SourceMap);
126 
127  TargetFourX.Import(TargetThreeX, ExporterOne, Add);
128 
129  cout << TargetFourX << endl;
130 
131 
132 
133  MPI_Finalize();
134  return 0;
135 }
int NumGlobalElements() const
Number of elements across all processors.
Epetra_Map: A class for partitioning vectors and matrices.
Definition: Epetra_Map.h:127
int NumProc() const
Returns total number of processes.
void Barrier() const
Epetra_MpiComm Barrier function.
Epetra_Export: This class builds an export object for efficient exporting of off-processor elements...
Definition: Epetra_Export.h:70
Epetra_Vector: A class for constructing and using dense vectors on a parallel computer.
Epetra_MpiComm: The Epetra MPI Communication Class.
Epetra_Import: This class builds an import object for efficient importing of off-processor elements...
Definition: Epetra_Import.h:71
int GID(int LID) const
Returns global ID of local ID, return IndexBase-1 if not found on this processor. ...
int main(int argc, char *argv[])
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.
Epetra_LocalMap: A class for replicating vectors and matrices across multiple processors.
int MyPID() const
Return my process ID.