-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathLATfield2_save_hdf5.h
executable file
·384 lines (276 loc) · 10.5 KB
/
LATfield2_save_hdf5.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
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/*! \file LATfield2_save_hdf5.h
\brief LATfield2_save_hdf5.h contains the definition of the function used for hdf5 i/o.
\author David Daverio
*/
extern "C"{
#include <math.h>
#include <hdf5.h>
#include <stdlib.h>
#include <string.h>
int save_hdf5_externC(char *data,long file_offset[2],int *size,int * sizeLocal,int halo, int lat_dim,int comp,hid_t array_type,int array_size,string filename_str, string dataset_name_str)
{
hid_t file_id, plist_id,filespace,memspace,dset_id,dtype_id,dtbase_id,root_id;
hsize_t * components;
char * filename;
filename = (char*)malloc((filename_str.size()+1)*sizeof(char));
for(int i = 0;i<filename_str.size();i++)filename[i]=filename_str[i];
filename[filename_str.size()] = '\0';
char dataset_name[128];
for(int i = 0;i<dataset_name_str.size();i++)dataset_name[i]=dataset_name_str[i];
dataset_name[dataset_name_str.size()] = '\0';
herr_t status;
hsize_t * sizeGlobal;
sizeGlobal = new hsize_t[lat_dim];
hsize_t * localSize;
localSize = new hsize_t[lat_dim];
hsize_t * offset;
offset = new hsize_t[lat_dim];
hsize_t * offsetf;
offsetf = new hsize_t[lat_dim];
hsize_t * count;
count = new hsize_t[lat_dim];
hsize_t haloSize = 2*halo;
for(int i=0;i<lat_dim;i++)
{
sizeGlobal[i]=size[lat_dim -1 - i];
localSize[i]=sizeLocal[lat_dim -1 - i]+haloSize;
count[i]=sizeLocal[lat_dim -1 -i];
offset[i]=halo;
offsetf[i]=0;
}
offsetf[0]=file_offset[0];
offsetf[1]=file_offset[1];
///////////////////////////////
// creat datatype
///////////////////////////////
if(comp == 1 && array_size ==1)
{
dtype_id = H5Tcopy(array_type);
status = H5Tset_order(dtype_id, DATA_ORDER);
components = new hsize_t[1]; //to be sure is allocated when freed
}
if(comp == 1 && array_size !=1)
{
components = new hsize_t[1];
components[0] = array_size;
dtbase_id = H5Tcopy(array_type);
status = H5Tset_order(dtbase_id, DATA_ORDER);
dtype_id = H5Tarray_create(dtbase_id,1,components);
}
if(comp != 1 && array_size ==1)
{
components = new hsize_t[1];
components[0] = comp;
dtbase_id = H5Tcopy(array_type);
status = H5Tset_order(dtbase_id, DATA_ORDER);
dtype_id = H5Tarray_create(dtbase_id,1,components);
}
if(comp != 1 && array_size !=1)
{
components = new hsize_t[2];
components[0] = array_size;
components[1] = comp;
dtbase_id = H5Tcopy(array_type);
status = H5Tset_order(dtbase_id, DATA_ORDER);
dtype_id = H5Tarray_create(dtbase_id,2,components);
}
///////////////////////////////
///////////////////////////////
#ifdef H5_HAVE_PARALLEL //Parallel version, H5_HAVE_PARALLEL definition is needed by hdf5 to run in parallel too !
MPI_Comm comm = parallel.lat_world_comm();
MPI_Info info = MPI_INFO_NULL;
//creat the file
plist_id = H5Pcreate(H5P_FILE_ACCESS);
H5Pset_fapl_mpio(plist_id, comm, info);
file_id = H5Fcreate(filename, H5F_ACC_TRUNC, H5P_DEFAULT, plist_id);
H5Pclose(plist_id);
filespace = H5Screate_simple(lat_dim,sizeGlobal,NULL);
memspace = H5Screate_simple(lat_dim,localSize,NULL);
plist_id = H5Pcreate(H5P_DATASET_CREATE);
//H5Pset_chunk(plist_id, lat_dim,count);
dset_id = H5Dcreate1(file_id, dataset_name, dtype_id, filespace,plist_id);
H5Pclose(plist_id);
H5Sclose(filespace);
//save data
filespace = H5Dget_space(dset_id);
status = H5Sselect_hyperslab(filespace, H5S_SELECT_SET, offsetf, NULL, count, NULL);
status = H5Sselect_hyperslab(memspace, H5S_SELECT_SET, offset, NULL, count, NULL);
plist_id = H5Pcreate(H5P_DATASET_XFER);
H5Pset_dxpl_mpio(plist_id, H5FD_MPIO_COLLECTIVE);
status = H5Dwrite(dset_id, dtype_id, memspace, filespace, plist_id, data);
H5Dclose(dset_id);
H5Sclose(filespace);
H5Sclose(memspace);
H5Pclose(plist_id);
H5Fclose(file_id);
free(filename);
return 1;
#else // serial version, without H5_HAVE_PARALLEL definition hdf5 will run in serial !
int mpi_size,mpi_rank,p;
mpi_size = parallel.size();
mpi_rank = parallel.rank();
//cout<<"rank: "<<mpi_rank<<" , calling save HDF5 extern c serial"<<endl;
if(mpi_rank==0)
{
plist_id = H5Pcreate(H5P_FILE_ACCESS);
file_id = H5Fcreate(filename, H5F_ACC_TRUNC, H5P_DEFAULT, plist_id);
H5Pclose(plist_id);
filespace = H5Screate_simple(lat_dim,sizeGlobal,NULL);
plist_id = H5Pcreate(H5P_DATASET_CREATE);
H5Pset_chunk(plist_id, lat_dim, sizeGlobal);
dset_id = H5Dcreate1(file_id, dataset_name, dtype_id, filespace,plist_id);
H5Pclose(plist_id);
H5Sclose(filespace);
H5Dclose(dset_id);
H5Fclose(file_id);
}
MPI_Barrier(parallel.lat_world_comm());
for(p=0;p < mpi_size;p++)
{
if(mpi_rank==p)
{
//cout<<"rank: "<<mpi_rank<<" , writting data"<<endl;
plist_id = H5Pcreate(H5P_FILE_ACCESS);
file_id = H5Fopen(filename,H5F_ACC_RDWR,plist_id);
H5Pclose(plist_id);
root_id = H5Gopen(file_id,"/",H5P_DEFAULT);
dset_id = H5Dopen(root_id, dataset_name, H5P_DEFAULT);
filespace = H5Dget_space(dset_id);
dtype_id = H5Dget_type(dset_id);
plist_id = H5Pcreate(H5P_DATASET_XFER);
memspace = H5Screate_simple(lat_dim,localSize,NULL);
int status;
status = H5Sselect_hyperslab(memspace, H5S_SELECT_SET, offset, NULL,count, NULL);
status = H5Sselect_hyperslab(filespace, H5S_SELECT_SET, offsetf, NULL,count, NULL);
status = H5Dwrite(dset_id, dtype_id, memspace, filespace, plist_id, data);
H5Pclose(plist_id);
H5Sclose(filespace);
H5Sclose(memspace);
H5Dclose(dset_id);
H5Gclose(root_id);
H5Fclose(file_id);
}
MPI_Barrier(parallel.lat_world_comm());
}
free(filename);
return 1;
#endif
return -1;
}
int load_hdf5_externC(char *data,long file_offset[2],int *size,int * sizeLocal,int halo, int lat_dim,string filename_str, string dataset_name_str)
{
hid_t file_id, plist_id, plistxfer_id,filespace,memspace,dset_id,dtype_id,dtbase_id,group_id,root_id;
char * filename;
filename = (char*)malloc((filename_str.size()+1)*sizeof(char));
for(int i = 0;i<filename_str.size();i++)filename[i]=filename_str[i];
filename[filename_str.size()] = '\0';
char dataset_name[128];
for(int i = 0;i<dataset_name_str.size();i++)dataset_name[i]=dataset_name_str[i];
dataset_name[dataset_name_str.size()] = '\0';
herr_t status;
hsize_t * sizeGlobal;
sizeGlobal = new hsize_t[lat_dim];
hsize_t * localSize;
localSize = new hsize_t[lat_dim];
hsize_t * offset;
offset = new hsize_t[lat_dim];
hsize_t * offsetf;
offsetf = new hsize_t[lat_dim];
hsize_t * count;
count = new hsize_t[lat_dim];
hsize_t haloSize = 2*halo;
for(int i=0;i<lat_dim;i++)
{
sizeGlobal[i]=size[lat_dim -1 - i];
localSize[i]=sizeLocal[lat_dim -1 - i]+haloSize;
count[i]=sizeLocal[lat_dim -1 -i];
offset[i]=halo;
offsetf[i]=0;
}
offsetf[0]=file_offset[0];
offsetf[1]=file_offset[1];
#ifdef H5_HAVE_PARALLEL //Parallel version, H5_HAVE_PARALLEL definition is needed by hdf5 to run in parallel too
MPI_Comm comm = parallel.lat_world_comm();
MPI_Info info = MPI_INFO_NULL;
plist_id = H5Pcreate(H5P_FILE_ACCESS);
H5Pset_fapl_mpio(plist_id, comm, info);
file_id = H5Fopen(filename,H5F_ACC_RDONLY,plist_id);
//H5Pclose(plist_id);
root_id = H5Gopen(file_id,"/",H5P_DEFAULT);
dset_id = H5Dopen(root_id, dataset_name, H5P_DEFAULT);
filespace = H5Dget_space(dset_id);
dtype_id = H5Dget_type(dset_id);
//plist_id = H5Pcreate(H5P_DATASET_XFER);
memspace = H5Screate_simple(lat_dim,localSize,NULL);
//// verifier si l "offset" ne doit pas tenire compte du ....
status = H5Sselect_hyperslab(filespace, H5S_SELECT_SET, offsetf, NULL, count, NULL);
status = H5Sselect_hyperslab(memspace, H5S_SELECT_SET, offset, NULL, count, NULL);
plistxfer_id = H5Pcreate(H5P_DATASET_XFER);
H5Pset_dxpl_mpio(plistxfer_id, H5FD_MPIO_COLLECTIVE);
status = H5Dread(dset_id, dtype_id, memspace, filespace, plistxfer_id, data);
H5Dclose(dset_id);
H5Gclose(root_id);
H5Sclose(filespace);
H5Sclose(memspace);
H5Pclose(plistxfer_id);
H5Fclose(file_id);
H5Pclose(plist_id);
return 1;
#else // serial version, without H5_HAVE_PARALLEL definition hdf5 will run in serial !
int mpi_size,mpi_rank,p;
//MPI_Comm_size(MPI_COMM_WORLD, &mpi_size);
//MPI_Comm_rank(MPI_COMM_WORLD, &mpi_rank);
mpi_size = parallel.size();
mpi_rank = parallel.rank();
for(p=0;p < mpi_size;p++)
{
if(mpi_rank==p)
{
plist_id = H5Pcreate(H5P_FILE_ACCESS);
file_id = H5Fopen(filename,H5F_ACC_RDONLY,plist_id);
if(file_id<0)
{
cout<<"process "<<p << ", cant open file: "<<filename_str<<endl;
parallel.abortForce();
}
H5Pclose(plist_id);
root_id = H5Gopen(file_id,"/",H5P_DEFAULT);
dset_id = H5Dopen(root_id, dataset_name, H5P_DEFAULT);
if(dset_id<0)
{
cout<<"process "<<p << ", cant open dataset : "
<<dataset_name_str<<" in file "<<filename_str<<endl;
parallel.abortForce();
}
filespace = H5Dget_space(dset_id);
dtype_id = H5Dget_type(dset_id);
plist_id = H5Pcreate(H5P_DATASET_XFER);
memspace = H5Screate_simple(lat_dim,localSize,NULL);
int status;
status = H5Sselect_hyperslab(memspace, H5S_SELECT_SET, offset, NULL,count, NULL);
status = H5Sselect_hyperslab(filespace, H5S_SELECT_SET, offsetf, NULL,count, NULL);
status = H5Dread(dset_id, dtype_id, memspace, filespace, plist_id, data);
H5Pclose(plist_id);
H5Sclose(filespace);
H5Sclose(memspace);
H5Dclose(dset_id);
H5Gclose(root_id);
H5Fclose(file_id);
}
MPI_Barrier(MPI_COMM_WORLD);
}
return 1;
#endif
return -1;
}
}
template<class fieldType>
int save_hdf5(fieldType *data,hid_t type_id,int array_size,long file_offset[2],int *size,int * sizeLocal,int halo, int lat_dim,int comp,string filename_str, string dataset_name_str)
{
return save_hdf5_externC((char*)data, file_offset, size, sizeLocal, halo, lat_dim, comp, type_id, array_size, filename_str, dataset_name_str);
}
template<class fieldType>
int load_hdf5(fieldType *data,long file_offset[2],int *size,int * sizeLocal,int halo, int lat_dim,int comp,string filename_str, string dataset_name_str)
{
return load_hdf5_externC( (char*) data, file_offset, size, sizeLocal, halo, lat_dim, filename_str, dataset_name_str);
}