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