1c4762a1bSJed Brown 2c4762a1bSJed Brown static char help[] = "Demonstrates calling Trilinos and then PETSc in the same program.\n\n"; 3c4762a1bSJed Brown 41bb3edfdSPatrick Sanan /* 5c4762a1bSJed Brown Example obtained from: http://trilinos.org/docs/dev/packages/tpetra/doc/html/Tpetra_Lesson01.html 61bb3edfdSPatrick Sanan */ 7c4762a1bSJed Brown 8c4762a1bSJed Brown #include <petscsys.h> 9c4762a1bSJed Brown #include <Tpetra_DefaultPlatform.hpp> 10c4762a1bSJed Brown #include <Tpetra_Version.hpp> 11c4762a1bSJed Brown #include <Teuchos_GlobalMPISession.hpp> // used if Trilinos is the one that starts up MPI 12c4762a1bSJed Brown 13c4762a1bSJed Brown // Do something with the given communicator. In this case, we just 14c4762a1bSJed Brown // print Tpetra's version to stdout on Process 0 in the given 15c4762a1bSJed Brown // communicator. 169371c9d4SSatish Balay void exampleRoutine(const Teuchos::RCP<const Teuchos::Comm<int>> &comm) { 17c4762a1bSJed Brown if (comm->getRank() == 0) { 18c4762a1bSJed Brown // On (MPI) Process 0, print out the Tpetra software version. 19c4762a1bSJed Brown std::cout << Tpetra::version() << std::endl << std::endl; 20c4762a1bSJed Brown } 21c4762a1bSJed Brown } 22c4762a1bSJed Brown 239371c9d4SSatish Balay int main(int argc, char **argv) { 24c4762a1bSJed Brown // These "using" declarations make the code more concise, in that 25c4762a1bSJed Brown // you don't have to write the namespace along with the class or 26c4762a1bSJed Brown // object name. This is especially helpful with commonly used 27c4762a1bSJed Brown // things like std::endl. 28c4762a1bSJed Brown using std::cout; 29c4762a1bSJed Brown using std::endl; 30c4762a1bSJed Brown // Start up MPI, if using MPI. Trilinos doesn't have to be built 31c4762a1bSJed Brown // with MPI; it's called a "serial" build if you build without MPI. 32c4762a1bSJed Brown // GlobalMPISession hides this implementation detail. 33c4762a1bSJed Brown // 34c4762a1bSJed Brown // Note the third argument. If you pass GlobalMPISession the 35c4762a1bSJed Brown // address of an std::ostream, it will print a one-line status 36c4762a1bSJed Brown // message with the rank on each MPI process. This may be 37*811af0c4SBarry Smith // undesirable if running with a large number of MPI ranks. 38c4762a1bSJed Brown // You can avoid printing anything here by passing in either 39c4762a1bSJed Brown // NULL or the address of a Teuchos::oblackholestream. 40c4762a1bSJed Brown Teuchos::GlobalMPISession mpiSession(&argc, &argv, NULL); 41c4762a1bSJed Brown // Get a pointer to the communicator object representing 42c4762a1bSJed Brown // MPI_COMM_WORLD. getDefaultPlatform.getComm() doesn't create a 43c4762a1bSJed Brown // new object every time you call it; it just returns the same 44c4762a1bSJed Brown // communicator each time. Thus, you can call it anywhere and get 45c4762a1bSJed Brown // the same communicator. (This is handy if you don't want to pass 46c4762a1bSJed Brown // a communicator around everywhere, though it's always better to 47c4762a1bSJed Brown // parameterize your algorithms on the communicator.) 48c4762a1bSJed Brown // 49c4762a1bSJed Brown // "Tpetra::DefaultPlatform" knows whether or not we built with MPI 50c4762a1bSJed Brown // support. If we didn't build with MPI, we'll get a "communicator" 51c4762a1bSJed Brown // with size 1, whose only process has rank 0. 52c4762a1bSJed Brown Teuchos::RCP<const Teuchos::Comm<int>> comm = Tpetra::DefaultPlatform::getDefaultPlatform().getComm(); 53c4762a1bSJed Brown 54327415f7SBarry Smith PetscFunctionBeginUser; 55b8abcfdeSJacob Faibussowitsch PetscCall(PetscInitialize(&argc, &argv, (char *)0, help)); 56c4762a1bSJed Brown 57c4762a1bSJed Brown // Get my process' rank, and the total number of processes. 58c4762a1bSJed Brown // Equivalent to MPI_Comm_rank resp. MPI_Comm_size. 59c4762a1bSJed Brown const int myRank = comm->getRank(); 60c4762a1bSJed Brown const int size = comm->getSize(); 61ad540459SPierre Jolivet if (myRank == 0) cout << "Total number of processes: " << size << endl; 62c4762a1bSJed Brown // Do something with the new communicator. 63c4762a1bSJed Brown exampleRoutine(comm); 64c4762a1bSJed Brown // This tells the Trilinos test framework that the test passed. 65ad540459SPierre Jolivet if (myRank == 0) cout << "End Result: TEST PASSED" << endl; 66c4762a1bSJed Brown // GlobalMPISession calls MPI_Finalize() in its destructor, if 67c4762a1bSJed Brown // appropriate. You don't have to do anything here! Just return 68c4762a1bSJed Brown // from main(). Isn't that helpful? 69b8abcfdeSJacob Faibussowitsch PetscCall(PetscFinalize()); 70b8abcfdeSJacob Faibussowitsch return 0; 71c4762a1bSJed Brown } 72c4762a1bSJed Brown 73c4762a1bSJed Brown /*TEST 74c4762a1bSJed Brown 75c4762a1bSJed Brown build: 76c4762a1bSJed Brown requires: trilinos 77c4762a1bSJed Brown 78c4762a1bSJed Brown test: 79c4762a1bSJed Brown nsize: 3 80c4762a1bSJed Brown filter: grep -v "Tpetra in Trilinos" 81c4762a1bSJed Brown 82c4762a1bSJed Brown TEST*/ 83