Epetra Package Browser (Single Doxygen Collection) Development
Loading...
Searching...
No Matches
lesson01_mpi_only_through_Epetra.cpp
Go to the documentation of this file.
1
8//
9// This example includes conditional MPI initialization, getting an
10// Epetra communicator wrapper, and printing out Epetra version
11// information.
12//
13
14// This defines useful macros like HAVE_MPI, which is defined if and
15// only if Epetra was built with MPI enabled.
16#include <Epetra_config.h>
17
18#ifdef HAVE_MPI
19// Your code is an existing MPI code, so it presumably includes mpi.h directly.
20# include <mpi.h>
21// Epetra's wrapper for MPI_Comm. This header file only exists if
22// Epetra was built with MPI enabled.
23# include <Epetra_MpiComm.h>
24#else
25# include <Epetra_SerialComm.h>
26#endif // HAVE_MPI
27
28#include <Epetra_Version.h>
29
30
31//
32// ... Your other include files go here ...
33//
34
35// Do something with the given communicator. In this case, we just
36// print Epetra's version to the given output stream, on Process 0.
37void
39 std::ostream& out)
40{
41 if (comm.MyPID () == 0) {
42 // On (MPI) Process 0, print out the Epetra software version.
43 out << Epetra_Version () << std::endl << std::endl;
44 }
45}
46
47int
48main (int argc, char *argv[])
49{
50 // These "using" declarations make the code more concise, in that
51 // you don't have to write the namespace along with the class or
52 // object name. This is especially helpful with commonly used
53 // things like std::endl.
54 using std::cout;
55 using std::endl;
56
57#ifdef HAVE_MPI
58 // Start up MPI, if using MPI. Trilinos doesn't have to be built
59 // with MPI; it's called a "serial" build if you build without MPI.
60 //
61 // It's bad form to ignore the error codes returned by MPI
62 // functions, but we do so here for brevity.
63 (void) MPI_Init (&argc, &argv);
64
65 // Wrap MPI_COMM_WORLD in an Epetra communicator wrapper.
66 // Epetra_MpiComm is a subclass of Epetra_Comm, so you may use it
67 // wherever an Epetra_Comm is required.
68 Epetra_MpiComm comm (MPI_COMM_WORLD);
69#else
70 // Make a "serial" (non-MPI) communicator. It doesn't actually
71 // "communicate," because it only has one process, whose rank is
72 // always 0. Epetra_SerialComm is a subclass of Epetra_Comm, so you
73 // may use it wherever an Epetra_Comm is required.
75#endif
76
77 // Epetra_Comm has methods that wrap basic MPI functionality.
78 // MyPID() is equivalent to MPI_Comm_rank, and NumProc() to
79 // MPI_Comm_size.
80 //
81 // With a "serial" communicator, the rank is always 0, and the
82 // number of processes is always 1.
83 const int myRank = comm.MyPID ();
84 const int numProcs = comm.NumProc ();
85
86 if (myRank == 0) {
87 cout << "Total number of processes: " << numProcs << endl;
88 }
89
90 // Do something with the new Epetra communicator.
91 exampleRoutine (comm, cout);
92
93 // This tells the Trilinos test framework that the test passed.
94 if (comm.MyPID () == 0) {
95 cout << "End Result: TEST PASSED" << endl;
96 }
97
98#ifdef HAVE_MPI
99 // Since you called MPI_Init, you are responsible for calling
100 // MPI_Finalize after you are done using MPI.
101 (void) MPI_Finalize ();
102#endif // HAVE_MPI
103
104 return 0;
105}
std::string Epetra_Version()
Epetra_Comm: The Epetra Communication Abstract Base Class.
Definition: Epetra_Comm.h:73
virtual int MyPID() const =0
Return my process ID.
Epetra_MpiComm: The Epetra MPI Communication Class.
int NumProc() const
Returns total number of processes.
int MyPID() const
Return my process ID.
Epetra_SerialComm: The Epetra Serial Communication Class.
int main(int argc, char *argv[])
void exampleRoutine(const Epetra_Comm &comm, std::ostream &out)