Actual source code: ex14.cxx

petsc-3.7.3 2016-08-01
Report Typos and Errors
  2: static char help[] = "Demonstrates calling Trilinos and then PETSc in the same program.\n\n";

  4: /*T
  5:    Concepts: introduction to PETSc^Trilinos
  6:    Processors: n

  8:    Example obtained from: http://trilinos.org/docs/dev/packages/tpetra/doc/html/Tpetra_Lesson01.html
  9: T*/

 11: #include <petscsys.h>
 12: #include <Tpetra_DefaultPlatform.hpp>
 13: #include <Tpetra_Version.hpp>
 14: #include <Teuchos_GlobalMPISession.hpp>    // used if Trilinos is the one that starts up MPI

 16: // Do something with the given communicator.  In this case, we just
 17: // print Tpetra's version to stdout on Process 0 in the given
 18: // communicator.
 19: void
 20: exampleRoutine (const Teuchos::RCP<const Teuchos::Comm<int> >& comm)
 21: {
 22:   if (comm->getRank () == 0) {
 23:     // On (MPI) Process 0, print out the Tpetra software version.
 24:     std::cout << Tpetra::version () << std::endl << std::endl;
 25:   }
 26: }

 28: int main(int argc,char **argv)
 29: {
 31:   // These "using" declarations make the code more concise, in that
 32:   // you don't have to write the namespace along with the class or
 33:   // object name.  This is especially helpful with commonly used
 34:   // things like std::endl.
 35:   using std::cout;
 36:   using std::endl;
 37:   // Start up MPI, if using MPI.  Trilinos doesn't have to be built
 38:   // with MPI; it's called a "serial" build if you build without MPI.
 39:   // GlobalMPISession hides this implementation detail.
 40:   //
 41:   // Note the third argument.  If you pass GlobalMPISession the
 42:   // address of an std::ostream, it will print a one-line status
 43:   // message with the rank on each MPI process.  This may be
 44:   // undesirable if running with a large number of MPI processes.
 45:   // You can avoid printing anything here by passing in either
 46:   // NULL or the address of a Teuchos::oblackholestream.
 47:   Teuchos::GlobalMPISession mpiSession (&argc, &argv, NULL);
 48:   // Get a pointer to the communicator object representing
 49:   // MPI_COMM_WORLD.  getDefaultPlatform.getComm() doesn't create a
 50:   // new object every time you call it; it just returns the same
 51:   // communicator each time.  Thus, you can call it anywhere and get
 52:   // the same communicator.  (This is handy if you don't want to pass
 53:   // a communicator around everywhere, though it's always better to
 54:   // parameterize your algorithms on the communicator.)
 55:   //
 56:   // "Tpetra::DefaultPlatform" knows whether or not we built with MPI
 57:   // support.  If we didn't build with MPI, we'll get a "communicator"
 58:   // with size 1, whose only process has rank 0.
 59:   Teuchos::RCP<const Teuchos::Comm<int> > comm = Tpetra::DefaultPlatform::getDefaultPlatform ().getComm ();

 61:   PetscInitialize(&argc,&argv,(char*)0,help);

 63:   // Get my process' rank, and the total number of processes.
 64:   // Equivalent to MPI_Comm_rank resp. MPI_Comm_size.
 65:   const int myRank = comm->getRank ();
 66:   const int numProcs = comm->getSize ();
 67:   if (myRank == 0) {
 68:     cout << "Total number of processes: " << numProcs << endl;
 69:   }
 70:   // Do something with the new communicator.
 71:   exampleRoutine (comm);
 72:   // This tells the Trilinos test framework that the test passed.
 73:   if (myRank == 0) {
 74:     cout << "End Result: TEST PASSED" << endl;
 75:   }
 76:   // GlobalMPISession calls MPI_Finalize() in its destructor, if
 77:   // appropriate.  You don't have to do anything here!  Just return
 78:   // from main().  Isn't that helpful?
 79:   PetscFinalize();
 80:   return 0;
 81: }