push_finalize_hook

Defined in header <Kokkos_InitializeFinalize.hpp>since Kokkos 5.3 which is included from <Kokkos_Core.hpp>

Usage

Kokkos::push_finalize_hook(func);

Registers the callable object func to be called when the Kokkos execution environment is terminated.

The functions registered via push_finalize_hook() will be called in reverse order (last-in, first-out) when entering finalize(), before releasing acquired resources and finalizing all backends.

If a function exits via a thrown exception, std::terminate is called.

Interface

void push_finalize_hook(std::function<void()> func);
Parameters:

func – Function object to be called when entering finalize()

Changed in version 5.3: Thread-safe: may be called concurrently from multiple threads without additional synchronization.

Notes

Note

push_finalize_hook() may be called at any point in the program, including before initialize(). Hooks registered before initialize() are retained and invoked during finalize(), just like hooks registered after initialization. Because hooks run in reverse order of registration, a hook registered before initialize() will be among the last to execute.

Conversely, since finalize() may only be called once per program, any hook registered after finalize() has run will never be called.

Example

#include <Kokkos_Core.hpp>
#include <iostream>

void my_hook() {
  std::cout << "Cruel world!\n";
}

int main(int argc, char* argv[]) {
    // Legal to register a hook before Kokkos::initialize()
    Kokkos::push_finalize_hook(my_hook);
    Kokkos::initialize(argc, argv);
    Kokkos::push_finalize_hook([]{ std::cout << "Goodbye\n"; });
    std::cout << "Calling Kokkos::finalize() ...\n";
    Kokkos::finalize();
    // Never called: finalize() has already run and may only be called once,
    // so this hook is never invoked (otherwise it would call std::terminate).
    Kokkos::push_finalize_hook([]{ throw 42; });
}

Output:

Calling Kokkos::finalize() ...
Goodbye
Cruel world!

See also

See also

finalize

Terminate the Kokkos execution environment