xref: /petsc/src/sys/tests/ex46.cxx (revision 732aec7a18f2199fb53bb9a2f3aef439a834ce31)
1c4762a1bSJed Brown static char help[] = "Demonstrates calling Trilinos and then PETSc in the same program.\n\n";
2c4762a1bSJed Brown 
31bb3edfdSPatrick Sanan /*
4c4762a1bSJed Brown    Example obtained from: http://trilinos.org/docs/dev/packages/tpetra/doc/html/Tpetra_Lesson01.html
51bb3edfdSPatrick Sanan */
6c4762a1bSJed Brown 
7c4762a1bSJed Brown #include <petscsys.h>
8c4762a1bSJed Brown #include <Tpetra_DefaultPlatform.hpp>
9c4762a1bSJed Brown #include <Tpetra_Version.hpp>
10c4762a1bSJed Brown #include <Teuchos_GlobalMPISession.hpp> // used if Trilinos is the one that starts up MPI
11c4762a1bSJed Brown 
12c4762a1bSJed Brown // Do something with the given communicator.  In this case, we just
13c4762a1bSJed Brown // print Tpetra's version to stdout on Process 0 in the given
14c4762a1bSJed Brown // communicator.
exampleRoutine(const Teuchos::RCP<const Teuchos::Comm<int>> & comm)15d71ae5a4SJacob Faibussowitsch void exampleRoutine(const Teuchos::RCP<const Teuchos::Comm<int>> &comm)
16d71ae5a4SJacob Faibussowitsch {
17c4762a1bSJed Brown   if (comm->getRank() == 0) {
18c4762a1bSJed Brown     // On (MPI) Process 0, print out the Tpetra software version.
19c4762a1bSJed Brown     std::cout << Tpetra::version() << std::endl << std::endl;
20c4762a1bSJed Brown   }
21c4762a1bSJed Brown }
22c4762a1bSJed Brown 
main(int argc,char ** argv)23d71ae5a4SJacob Faibussowitsch int main(int argc, char **argv)
24d71ae5a4SJacob Faibussowitsch {
25c4762a1bSJed Brown   // These "using" declarations make the code more concise, in that
26c4762a1bSJed Brown   // you don't have to write the namespace along with the class or
27c4762a1bSJed Brown   // object name.  This is especially helpful with commonly used
28c4762a1bSJed Brown   // things like std::endl.
29c4762a1bSJed Brown   using std::cout;
30c4762a1bSJed Brown   using std::endl;
31c4762a1bSJed Brown   // Start up MPI, if using MPI.  Trilinos doesn't have to be built
32c4762a1bSJed Brown   // with MPI; it's called a "serial" build if you build without MPI.
33c4762a1bSJed Brown   // GlobalMPISession hides this implementation detail.
34c4762a1bSJed Brown   //
35c4762a1bSJed Brown   // Note the third argument.  If you pass GlobalMPISession the
36c4762a1bSJed Brown   // address of an std::ostream, it will print a one-line status
37c4762a1bSJed Brown   // message with the rank on each MPI process.  This may be
38811af0c4SBarry Smith   // undesirable if running with a large number of MPI ranks.
39c4762a1bSJed Brown   // You can avoid printing anything here by passing in either
40c4762a1bSJed Brown   // NULL or the address of a Teuchos::oblackholestream.
41c4762a1bSJed Brown   Teuchos::GlobalMPISession mpiSession(&argc, &argv, NULL);
42c4762a1bSJed Brown   // Get a pointer to the communicator object representing
43c4762a1bSJed Brown   // MPI_COMM_WORLD.  getDefaultPlatform.getComm() doesn't create a
44c4762a1bSJed Brown   // new object every time you call it; it just returns the same
45c4762a1bSJed Brown   // communicator each time.  Thus, you can call it anywhere and get
46c4762a1bSJed Brown   // the same communicator.  (This is handy if you don't want to pass
47c4762a1bSJed Brown   // a communicator around everywhere, though it's always better to
48c4762a1bSJed Brown   // parameterize your algorithms on the communicator.)
49c4762a1bSJed Brown   //
50c4762a1bSJed Brown   // "Tpetra::DefaultPlatform" knows whether or not we built with MPI
51c4762a1bSJed Brown   // support.  If we didn't build with MPI, we'll get a "communicator"
52c4762a1bSJed Brown   // with size 1, whose only process has rank 0.
53c4762a1bSJed Brown   Teuchos::RCP<const Teuchos::Comm<int>> comm = Tpetra::DefaultPlatform::getDefaultPlatform().getComm();
54c4762a1bSJed Brown 
55327415f7SBarry Smith   PetscFunctionBeginUser;
56*c8025a54SPierre Jolivet   PetscCall(PetscInitialize(&argc, &argv, nullptr, help));
57c4762a1bSJed Brown 
58c4762a1bSJed Brown   // Get my process' rank, and the total number of processes.
59c4762a1bSJed Brown   // Equivalent to MPI_Comm_rank resp. MPI_Comm_size.
60c4762a1bSJed Brown   const int myRank = comm->getRank();
61c4762a1bSJed Brown   const int size   = comm->getSize();
62ad540459SPierre Jolivet   if (myRank == 0) cout << "Total number of processes: " << size << endl;
63c4762a1bSJed Brown   // Do something with the new communicator.
64c4762a1bSJed Brown   exampleRoutine(comm);
65c4762a1bSJed Brown   // This tells the Trilinos test framework that the test passed.
66ad540459SPierre Jolivet   if (myRank == 0) cout << "End Result: TEST PASSED" << endl;
67c4762a1bSJed Brown   // GlobalMPISession calls MPI_Finalize() in its destructor, if
68c4762a1bSJed Brown   // appropriate.  You don't have to do anything here!  Just return
69c4762a1bSJed Brown   // from main().  Isn't that helpful?
70b8abcfdeSJacob Faibussowitsch   PetscCall(PetscFinalize());
71b8abcfdeSJacob Faibussowitsch   return 0;
72c4762a1bSJed Brown }
73c4762a1bSJed Brown 
74c4762a1bSJed Brown /*TEST
75c4762a1bSJed Brown 
76c4762a1bSJed Brown    build:
77c4762a1bSJed Brown      requires: trilinos
78c4762a1bSJed Brown 
79c4762a1bSJed Brown    test:
80c4762a1bSJed Brown       nsize: 3
81c4762a1bSJed Brown       filter: grep -v "Tpetra in Trilinos"
82c4762a1bSJed Brown 
83c4762a1bSJed Brown TEST*/
84