Toggle table of contents sidebar
ScopeGuard
¶
Defined in header <Kokkos_Core.hpp>
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_initialized()
or is_finalized()
return true
.
A class calling Kokkos::initialize
at the start of its lifetime and Kokkos::finalize
at the end of its lifetime.
Constructors
argc – number of command line arguments
argv – array of character pointers to null-terminated strings storing the command line arguments
arguments – struct
object with valid initialization arguments
args –
arguments to pass to Kokkos::initialize
Possible implementation:
template <class... Args> ScopeGuard(Args&&... args){ initialize(std::forward<Args>(args)...); }
Destructor
Possible implementation:
~ScopeGuard() { finalize(); }
Copy constructor
Move constructor
Copy assignment operator
Move assignment operator
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.
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
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4