-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtst_var_bput_varn.py
222 lines (187 loc) · 8.22 KB
/
tst_var_bput_varn.py
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
#
# Copyright (C) 2024, Northwestern University and Argonne National Laboratory
# See COPYRIGHT notice in top-level directory.
#
"""
This program test ibput_varn() method of `Variable` class, a nonblocking,
buffered API, by making multiple calls to it to write to different NetCDF
variables of an opened netCDF file. Each call also writes to multiple
subarrays of the same variable. This method is a buffered version of
iput_varn() and requires the user to first attach an internal buffer of size
equal to the sum of all requests using attach_buff() method of `File` class.
The library will internally invoke ncmpi_bput_varn() and ncmpi_attach_buffer()
in C.
"""
import numpy as np
from mpi4py import MPI
import os, sys
import pnetcdf
from utils import validate_nc_file
def run_test(format):
verbose = False
comm = MPI.COMM_WORLD
rank = comm.Get_rank()
nprocs = comm.Get_size()
# dimension sizes
ndims = 2
ydim = 4
xdim = 10
# allocate and initialize access patterns
max_num_subarray = 6
starts = np.zeros((max_num_subarray, ndims), dtype=np.int64)
counts = np.zeros((max_num_subarray, ndims), dtype=np.int64)
# only the first 4 ranks have non-zero access amount
if rank == 0:
# number of subarrays to request for each process
num_subarrays = 4
starts[0][0] = 0; starts[0][1] = 5; counts[0][0] = 1; counts[0][1] = 2
starts[1][0] = 1; starts[1][1] = 0; counts[1][0] = 1; counts[1][1] = 1
starts[2][0] = 2; starts[2][1] = 6; counts[2][0] = 1; counts[2][1] = 2
starts[3][0] = 3; starts[3][1] = 0; counts[3][0] = 1; counts[3][1] = 3
# rank 0 is writing the following locations: ("-" means skip)
# - - - - - 0 0 - - -
# 0 - - - - - - - - -
# - - - - - - 0 0 - -
# 0 0 0 - - - - - - -
elif rank == 1:
num_subarrays = 6
starts[0][0] = 0; starts[0][1] = 3; counts[0][0] = 1; counts[0][1] = 2
starts[1][0] = 0; starts[1][1] = 8; counts[1][0] = 1; counts[1][1] = 2
starts[2][0] = 1; starts[2][1] = 5; counts[2][0] = 1; counts[2][1] = 2
starts[3][0] = 2; starts[3][1] = 0; counts[3][0] = 1; counts[3][1] = 2
starts[4][0] = 2; starts[4][1] = 8; counts[4][0] = 1; counts[4][1] = 2
starts[5][0] = 3; starts[5][1] = 4; counts[5][0] = 1; counts[5][1] = 3
# rank 1 is writing the following locations: ("-" means skip)
# - - - 1 1 - - - 1 1
# - - - - - 1 1 - - -
# 1 1 - - - - - - 1 1
# - - - - 1 1 1 - - -
elif rank == 2:
num_subarrays = 5
starts[0][0] = 0; starts[0][1] = 7; counts[0][0] = 1; counts[0][1] = 1
starts[1][0] = 1; starts[1][1] = 1; counts[1][0] = 1; counts[1][1] = 3
starts[2][0] = 1; starts[2][1] = 7; counts[2][0] = 1; counts[2][1] = 3
starts[3][0] = 2; starts[3][1] = 2; counts[3][0] = 1; counts[3][1] = 1
starts[4][0] = 3; starts[4][1] = 3; counts[4][0] = 1; counts[4][1] = 1
# rank 2 is writing the following locations: ("-" means skip)
# - - - - - - - 2 - -
# - 2 2 2 - - - 2 2 2
# - - 2 - - - - - - -
# - - - 2 - - - - - -
elif rank == 3:
num_subarrays = 4
starts[0][0] = 0; starts[0][1] = 0; counts[0][0] = 1; counts[0][1] = 3
starts[1][0] = 1; starts[1][1] = 4; counts[1][0] = 1; counts[1][1] = 1
starts[2][0] = 2; starts[2][1] = 3; counts[2][0] = 1; counts[2][1] = 3
starts[3][0] = 3; starts[3][1] = 7; counts[3][0] = 1; counts[3][1] = 3
# rank 3 is writing the following locations: ("-" means skip)
# 3 3 3 - - - - - - -
# - - - - 3 - - - - -
# - - - 3 3 3 - - - -
# - - - - - - - 3 3 3
else:
num_subarrays = 0
# expected contents of variables created in the file
dataref = np.array([[3, 3, 3, 1, 1, 0, 0, 2, 1, 1],
[0, 2, 2, 2, 3, 1, 1, 2, 2, 2],
[1, 1, 2, 3, 3, 3, 0, 0, 1, 1],
[0, 0, 0, 2, 1, 1, 1, 3, 3, 3]], np.float32)
# total number of variables to be defined
num_vars = 20
# number of bput requests
num_reqs = num_vars // 2 if rank < 4 else 0
# allocate write buffer
buf_len = 0
for i in range(num_subarrays):
w_req_len = np.prod(counts[i,:])
buf_len += w_req_len
data = np.empty(buf_len, dtype=np.float32)
data.fill(rank)
# construct output file name
file_name = "tst_var_bput_varn"
if format == '64BIT_DATA':
file_ext = ".nc5"
elif format == '64BIT_OFFSET':
file_ext = ".nc2"
else:
file_ext = ".nc"
if (len(sys.argv) == 2) and os.path.isdir(sys.argv[1]):
name = os.path.join(sys.argv[1], file_name)
file_path = name + file_ext
else:
file_path = file_name + file_ext
if verbose and rank == 0:
print("output file name: ", file_path)
# create a new file with selected file format
f = pnetcdf.File(filename=file_path, mode = 'w', format=format, comm=comm, info=None)
# define dimensions
dy = f.def_dim('y',ydim)
dx = f.def_dim('x',xdim)
if verbose and rank == 0:
print("define dimensions Y and X of sizes: ", ydim, ", ", xdim)
# estimate the buffer size to be used in buffered put requests
buffsize = num_reqs * data.nbytes
if buffsize > 0:
f.attach_buff(buffsize)
# check the size of attached buffer
assert(f.inq_buff_size() == buffsize)
# define netCDF variables
for i in range(num_vars):
v = f.def_var(f'data{i}', pnetcdf.NC_FLOAT, (dy, dx))
if verbose and rank == 0:
print("define ", num_vars, " variables of type NC_FLOAT")
# leave define mode
f.enddef()
# initialize contents of write buffers
for i in range(num_vars):
v = f.variables[f'data{i}']
v[:] = np.full((ydim, xdim), -1, dtype=np.float32)
# each process post num_reqs requests to write
req_ids = []
for i in range(num_reqs):
v = f.variables[f'data{i}']
assert(f.inq_buff_size() - f.inq_buff_usage() > 0)
# post the request to write multiple subarrays
req_id = v.bput_varn(data, num_subarrays, starts, counts)
# track the request ID for each write request
req_ids.append(req_id)
# commit the posted requests all at once using wait_all (collective i/o)
req_errs = [None] * num_reqs
f.wait_all(num_reqs, req_ids, req_errs)
# check request error msg for each unsuccessful requests
for i in range(num_reqs):
if pnetcdf.strerrno(req_errs[i]) != "NC_NOERR":
print(f"Error on request {i}:", pnetcdf.strerror(req_errs[i]))
# post requests to write the 2nd half of variables w/o tracking req ids
for i in range(num_reqs):
v = f.variables[f'data{num_reqs + i}']
v.bput_varn(data, num_subarrays, starts, counts)
# commit the posted requests all at once using wait_all (collective i/o)
f.wait_all(num = pnetcdf.NC_PUT_REQ_ALL)
# release the internal buffer
if buffsize > 0:
f.detach_buff()
# close the file
f.close()
if rank == 0:
if verbose:
print("check if the newly created file is a valid NetCDF file")
if os.environ.get('PNETCDF_DIR') is not None:
assert validate_nc_file(os.environ.get('PNETCDF_DIR'), file_path) == 0
if nprocs >= 4:
if verbose:
print("check if the file contents are expected")
f = pnetcdf.File(file_path, 'r', comm=MPI.COMM_SELF)
for i in range(num_vars):
v = f.variables[f'data{i}']
np.testing.assert_array_equal(v[:], dataref)
f.close()
if __name__ == '__main__':
# test CDF-1, CDF-2, CDF-5 classic NetCDF file formats
file_formats = ['NC_64BIT_DATA', 'NC_64BIT_OFFSET', None]
for i in range(len(file_formats)):
try:
run_test(file_formats[i])
except BaseException as err:
print("Error: type:", type(err), str(err))
raise