forked from hxmhuang/OpenArray_Dev
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIO.cpp
More file actions
230 lines (182 loc) · 6.42 KB
/
IO.cpp
File metadata and controls
230 lines (182 loc) · 6.42 KB
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
/*
* IO.cpp
* use pnetcdf to support parallel IO
*
=======================================================*/
#include "IO.hpp"
#include "utils/utils.hpp"
#include "pnetcdf.h"
#include "armadillo"
#include "Function.hpp"
#include "Partition.hpp"
#include <assert.h>
#define CHECK_ERR(status) \
if (status != NC_NOERR) { \
fprintf(stderr, "error found line:%d, msg : %s\n", \
__LINE__, ncmpi_strerror(status)); \
exit(-1); \
}
namespace oa {
namespace io {
void save(const ArrayPtr& A,
const std::string& filename,
const std::string& varname) {
DataType dt = A->get_data_type();
int ncid;
MPI_Comm comm = A->get_partition()->get_comm();
Shape arr_shape = A->shape();
int status =
ncmpi_create(comm, filename.c_str(),
NC_64BIT_OFFSET, MPI_INFO_NULL, &ncid);
assert(status == NC_NOERR);
int gx = A->shape()[0];
int gy = A->shape()[1];
int gz = A->shape()[2];
int dimid[3];
ncmpi_def_dim(ncid, "x", gx, &dimid[2]);
ncmpi_def_dim(ncid, "y", gy, &dimid[1]);
ncmpi_def_dim(ncid, "z", gz, &dimid[0]);
int varid;
int err;
switch(dt) {
case(DATA_INT):
err = ncmpi_def_var(ncid, varname.c_str(),
NC_INT, 3, dimid, &varid);
CHECK_ERR(err);
break;
case(DATA_FLOAT):
err = ncmpi_def_var(ncid, varname.c_str(),
NC_FLOAT, 3, dimid, &varid);
CHECK_ERR(err);
break;
case(DATA_DOUBLE):
err = ncmpi_def_var(ncid, varname.c_str(),
NC_DOUBLE, 3, dimid, &varid);
CHECK_ERR(err);
break;
}
ncmpi_enddef(ncid);
MPI_Offset start[3], count[3];
start[2] = A->get_local_box().get_range_x().get_lower();
start[1] = A->get_local_box().get_range_y().get_lower();
start[0] = A->get_local_box().get_range_z().get_lower();
count[2] = A->local_shape()[0];
count[1] = A->local_shape()[1];
count[0] = A->local_shape()[2];
bool has_valid_data = count[0] * count[1] * count[2] > 0;
const int bsx = A->buffer_shape()[0];
const int bsy = A->buffer_shape()[1];
const int bsz = A->buffer_shape()[2];
// printf("dim: %d, %d, %d\n", gx, gy, gz);
// printf("start: %d, %d, %d\n", start[0],start[1],start[2]);
// printf("count: %d, %d, %d\n", count[0],count[1],count[2]);
// printf("stride: %d, %d, %d\n",stride[0],stride[1],stride[2]);
int sw = A->get_partition()->get_stencil_width();
cube_int c_int;
cube_float c_float;
cube_double c_double;
switch(dt) {
///:for T in [['INT','int'], ['FLOAT','float'], ['DOUBLE','double']]
case (DATA_${T[0]}$):
if(has_valid_data){
c_${T[1]}$ = utils::make_cube<${T[1]}$>(
A->buffer_shape(),
A->get_buffer())(arma::span(sw, bsx-sw-1),
arma::span(sw, bsy-sw-1),
arma::span(sw, bsz-sw-1));
err = ncmpi_put_vara_${T[1]}$_all(ncid, varid, start,
count, c_${T[1]}$.memptr());
CHECK_ERR(err);
}else{
start[0] = start[1] = start[2] = 0;
count[0] = count[1] = count[2] = 0;
err = ncmpi_put_vara_${T[1]}$_all(ncid, varid, start,
count, NULL);
CHECK_ERR(err);
}
break;
///:endfor
default:
break;
}
ncmpi_close(ncid);
}
ArrayPtr load(const std::string& filename,
const std::string& varname,
const MPI_Comm& comm) {
int status;
int ncid, varid;
nc_type var_type;
int var_dimids[3];
int ndim;
status = ncmpi_open(comm, filename.c_str(),
NC_NOWRITE, MPI_INFO_NULL, &ncid);
CHECK_ERR(status);
status = ncmpi_inq_varid(ncid, varname.c_str(), &varid);
CHECK_ERR(status);
status = ncmpi_inq_var(ncid, varid, 0, &var_type, &ndim,
var_dimids, NULL);
CHECK_ERR(status);
MPI_Offset gx, gy, gz;
status = ncmpi_inq_dimlen(ncid, var_dimids[0], &gz);
CHECK_ERR(status);
status = ncmpi_inq_dimlen(ncid, var_dimids[1], &gy);
CHECK_ERR(status);
status = ncmpi_inq_dimlen(ncid, var_dimids[2], &gx);
CHECK_ERR(status);
int sw = Partition::get_default_stencil_width();
ArrayPtr A;
int xs, ys, zs, xe, ye, ze;
MPI_Offset starts[3];
MPI_Offset counts[3];
cube_int c1_int, c2_int;
cube_float c1_float, c2_float;
cube_double c1_double, c2_double;
void* buf;
int bsx, bsy, bsz;
bool is_valid;
switch(var_type) {
///:for T in [['INT','int'], ['FLOAT','float'], ['DOUBLE','double']]
case(NC_${T[0]}$):
A = oa::funcs::zeros(comm, {int(gx), int(gy), int(gz)}, sw, DATA_${T[0]}$);
A->get_local_box().get_corners(xs, xe, ys, ye, zs, ze);
starts[0] = zs;
starts[1] = ys;
starts[2] = xs;
counts[0] = ze-zs;
counts[1] = ye-ys;
counts[2] = xe-xs;
is_valid = (counts[0] * counts[1] * counts[2] > 0);
if(is_valid){
c1_${T[1]}$ = oa::utils::make_cube<${T[1]}$>(
A->buffer_shape(),
A->get_buffer());
c2_${T[1]}$ = oa::utils::make_cube<${T[1]}$>(
A->local_shape());
status = ncmpi_get_vara_${T[1]}$_all(ncid, varid,
starts, counts,
c2_${T[1]}$.memptr());
CHECK_ERR(status);
bsx = A->buffer_shape()[0];
bsy = A->buffer_shape()[1];
bsz = A->buffer_shape()[2];
c1_${T[1]}$(arma::span(sw, bsx-sw-1),
arma::span(sw, bsy-sw-1),
arma::span(sw, bsz-sw-1)) = c2_${T[1]}$;
}else{
starts[0] = starts[1] = starts[2] = 0;
counts[0] = counts[1] = counts[2] = 0;
status = ncmpi_get_vara_${T[1]}$_all(ncid, varid,
starts, counts, NULL);
CHECK_ERR(status);
}
break;
///:endfor
default:
break;
}
ncmpi_close(ncid);
return A;
}
}
}