Tpetra parallel linear algebra  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
Public Member Functions | List of all members
Tpetra::ScopeGuard Class Reference

Scope guard whose destructor automatically calls Tpetra::finalize for you. More...

#include <Tpetra_Core.hpp>

Public Member Functions

 ScopeGuard (int *argc, char ***argv)
 Initialize Tpetra. More...
 
 ScopeGuard ()=delete
 Default constructor (FORBIDDEN) More...
 
 ~ScopeGuard ()
 Finalize Tpetra. More...
 

Detailed Description

Scope guard whose destructor automatically calls Tpetra::finalize for you.

This class' constructor does the same thing as Tpetra::initialize (see above). Its destructor automatically calls Tpetra::finalize. This ensures correct Tpetra finalization even if intervening code throws an exception.

Compare to Kokkos::ScopeGuard and Teuchos::GlobalMPISession.

Always give the ScopeGuard instance a name. Otherwise, you'll create a temporary object whose destructor will be called right away. That's not what you want.

Here is an example of how to use this class:

#include "Tpetra_Core.hpp"
#include "Tpetra_Map.hpp"
int main (int argc, char* argv[]) {
Tpetra::ScopeGuard tpetraScope (&argc, &argv);
// Never create Tpetra or Kokkos objects (other than
// the ScopeGuard object itself) at main scope.
// Otherwise, their destructors will be called after
// MPI_Finalize and Kokkos::finalize are called, which
// is forbidden.
{
auto comm = Tpetra::getDefaultComm ();
const GO gblNumInds = 1000;
const GO indexBase = 0;
Tpetra::Map<> map (gblNumInds, indexBase, comm,
Tpetra::GloballyDistributed);
// ... code that uses map ...
}
return EXIT_SUCCESS;
}

Definition at line 221 of file Tpetra_Core.hpp.

Constructor & Destructor Documentation

Tpetra::ScopeGuard::ScopeGuard ( int *  argc,
char ***  argv 
)

Initialize Tpetra.

If MPI_Init has not yet been called, then call it. If Kokkos::initialize has not yet been called, then call it.

Parameters
argc[in/out] Address of the first argument to main().
argv[in/out] Address of the second argument to main().

Definition at line 347 of file Tpetra_Core.cpp.

Tpetra::ScopeGuard::ScopeGuard ( )
delete

Default constructor (FORBIDDEN)

You must give ScopeGuard's constructor argc and argv. Use the constructor above this one.

Tpetra::ScopeGuard::~ScopeGuard ( )

Finalize Tpetra.

If the constructor called Kokkos::initialize, then call Kokkos::finalize. If the constructor called MPI_Init, then call MPI_Finalize.

Definition at line 359 of file Tpetra_Core.cpp.


The documentation for this class was generated from the following files: