xref: /petsc/src/sys/tests/ex45.cxx (revision 58d68138c660dfb4e9f5b03334792cd4f2ffd7cc)
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   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 
22 int main(int argc, char **argv) {
23   // These "using" declarations make the code more concise, in that
24   // you don't have to write the namespace along with the class or
25   // object name.  This is especially helpful with commonly used
26   // things like std::endl or Teuchos::RCP.
27   using std::cout;
28   using std::endl;
29   using Teuchos::Comm;
30   using Teuchos::MpiComm;
31   using Teuchos::RCP;
32   using Teuchos::rcp;
33 
34   /*
35     Every PETSc routine should begin with the PetscInitialize() routine.
36     argc, argv - These command line arguments are taken to extract the options
37                  supplied to PETSc and options supplied to MPI.
38     help       - When PETSc executable is invoked with the option -help,
39                  it prints the various options that can be applied at
40                  runtime.  The user can use the "help" variable place
41                  additional help messages in this printout.
42   */
43   PetscFunctionBeginUser;
44   PetscCall(PetscInitialize(&argc, &argv, (char *)0, help));
45   RCP<const Comm<int>> comm(new MpiComm<int>(PETSC_COMM_WORLD));
46   // Get my process' rank, and the total number of processes.
47   // Equivalent to MPI_Comm_rank resp. MPI_Comm_size.
48   const int            myRank = comm->getRank();
49   const int            size   = comm->getSize();
50   if (myRank == 0) { cout << "Total number of processes: " << size << endl; }
51   // Do something with the new communicator.
52   exampleRoutine(comm);
53   // This tells the Trilinos test framework that the test passed.
54   if (myRank == 0) { cout << "End Result: TEST PASSED" << endl; }
55 
56   PetscCall(PetscFinalize());
57   return 0;
58 }
59 
60 /*TEST
61 
62    build:
63      requires: trilinos
64 
65    test:
66       nsize: 3
67       filter: grep -v "Tpetra in Trilinos"
68 
69 TEST*/
70