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