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