-
Notifications
You must be signed in to change notification settings - Fork 2
/
restart.c
307 lines (249 loc) · 8.31 KB
/
restart.c
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <mpi.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/file.h>
#include <unistd.h>
#include <gsl/gsl_rng.h>
#include "allvars.h"
#include "proto.h"
/*! \file restart.c
* \brief Code for reading and writing restart files
*/
static FILE *fd;
static void in(int *x, int modus);
static void byten(void *x, size_t n, int modus);
/*! This function reads or writes the restart files. Each processor writes
* its own restart file, with the I/O being done in parallel. To avoid
* congestion of the disks you can tell the program to restrict the number of
* files that are simultaneously written to NumFilesWrittenInParallel.
*
* If modus>0 the restart()-routine reads, if modus==0 it writes a restart
* file.
*/
void restart(int modus)
{
char buf[200], buf_bak[200], buf_mv[500];
double save_PartAllocFactor, save_TreeAllocFactor;
int i, nprocgroup, masterTask, groupTask, old_MaxPart, old_MaxNodes;
struct global_data_all_processes all_task0;
sprintf(buf, "%s%s.%d", All.OutputDir, All.RestartFile, ThisTask);
sprintf(buf_bak, "%s%s.%d.bak", All.OutputDir, All.RestartFile, ThisTask);
sprintf(buf_mv, "mv %s %s", buf, buf_bak);
if((NTask < All.NumFilesWrittenInParallel))
{
printf
("Fatal error.\nNumber of processors must be a smaller or equal than `NumFilesWrittenInParallel'.\n");
endrun(2131);
}
nprocgroup = NTask / All.NumFilesWrittenInParallel;
if((NTask % All.NumFilesWrittenInParallel))
{
nprocgroup++;
}
masterTask = (ThisTask / nprocgroup) * nprocgroup;
for(groupTask = 0; groupTask < nprocgroup; groupTask++)
{
if(ThisTask == (masterTask + groupTask)) /* ok, it's this processor's turn */
{
if(modus)
{
if(!(fd = fopen(buf, "r")))
{
printf("Restart file '%s' not found.\n", buf);
endrun(7870);
}
}
else
{
system(buf_mv); /* move old restart files to .bak files */
if(!(fd = fopen(buf, "w")))
{
printf("Restart file '%s' cannot be opened.\n", buf);
endrun(7878);
}
}
save_PartAllocFactor = All.PartAllocFactor;
save_TreeAllocFactor = All.TreeAllocFactor;
/* common data */
byten(&All, sizeof(struct global_data_all_processes), modus);
if(ThisTask == 0 && modus > 0)
all_task0 = All;
if(modus > 0 && groupTask == 0) /* read */
{
MPI_Bcast(&all_task0, sizeof(struct global_data_all_processes), MPI_BYTE, 0, MPI_COMM_WORLD);
}
old_MaxPart = All.MaxPart;
old_MaxNodes = All.TreeAllocFactor * All.MaxPart;
if(modus) /* read */
{
if(All.PartAllocFactor != save_PartAllocFactor)
{
All.PartAllocFactor = save_PartAllocFactor;
All.MaxPart = All.PartAllocFactor * (All.TotNumPart / NTask);
All.MaxPartSph = All.PartAllocFactor * (All.TotN_gas / NTask);
save_PartAllocFactor = -1;
}
if(All.TreeAllocFactor != save_TreeAllocFactor)
{
All.TreeAllocFactor = save_TreeAllocFactor;
save_TreeAllocFactor = -1;
}
if(all_task0.Time != All.Time)
{
printf("The restart file on task=%d is not consistent with the one on task=0\n", ThisTask);
fflush(stdout);
endrun(16);
}
allocate_memory();
}
in(&NumPart, modus);
if(NumPart > All.MaxPart)
{
printf
("it seems you have reduced(!) 'PartAllocFactor' below the value of %g needed to load the restart file.\n",
NumPart / (((double) All.TotNumPart) / NTask));
printf("fatal error\n");
endrun(22);
}
/* Particle data */
byten(&P[0], NumPart * sizeof(struct particle_data), modus);
in(&N_gas, modus);
if(N_gas > 0)
{
if(N_gas > All.MaxPartSph)
{
printf
("SPH: it seems you have reduced(!) 'PartAllocFactor' below the value of %g needed to load the restart file.\n",
N_gas / (((double) All.TotN_gas) / NTask));
printf("fatal error\n");
endrun(222);
}
/* Sph-Particle data */
byten(&SphP[0], N_gas * sizeof(struct sph_particle_data), modus);
}
/* write state of random number generator */
byten(gsl_rng_state(random_generator), gsl_rng_size(random_generator), modus);
/* now store relevant data for tree */
if(modus) /* read */
{
ngb_treeallocate(MAX_NGB);
force_treeallocate(All.TreeAllocFactor * All.MaxPart, All.MaxPart);
}
in(&Numnodestree, modus);
in(&NTopleaves, modus);
if(Numnodestree > MaxNodes)
{
printf
("Tree storage: it seems you have reduced(!) 'PartAllocFactor' below the value needed to load the restart file (task=%d). "
"Numnodestree=%d MaxNodes=%d\n", ThisTask, Numnodestree, MaxNodes);
endrun(221);
}
byten(Nodes_base, Numnodestree * sizeof(struct NODE), modus);
byten(Extnodes_base, Numnodestree * sizeof(struct extNODE), modus);
byten(Father, NumPart * sizeof(int), modus);
byten(Nextnode, NumPart * sizeof(int), modus);
byten(Nextnode + All.MaxPart, MAXTOPNODES * sizeof(int), modus);
byten(DomainStartList, NTask * sizeof(int), modus);
byten(DomainEndList, NTask * sizeof(int), modus);
byten(DomainTask, MAXTOPNODES * sizeof(int), modus);
byten(DomainNodeIndex, MAXTOPNODES * sizeof(int), modus);
byten(DomainTreeNodeLen, MAXTOPNODES * sizeof(FLOAT), modus);
byten(DomainHmax, MAXTOPNODES * sizeof(FLOAT), modus);
byten(DomainMoment, MAXTOPNODES * sizeof(struct DomainNODE), modus);
byten(DomainCorner, 3 * sizeof(double), modus);
byten(DomainCenter, 3 * sizeof(double), modus);
byten(&DomainLen, sizeof(double), modus);
byten(&DomainFac, sizeof(double), modus);
byten(&DomainMyStart, sizeof(int), modus);
byten(&DomainMyLast, sizeof(int), modus);
if(modus) /* read */
if(All.PartAllocFactor != save_PartAllocFactor || All.TreeAllocFactor != save_TreeAllocFactor)
{
for(i = 0; i < NumPart; i++)
Father[i] += (All.MaxPart - old_MaxPart);
for(i = 0; i < NumPart; i++)
if(Nextnode[i] >= old_MaxPart)
{
if(Nextnode[i] >= old_MaxPart + old_MaxNodes)
Nextnode[i] += (All.MaxPart - old_MaxPart) + (MaxNodes - old_MaxPart);
else
Nextnode[i] += (All.MaxPart - old_MaxPart);
}
for(i = 0; i < Numnodestree; i++)
{
if(Nodes_base[i].u.d.sibling >= old_MaxPart)
{
if(Nodes_base[i].u.d.sibling >= old_MaxPart + old_MaxNodes)
Nodes_base[i].u.d.sibling +=
(All.MaxPart - old_MaxPart) + (MaxNodes - old_MaxNodes);
else
Nodes_base[i].u.d.sibling += (All.MaxPart - old_MaxPart);
}
if(Nodes_base[i].u.d.father >= old_MaxPart)
{
if(Nodes_base[i].u.d.father >= old_MaxPart + old_MaxNodes)
Nodes_base[i].u.d.father += (All.MaxPart - old_MaxPart) + (MaxNodes - old_MaxNodes);
else
Nodes_base[i].u.d.father += (All.MaxPart - old_MaxPart);
}
if(Nodes_base[i].u.d.nextnode >= old_MaxPart)
{
if(Nodes_base[i].u.d.nextnode >= old_MaxPart + old_MaxNodes)
Nodes_base[i].u.d.nextnode +=
(All.MaxPart - old_MaxPart) + (MaxNodes - old_MaxNodes);
else
Nodes_base[i].u.d.nextnode += (All.MaxPart - old_MaxPart);
}
}
for(i = 0; i < MAXTOPNODES; i++)
if(Nextnode[i + All.MaxPart] >= old_MaxPart)
{
if(Nextnode[i + All.MaxPart] >= old_MaxPart + old_MaxNodes)
Nextnode[i + All.MaxPart] += (All.MaxPart - old_MaxPart) + (MaxNodes - old_MaxNodes);
else
Nextnode[i + All.MaxPart] += (All.MaxPart - old_MaxPart);
}
for(i = 0; i < MAXTOPNODES; i++)
if(DomainNodeIndex[i] >= old_MaxPart)
{
if(DomainNodeIndex[i] >= old_MaxPart + old_MaxNodes)
DomainNodeIndex[i] += (All.MaxPart - old_MaxPart) + (MaxNodes - old_MaxNodes);
else
DomainNodeIndex[i] += (All.MaxPart - old_MaxPart);
}
}
fclose(fd);
}
else /* wait inside the group */
{
if(modus > 0 && groupTask == 0) /* read */
{
MPI_Bcast(&all_task0, sizeof(struct global_data_all_processes), MPI_BYTE, 0, MPI_COMM_WORLD);
}
}
MPI_Barrier(MPI_COMM_WORLD);
}
}
/*! reads/writes n bytes in restart routine
*/
void byten(void *x, size_t n, int modus)
{
if(modus)
my_fread(x, n, 1, fd);
else
my_fwrite(x, n, 1, fd);
}
/*! reads/writes one `int' variable in restart routine
*/
void in(int *x, int modus)
{
if(modus)
my_fread(x, 1, sizeof(int), fd);
else
my_fwrite(x, 1, sizeof(int), fd);
}