-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserialization.h
80 lines (66 loc) · 2.41 KB
/
serialization.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
namespace serialization
{
// Calculate buffer size required for serialization
int64_t calculateSerializeMapSize(const std::unordered_map<std::string_view, int64_t>& words)
{
int64_t buffer_size = sizeof(int);
for(const auto& it : words)
{
buffer_size += sizeof(int) + it.first.size() + sizeof(int64_t);
}
return buffer_size;
}
// Serialize unordered_map in order to send via MPI_Send
void serializeMap(const std::unordered_map<std::string_view, int64_t>& words, char* buffer)
{
// Serialization buffer
char* current_ptr = buffer;
// Write the size of the unordered_map
int map_size = words.size();
std::memcpy(current_ptr, &map_size, sizeof(int));
current_ptr += sizeof(int);
// Write each serialized key-value pair
for(const auto& it : words)
{
const std::string_view& key = it.first;
int64_t value = it.second;
// Write key size
int key_size = key.size();
std::memcpy(current_ptr, &key_size, sizeof(int));
current_ptr += sizeof(int);
// Write key
std::memcpy(current_ptr, key.data(), key_size);
current_ptr += key_size;
// Write value
std::memcpy(current_ptr, &value, sizeof(int64_t));
current_ptr += sizeof(int64_t);
}
}
// Deserialize received buffer into unordered map
void deserializeBuffer(char* serial_buffer, std::unordered_map<std::string_view, int64_t>& words)
{
char* current_ptr = serial_buffer;
int map_size;
std::memcpy(&map_size, current_ptr, sizeof(int));
current_ptr += sizeof(int);
for(int i = 0; i < map_size; ++i)
{
// Read key size
int key_size;
std::memcpy(&key_size, current_ptr, sizeof(int));
current_ptr += sizeof(int);
// Read key string
std::string_view key(current_ptr, key_size);
current_ptr += key_size;
// Read value
int64_t value;
std::memcpy(&value, current_ptr, sizeof(int64_t));
current_ptr += sizeof(int64_t);
auto results = words.try_emplace(key, value);
if (!results.second)
{
results.first->second += value;
}
}
}
}