/src/parallel-hashmap/parallel_hashmap/phmap_dump.h
Line | Count | Source |
1 | | #if !defined(phmap_dump_h_guard_) |
2 | | #define phmap_dump_h_guard_ |
3 | | |
4 | | // --------------------------------------------------------------------------- |
5 | | // Copyright (c) 2019, Gregory Popovitch - greg7mdp@gmail.com |
6 | | // |
7 | | // providing dump/load/mmap_load |
8 | | // |
9 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
10 | | // you may not use this file except in compliance with the License. |
11 | | // You may obtain a copy of the License at |
12 | | // |
13 | | // https://www.apache.org/licenses/LICENSE-2.0 |
14 | | // |
15 | | // Unless required by applicable law or agreed to in writing, software |
16 | | // distributed under the License is distributed on an "AS IS" BASIS, |
17 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
18 | | // See the License for the specific language governing permissions and |
19 | | // limitations under the License. |
20 | | // --------------------------------------------------------------------------- |
21 | | |
22 | | #include <iostream> |
23 | | #include <fstream> |
24 | | #include <functional> |
25 | | #include "phmap.h" |
26 | | namespace phmap |
27 | | { |
28 | | |
29 | | namespace type_traits_internal { |
30 | | |
31 | | #if defined(__GLIBCXX__) && __GLIBCXX__ < 20150801 |
32 | | template<typename T> struct IsTriviallyCopyable : public std::integral_constant<bool, __has_trivial_copy(T)> {}; |
33 | | #else |
34 | | template<typename T> struct IsTriviallyCopyable : public std::is_trivially_copyable<T> {}; |
35 | | #endif |
36 | | |
37 | | template <class T1, class T2> |
38 | | struct IsTriviallyCopyable<std::pair<T1, T2>> { |
39 | | static constexpr bool value = IsTriviallyCopyable<T1>::value && IsTriviallyCopyable<T2>::value; |
40 | | }; |
41 | | } |
42 | | |
43 | | namespace priv { |
44 | | |
45 | | #if !defined(PHMAP_NON_DETERMINISTIC) && !defined(PHMAP_DISABLE_DUMP) |
46 | | |
47 | | static constexpr size_t s_version_base = std::numeric_limits<size_t>::max() - 10; |
48 | | static constexpr size_t s_version = s_version_base; |
49 | | // ------------------------------------------------------------------------ |
50 | | // dump/load for raw_hash_set |
51 | | // ------------------------------------------------------------------------ |
52 | | template <class Policy, class Hash, class Eq, class Alloc> |
53 | | template<typename OutputArchive> |
54 | 601 | bool raw_hash_set<Policy, Hash, Eq, Alloc>::phmap_dump(OutputArchive& ar) const { |
55 | 601 | static_assert(type_traits_internal::IsTriviallyCopyable<value_type>::value, |
56 | 601 | "value_type should be trivially copyable"); |
57 | | |
58 | 601 | ar.saveBinary(&s_version, sizeof(size_t)); |
59 | 601 | ar.saveBinary(&size_, sizeof(size_t)); |
60 | 601 | ar.saveBinary(&capacity_, sizeof(size_t)); |
61 | 601 | if (size_ == 0) |
62 | 34 | return true; |
63 | 567 | ar.saveBinary(ctrl_, sizeof(ctrl_t) * (capacity_ + Group::kWidth + 1)); |
64 | 567 | ar.saveBinary(slots_, sizeof(slot_type) * capacity_); |
65 | 567 | ar.saveBinary(&growth_left(), sizeof(size_t)); |
66 | 567 | return true; |
67 | 601 | } |
68 | | |
69 | | template <class Policy, class Hash, class Eq, class Alloc> |
70 | | template<typename InputArchive> |
71 | 601 | bool raw_hash_set<Policy, Hash, Eq, Alloc>::phmap_load(InputArchive& ar) { |
72 | 601 | static_assert(type_traits_internal::IsTriviallyCopyable<value_type>::value, |
73 | 601 | "value_type should be trivially copyable"); |
74 | 601 | raw_hash_set<Policy, Hash, Eq, Alloc>().swap(*this); // clear any existing content |
75 | | |
76 | 601 | size_t version = 0; |
77 | 601 | ar.loadBinary(&version, sizeof(size_t)); |
78 | 601 | if (version < s_version_base) { |
79 | | // we didn't store the version, version actually contains the size |
80 | 462 | size_ = version; |
81 | 462 | } else { |
82 | 139 | ar.loadBinary(&size_, sizeof(size_t)); |
83 | 139 | } |
84 | 601 | ar.loadBinary(&capacity_, sizeof(size_t)); |
85 | | |
86 | | // reject a header that no table could have produced (e.g. a size without |
87 | | // storage), as the ctrl_/slots_ reads below are sized from these fields. |
88 | 601 | if ((capacity_ && !IsValidCapacity(capacity_)) || |
89 | 601 | size_ > (capacity_ ? CapacityToGrowth(capacity_) : 0)) { |
90 | 0 | size_ = 0; |
91 | 0 | capacity_ = 0; |
92 | 0 | return false; |
93 | 0 | } |
94 | | |
95 | 601 | if (capacity_) { |
96 | | // allocate memory for ctrl_ and slots_ |
97 | 139 | initialize_slots(capacity_); |
98 | 139 | } |
99 | 601 | if (size_ == 0) |
100 | 462 | return true; |
101 | 139 | ar.loadBinary(ctrl_, sizeof(ctrl_t) * (capacity_ + Group::kWidth + 1)); |
102 | 139 | ar.loadBinary(slots_, sizeof(slot_type) * capacity_); |
103 | 139 | if (version >= s_version_base) { |
104 | | // growth_left should be restored after calling initialize_slots() which resets it. |
105 | 139 | ar.loadBinary(&growth_left(), sizeof(size_t)); |
106 | 139 | } else { |
107 | 0 | drop_deletes_without_resize(); |
108 | 0 | } |
109 | 139 | return true; |
110 | 601 | } |
111 | | |
112 | | // ------------------------------------------------------------------------ |
113 | | // dump/load for parallel_hash_set |
114 | | // ------------------------------------------------------------------------ |
115 | | template <size_t N, |
116 | | template <class, class, class, class> class RefSet, |
117 | | class Mtx_, |
118 | | class Policy, class Hash, class Eq, class Alloc> |
119 | | template<typename OutputArchive> |
120 | | bool parallel_hash_set<N, RefSet, Mtx_, Policy, Hash, Eq, Alloc>::phmap_dump(OutputArchive& ar) const { |
121 | | static_assert(type_traits_internal::IsTriviallyCopyable<value_type>::value, |
122 | | "value_type should be trivially copyable"); |
123 | | |
124 | | size_t submap_count = subcnt(); |
125 | | ar.saveBinary(&submap_count, sizeof(size_t)); |
126 | | for (size_t i = 0; i < sets_.size(); ++i) { |
127 | | auto& inner = sets_[i]; |
128 | | typename Lockable::UniqueLock m(const_cast<Inner&>(inner)); |
129 | | if (!inner.set_.phmap_dump(ar)) { |
130 | | std::cerr << "Failed to dump submap " << i << std::endl; |
131 | | return false; |
132 | | } |
133 | | } |
134 | | return true; |
135 | | } |
136 | | |
137 | | template <size_t N, |
138 | | template <class, class, class, class> class RefSet, |
139 | | class Mtx_, |
140 | | class Policy, class Hash, class Eq, class Alloc> |
141 | | template<typename InputArchive> |
142 | | bool parallel_hash_set<N, RefSet, Mtx_, Policy, Hash, Eq, Alloc>::phmap_load(InputArchive& ar) { |
143 | | static_assert(type_traits_internal::IsTriviallyCopyable<value_type>::value, |
144 | | "value_type should be trivially copyable"); |
145 | | |
146 | | size_t submap_count = 0; |
147 | | ar.loadBinary(&submap_count, sizeof(size_t)); |
148 | | if (submap_count != subcnt()) { |
149 | | std::cerr << "submap count(" << submap_count << ") != N(" << N << ")" << std::endl; |
150 | | return false; |
151 | | } |
152 | | |
153 | | for (size_t i = 0; i < submap_count; ++i) { |
154 | | auto& inner = sets_[i]; |
155 | | typename Lockable::UniqueLock m(const_cast<Inner&>(inner)); |
156 | | if (!inner.set_.phmap_load(ar)) { |
157 | | std::cerr << "Failed to load submap " << i << std::endl; |
158 | | return false; |
159 | | } |
160 | | } |
161 | | return true; |
162 | | } |
163 | | |
164 | | #endif // !defined(PHMAP_NON_DETERMINISTIC) && !defined(PHMAP_DISABLE_DUMP) |
165 | | |
166 | | } // namespace priv |
167 | | |
168 | | |
169 | | |
170 | | // ------------------------------------------------------------------------ |
171 | | // BinaryArchive |
172 | | // File is closed when archive object is destroyed |
173 | | // ------------------------------------------------------------------------ |
174 | | |
175 | | // ------------------------------------------------------------------------ |
176 | | // ------------------------------------------------------------------------ |
177 | | class BinaryOutputArchive { |
178 | | public: |
179 | 601 | BinaryOutputArchive(const char *file_path) { |
180 | 601 | os_ = new std::ofstream(file_path, std::ofstream::out | |
181 | 601 | std::ofstream::trunc | |
182 | 601 | std::ofstream::binary); |
183 | 601 | destruct_ = [this]() { delete os_; }; |
184 | 601 | } |
185 | | |
186 | 0 | BinaryOutputArchive(std::ostream &os) : os_(&os) {} |
187 | | |
188 | 601 | ~BinaryOutputArchive() { |
189 | 601 | if (destruct_) { |
190 | 601 | destruct_(); |
191 | 601 | } |
192 | 601 | } |
193 | | |
194 | | BinaryOutputArchive(const BinaryOutputArchive&) = delete; |
195 | | BinaryOutputArchive& operator=(const BinaryOutputArchive&) = delete; |
196 | | |
197 | 3.50k | bool saveBinary(const void *p, size_t sz) { |
198 | 3.50k | os_->write(reinterpret_cast<const char*>(p), (std::streamsize)sz); |
199 | 3.50k | return true; |
200 | 3.50k | } |
201 | | |
202 | | template<typename V> |
203 | | typename std::enable_if<type_traits_internal::IsTriviallyCopyable<V>::value, bool>::type |
204 | | saveBinary(const V& v) { |
205 | | os_->write(reinterpret_cast<const char *>(&v), sizeof(V)); |
206 | | return true; |
207 | | } |
208 | | |
209 | | template<typename Map> |
210 | | auto saveBinary(const Map& v) -> decltype(v.phmap_dump(*this), bool()) |
211 | | { |
212 | | return v.phmap_dump(*this); |
213 | | } |
214 | | |
215 | | private: |
216 | | std::ostream* os_; |
217 | | std::function<void()> destruct_; |
218 | | }; |
219 | | |
220 | | |
221 | | class BinaryInputArchive { |
222 | | public: |
223 | 601 | BinaryInputArchive(const char * file_path) { |
224 | 601 | is_ = new std::ifstream(file_path, |
225 | 601 | std::ifstream::in | std::ifstream::binary); |
226 | 601 | destruct_ = [this]() { delete is_; }; |
227 | 601 | } |
228 | | |
229 | 0 | BinaryInputArchive(std::istream& is) : is_(&is) {} |
230 | | |
231 | 601 | ~BinaryInputArchive() { |
232 | 601 | if (destruct_) { |
233 | 601 | destruct_(); |
234 | 601 | } |
235 | 601 | } |
236 | | |
237 | | BinaryInputArchive(const BinaryInputArchive&) = delete; |
238 | | BinaryInputArchive& operator=(const BinaryInputArchive&) = delete; |
239 | | |
240 | 1.75k | bool loadBinary(void* p, size_t sz) { |
241 | 1.75k | is_->read(reinterpret_cast<char*>(p), (std::streamsize)sz); |
242 | 1.75k | return true; |
243 | 1.75k | } |
244 | | |
245 | | template<typename V> |
246 | | typename std::enable_if<type_traits_internal::IsTriviallyCopyable<V>::value, bool>::type |
247 | | loadBinary(V* v) { |
248 | | is_->read(reinterpret_cast<char *>(v), sizeof(V)); |
249 | | return true; |
250 | | } |
251 | | |
252 | | template<typename Map> |
253 | | auto loadBinary(Map* v) -> decltype(v->phmap_load(*this), bool()) |
254 | | { |
255 | | return v->phmap_load(*this); |
256 | | } |
257 | | |
258 | | private: |
259 | | std::istream* is_; |
260 | | std::function<void()> destruct_; |
261 | | }; |
262 | | |
263 | | } // namespace phmap |
264 | | |
265 | | |
266 | | #ifdef CEREAL_SIZE_TYPE |
267 | | |
268 | | template <class T> |
269 | | using PhmapTrivCopyable = typename phmap::type_traits_internal::IsTriviallyCopyable<T>; |
270 | | |
271 | | namespace cereal |
272 | | { |
273 | | // Overload Cereal serialization code for phmap::flat_hash_map |
274 | | // ----------------------------------------------------------- |
275 | | template <class K, class V, class Hash, class Eq, class A> |
276 | | void save(typename std::enable_if<PhmapTrivCopyable<K>::value && PhmapTrivCopyable<V>::value, typename cereal::BinaryOutputArchive>::type &ar, |
277 | | phmap::flat_hash_map<K, V, Hash, Eq, A> const &hmap) |
278 | | { |
279 | | hmap.phmap_dump(ar); |
280 | | } |
281 | | |
282 | | template <class K, class V, class Hash, class Eq, class A> |
283 | | void load(typename std::enable_if<PhmapTrivCopyable<K>::value && PhmapTrivCopyable<V>::value, typename cereal::BinaryInputArchive>::type &ar, |
284 | | phmap::flat_hash_map<K, V, Hash, Eq, A> &hmap) |
285 | | { |
286 | | hmap.phmap_load(ar); |
287 | | } |
288 | | |
289 | | |
290 | | // Overload Cereal serialization code for phmap::parallel_flat_hash_map |
291 | | // -------------------------------------------------------------------- |
292 | | template <class K, class V, class Hash, class Eq, class A, size_t N, class Mtx_> |
293 | | void save(typename std::enable_if<PhmapTrivCopyable<K>::value && PhmapTrivCopyable<V>::value, typename cereal::BinaryOutputArchive>::type &ar, |
294 | | phmap::parallel_flat_hash_map<K, V, Hash, Eq, A, N, Mtx_> const &hmap) |
295 | | { |
296 | | hmap.phmap_dump(ar); |
297 | | } |
298 | | |
299 | | template <class K, class V, class Hash, class Eq, class A, size_t N, class Mtx_> |
300 | | void load(typename std::enable_if<PhmapTrivCopyable<K>::value && PhmapTrivCopyable<V>::value, typename cereal::BinaryInputArchive>::type &ar, |
301 | | phmap::parallel_flat_hash_map<K, V, Hash, Eq, A, N, Mtx_> &hmap) |
302 | | { |
303 | | hmap.phmap_load(ar); |
304 | | } |
305 | | |
306 | | // Overload Cereal serialization code for phmap::flat_hash_set |
307 | | // ----------------------------------------------------------- |
308 | | template <class K, class Hash, class Eq, class A> |
309 | | void save(typename std::enable_if<PhmapTrivCopyable<K>::value, typename cereal::BinaryOutputArchive>::type &ar, |
310 | | phmap::flat_hash_set<K, Hash, Eq, A> const &hset) |
311 | | { |
312 | | hset.phmap_dump(ar); |
313 | | } |
314 | | |
315 | | template <class K, class Hash, class Eq, class A> |
316 | | void load(typename std::enable_if<PhmapTrivCopyable<K>::value, typename cereal::BinaryInputArchive>::type &ar, |
317 | | phmap::flat_hash_set<K, Hash, Eq, A> &hset) |
318 | | { |
319 | | hset.phmap_load(ar); |
320 | | } |
321 | | |
322 | | // Overload Cereal serialization code for phmap::parallel_flat_hash_set |
323 | | // -------------------------------------------------------------------- |
324 | | template <class K, class Hash, class Eq, class A, size_t N, class Mtx_> |
325 | | void save(typename std::enable_if<PhmapTrivCopyable<K>::value, typename cereal::BinaryOutputArchive>::type &ar, |
326 | | phmap::parallel_flat_hash_set<K, Hash, Eq, A, N, Mtx_> const &hset) |
327 | | { |
328 | | hset.phmap_dump(ar); |
329 | | } |
330 | | |
331 | | template <class K, class Hash, class Eq, class A, size_t N, class Mtx_> |
332 | | void load(typename std::enable_if<PhmapTrivCopyable<K>::value, typename cereal::BinaryInputArchive>::type &ar, |
333 | | phmap::parallel_flat_hash_set<K, Hash, Eq, A, N, Mtx_> &hset) |
334 | | { |
335 | | hset.phmap_load(ar); |
336 | | } |
337 | | } |
338 | | |
339 | | #endif |
340 | | |
341 | | |
342 | | |
343 | | |
344 | | #endif // phmap_dump_h_guard_ |