ScopeGuard#

Defined in header <Kokkos_Core.hpp>

Usage#

Kokkos::ScopeGuard guard(argc, argv);
Kokkos::ScopeGuard guard(Kokkos::InitializationSettings()  // (since 3.7)
                            .set_map_device_id_by("random")
                            .set_num_threads(1));

ScopeGuard is a class to initialize and finalize Kokkos using RAII. It calls Kokkos::initialize with the provided arguments in the constructor and Kokkos::finalize in the destructor. For correct usage, it is mandatory to create a named instance of a ScopeGuard before any calls to Kokkos are issued.

Warning

Change of behavior in version 3.7 (see below). ScopeGuard will abort if either is_intialized() or is_finalized() return true.

Description#

class ScopeGuard#

A class calling Kokkos::initialize at the start of its lifetime and Kokkos::finalize at the end of its lifetime.

Constructors

ScopeGuard(int &argc, char *argv[]);#
Parameters:
  • argc – number of command line arguments

  • argv – array of character pointers to null-terminated strings storing the command line arguments

Warning

Valid until 3.7

ScopeGuard(InitArguments const &arguments = InitArguments());#
Parameters:

argumentsstruct object with valid initialization arguments

Warning

Valid until 3.7

template<class ...Args>
ScopeGuard(Args&&... args);#
Parameters:

args

arguments to pass to Kokkos::initialize

Possible implementation:

template <class... Args> ScopeGuard(Args&&... args){ initialize(std::forward<Args>(args)...); }
~ScopeGuard();#

Destructor

Possible implementation:

~ScopeGuard() { finalize(); }
ScopeGuard(ScopeGuard const&) = delete;#

Copy constructor

ScopeGuard(ScopeGuard&&) = delete;#

Move constructor

ScopeGuard &operator=(ScopeGuard const&) = delete;#

Copy assignment operator

ScopeGuard &operator=(ScopeGuard&&) = delete;#

Move assignment operator

Notes#

  • In the constructors, all the parameters are passed to the Kokkos::initialize called internally. See Kokkos::initialize for more details.

  • Since Kokkos version 3.7, ScopeGuard unconditionally forwards the provided arguments to Kokkos::initialize, which means they have the same preconditions. Until version 3.7, ScopeGuard was calling Kokkos::initialize in its constructor only if Kokkos::is_initialized() was false, and it was calling Kokkos::finalize in its destructor only if it called Kokkos::initialize in its constructor.

    We dropped support for the old behavior. If you think you really need it, you may do:

    auto guard = std::unique_ptr<Kokkos::ScopeGuard>(
        Kokkos::is_initialized() ? new Kokkos::ScopeGuard() : nullptr);
    

    or

    auto guard = Kokkos::is_initialized() ? std::make_optional<Kokkos::ScopeGuard>()
                                        : std::nullopt;
    

    with C++17. This will work regardless of the Kokkos version.

Example#

int main(int argc, char* argv[]) {
    Kokkos::ScopeGuard guard(argc, argv);
    Kokkos::View<double*> my_view("my_view", 10);
    // my_view destructor called before Kokkos::finalize
    // ScopeGuard destructor called, calls Kokkos::finalize
}

See also#

Kokkos::initialize, Kokkos::finalize