-
Notifications
You must be signed in to change notification settings - Fork 6
/
write.c
311 lines (251 loc) · 8.9 KB
/
write.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
308
309
310
311
//
// Writes Gadget binary file
//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <assert.h>
#include "msg.h"
#include "comm.h"
#include "write.h"
#include "gadget_file.h"
void write_snapshot(const char filebase[], Snapshot const * const snapshot,
int use_long_id)
{
const double h= snapshot->h;
char filename[256];
sprintf(filename, "%s.%d", filebase, comm_this_node());
FILE* fp= fopen(filename, "w");
if(fp == 0)
msg_abort(9000, "Error: Unable to write to file: %s\n", filename);
ParticleMinimum* const p= snapshot->p;
const int np= snapshot->np_local;
const double boxsize= snapshot->boxsize;
const double omega_m= snapshot->omega_m;
if(use_long_id)
msg_printf(normal, "Longid is used for GADGET snapshot. %d-byte.\n",
sizeof(unsigned long long));
else
msg_printf(normal, "ID is %d-byte unsigned int\n", sizeof(unsigned int));
long long np_send= np, np_total;
MPI_Reduce(&np_send, &np_total, 1, MPI_LONG_LONG, MPI_SUM, 0, MPI_COMM_WORLD);
MPI_Bcast(&np_total, 1, MPI_LONG_LONG, 0, MPI_COMM_WORLD);
GadgetHeader header; assert(sizeof(GadgetHeader) == 256);
memset(&header, 0, sizeof(GadgetHeader));
const double rho_crit = 27.7455;
const double m= omega_m*rho_crit*pow(boxsize, 3.0)/np_total;
header.np[1]= np;
header.mass[1]= m;
header.time= snapshot->a;
header.redshift= 1.0/header.time - 1;
header.np_total[1]= (unsigned int) np_total;
header.np_total_highword[1]= (unsigned int) (np_total >> 32);
header.num_files= comm_nnode();
header.boxsize= boxsize;
header.omega0= omega_m;
header.omega_lambda= 1.0 - omega_m;
header.hubble_param= h;
int blklen= sizeof(GadgetHeader);
fwrite(&blklen, sizeof(blklen), 1, fp);
fwrite(&header, sizeof(GadgetHeader), 1, fp);
fwrite(&blklen, sizeof(blklen), 1, fp);
// position
blklen= np*sizeof(float)*3;
fwrite(&blklen, sizeof(blklen), 1, fp);
for(int i=0; i<np; i++)
fwrite(p[i].x, sizeof(float), 3, fp);
fwrite(&blklen, sizeof(blklen), 1, fp);
// velocity
const float vfac= 1.0/sqrt(snapshot->a); // Gadget convention
fwrite(&blklen, sizeof(blklen), 1, fp);
for(int i=0; i<np; i++) {
float vout[]= {vfac*p[i].v[0], vfac*p[i].v[1], vfac*p[i].v[2]};
fwrite(vout, sizeof(float), 3, fp);
}
fwrite(&blklen, sizeof(blklen), 1, fp);
// id
if(use_long_id) {
blklen= np*sizeof(unsigned long long);
fwrite(&blklen, sizeof(blklen), 1, fp);
for(int i=0; i<np; i++) {
unsigned long long id_out= p[i].id;
fwrite(&id_out, sizeof(unsigned long long), 1, fp);
}
}
else {
blklen= np*sizeof(unsigned int);
fwrite(&blklen, sizeof(blklen), 1, fp);
for(int i=0; i<np; i++) {
unsigned int id_out= p[i].id;
fwrite(&id_out, sizeof(unsigned int), 1, fp);
}
}
fwrite(&blklen, sizeof(blklen), 1, fp);
fclose(fp);
msg_printf(normal, "snapshot %s written\n", filebase);
}
// Writing Gadget file for subsampled particles
void write_snapshot1(const char filename[], Snapshot const * const snapshot)
{
FILE* fp= fopen(filename, "w");
if(fp == 0)
msg_abort(9000, "Error: Unable to write to file: %s\n", filename);
ParticleMinimum* const p= snapshot->p;
const int np= snapshot->np_local;
const double boxsize= snapshot->boxsize;
const double omega_m= snapshot->omega_m;
GadgetHeader header; assert(sizeof(GadgetHeader) == 256);
memset(&header, 0, sizeof(GadgetHeader));
const double rho_crit = 27.7455;
const double m= omega_m*rho_crit*pow(boxsize, 3.0)/np;
header.np[1]= np;
header.mass[1]= m;
header.time= snapshot->a;
header.redshift= 1.0/header.time - 1;
header.np_total[1]= (unsigned int) np;
header.np_total_highword[1]= 0;
header.num_files= comm_nnode();
header.boxsize= boxsize;
header.omega0= omega_m;
header.omega_lambda= 1.0 - omega_m;
header.hubble_param= snapshot->h;
int blklen= sizeof(GadgetHeader);
fwrite(&blklen, sizeof(blklen), 1, fp);
fwrite(&header, sizeof(GadgetHeader), 1, fp);
fwrite(&blklen, sizeof(blklen), 1, fp);
// position
blklen= np*sizeof(float)*3;
fwrite(&blklen, sizeof(blklen), 1, fp);
for(int i=0; i<np; i++)
fwrite(p[i].x, sizeof(float), 3, fp);
fwrite(&blklen, sizeof(blklen), 1, fp);
// velocity
const float vfac= 1.0/sqrt(snapshot->a); // Gadget convention
fwrite(&blklen, sizeof(blklen), 1, fp);
for(int i=0; i<np; i++) {
float vout[]= {vfac*p[i].v[0], vfac*p[i].v[1], vfac*p[i].v[2]};
fwrite(vout, sizeof(float), 3, fp);
}
fwrite(&blklen, sizeof(blklen), 1, fp);
// id
blklen= np*sizeof(int);
fwrite(&blklen, sizeof(blklen), 1, fp);
for(int i=0; i<np; i++) {
int id_out= p[i].id;
fwrite(&id_out, sizeof(int), 1, fp);
}
fwrite(&blklen, sizeof(blklen), 1, fp);
fclose(fp);
msg_printf(normal, "subsample %s written\n", filename);
}
//
// Write particle force
//
void write_force(const char filebase[], Particles const * const particles)
{
char filename[256];
int inode= comm_this_node();
sprintf(filename, "%s.%d", filebase, inode);
FILE* fp= fopen(filename, "w");
if(fp == 0) {
msg_abort(9010, "Unable to write force to %s\n", filename);
}
Particle const * const p= particles->p;
float3 * const f= particles->force;
const int np= particles->np_local;
fwrite(&np, sizeof(int), 1, fp);
for(int i=0; i<np; i++) {
int id= (int) p[i].id;
fwrite(&id, sizeof(int), 1, fp);
fwrite(p[i].x, sizeof(float), 3, fp);
fwrite(f[i], sizeof(float), 3, fp);
}
fwrite(&np, sizeof(int), 1, fp);
fclose(fp);
}
// Writes binary file for subsampled particles. Old version, not in use.
void write_particles_binary(const char filename[], Snapshot const * const snapshot)
{
FILE* fp= fopen(filename, "w");
if(fp == 0)
msg_abort(9000, "Error: Unable to write to file: %s\n", filename);
ParticleMinimum* const p= snapshot->p;
const int np= snapshot->np_local;
const float boxsize= snapshot->boxsize;
const float omega_m= snapshot->omega_m;
const double rho_crit = 27.7455;
const float m= omega_m*rho_crit*pow(boxsize, 3.0)/np;
const float redshift= 1.0/snapshot->a - 1.0;
// Header 6 floats
fwrite(&snapshot->boxsize, sizeof(float), 1, fp);
fwrite(&m, sizeof(float), 1, fp);
fwrite(&snapshot->omega_m, sizeof(float), 1, fp);
fwrite(&snapshot->h, sizeof(float), 1, fp);
fwrite(&snapshot->a, sizeof(float), 1, fp);
fwrite(&redshift, sizeof(float), 1, fp);
fwrite(&np, sizeof(int), 1, fp);
// positions, velocities
for(int i=0; i<np; i++) {
fwrite(p[i].x, sizeof(float), 3, fp);
fwrite(p[i].v, sizeof(float), 3, fp);
}
fwrite(&np, sizeof(int), 1, fp);
int ret= fclose(fp); assert(ret == 0);
msg_printf(normal, "subsample binary %s written\n", filename);
}
// Writes binary file for subsampled particles
// Newer version using Parallel write MPI_File_write_at
void write_particles_binary_mpi(const char filename[], ParticleSubsample const * const p, const int np, float const * const header)
{
//
// Gather number of particles to compute the offset for writing
//
const int this_node= comm_this_node();
const int nnode= comm_nnode();
int* const np_local= malloc(sizeof(int)*nnode); assert(np_local);
int ret=
MPI_Gather(&np, 1, MPI_INT, np_local, 1, MPI_INT, 0, MPI_COMM_WORLD);
assert(ret == MPI_SUCCESS);
int np_total= 0; // Total number of subsample particles
if(this_node == 0) {
for(int i=0; i<nnode; i++) {
int np_i= np_local[i];
np_local[i]= np_total;
// np_local <- partial_sum = Sum_{inode}^{this node - 1} np
np_total += np_i;
}
}
int np_partial_sum= 0;
ret= MPI_Scatter(np_local, 1, MPI_INT,
&np_partial_sum, 1, MPI_INT, 0, MPI_COMM_WORLD);
assert(ret == MPI_SUCCESS);
const size_t nfloat_header= 6;
size_t offset= sizeof(float)*nfloat_header + sizeof(int) +
sizeof(ParticleSubsample)*np_partial_sum;
// Write particles to a file
MPI_File fh;
MPI_Status stat;
ret= MPI_File_open(MPI_COMM_WORLD, filename,
MPI_MODE_CREATE | MPI_MODE_WRONLY,
MPI_INFO_NULL, &fh); assert(ret == MPI_SUCCESS);
if(this_node == 0) {
// header: boxsize, m_particle, omega_m, h, a, redshift & np_total
ret= MPI_File_write_at(fh, 0, header, sizeof(float)*nfloat_header,
MPI_BYTE, &stat); assert(ret == MPI_SUCCESS);
ret= MPI_File_write_at(fh, sizeof(float)*nfloat_header, &np_total,
sizeof(int), MPI_BYTE, &stat); assert(ret == MPI_SUCCESS);
}
assert(sizeof(ParticleSubsample) % sizeof(float) == 0);
MPI_File_write_at(fh, offset, p, sizeof(ParticleSubsample)*np,
MPI_BYTE, &stat);
if(this_node == 0) {
// footer: np_total
size_t offset_footer= sizeof(float)*nfloat_header + sizeof(int) +
sizeof(ParticleSubsample)*np_total;
ret= MPI_File_write_at(fh, offset_footer, &np_total, sizeof(int),
MPI_BYTE, &stat); assert(ret == MPI_SUCCESS);
}
ret= MPI_File_close(&fh); assert(ret == MPI_SUCCESS);
msg_printf(verbose, "Subsampled particles %d written\n", np_total);
}