xref: /petsc/src/sys/tests/ex45.cxx (revision 732aec7a18f2199fb53bb9a2f3aef439a834ce31)
1 static char help[] = "Demonstrates call PETSc first and then Trilinos in the same program.\n\n";
2 
3 /*
4    Example obtained from: http://trilinos.org/docs/dev/packages/tpetra/doc/html/Tpetra_Lesson01.html
5 */
6 
7 #include <petscsys.h>
8 #include <Teuchos_DefaultMpiComm.hpp> // wrapper for MPI_Comm
9 #include <Tpetra_Version.hpp>         // Tpetra version string
10 
11 // Do something with the given communicator.  In this case, we just
12 // print Tpetra's version to stdout on Process 0 in the given
13 // communicator.
exampleRoutine(const Teuchos::RCP<const Teuchos::Comm<int>> & comm)14 void exampleRoutine(const Teuchos::RCP<const Teuchos::Comm<int>> &comm)
15 {
16   if (comm->getRank() == 0) {
17     // On (MPI) Process 0, print out the Tpetra software version.
18     std::cout << Tpetra::version() << std::endl << std::endl;
19   }
20 }
21 
main(int argc,char ** argv)22 int main(int argc, char **argv)
23 {
24   // These "using" declarations make the code more concise, in that
25   // you don't have to write the namespace along with the class or
26   // object name.  This is especially helpful with commonly used
27   // things like std::endl or Teuchos::RCP.
28   using std::cout;
29   using std::endl;
30   using Teuchos::Comm;
31   using Teuchos::MpiComm;
32   using Teuchos::RCP;
33   using Teuchos::rcp;
34 
35   /*
36     Every PETSc routine should begin with the PetscInitialize() routine.
37     argc, argv - These command line arguments are taken to extract the options
38                  supplied to PETSc and options supplied to MPI.
39     help       - When PETSc executable is invoked with the option -help,
40                  it prints the various options that can be applied at
41                  runtime.  The user can use the "help" variable place
42                  additional help messages in this printout.
43   */
44   PetscFunctionBeginUser;
45   PetscCall(PetscInitialize(&argc, &argv, nullptr, help));
46   RCP<const Comm<int>> comm(new MpiComm<int>(PETSC_COMM_WORLD));
47   // Get my process' rank, and the total number of processes.
48   // Equivalent to MPI_Comm_rank resp. MPI_Comm_size.
49   const int myRank = comm->getRank();
50   const int size   = comm->getSize();
51   if (myRank == 0) cout << "Total number of processes: " << size << endl;
52   // Do something with the new communicator.
53   exampleRoutine(comm);
54   // This tells the Trilinos test framework that the test passed.
55   if (myRank == 0) cout << "End Result: TEST PASSED" << endl;
56 
57   PetscCall(PetscFinalize());
58   return 0;
59 }
60 
61 /*TEST
62 
63    build:
64      requires: trilinos
65 
66    test:
67       nsize: 3
68       filter: grep -v "Tpetra in Trilinos"
69 
70 TEST*/
71