-
Notifications
You must be signed in to change notification settings - Fork 62
/
Checkpoint.cpp
284 lines (223 loc) · 5.94 KB
/
Checkpoint.cpp
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#include <stdio.h>
#include "Checkpoint.hpp"
#include "io/binary_io.hpp"
#include "io/file_io.hpp"
using namespace std;
void Checkpoint::save_ml_tree()
{
if (ml_trees.empty() || loglh() > ml_trees.best_score())
best_models = models;
ml_trees.push_back(loglh(), tree);
}
void Checkpoint::save_bs_tree()
{
bs_trees.push_back(loglh(), tree);
}
void CheckpointManager::write(const std::string& ckp_fname) const
{
/* for now, only master thread in master MPI rank will write ckp file */
if (!ParallelContext::master())
return;
backup();
BinaryFileStream fs(ckp_fname, std::ios::out);
fs << _checkp;
remove_backup();
}
bool CheckpointManager::read(const std::string& ckp_fname)
{
if (sysutil_file_exists(ckp_fname))
{
try
{
BinaryFileStream fs(ckp_fname, std::ios::in);
fs >> _checkp;
return true;
}
catch (runtime_error& e)
{
_checkp.search_state = SearchState();
LOG_DEBUG << "Error reading checkpoint: " << e.what() << endl;
return false;
}
}
else
return false;
}
void CheckpointManager::remove()
{
if (sysutil_file_exists(_ckp_fname))
std::remove(_ckp_fname.c_str());
}
void CheckpointManager::backup() const
{
if (sysutil_file_exists(_ckp_fname))
std::rename(_ckp_fname.c_str(), backup_fname().c_str());
}
void CheckpointManager::remove_backup() const
{
if (sysutil_file_exists(backup_fname()))
std::remove(backup_fname().c_str());
}
SearchState& CheckpointManager::search_state()
{
if (_active)
return _checkp.search_state;
else
{
_empty_search_state = SearchState();
return _empty_search_state;
}
};
void CheckpointManager::reset_search_state()
{
ParallelContext::thread_barrier();
if (ParallelContext::master_thread())
_checkp.search_state = SearchState();
ParallelContext::thread_barrier();
};
void CheckpointManager::save_ml_tree()
{
if (ParallelContext::master_thread())
{
_checkp.save_ml_tree();
if (_active)
write();
}
}
void CheckpointManager::save_bs_tree()
{
if (ParallelContext::master_thread())
{
_checkp.save_bs_tree();
if (_active)
write();
}
}
void CheckpointManager::update_and_write(const TreeInfo& treeinfo)
{
if (!_active)
return;
if (ParallelContext::master_thread())
_updated_models.clear();
ParallelContext::barrier();
for (auto p: treeinfo.parts_master())
{
/* we will modify a global map -> define critical section */
ParallelContext::UniqueLock lock;
assign(_checkp.models.at(p), treeinfo, p);
/* remember which models were updated but this rank ->
* will be used later to collect them at the master */
_updated_models.insert(p);
}
ParallelContext::barrier();
if (ParallelContext::num_ranks() > 1)
gather_model_params();
if (ParallelContext::master())
{
assign_tree(_checkp, treeinfo);
write();
}
}
void CheckpointManager::gather_model_params()
{
/* send callback -> worker ranks */
auto worker_cb = [this](void * buf, size_t buf_size) -> int
{
BinaryStream bs((char*) buf, buf_size);
bs << _updated_models.size();
for (auto p: _updated_models)
{
bs << p << _checkp.models.at(p);
}
return (int) bs.pos();
};
/* receive callback -> master rank */
auto master_cb = [this](void * buf, size_t buf_size)
{
BinaryStream bs((char*) buf, buf_size);
auto model_count = bs.get<size_t>();
for (size_t m = 0; m < model_count; ++m)
{
size_t part_id;
bs >> part_id;
// read parameter estimates from binary stream
bs >> _checkp.models[part_id];
}
};
ParallelContext::mpi_gather_custom(worker_cb, master_cb);
}
BasicBinaryStream& operator<<(BasicBinaryStream& stream, const Checkpoint& ckp)
{
stream << ckp.version;
// NB: accumulated runtime from past runs + current elapsed time
stream << ckp.elapsed_seconds + global_timer().elapsed_seconds();
stream << ckp.search_state;
stream << ckp.tree.topology();
stream << ckp.models.size();
for (const auto& m: ckp.models)
stream << m.first << m.second;
stream << ckp.ml_trees;
stream << ckp.bs_trees;
return stream;
}
BasicBinaryStream& operator>>(BasicBinaryStream& stream, Checkpoint& ckp)
{
stream >> ckp.version;
if (ckp.version < RAXML_CKP_MIN_SUPPORTED_VERSION || ckp.version > RAXML_CKP_VERSION)
{
throw runtime_error("Unsupported checkpoint file version!");
}
stream >> ckp.elapsed_seconds;
stream >> ckp.search_state;
// auto topol = fs.get<TreeTopology>();
//
// printf("READ topology size: %u\n", topol.size());
ckp.tree.topology(stream.get<TreeTopology>());
size_t num_models, part_id;
stream >> num_models;
assert(num_models == ckp.models.size());
for (size_t m = 0; m < num_models; ++m)
{
stream >> part_id;
stream >> ckp.models[part_id];
}
stream >> ckp.ml_trees;
stream >> ckp.bs_trees;
return stream;
}
void assign_tree(Checkpoint& ckp, const TreeInfo& treeinfo)
{
ckp.tree = treeinfo.tree();
}
void assign_model(Checkpoint& ckp, const TreeInfo& treeinfo, size_t index)
{
assign(ckp.models.at(index), treeinfo, index);
}
void assign_models(Checkpoint& ckp, const TreeInfo& treeinfo)
{
for (auto p: treeinfo.parts_master())
assign_model(ckp, treeinfo, p);
}
void assign_models(TreeInfo& treeinfo, const Checkpoint& ckp)
{
const pllmod_treeinfo_t& pll_treeinfo = treeinfo.pll_treeinfo();
for (auto& m: ckp.models)
{
if (!pll_treeinfo.partitions[m.first])
continue;
treeinfo.model(m.first, m.second);
}
}
void assign(Checkpoint& ckp, const TreeInfo& treeinfo)
{
assign_tree(ckp, treeinfo);
assign_models(ckp, treeinfo);
}
void assign(TreeInfo& treeinfo, const Checkpoint& ckp)
{
// TODO: it is currently not possible to change tree after pll_treeinfo has been created
// this should be fixed while doing refactoring to change pll_unode_t -> pll_tree_t
assert(0);
treeinfo.tree(ckp.tree);
assign_models(treeinfo, ckp);
}