Epetra Package Browser (Single Doxygen Collection)  Development
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
lesson01_no_mpi.cpp
Go to the documentation of this file.
1 
8 #include <Epetra_config.h>
9 // Wrapper for a "communicator" containing only one process. This
10 // header file always exists, whether or not Epetra was built with MPI
11 // enabled.
12 #include <Epetra_SerialComm.h>
13 #include <Epetra_Version.h>
14 
15 #include <cstdlib>
16 #include <sstream>
17 #include <stdexcept>
18 
19 //
20 // ... Your other include files go here ...
21 //
22 
23 
24 // Do something with the given communicator. In this case, we just
25 // print Epetra's version to the given output stream, on Process 0.
26 void
28  std::ostream& out)
29 {
30  if (comm.MyPID () == 0) {
31  // On (MPI) Process 0, print out the Epetra software version.
32  out << Epetra_Version () << std::endl << std::endl;
33  }
34 }
35 
36 int
37 main (int /* argc */, char * /* argv */[])
38 {
39  // These "using" declarations make the code more concise, in that
40  // you don't have to write the namespace along with the class or
41  // object name. This is especially helpful with commonly used
42  // things like std::endl.
43  using std::cout;
44  using std::endl;
45 
46  // Make a "serial" (non-MPI) communicator. It doesn't actually
47  // "communicate," because it only has one process, whose rank is
48  // always 0. Epetra_SerialComm is a subclass of Epetra_Comm, so you
49  // may use it wherever an Epetra_Comm is required.
50  Epetra_SerialComm comm;
51 
52  // Epetra_Comm has methods that wrap basic MPI functionality.
53  // MyPID() is equivalent to MPI_Comm_rank, and NumProc() to
54  // MPI_Comm_size.
55  //
56  // With a "serial" communicator, the rank is always 0, and the
57  // number of processes is always 1.
58  const int myRank = comm.MyPID ();
59  const int numProcs = comm.NumProc ();
60 
61  // Test the two assertions in the previous comment.
62  if (numProcs != 1) {
63  std::ostringstream err;
64  err << "This is a serial (non-MPI) example, but the number of processes "
65  << "in the Epetra_Comm is " << numProcs << " != 1. Please report "
66  << "this bug.";
67  throw std::logic_error (err.str ());
68  }
69  if (myRank != 0) {
70  std::ostringstream err;
71  err << "This is a serial (non-MPI) example, but the rank of the calling "
72  "process in the Epetra_Comm is " << myRank << " != 0. Please report "
73  "this bug.";
74  throw std::logic_error (err.str ());
75  }
76 
77  // Do something with the new communicator.
78  exampleRoutine (comm, cout);
79 
80  // This tells the Trilinos test framework that the test passed.
81  if (myRank == 0) {
82  cout << "End Result: TEST PASSED" << endl;
83  }
84 
85  return 0;
86 }
int MyPID() const
Return my process ID.
std::string Epetra_Version()
virtual int MyPID() const =0
Return my process ID.
Epetra_Comm: The Epetra Communication Abstract Base Class.
Definition: Epetra_Comm.h:73
int NumProc() const
Returns total number of processes (always returns 1 for SerialComm).
Epetra_SerialComm: The Epetra Serial Communication Class.
int main(int argc, char *argv[])
void exampleRoutine(const Epetra_Comm &comm, std::ostream &out)