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