1 2 static char help[] = "Demonstrates calling Trilinos and then PETSc 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 <Tpetra_DefaultPlatform.hpp> 10 #include <Tpetra_Version.hpp> 11 #include <Teuchos_GlobalMPISession.hpp> // used if Trilinos is the one that starts up MPI 12 13 // Do something with the given communicator. In this case, we just 14 // print Tpetra's version to stdout on Process 0 in the given 15 // communicator. 16 void exampleRoutine(const Teuchos::RCP<const Teuchos::Comm<int>> &comm) { 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 // 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. 28 using std::cout; 29 using std::endl; 30 // Start up MPI, if using MPI. Trilinos doesn't have to be built 31 // with MPI; it's called a "serial" build if you build without MPI. 32 // GlobalMPISession hides this implementation detail. 33 // 34 // Note the third argument. If you pass GlobalMPISession the 35 // address of an std::ostream, it will print a one-line status 36 // message with the rank on each MPI process. This may be 37 // undesirable if running with a large number of MPI processes. 38 // You can avoid printing anything here by passing in either 39 // NULL or the address of a Teuchos::oblackholestream. 40 Teuchos::GlobalMPISession mpiSession(&argc, &argv, NULL); 41 // Get a pointer to the communicator object representing 42 // MPI_COMM_WORLD. getDefaultPlatform.getComm() doesn't create a 43 // new object every time you call it; it just returns the same 44 // communicator each time. Thus, you can call it anywhere and get 45 // the same communicator. (This is handy if you don't want to pass 46 // a communicator around everywhere, though it's always better to 47 // parameterize your algorithms on the communicator.) 48 // 49 // "Tpetra::DefaultPlatform" knows whether or not we built with MPI 50 // support. If we didn't build with MPI, we'll get a "communicator" 51 // with size 1, whose only process has rank 0. 52 Teuchos::RCP<const Teuchos::Comm<int>> comm = Tpetra::DefaultPlatform::getDefaultPlatform().getComm(); 53 54 PetscFunctionBeginUser; 55 PetscCall(PetscInitialize(&argc, &argv, (char *)0, help)); 56 57 // Get my process' rank, and the total number of processes. 58 // Equivalent to MPI_Comm_rank resp. MPI_Comm_size. 59 const int myRank = comm->getRank(); 60 const int size = comm->getSize(); 61 if (myRank == 0) cout << "Total number of processes: " << size << endl; 62 // Do something with the new communicator. 63 exampleRoutine(comm); 64 // This tells the Trilinos test framework that the test passed. 65 if (myRank == 0) cout << "End Result: TEST PASSED" << endl; 66 // GlobalMPISession calls MPI_Finalize() in its destructor, if 67 // appropriate. You don't have to do anything here! Just return 68 // from main(). Isn't that helpful? 69 PetscCall(PetscFinalize()); 70 return 0; 71 } 72 73 /*TEST 74 75 build: 76 requires: trilinos 77 78 test: 79 nsize: 3 80 filter: grep -v "Tpetra in Trilinos" 81 82 TEST*/ 83