Skip to content
This repository has been archived by the owner on Apr 15, 2024. It is now read-only.

Initialize and Finalize

jeffmiles63 edited this page May 1, 2020 · 2 revisions

Initialize and Finalize

Kokkos::initialize

Initializes Kokkos internal objects and all enabled Kokkos backends.

See Kokkos::initialize for details.

Kokkos::finalize

Shutdown Kokkos initialized execution spaces and release internally managed resources.

See Kokkos::finalize for details.

Kokkos::ScopeGuard

Kokkos::ScopeGuard is class a class which aggregates the resources managed by Kokkos. ScopeGuard will call Kokkos::initialize when constructed and Kokkos::finalize when destructed, thus the Kokkos context can is automatically managed via the scope of the ScopeGuard object.

See Kokkos::ScopeGuard for details.

ScopeGuard aids in the following common mistake which is allowing Kokkos objects to live past Kokkos::finalize:

int main(int argc, char** argv) {
  Kokkos::initialize(argc, argv);
  Kokkos::View<double*> my_view("my_view", 10);
  Kokkos::finalize();
  // my_view destructor called after Kokkos::finalize !
}

Switching to Kokkos::ScopeGuard fixes it:

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

In the above example, my_view will not go out of scope until the end of the main() function. Without ScopeGuard, Kokkos::finalize will be called before my_view is out of scope. With ScopeGuard, ScopeGuard will be dereferenced (subsequently calling Kokkos::finalize) after my_view is dereferenced, which ensures the proper order during shutdown.

Clone this wiki locally