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