YAKL
YAKL_init.h
Go to the documentation of this file.
1 
6 #pragma once
7 // Included by YAKL.h
8 
10 namespace yakl {
11 
16  inline bool isInitialized() { return get_yakl_instance().yakl_is_initialized; }
17 
18 
32  // Set global std::functions for alloc and free
33  inline void init( InitConfig config = InitConfig() ) {
34  yakl_mtx_lock();
35 
36  // If YAKL is already initialized, then don't do anything
37  if ( ! isInitialized() ) {
38  #if defined(YAKL_PROFILE)
39  if (yakl_mainproc()) std::cout << "Using YAKL Timers\n";
40  #endif
41 
42  get_yakl_instance().yakl_is_initialized = true;
43 
44  get_yakl_instance().pool_enabled = config.get_pool_enabled();
45 
46  // Initialize the memory pool and default allocators
47  if (use_pool()) {
48  // Set pool defaults if environment variables are not set
49  size_t initialSize = config.get_pool_initial_mb()*1024*1024;
50  size_t growSize = config.get_pool_grow_mb()*1024*1024;
51  size_t blockSize = config.get_pool_block_bytes();
52 
53  // Set the allocation and deallocation functions
54  std::function<void *( size_t )> alloc;
55  std::function<void ( void * )> dealloc;
56  set_device_alloc_free(alloc , dealloc);
57  auto zero = [] (void *ptr, size_t bytes) {};
58  std::string pool_name = "Gator: YAKL's primary memory pool";
59  std::string error_message_out_of_memory = "Please see https://github.com/mrnorman/YAKL/wiki/Pool-Allocator for more information\n";
60  std::string error_message_cannot_grow = error_message_out_of_memory;
61  get_yakl_instance().pool.init(alloc,dealloc,zero,initialSize,growSize,blockSize,pool_name,
62  error_message_out_of_memory,error_message_cannot_grow);
63  if (yakl_mainproc()) std::cout << "Using memory pool. Initial size: " << (float) initialSize/1024./1024./1024.
64  << "GB ; Grow size: " << (float) growSize/1024./1024./1024. << "GB." << std::endl;
65  }
66 
68 
69  // Initialize the default timers
70  get_yakl_instance().timer_init_func = [] () {};
71  get_yakl_instance().timer_finalize_func = [] () {
72  #if defined(YAKL_PROFILE)
73  if (yakl_mainproc()) { get_yakl_instance().timer.print_all_threads(); }
74  #endif
75  };
76  get_yakl_instance().timer_start_func = [] (char const *label) {
77  #if defined(YAKL_PROFILE)
78  fence(); get_yakl_instance().timer.start( label );
79  #endif
80  };
81  get_yakl_instance().timer_stop_func = [] (char const * label) {
82  #if defined(YAKL_PROFILE)
83  fence(); get_yakl_instance().timer.stop( label );
84  #endif
85  };
86 
87  get_yakl_instance().device_allocators_are_default = true;
88 
89  // If the user specified overrides in the InitConfig, apply them here
90  if (config.get_device_allocator ()) {
91  get_yakl_instance().alloc_device_func = config.get_device_allocator ();
92  get_yakl_instance().device_allocators_are_default = false;
93  }
94  if (config.get_device_deallocator()) {
95  get_yakl_instance().free_device_func = config.get_device_deallocator();
96  get_yakl_instance().device_allocators_are_default = false;
97  }
98  if (config.get_timer_init ()) get_yakl_instance().timer_init_func = config.get_timer_init ();
99  if (config.get_timer_finalize ()) get_yakl_instance().timer_finalize_func = config.get_timer_finalize();
100  if (config.get_timer_start ()) get_yakl_instance().timer_start_func = config.get_timer_start ();
101  if (config.get_timer_stop ()) get_yakl_instance().timer_stop_func = config.get_timer_stop ();
102 
103  #ifdef YAKL_ARCH_SYCL
104  if (yakl_mainproc()) std::cout << "Running on "
105  << sycl_default_stream().get_device().get_info<sycl::info::device::name>()
106  << "\n";
107  #endif
108 
109  // Print the device name being run on
110  #if defined(YAKL_ARCH_CUDA)
111  int id;
112  cudaGetDevice(&id);
113  cudaDeviceProp props;
114  cudaGetDeviceProperties(&props,id);
115  if (yakl_mainproc()) std::cout << props.name << std::endl;
116  #endif
117 
118  #if defined(YAKL_ARCH_HIP)
119  int id;
120  hipGetDevice(&id);
121  hipDeviceProp_t props;
122  hipGetDeviceProperties(&props,id);
123  rocfft_setup();
124  if (yakl_mainproc()) std::cout << props.name << std::endl;
125  #endif
126 
127  #if defined(YAKL_AUTO_FENCE)
128  if (yakl_mainproc()) std::cout << "INFORM: Automatically inserting fence() after every parallel_for"
129  << std::endl;
130  #endif
131  #ifdef YAKL_VERBOSE_FILE
132  int rank = 0;
133  #ifdef HAVE_MPI
134  int is_initialized;
135  MPI_Initialized(&is_initialized);
136  if (is_initialized) { MPI_Comm_rank(MPI_COMM_WORLD, &rank); }
137  #endif
138  std::ofstream myfile;
139  std::string fname = std::string("yakl_verbose_output_task_") + std::to_string(rank) + std::string(".log");
140  myfile.open(fname , std::ofstream::out);
141  myfile.close();
142  #endif
143  } else {
144  std::cerr << "WARNING: Calling yakl::initialize() when YAKL is already initialized. ";
145  }
146 
147  yakl_mtx_unlock();
148  } //
149 }
151 
152 
yakl::InitConfig
An object of this class can optionally be passed to yakl::init() to configure the initialization....
Definition: YAKL_InitConfig.h:20
yakl::use_pool
bool use_pool()
If true, then the pool allocator is being used for all device allocations.
Definition: YAKL_allocators.h:16
__YAKL_NAMESPACE_WRAPPER_END__
#define __YAKL_NAMESPACE_WRAPPER_END__
Definition: YAKL.h:20
yakl::init
void init(InitConfig config=InitConfig())
Initialize the YAKL runtime.
Definition: YAKL_init.h:33
__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::set_yakl_allocators_to_default
void set_yakl_allocators_to_default()
Return all YAKL allocators to their defaults.
Definition: YAKL_allocators.h:150
yakl::yakl_mainproc
bool yakl_mainproc()
If true, this is the main MPI process (task number == 0)
Definition: YAKL_error.h:64
yakl