Epetra Package Browser (Single Doxygen Collection) Development
Loading...
Searching...
No Matches
lesson01_mpi_on_its_own.cpp
Go to the documentation of this file.
1
8//
9// This example shows how to wrap the MPI_Comm (MPI communicator) that
10// you are using, so that Epetra can use it as well. it includes MPI
11// initialization, wrapping your MPI_Comm in an Epetra communicator
12// wrapper, and printing out Epetra version information.
13//
14
15// This defines useful macros like HAVE_MPI, which is defined if and
16// only if Epetra was built with MPI enabled.
17#include <Epetra_config.h>
18
19#ifdef HAVE_MPI
20// Your code is an existing MPI code, so it presumably includes mpi.h directly.
21# include <mpi.h>
22// Epetra's wrapper for MPI_Comm. This header file only exists if
23// Epetra was built with MPI enabled.
24# include <Epetra_MpiComm.h>
25#else
26# error "This example requires MPI in order to build."
27#endif // HAVE_MPI
28
29#include <Epetra_Version.h>
30
31//
32// ... Your other include files go here ...
33//
34
35
36// Do something with the given communicator. In this case, we just
37// print Epetra's version to the given output stream, on Process 0.
38void
40 std::ostream& out)
41{
42 if (comm.MyPID () == 0) {
43 // On (MPI) Process 0, print out the Epetra software version.
44 out << Epetra_Version () << std::endl << std::endl;
45 }
46}
47
48int
49main (int argc, char *argv[])
50{
51 // These "using" declarations make the code more concise, in that
52 // you don't have to write the namespace along with the class or
53 // object name. This is especially helpful with commonly used
54 // things like std::endl.
55 using std::cout;
56 using std::endl;
57
58 // We assume that your code calls MPI_Init. It's bad form
59 // to ignore the error codes returned by MPI functions, but
60 // we do so here for brevity.
61 (void) MPI_Init (&argc, &argv);
62
63 // This code takes the place of whatever you do to get an MPI_Comm.
64 MPI_Comm yourComm = MPI_COMM_WORLD;
65
66 // If your code plans to use MPI on its own, as well as through
67 // Trilinos, you should strongly consider giving Trilinos a copy
68 // of your MPI_Comm (created via MPI_Comm_dup). Trilinos may in
69 // the future duplicate the MPI_Comm automatically, but it does
70 // not currently do this.
71
72 // Wrap the MPI_Comm. You are responsible for calling MPI_Comm_free
73 // on your MPI_Comm after use, if necessary. (It's not necessary or
74 // legal to do this for built-in communicators like MPI_COMM_WORLD
75 // or MPI_COMM_SELF.)
76 Epetra_MpiComm comm (yourComm);
77
78 // Epetra_Comm has methods that wrap basic MPI functionality.
79 // MyPID() is equivalent to MPI_Comm_rank; it returns my process'
80 // rank. NumProc() is equivalent to MPI_Comm_size; it returns the
81 // total number of processes in the communicator.
82 const int myRank = comm.MyPID ();
83 const int numProcs = comm.NumProc ();
84
85 if (myRank == 0) {
86 cout << "Total number of processes: " << numProcs << endl;
87 }
88
89 // Do something with the new Epetra communicator.
90 exampleRoutine (comm, cout);
91
92 // This tells the Trilinos test framework that the test passed.
93 if (myRank == 0) {
94 cout << "End Result: TEST PASSED" << endl;
95 }
96
97 // If you need to call MPI_Comm_free on your MPI_Comm, now would be
98 // the time to do so, before calling MPI_Finalize.
99
100 // Since you called MPI_Init, you are responsible for calling
101 // MPI_Finalize after you are done using MPI.
102 (void) MPI_Finalize ();
103 return 0;
104}
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.
int main(int argc, char *argv[])
void exampleRoutine(const Epetra_Comm &comm, std::ostream &out)