Itoyori  v0.0.1
options.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <cstdio>
4 #include <vector>
5 #include <algorithm>
6 
7 #include "ityr/common/util.hpp"
9 
10 #define ITYR_STR_EXPAND(x) #x
11 #define ITYR_STR(x) ITYR_STR_EXPAND(x)
12 #define ITYR_PRINT_MACRO(x) printf(#x "=" ITYR_STR_EXPAND(x) "\n")
13 
14 namespace ityr::common {
15 
16 inline void print_compile_options() {
17 #ifndef ITYR_MAX_VERBOSE_LEVEL
18 #define ITYR_MAX_VERBOSE_LEVEL 0
19 #endif
21 
22 #ifndef ITYR_PROFILER_MODE
23 #define ITYR_PROFILER_MODE disabled
24 #endif
26 
27 #ifndef ITYR_RMA_IMPL
28 #if __has_include(<utofu.h>)
29 #define ITYR_RMA_IMPL utofu
30 #else
31 #define ITYR_RMA_IMPL mpi
32 #endif
33 #endif
35 
36 #ifndef ITYR_ALLOCATOR_USE_BOOST
37 #define ITYR_ALLOCATOR_USE_BOOST 0
38 #endif
40 
41 #ifndef ITYR_ALLOCATOR_USE_DYNAMIC_WIN
42 #define ITYR_ALLOCATOR_USE_DYNAMIC_WIN false
43 #endif
45 }
46 
47 class option_base {
48 public:
49  virtual ~option_base() = default;
50  virtual void print() const = 0;
51 };
52 
53 template <typename Derived, typename T>
54 class option : public singleton<Derived>, public option_base {
55  using base_t = singleton<Derived>;
56 
57 public:
58  using value_type = T;
59 
60  option(value_type val) : val_(val) {}
61 
62  static value_type value() {
64  return base_t::get().val_;
65  }
66 
67  static void set(value_type val) {
68  base_t::init(val);
69  }
70 
71  static void unset() {
73  base_t::fini();
74  }
75 
76  void print() const override {
77  std::cout << Derived::name() << "=" << val_ << std::endl;
78  }
79 
80 protected:
82 };
83 
84 inline std::vector<option_base*>& get_options() {
85  static std::vector<option_base*> opts;
86  return opts;
87 }
88 
89 template <typename Option>
91 public:
93  : init_(getenv_coll(Option::name(), Option::default_value())) {
94  auto& opts = get_options();
95  option_base* opt = &Option::get();
96  if (std::find(opts.begin(), opts.end(), opt) == opts.end()) {
97  opts.push_back(&Option::get());
98  should_pop_ = true;
99  }
100  }
102  if (should_pop_) {
103  get_options().pop_back();
104  }
105  }
106 private:
108  bool should_pop_ = false;
109 };
110 
111 inline void print_runtime_options() {
112  for (option_base* opt : get_options()) {
113  opt->print();
114  }
115 }
116 
117 struct enable_shared_memory_option : public option<enable_shared_memory_option, bool> {
118  using option::option;
119  static std::string name() { return "ITYR_ENABLE_SHARED_MEMORY"; }
120  static bool default_value() { return true; }
121 };
122 
123 struct global_clock_sync_round_trips_option : public option<global_clock_sync_round_trips_option, std::size_t> {
124  using option::option;
125  static std::string name() { return "ITYR_GLOBAL_CLOCK_SYNC_ROUND_TRIPS"; }
126  static std::size_t default_value() { return 100; }
127 };
128 
129 struct prof_output_per_rank_option : public option<prof_output_per_rank_option, bool> {
130  using option::option;
131  static std::string name() { return "ITYR_PROF_OUTPUT_PER_RANK"; }
132  static bool default_value() { return false; }
133 };
134 
135 struct rma_use_mpi_win_allocate : public option<rma_use_mpi_win_allocate, bool> {
136  using option::option;
137  static std::string name() { return "ITYR_RMA_USE_MPI_WIN_ALLOCATE"; }
138  static bool default_value() { return false; }
139 };
140 
141 struct allocator_block_size_option : public option<allocator_block_size_option, std::size_t> {
142  using option::option;
143  static std::string name() { return "ITYR_ALLOCATOR_BLOCK_SIZE"; }
144  static std::size_t default_value() { return std::size_t(2) * 1024 * 1024; }
145 };
146 
147 struct allocator_max_unflushed_free_objs_option : public option<allocator_max_unflushed_free_objs_option, std::size_t> {
148  using option::option;
149  static std::string name() { return "ITYR_ALLOCATOR_MAX_UNFLUSHED_FREE_OBJS"; }
150  static std::size_t default_value() { return 10; }
151 };
152 
160 };
161 
162 }
Definition: options.hpp:47
virtual ~option_base()=default
virtual void print() const =0
Definition: options.hpp:90
~option_initializer()
Definition: options.hpp:101
option_initializer()
Definition: options.hpp:92
Definition: options.hpp:54
void print() const override
Definition: options.hpp:76
option(value_type val)
Definition: options.hpp:60
T value_type
Definition: options.hpp:58
value_type val_
Definition: options.hpp:81
static value_type value()
Definition: options.hpp:62
static void set(value_type val)
Definition: options.hpp:67
static void unset()
Definition: options.hpp:71
Definition: util.hpp:176
static bool initialized()
Definition: util.hpp:185
static auto & get()
Definition: util.hpp:180
static void init(Args &&... args)
Definition: util.hpp:190
static void fini()
Definition: util.hpp:194
#define ITYR_PRINT_MACRO(x)
Definition: options.hpp:12
#define ITYR_RMA_IMPL
#define ITYR_PROFILER_MODE
#define ITYR_MAX_VERBOSE_LEVEL
#define ITYR_ALLOCATOR_USE_BOOST
#define ITYR_ALLOCATOR_USE_DYNAMIC_WIN
#define ITYR_CHECK(cond)
Definition: util.hpp:48
Definition: allocator.hpp:16
void print_runtime_options()
Definition: options.hpp:111
std::vector< option_base * > & get_options()
Definition: options.hpp:84
void print_compile_options()
Definition: options.hpp:16
T getenv_coll(const std::string &env_var, T default_val)
Definition: mpi_util.hpp:467
void get(global_ptr< ConstT > from_ptr, T *to_ptr, std::size_t count)
Definition: ori.hpp:80
static std::size_t default_value()
Definition: options.hpp:144
static std::string name()
Definition: options.hpp:143
static std::string name()
Definition: options.hpp:149
static std::size_t default_value()
Definition: options.hpp:150
static bool default_value()
Definition: options.hpp:120
static std::string name()
Definition: options.hpp:119
static std::string name()
Definition: options.hpp:125
static std::size_t default_value()
Definition: options.hpp:126
static bool default_value()
Definition: options.hpp:132
static std::string name()
Definition: options.hpp:131
Definition: options.hpp:135
static std::string name()
Definition: options.hpp:137
static bool default_value()
Definition: options.hpp:138
Definition: options.hpp:153
option_initializer< enable_shared_memory_option > ITYR_ANON_VAR
Definition: options.hpp:154
option_initializer< allocator_max_unflushed_free_objs_option > ITYR_ANON_VAR
Definition: options.hpp:159
option_initializer< rma_use_mpi_win_allocate > ITYR_ANON_VAR
Definition: options.hpp:157
option_initializer< global_clock_sync_round_trips_option > ITYR_ANON_VAR
Definition: options.hpp:155
option_initializer< allocator_block_size_option > ITYR_ANON_VAR
Definition: options.hpp:158
option_initializer< prof_output_per_rank_option > ITYR_ANON_VAR
Definition: options.hpp:156