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