YAKL
YAKL_finalize.h
Go to the documentation of this file.
1 
6 #pragma once
7 // Included by YAKL.h
8 
10 namespace yakl {
11 
18  inline void finalize() {
19  // We don't know what happens in finalize_callbacks, so to be safe, let's use a unique mutex here.
20  get_yakl_instance().yakl_final_mtx.lock();
21 
22  // Only finalize if YAKL's already initialized
23  if ( isInitialized() ) {
24  fence(); // Make sure all device work is done before we start freeing pool memory
25 
26  for (int i=0; i < get_yakl_instance().finalize_callbacks.size(); i++) {
27  get_yakl_instance().finalize_callbacks[i]();
28  }
29 
30  #if defined(YAKL_ARCH_HIP)
31  rocfft_cleanup();
32  #endif
33 
34  // Free the pools
35  get_yakl_instance().pool.finalize();
36 
37  get_yakl_instance().yakl_is_initialized = false;
38 
39  // Finalize the timers
40  #if defined(YAKL_PROFILE)
42  #endif
43 
44  get_yakl_instance().timer_init_func = [] () {
45  yakl_throw("ERROR: attempting to call the yakl::timer_init(); before calling yakl::init()");
46  };
47  get_yakl_instance().timer_finalize_func = [] () {
48  yakl_throw("ERROR: attempting to call the yakl::timer_finalize(); before calling yakl::init()");
49  };
50  get_yakl_instance().timer_start_func = [] (char const *label) {
51  yakl_throw("ERROR: attempting to call the yakl::timer_start(); before calling yakl::init()");
52  };
53  get_yakl_instance().timer_stop_func = [] (char const * label) {
54  yakl_throw("ERROR: attempting to call the yakl::timer_stop(); before calling yakl::init()");
55  };
56  get_yakl_instance().alloc_device_func = [] ( size_t bytes , char const *label ) -> void* {
57  yakl_throw("ERROR: attempting memory alloc before calling yakl::init()");
58  return nullptr;
59  };
60  get_yakl_instance().free_device_func = [] ( void *ptr , char const *label ) {
61  yakl_throw("ERROR: attempting memory free before calling yakl::init()");
62  };
63 
64  get_yakl_instance().device_allocators_are_default = false;
65  get_yakl_instance().pool_enabled = false;
66 
67  } else {
68 
69  std::cerr << "WARNING: Calling yakl::finalize() when YAKL is not initialized. ";
70  std::cerr << "This might mean you've called yakl::finalize() more than once.\n";
71 
72  }
73 
74  get_yakl_instance().yakl_is_initialized = false;
75 
76  // We don't know what happens in finalize_callbacks, so to be safe, let's use a unique mutex here.
77  get_yakl_instance().yakl_final_mtx.unlock();
78  }
79 
88  inline void register_finalize_callback( std::function<void ()> callback ) { get_yakl_instance().finalize_callbacks.push_back(callback); }
89 }
91 
92 
yakl::timer_finalize
void timer_finalize()
Finalize the YAKL timers.
Definition: YAKL_timers.h:20
yakl::register_finalize_callback
void register_finalize_callback(std::function< void()> callback)
Add a host-only callback to be called just before YAKL finalizes. This is useful for ensuring allcoat...
Definition: YAKL_finalize.h:88
__YAKL_NAMESPACE_WRAPPER_END__
#define __YAKL_NAMESPACE_WRAPPER_END__
Definition: YAKL.h:20
__YAKL_NAMESPACE_WRAPPER_BEGIN__
#define __YAKL_NAMESPACE_WRAPPER_BEGIN__
Definition: YAKL.h:19
yakl::fence
void fence()
Block the host code until all device code has completed.
Definition: YAKL_fence.h:16
yakl::isInitialized
bool isInitialized()
Determine if the YAKL runtime has been initialized. I.e., yakl::init() has been called without a corr...
Definition: YAKL_init.h:16
yakl::yakl_throw
YAKL_INLINE void yakl_throw(const char *msg)
Throw an error message. Works from the host or device.
Definition: YAKL_error.h:17
yakl::finalize
void finalize()
Finalize the YAKL runtime.
Definition: YAKL_finalize.h:18
yakl