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