YAKL
YAKL_InitConfig.h
Go to the documentation of this file.
1 
6 #pragma once
7 
8 #include <unistd.h>
9 
11 namespace yakl {
12 
20  class InitConfig {
21  protected:
23  std::function<void *( size_t , char const *)> alloc_device_func;
25  std::function<void ( void * , char const *)> free_device_func;
27  std::function<void ()> timer_init;
29  std::function<void ()> timer_finalize;
31  std::function<void (char const *)> timer_start;
33  std::function<void (char const *)> timer_stop;
35  bool pool_enabled;
37  size_t pool_initial_mb;
39  size_t pool_grow_mb;
41  size_t pool_block_bytes;
42 
43  public:
48  size_t mem_total, mem_free;
49  #if defined(YAKL_ARCH_CUDA)
50  cudaMemGetInfo(&mem_free, &mem_total);
51  mem_free /= 1024*1024;
52  mem_total /= 1024*1024;
53  #elif defined(YAKL_ARCH_HIP)
54  hipMemGetInfo(&mem_free, &mem_total);
55  mem_free /= 1024*1024;
56  mem_total /= 1024*1024;
57  #elif defined(YAKL_ARCH_SYCL)
58  mem_total = sycl_default_stream().get_device().get_info<sycl::info::device::global_mem_size>();
59  mem_free = sycl_default_stream().get_device().get_info<sycl::ext::intel::info::device::free_memory>();
60  mem_free /= 1024*1024;
61  mem_total /= 1024*1024;
62  #else
63  long pages = sysconf(_SC_PHYS_PAGES);
64  long page_size = sysconf(_SC_PAGE_SIZE);
65  mem_total = (size_t)pages*(size_t)page_size;
66  mem_free = mem_total;
67  mem_free /= 1024*1024;
68  mem_total /= 1024*1024;
69  #endif
70  pool_enabled = true;
71  pool_initial_mb = mem_total / 8;
72  if (mem_free < pool_initial_mb) pool_initial_mb = 0.5*mem_free;
73  pool_grow_mb = pool_initial_mb;
74  pool_block_bytes = 16*sizeof(size_t);
75 
76  char * env = std::getenv("GATOR_DISABLE");
77  if ( env != nullptr ) {
78  std::string resp(env);
79  if (resp == "yes" || resp == "YES" || resp == "1" || resp == "true" || resp == "TRUE" || resp == "T") {
80  pool_enabled = false;
81  }
82  }
83 
84  // Check for GATOR_INITIAL_MB environment variable
85  env = std::getenv("GATOR_INITIAL_MB");
86  if ( env != nullptr ) {
87  long int initial_mb = atol(env);
88  if (initial_mb != 0) {
89  pool_initial_mb = initial_mb;
90  pool_grow_mb = pool_initial_mb;
91  } else {
92  if (yakl_mainproc()) std::cout << "WARNING: Invalid GATOR_INITIAL_MB. Defaulting to 1GB\n";
93  }
94  }
95  // Check for GATOR_GROW_MB environment variable
96  env = std::getenv("GATOR_GROW_MB");
97  if ( env != nullptr ) {
98  long int grow_mb = atol(env);
99  if (grow_mb != 0) {
100  pool_grow_mb = grow_mb;
101  } else {
102  if (yakl_mainproc()) std::cout << "WARNING: Invalid GATOR_GROW_MB. Defaulting to 1GB\n";
103  }
104  }
105 
106  // Check for GATOR_BLOCK_BYTES environment variable
107  env = std::getenv("GATOR_BLOCK_BYTES");
108  if ( env != nullptr ) {
109  long int block_bytes = atol(env);
110  if (block_bytes != 0 && block_bytes%(2*sizeof(size_t)) == 0) {
111  pool_block_bytes = block_bytes;
112  } else {
113  if (yakl_mainproc()) std::cout << "WARNING: Invalid GATOR_BLOCK_BYTES. Defaulting to 16*sizeof(size_t)\n";
114  if (yakl_mainproc()) std::cout << " GATOR_BLOCK_BYTES must be > 0 and a multiple of 2*sizeof(size_t)\n";
115  }
116  }
117  }
119  InitConfig set_device_allocator( std::function<void *( size_t )> func ) {
120  alloc_device_func = [=] (size_t bytes , char const *label) -> void * { return func(bytes); };
121  return *this;
122  }
124  InitConfig set_device_deallocator( std::function<void ( void * )> func ) {
125  free_device_func = [=] (void *ptr , char const *label) { func(ptr); };
126  return *this;
127  }
129  InitConfig set_device_allocator ( std::function<void *( size_t , char const *)> func ) { alloc_device_func = func; return *this; }
131  InitConfig set_device_deallocator( std::function<void ( void * , char const *)> func ) { free_device_func = func; return *this; }
133  InitConfig set_timer_init ( std::function<void ( )> func ) { timer_init = func; return *this; }
135  InitConfig set_timer_finalize ( std::function<void ( )> func ) { timer_finalize = func; return *this; }
137  InitConfig set_timer_start ( std::function<void (char const *)> func ) { timer_start = func; return *this; }
139  InitConfig set_timer_stop ( std::function<void (char const *)> func ) { timer_stop = func; return *this; }
141  InitConfig set_pool_enabled ( bool enabled ) { this->pool_enabled = enabled ; return *this; }
143  InitConfig set_pool_initial_mb ( size_t initial_mb ) { this->pool_initial_mb = initial_mb ; return *this; }
145  InitConfig set_pool_grow_mb ( size_t grow_mb ) { this->pool_grow_mb = grow_mb ; return *this; }
147  InitConfig set_pool_block_bytes( size_t block_bytes) { this->pool_block_bytes = block_bytes; return *this; }
149  std::function<void *( size_t , char const *)> get_device_allocator () const { return alloc_device_func; }
151  std::function<void ( void * , char const *)> get_device_deallocator() const { return free_device_func ; }
153  std::function<void ()> get_timer_init () const { return timer_init ; }
155  std::function<void ()> get_timer_finalize () const { return timer_finalize ; }
157  std::function<void (char const *)> get_timer_start () const { return timer_start ; }
159  std::function<void (char const *)> get_timer_stop () const { return timer_stop ; }
161  bool get_pool_enabled () const { return pool_enabled ; }
163  size_t get_pool_initial_mb () const { return pool_initial_mb ; }
165  size_t get_pool_grow_mb () const { return pool_grow_mb ; }
167  size_t get_pool_block_bytes() const { return pool_block_bytes; }
168  };
169 
170 }
172 
173 
yakl::InitConfig
An object of this class can optionally be passed to yakl::init() to configure the initialization....
Definition: YAKL_InitConfig.h:20
yakl::timer_stop
void timer_stop(char const *lab)
Stop a timer with the given string label. NOTE: Timers must be perfectly nested.
Definition: YAKL_timers.h:26
yakl::InitConfig::set_timer_start
InitConfig set_timer_start(std::function< void(char const *)> func)
Pass the timer start function you wish to use to override YAKL's default.
Definition: YAKL_InitConfig.h:137
yakl::InitConfig::get_device_allocator
std::function< void *(size_t, char const *)> get_device_allocator() const
Get the device allocator function. Returns an empty std::function if the user has not set one.
Definition: YAKL_InitConfig.h:149
yakl::InitConfig::set_pool_grow_mb
InitConfig set_pool_grow_mb(size_t grow_mb)
Tell YAKL how big each additional pool should be in MB.
Definition: YAKL_InitConfig.h:145
yakl::InitConfig::set_timer_init
InitConfig set_timer_init(std::function< void()> func)
Pass the timer init function you wish to use to override YAKL's default.
Definition: YAKL_InitConfig.h:133
yakl::timer_finalize
void timer_finalize()
Finalize the YAKL timers.
Definition: YAKL_timers.h:20
yakl::InitConfig::get_timer_start
std::function< void(char const *)> get_timer_start() const
Get the timer start function. Returns an empty std::function if the user has not set one.
Definition: YAKL_InitConfig.h:157
yakl::InitConfig::InitConfig
InitConfig()
Creating an InitConfig() controls the memory pool parameters, timer function overrides,...
Definition: YAKL_InitConfig.h:47
__YAKL_NAMESPACE_WRAPPER_END__
#define __YAKL_NAMESPACE_WRAPPER_END__
Definition: YAKL.h:20
yakl::InitConfig::set_device_deallocator
InitConfig set_device_deallocator(std::function< void(void *, char const *)> func)
Pass the device deallocator function you wish to use to override YAKL's default (LABEL)
Definition: YAKL_InitConfig.h:131
yakl::InitConfig::set_timer_finalize
InitConfig set_timer_finalize(std::function< void()> func)
Pass the timer finalize function you wish to use to override YAKL's default.
Definition: YAKL_InitConfig.h:135
__YAKL_NAMESPACE_WRAPPER_BEGIN__
#define __YAKL_NAMESPACE_WRAPPER_BEGIN__
Definition: YAKL.h:19
yakl::InitConfig::set_device_allocator
InitConfig set_device_allocator(std::function< void *(size_t, char const *)> func)
Pass the device allocator function you wish to use to override YAKL's default (LABEL)
Definition: YAKL_InitConfig.h:129
yakl::InitConfig::get_timer_stop
std::function< void(char const *)> get_timer_stop() const
Get the timer stop function. Returns an empty std::function if the user has not set one.
Definition: YAKL_InitConfig.h:159
yakl::InitConfig::set_pool_block_bytes
InitConfig set_pool_block_bytes(size_t block_bytes)
Tell YAKL how big each additional pool should be in MB.
Definition: YAKL_InitConfig.h:147
yakl::InitConfig::set_pool_initial_mb
InitConfig set_pool_initial_mb(size_t initial_mb)
Tell YAKL how big the initial pool should be in MB.
Definition: YAKL_InitConfig.h:143
yakl::InitConfig::set_pool_enabled
InitConfig set_pool_enabled(bool enabled)
Tell YAKL whether to enable the pool or not.
Definition: YAKL_InitConfig.h:141
yakl::InitConfig::get_timer_finalize
std::function< void()> get_timer_finalize() const
Get the timer finalize function. Returns an empty std::function if the user has not set one.
Definition: YAKL_InitConfig.h:155
yakl::yakl_mainproc
bool yakl_mainproc()
If true, this is the main MPI process (task number == 0)
Definition: YAKL_error.h:64
yakl::InitConfig::set_device_deallocator
InitConfig set_device_deallocator(std::function< void(void *)> func)
Pass the device deallocator function you wish to use to override YAKL's default (NO LABEL)
Definition: YAKL_InitConfig.h:124
yakl::InitConfig::get_timer_init
std::function< void()> get_timer_init() const
Get the timer init function. Returns an empty std::function if the user has not set one.
Definition: YAKL_InitConfig.h:153
yakl
yakl::InitConfig::get_pool_enabled
bool get_pool_enabled() const
Determine whether this config object will enable the device memory pool.
Definition: YAKL_InitConfig.h:161
yakl::InitConfig::get_pool_grow_mb
size_t get_pool_grow_mb() const
Determine how many MB this config will request the pool to use for additional pools.
Definition: YAKL_InitConfig.h:165
yakl::InitConfig::set_device_allocator
InitConfig set_device_allocator(std::function< void *(size_t)> func)
Pass the device allocator function you wish to use to override YAKL's default (NO LABEL)
Definition: YAKL_InitConfig.h:119
yakl::timer_start
void timer_start(char const *lab)
Start a timer with the given string label. NOTE: Timers must be perfectly nested.
Definition: YAKL_timers.h:23
yakl::InitConfig::get_pool_initial_mb
size_t get_pool_initial_mb() const
Determine how many MB this config will request the pool to use for the initial device memory pool.
Definition: YAKL_InitConfig.h:163
yakl::InitConfig::set_timer_stop
InitConfig set_timer_stop(std::function< void(char const *)> func)
Pass the timer stop function you wish to use to override YAKL's default.
Definition: YAKL_InitConfig.h:139
yakl::timer_init
void timer_init()
Initialize the YAKL timers.
Definition: YAKL_timers.h:17
yakl::InitConfig::get_device_deallocator
std::function< void(void *, char const *)> get_device_deallocator() const
Get the device deallocator function. Returns an empty std::function if the user has not set one.
Definition: YAKL_InitConfig.h:151
yakl::InitConfig::get_pool_block_bytes
size_t get_pool_block_bytes() const
Determine how many bytes this config will request the pool to use for block size.
Definition: YAKL_InitConfig.h:167