Itoyori  v0.0.1
ori.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include "ityr/common/util.hpp"
9 #include "ityr/common/rma.hpp"
10 #include "ityr/ori/util.hpp"
11 #include "ityr/ori/options.hpp"
12 #include "ityr/ori/core.hpp"
13 #include "ityr/ori/global_ptr.hpp"
15 #include "ityr/ori/prof_events.hpp"
16 
17 namespace ityr::ori {
18 
20 
21 class ori {
22 public:
23  ori(MPI_Comm comm)
24  : mi_(comm),
25  topo_(comm),
26  core_(cache_size_option::value(), sub_block_size_option::value()) {}
27 
28 private:
30  common::runtime_options common_opts_;
35  common::prof_events common_prof_events_;
36 
37  runtime_options ori_opts_;
40  prof_events prof_events_;
41 };
42 
44 
45 inline void init(MPI_Comm comm = MPI_COMM_WORLD) {
46  instance::init(comm);
47 }
48 
49 inline void fini() {
51 }
52 
53 template <typename T>
54 inline global_ptr<T> malloc_coll(std::size_t count) {
55  return global_ptr<T>(reinterpret_cast<T*>(core::instance::get().malloc_coll(count * sizeof(T))));
56 }
57 
58 template <typename T, template <block_size_t> typename MemMapper, typename... MemMapperArgs>
59 inline global_ptr<T> malloc_coll(std::size_t count, MemMapperArgs&&... mmargs) {
60  return global_ptr<T>(reinterpret_cast<T*>(core::instance::get().malloc_coll<MemMapper>(count * sizeof(T),
61  std::forward<MemMapperArgs>(mmargs)...)));
62 }
63 
64 template <typename T>
65 inline global_ptr<T> malloc(std::size_t count) {
66  return global_ptr<T>(reinterpret_cast<T*>(core::instance::get().malloc(count * sizeof(T))));
67 }
68 
69 template <typename T>
70 inline void free_coll(global_ptr<T> ptr) {
71  core::instance::get().free_coll(ptr.raw_ptr());
72 }
73 
74 template <typename T>
75 inline void free(global_ptr<T> ptr, std::size_t count) {
76  core::instance::get().free(ptr.raw_ptr(), count * sizeof(T));
77 }
78 
79 template <typename ConstT, typename T>
80 inline void get(global_ptr<ConstT> from_ptr, T* to_ptr, std::size_t count) {
81  static_assert(std::is_same_v<std::remove_const_t<ConstT>, T>,
82  "from_ptr must be of the same type as to_ptr ignoring const");
83  static_assert(std::is_trivially_copyable_v<T>, "GET requires trivially copyable types");
84  core::instance::get().get(from_ptr.raw_ptr(), to_ptr, count * sizeof(T));
85 }
86 
87 template <typename T>
88 inline void put(const T* from_ptr, global_ptr<T> to_ptr, std::size_t count) {
89  static_assert(std::is_trivially_copyable_v<T>, "PUT requires trivially copyable types");
90  core::instance::get().put(from_ptr, to_ptr.raw_ptr(), count * sizeof(T));
91 }
92 
93 inline constexpr bool force_getput = ITYR_ORI_FORCE_GETPUT;
94 
95 template <bool SkipFetch, typename T>
96 inline T* checkout_with_getput(global_ptr<T> ptr, std::size_t count) {
97  std::size_t size = count * sizeof(T);
98  auto ret = reinterpret_cast<std::remove_const_t<T>*>(std::malloc(size + sizeof(void*)));
99  if (!SkipFetch) {
100  core::instance::get().get(ptr.raw_ptr(), ret, size);
101  }
102  *reinterpret_cast<void**>(reinterpret_cast<std::byte*>(ret) + size) =
103  const_cast<std::remove_const_t<T>*>(ptr.raw_ptr());
104  return ret;
105 }
106 
107 template <typename T>
108 inline std::pair<T*, bool> checkout_nb(global_ptr<T> ptr, std::size_t count, mode::read_t) {
109  if constexpr (force_getput) {
110  return {checkout_with_getput<false>(ptr, count), true};
111  }
112  bool completed =
113  core::instance::get().checkout_nb(const_cast<std::remove_const_t<T>*>(ptr.raw_ptr()), count * sizeof(T), mode::read);
114  return {ptr.raw_ptr(), completed};
115 }
116 
117 template <typename T>
118 inline std::pair<T*, bool> checkout_nb(global_ptr<T> ptr, std::size_t count, mode::write_t) {
119  static_assert(!std::is_const_v<T>, "Const pointers cannot be checked out with write access mode");
120  if constexpr (force_getput) {
121  return {checkout_with_getput<true>(ptr, count), true};
122  }
123  bool completed =
124  core::instance::get().checkout_nb(ptr.raw_ptr(), count * sizeof(T), mode::write);
125  return {ptr.raw_ptr(), completed};
126 }
127 
128 template <typename T>
129 inline std::pair<T*, bool> checkout_nb(global_ptr<T> ptr, std::size_t count, mode::read_write_t) {
130  static_assert(!std::is_const_v<T>, "Const pointers cannot be checked out with read+write access mode");
131  if constexpr (force_getput) {
132  return {checkout_with_getput<false>(ptr, count), true};
133  }
134  bool completed =
135  core::instance::get().checkout_nb(ptr.raw_ptr(), count * sizeof(T), mode::read_write);
136  return {ptr.raw_ptr(), completed};
137 }
138 
139 inline void checkout_complete() {
140  if constexpr (force_getput) {
141  // TODO: consider nonblocking implementation for getput
142  return;
143  }
144  core::instance::get().checkout_complete();
145 }
146 
147 template <typename T, typename Mode>
148 inline auto checkout(global_ptr<T> ptr, std::size_t count, Mode mode) {
149  auto [ret, completed] = checkout_nb(ptr, count, mode);
150  if (!completed) {
152  }
153  return ret;
154 }
155 
156 template <bool RegisterDirty, typename T>
157 inline void checkin_with_getput(T* raw_ptr, std::size_t count) {
158  std::size_t size = count * sizeof(T);
159  void* gptr = *reinterpret_cast<void**>(reinterpret_cast<std::byte*>(
160  const_cast<std::remove_const_t<T>*>(raw_ptr)) + size);
161  if constexpr (RegisterDirty) {
162  core::instance::get().put(raw_ptr, gptr, size);
163  }
164  std::free(const_cast<std::remove_const_t<T>*>(raw_ptr));
165 }
166 
167 template <typename T>
168 inline void checkin(T* raw_ptr, std::size_t count, mode::read_t) {
169  if constexpr (force_getput) {
170  checkin_with_getput<false>(raw_ptr, count);
171  return;
172  }
173  core::instance::get().checkin(const_cast<std::remove_const_t<T>*>(raw_ptr), count * sizeof(T), mode::read);
174 }
175 
176 template <typename T>
177 inline void checkin(T* raw_ptr, std::size_t count, mode::write_t) {
178  static_assert(!std::is_const_v<T>, "Const pointers cannot be checked in with write access mode");
179  if constexpr (force_getput) {
180  checkin_with_getput<true>(raw_ptr, count);
181  return;
182  }
183  core::instance::get().checkin(raw_ptr, count * sizeof(T), mode::write);
184 }
185 
186 template <typename T>
187 inline void checkin(T* raw_ptr, std::size_t count, mode::read_write_t) {
188  static_assert(!std::is_const_v<T>, "Const pointers cannot be checked in with read+write access mode");
189  if constexpr (force_getput) {
190  checkin_with_getput<true>(raw_ptr, count);
191  return;
192  }
193  core::instance::get().checkin(raw_ptr, count * sizeof(T), mode::read_write);
194 }
195 
196 inline void release() {
197  core::instance::get().release();
198 }
199 
200 inline auto release_lazy() {
201  return core::instance::get().release_lazy();
202 }
203 
205 
206 inline void acquire() {
207  core::instance::get().acquire();
208 }
209 
210 inline void acquire(release_handler rh) {
211  core::instance::get().acquire(rh);
212 }
213 
214 template <typename T>
215 inline void set_readonly_coll(global_ptr<T> ptr, std::size_t count) {
216  core::instance::get().set_readonly_coll(const_cast<std::remove_const_t<T>*>(ptr.raw_ptr()), count * sizeof(T));
217 }
218 
219 template <typename T>
220 inline void unset_readonly_coll(global_ptr<T> ptr, std::size_t count) {
221  core::instance::get().unset_readonly_coll(const_cast<std::remove_const_t<T>*>(ptr.raw_ptr()), count * sizeof(T));
222 }
223 
224 inline void poll() {
225  core::instance::get().poll();
226 }
227 
228 inline void collect_deallocated() {
229  core::instance::get().collect_deallocated();
230 }
231 
232 inline void cache_prof_begin() {
233  core::instance::get().cache_prof_begin();
234 }
235 
236 inline void cache_prof_end() {
237  core::instance::get().cache_prof_end();
238 }
239 
240 inline void cache_prof_print() {
241  core::instance::get().cache_prof_print();
242 }
243 
244 inline void* file_mem_alloc_coll(const std::string& fpath, bool mlock) {
245  return file_mem_manager::instance::get().create(fpath, mlock);
246 }
247 
248 inline void file_mem_free_coll(void* addr) {
249  file_mem_manager::instance::get().destroy(addr);
250 }
251 
252 inline file_mem& file_mem_get(void* addr) {
253  return file_mem_manager::instance::get().get(addr);
254 }
255 
256 }
Definition: mpi_util.hpp:430
Definition: prof_events.hpp:124
Definition: util.hpp:207
Definition: util.hpp:176
static auto & get()
Definition: util.hpp:180
static void init(Args &&... args)
Definition: util.hpp:190
static void fini()
Definition: util.hpp:194
Definition: file_mem.hpp:12
Definition: global_ptr.hpp:19
constexpr pointer raw_ptr() const noexcept
Definition: global_ptr.hpp:40
Definition: ori.hpp:21
ori(MPI_Comm comm)
Definition: ori.hpp:23
Definition: prof_events.hpp:66
ITYR_CONCAT(mode_, ITYR_PROFILER_MODE) mode
Definition: profiler.hpp:257
constexpr read_write_t read_write
Definition: util.hpp:15
constexpr read_t read
Definition: util.hpp:11
constexpr write_t write
Definition: util.hpp:13
Definition: block_region_set.hpp:9
void fini()
Definition: ori.hpp:49
std::pair< T *, bool > checkout_nb(global_ptr< T > ptr, std::size_t count, mode::read_t)
Definition: ori.hpp:108
void collect_deallocated()
Definition: ori.hpp:228
void get(global_ptr< ConstT > from_ptr, T *to_ptr, std::size_t count)
Definition: ori.hpp:80
void init(MPI_Comm comm=MPI_COMM_WORLD)
Definition: ori.hpp:45
void checkin(T *raw_ptr, std::size_t count, mode::read_t)
Definition: ori.hpp:168
file_mem & file_mem_get(void *addr)
Definition: ori.hpp:252
void cache_prof_begin()
Definition: ori.hpp:232
global_ptr< T > malloc_coll(std::size_t count)
Definition: ori.hpp:54
void free(global_ptr< T > ptr, std::size_t count)
Definition: ori.hpp:75
auto release_lazy()
Definition: ori.hpp:200
void free_coll(global_ptr< T > ptr)
Definition: ori.hpp:70
auto checkout(global_ptr< T > ptr, std::size_t count, Mode mode)
Definition: ori.hpp:148
void put(const T *from_ptr, global_ptr< T > to_ptr, std::size_t count)
Definition: ori.hpp:88
core::instance::instance_type::release_handler release_handler
Definition: ori.hpp:204
void poll()
Definition: ori.hpp:224
global_ptr< T > malloc(std::size_t count)
Definition: ori.hpp:65
void checkout_complete()
Definition: ori.hpp:139
constexpr block_size_t block_size
Definition: ori.hpp:19
void file_mem_free_coll(void *addr)
Definition: ori.hpp:248
void release()
Definition: ori.hpp:196
T * checkout_with_getput(global_ptr< T > ptr, std::size_t count)
Definition: ori.hpp:96
uint32_t block_size_t
Definition: util.hpp:30
void unset_readonly_coll(global_ptr< T > ptr, std::size_t count)
Definition: ori.hpp:220
void cache_prof_print()
Definition: ori.hpp:240
void cache_prof_end()
Definition: ori.hpp:236
void * file_mem_alloc_coll(const std::string &fpath, bool mlock)
Definition: ori.hpp:244
void acquire()
Definition: ori.hpp:206
void checkin_with_getput(T *raw_ptr, std::size_t count)
Definition: ori.hpp:157
constexpr bool force_getput
Definition: ori.hpp:93
void set_readonly_coll(global_ptr< T > ptr, std::size_t count)
Definition: ori.hpp:215
constexpr auto size(const checkout_span< T, Mode > &cs) noexcept
Definition: checkout_span.hpp:178
#define ITYR_ORI_FORCE_GETPUT
#define ITYR_ORI_BLOCK_SIZE
Definition: options.hpp:153
Definition: options.hpp:60
Definition: util.hpp:10
Definition: util.hpp:14
Definition: util.hpp:12
Definition: options.hpp:96
Definition: options.hpp:66