-
Notifications
You must be signed in to change notification settings - Fork 3
/
parallel_io.C
346 lines (265 loc) · 9.23 KB
/
parallel_io.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
#include "h5test.h"
#include "parallel_io.h"
#include "comm.h"
#include "cmdLineOptions.h"
#include <stdlib.h>
#include <iostream>
#include <string.h>
#include "measure.h"
#include "t3pio.h"
struct Var_t
{
const char * name;
const char * descript;
};
Var_t varT[] =
{
{"T", "Temp in K"},
{"p", "Pressure in N/m^2"},
{"u", "X Velocity in m/s"},
{"v", "Y Velocity in m/s"},
{"w", "Z Velocity in m/s"},
{"a", "A Velocity in m/s"},
{"b", "B Velocity in m/s"},
{"c", "C Velocity in m/s"},
{"d", "D Velocity in m/s"},
{"e", "E Velocity in m/s"},
};
ParallelIO::ParallelIO()
: m_t(0.0), m_rate(0.0), m_totalSz(1.0), m_nStripes(1),
m_nIOUnits(1), m_stripeSz(-1), m_numvar(1), m_aggregators(0),
m_dne_stripes(-1), m_auto_max_stripes(-1), m_stripesT3(-1)
{}
#ifndef USE_HDF5
void ParallelIO::h5writer(CmdLineOptions& cmd)
{
if (P.myProc == 0)
printf("This program requires HDF5 which is not available => quitting\n");
}
void ParallelIO::add_attribute(hid_t id, const char* descript, const char* value)
{
}
#else
void ParallelIO::h5writer(CmdLineOptions& cmd)
{
hid_t file_id; //file identifier
hid_t group_id; //group identifier
hid_t dset_id; //Dataset identifier
hid_t filespace; //Dataspace id in file
hid_t memspace; //Dataspace id in memory.
hid_t plist_id; //Property List id
hsize_t sz[1], gsz[1], starts[1], count[1], block[1], h5stride[1], rem;
hsize_t is, num;
int ierr;
MPI_Comm commF;
H5FD_mpio_xfer_t xfer_mode; // HDF5 transfer mode (indep or collective)
const char * fn = "UNSTRUCT.h5";
// compute size info
rem = cmd.globalSz % P.nProcs;
if (P.myProc < rem)
is = P.myProc * cmd.localSz;
else
is = (cmd.localSz + 1)*rem + cmd.localSz*(P.myProc - rem);
double lSz = 1.0;
m_numvar = cmd.nvar;
num = cmd.localSz;
lSz = num;
count[0] = 1;
h5stride[0] = 1;
starts[0] = is;
sz[0] = num;
gsz[0] = cmd.globalSz;
m_totalSz = cmd.globalSz*m_numvar*sizeof(double);
int iTotalSz = m_totalSz/(1024*1024);
// Initialize data buffer
double xk = num*P.myProc;
double *data = new double[num];
for (int i = 0; i < num; ++i)
data[i] = xk++;
double t0, t1, t2;
// Delete old file
if (P.myProc == 0)
MPI_File_delete((char * )fn, MPI_INFO_NULL);
MPI_Barrier(P.comm);
// Build MPI info;
MPI_Info info = MPI_INFO_NULL;
MPI_Info infoF = MPI_INFO_NULL;
MPI_Info_create(&info);
MPI_Info_create(&infoF);
T3PIO_results_t results;
if (cmd.useT3PIO)
{
int ierr = t3pio_set_info(P.comm, info, "./",
T3PIO_GLOBAL_SIZE, iTotalSz,
T3PIO_STRIPE_COUNT, cmd.stripes,
T3PIO_STRIPE_SIZE_MB, cmd.stripeSz,
T3PIO_MAX_AGGREGATORS, cmd.maxWriters,
T3PIO_RESULTS, &results);
m_nIOUnits = results.numIO;
m_dne_stripes = results.S_dne_stripes;
m_auto_max_stripes = results.S_auto_max_stripes;
m_nStripesT3 = results.nStripesT3;
}
xfer_mode = (cmd.collective) ? H5FD_MPIO_COLLECTIVE : H5FD_MPIO_INDEPENDENT;
t0 = walltime();
plist_id = H5Pcreate(H5P_FILE_ACCESS);
H5Pset_fapl_mpio(plist_id, P.comm, info);
// Create file collectively
file_id = H5Fcreate(fn, H5F_ACC_TRUNC, H5P_DEFAULT, plist_id);
MPI_File* pFH = NULL;
ierr = H5Fget_vfd_handle(file_id, H5P_DEFAULT, (void **) &pFH);
ierr = MPI_File_get_info(*pFH, &infoF);
t3pio_extract_key_values(infoF, &results);
m_aggregators = results.numIO;
m_nStripes = results.numStripes;
m_stripeSz = results.stripeSize;
H5Pclose(plist_id);
// Create Group
group_id = H5Gcreate(file_id, "Solution", H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
std::string timeZ, timeL;
dateZ(timeZ);
add_attribute(group_id,"Zulu Time", timeZ.c_str());
dateL(timeL);
add_attribute(group_id,"Local Time", timeL.c_str());
for (int ivar = 0; ivar < m_numvar; ++ivar)
{
// Create the dataspace for the dataset
filespace = H5Screate_simple(1, &gsz[0], NULL);
memspace = H5Screate_simple(1, sz, NULL);
if (cmd.h5chunk)
{
plist_id = H5Pcreate(H5P_DATASET_CREATE);
H5Pset_chunk(plist_id,1, sz);
dset_id = H5Dcreate(group_id, varT[ivar].name, H5T_NATIVE_DOUBLE, filespace,
H5P_DEFAULT, plist_id, H5P_DEFAULT);
H5Pclose(plist_id);
H5Sclose(filespace);
filespace = H5Dget_space(dset_id);
H5Sselect_hyperslab(filespace, H5S_SELECT_SET, starts, h5stride, count, sz);
}
else if (cmd.h5slab)
{
// Create the dataset w/ default properties and close filespace
dset_id = H5Dcreate(group_id, varT[ivar].name, H5T_NATIVE_DOUBLE, filespace,
H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
H5Sclose(filespace);
// Select hyperslab in the file.
filespace = H5Dget_space(dset_id);
H5Sselect_hyperslab(filespace, H5S_SELECT_SET, starts, NULL, sz , NULL);
}
plist_id = H5Pcreate(H5P_DATASET_XFER);
H5Pset_dxpl_mpio(plist_id, xfer_mode);
add_attribute(dset_id, "Variable Description", varT[ivar].descript);
t1 = walltime();
herr_t status = H5Dwrite(dset_id, H5T_NATIVE_DOUBLE, memspace, filespace,
plist_id, data);
t2 = walltime();
m_t += (t2 - t1);
H5Dclose(dset_id);
H5Sclose(filespace);
H5Sclose(memspace);
H5Pclose(plist_id);
}
m_totalTime = walltime() - t0;
m_rate = m_totalSz /(m_totalTime * 1024.0 * 1024.0);
free(data);
H5Gclose(group_id);
H5Fclose(file_id);
MPI_Info_free(&info);
MPI_Info_free(&infoF);
}
void ParallelIO::add_attribute(hid_t id, const char* descript, const char* value)
{
hid_t attr_id, aspace_id, atype_id;
hsize_t attrlen, num[1];
attrlen = strlen(value);
num[0] = 1;
atype_id = H5Tcopy(H5T_C_S1);
H5Tset_size(atype_id, attrlen);
aspace_id = H5Screate_simple(1,num, NULL);
attr_id = H5Acreate(id, descript, atype_id, aspace_id, H5P_DEFAULT, H5P_DEFAULT);
H5Awrite(attr_id, atype_id, value);
H5Aclose(attr_id);
H5Sclose(aspace_id);
}
#endif
void ParallelIO::MPIIOwriter(CmdLineOptions& cmd)
{
MPI_File fh;
MPI_Offset is, rem, offset;
MPI_Datatype coreData, gblData, my_vector;
MPI_Status status;
int iTotalSz, ierr, nDim;
int sz[2], gsz[2], starts[2];
const char* fn = "UNSTRUCT.mpiio";
rem = cmd.globalSz % P.nProcs;
if (P.myProc < rem)
is = P.myProc * cmd.localSz;
else
is = (cmd.localSz + 1)*rem + cmd.localSz*(P.myProc - rem);
m_numvar = 1;
m_totalSz = cmd.globalSz*m_numvar*sizeof(double);
iTotalSz = m_totalSz/(1024*1024);
int num = cmd.localSz;
double *data = new double[num];
double xk = is;
for (int i = 0; i < num; ++i)
data[i] = xk++;
double t0, t1, t2;
// Delete old file
if (P.myProc == 0)
MPI_File_delete((char * )fn, MPI_INFO_NULL);
MPI_Barrier(P.comm);
// Build MPI info;
MPI_Info info = MPI_INFO_NULL;
MPI_Info infoF = MPI_INFO_NULL;
MPI_Info_create(&info);
MPI_Info_create(&infoF);
T3PIO_results_t results;
if (cmd.useT3PIO)
{
int ierr = t3pio_set_info(P.comm, info, "./",
T3PIO_GLOBAL_SIZE, iTotalSz,
T3PIO_STRIPE_COUNT, cmd.stripes,
T3PIO_STRIPE_SIZE_MB, cmd.stripeSz,
T3PIO_MAX_AGGREGATORS, cmd.maxWriters,
T3PIO_RESULTS, &results);
m_nIOUnits = results.numIO;
}
//nDim = 1;
//offset = is*sizeof(double);
//ierr = MPI_Type_contiguous(num, MPI_DOUBLE, &my_vector);
//ierr = MPI_Type_commit(&my_vector);
//ierr = MPI_File_set_view(fh, offset, MPI_DOUBLE, my_vector, "native", info);
offset = 0;
nDim = 2;
sz[0] = cmd.localSz/cmd.xwidth;
sz[1] = cmd.xwidth;
gsz[0] = cmd.globalSz/cmd.xwidth;
gsz[1] = cmd.xwidth;
starts[0] = 0;
starts[1] = 0;
ierr = MPI_Type_create_subarray(nDim, sz, sz, starts, MPI_ORDER_C, MPI_DOUBLE,
&coreData);
ierr = MPI_Type_commit(&coreData);
starts[0] = sz[0]*P.myProc;
ierr = MPI_Type_create_subarray(nDim, gsz, sz, starts, MPI_ORDER_C, MPI_DOUBLE,
&gblData);
ierr = MPI_Type_commit(&gblData);
t0 = walltime();
ierr = MPI_File_open(P.comm, (char *) fn, MPI_MODE_WRONLY | MPI_MODE_CREATE, info, &fh);
if (ierr)
MPI_Abort(P.comm, -1);
ierr = MPI_File_get_info(fh, &infoF);
t3pio_extract_key_values(infoF, &results);
m_aggregators = results.numIO;
m_nStripes = results.numStripes;
m_stripeSz = results.stripeSize;
ierr = MPI_File_set_view(fh, offset, MPI_DOUBLE, gblData, "native", info);
ierr = MPI_File_write_all(fh, &data[0], 1, coreData, &status);
ierr = MPI_File_close(&fh);
m_totalTime = walltime() - t0;
m_rate = m_totalSz/(m_totalTime * 1024.0 * 1024.0);
MPI_Info_free(&info);
MPI_Info_free(&infoF);
}