Loading...
Searching...
No Matches
init.hpp
Go to the documentation of this file.
1
2
3#pragma once
5#include "mpi/mpi.hpp"
6
7namespace terra::util {
8
9namespace detail {
10
12{
13 public:
14 KokkosContext( const KokkosContext& ) = delete;
18
19 static void initialize( int& argc, char** argv ) { instance( argc, argv ); }
20
21 private:
22 bool kokkos_initialized_ = false;
23
24 // private constructor
25 KokkosContext( int& argc, char** argv )
26 {
27 if ( Kokkos::is_initialized() )
28 {
29 throw std::runtime_error( "Kokkos already initialized!" );
30 }
31
32 Kokkos::initialize( argc, argv );
33
34 kokkos_initialized_ = true;
35 }
36
37 // private destructor
38 ~KokkosContext()
39 {
40 if ( kokkos_initialized_ && !Kokkos::is_finalized() )
41 {
42 Kokkos::finalize();
43 }
44 }
45
46 // singleton instance accessor
47 static KokkosContext& instance( int& argc, char** argv )
48 {
49 static KokkosContext guard( argc, argv );
50 return guard;
51 }
52};
53
54} // namespace detail
55
56/// @brief RAII approach to safely initialize MPI and Kokkos.
57///
58/// At the start of your main(), create this object to run MPI_Init/Finalize and to start the Kokkos scope.
59///
60/// Like this:
61///
62/// int main( int argc, char** argv)
63/// {
64/// // Make sure to not destroy it right away!
65/// // TerraScopeGuard( &argc, &argv );
66/// // will not work! Name the thing!
67///
68/// TerraScopeGuard terra_scope_guard( &argc, &argv );
69///
70/// // Here goes your cool app/test. MPI and Kokkos are finalized automatically (even if you throw an
71/// // exception).
72///
73/// // ...
74/// } // Destructor handles stuff here.
75inline void terra_initialize( int* argc, char*** argv )
76{
77 // The order is important!
80}
81
82} // namespace terra::util
static void initialize(int *argc, char ***argv)
Initialize MPI once. Safe to call only once.
Definition mpi.hpp:39
static void initialize(int &argc, char **argv)
Definition init.hpp:19
KokkosContext & operator=(KokkosContext &&)=delete
KokkosContext(KokkosContext &&)=delete
KokkosContext & operator=(const KokkosContext &)=delete
KokkosContext(const KokkosContext &)=delete
Definition solver.hpp:9
void terra_initialize(int *argc, char ***argv)
RAII approach to safely initialize MPI and Kokkos.
Definition init.hpp:75