Tpetra Matrix/Vector Services  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines
lesson01_mpi_on_its_own.cpp
00001 
00008 //
00009 // This example shows how to wrap the MPI_Comm (MPI communicator) that
00010 // you are using, so that Tpetra can use it as well.  it includes MPI
00011 // initialization, getting a Teuchos::Comm communicator, and printing
00012 // out Tpetra version information.
00013 //
00014 
00015 // Your code is an existing MPI code, so it presumably includes mpi.h directly.
00016 #include <mpi.h>
00017 
00018 #include <Teuchos_DefaultMpiComm.hpp> // wrapper for MPI_Comm
00019 #include <Tpetra_Version.hpp> // Tpetra version string
00020 
00021 //
00022 // ... Your other include files go here ...
00023 //
00024 
00025 // Do something with the given communicator.  In this case, we just
00026 // print Tpetra's version to stdout on Process 0.
00027 void
00028 exampleRoutine (const Teuchos::RCP<const Teuchos::Comm<int> >& comm)
00029 {
00030   if (comm->getRank () == 0) {
00031     // On (MPI) Process 0, print out the Tpetra software version.
00032     std::cout << Tpetra::version () << std::endl << std::endl;
00033   }
00034 }
00035 
00036 int
00037 main (int argc, char *argv[])
00038 {
00039   // These "using" declarations make the code more concise, in that
00040   // you don't have to write the namespace along with the class or
00041   // object name.  This is especially helpful with commonly used
00042   // things like std::endl or Teuchos::RCP.
00043   using std::cout;
00044   using std::endl;
00045   using Teuchos::Comm;
00046   using Teuchos::MpiComm;
00047   using Teuchos::RCP;
00048   using Teuchos::rcp;
00049 
00050   // We assume that your code calls MPI_Init.  It's bad form
00051   // to ignore the error codes returned by MPI functions, but
00052   // we do so here for brevity.
00053   (void) MPI_Init (&argc, &argv);
00054 
00055   // This code takes the place of whatever you do to get an MPI_Comm.
00056   MPI_Comm yourComm = MPI_COMM_WORLD;
00057 
00058   // If your code plans to use MPI on its own, as well as through
00059   // Trilinos, you should strongly consider giving Trilinos a copy
00060   // of your MPI_Comm (created via MPI_Comm_dup).  Trilinos may in
00061   // the future duplicate the MPI_Comm automatically, but it does
00062   // not currently do this.
00063 
00064   // Wrap the MPI_Comm.  If you wrap it in this way, you are telling
00065   // Trilinos that you are responsible for calling MPI_Comm_free on
00066   // your MPI_Comm after use, if necessary.  (It's not necessary for
00067   // MPI_COMM_WORLD.)  There is a way to tell Trilinos to call
00068   // MPI_Comm_free itself; we don't show it here.  (It involves
00069   // passing the result of Teuchos::opaqueWrapper to MpiComm's
00070   // constructor.)
00071 
00072   RCP<const Comm<int> > comm = rcp (new MpiComm<int> (yourComm));
00073 
00074   // In old versions of Trilinos, the above line of code might not
00075   // compile.  You might have to do the following:
00076   //
00077   // using Teuchos::opaqueWrapper;
00078   // RCP<const Comm<int> > comm = rcp (new MpiComm<int> (opaqueWrapper (yourComm)));
00079 
00080   // Get my process' rank, and the total number of processes.
00081   // Equivalent to MPI_Comm_rank resp. MPI_Comm_size.
00082   const int myRank = comm->getRank ();
00083   const int numProcs = comm->getSize ();
00084 
00085   if (myRank == 0) {
00086     cout << "Total number of processes: " << numProcs << endl;
00087   }
00088 
00089   // Do something with the new communicator.
00090   exampleRoutine (comm);
00091 
00092   // This tells the Trilinos test framework that the test passed.
00093   if (myRank == 0) {
00094     cout << "End Result: TEST PASSED" << endl;
00095   }
00096 
00097   // If you need to call MPI_Comm_free on your MPI_Comm, now would
00098   // be the time to do so, before calling MPI_Finalize.  You may also
00099   // automate this process; ask the tutorial presenter for more information.
00100 
00101   // Since you called MPI_Init, you are responsible for calling MPI_Finalize.
00102   (void) MPI_Finalize ();
00103   return 0;
00104 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines