25
25
26
26
comm = MPI .COMM_WORLD
27
27
rank = comm .Get_rank ()
28
- size = comm .Get_size ()
28
+ nprocs = comm .Get_size ()
29
29
xdim = 4
30
30
ydim = 10
31
31
95
95
[0 , 0 , 0 , 2 , 1 , 1 , 1 , 3 , 3 , 3 ]], np .float32 )
96
96
97
97
98
- # reference data for 1<=size<=3
99
- dataref [dataref >= size ] = - 1
100
98
# total number of put requests for the test programs
101
- num_reqs = 10
99
+ num_reqs = 10 if rank < 4 else 0
100
+ num_vars = 20
102
101
103
102
# allocate write buffer
104
103
buf_len = 0
@@ -121,11 +120,14 @@ def setUp(self):
121
120
dy = f .def_dim ('y' ,ydim )
122
121
123
122
# define 20 netCDF variables
124
- for i in range (num_reqs * 2 ):
123
+ for i in range (num_vars ):
125
124
v = f .def_var (f'data{ i } ' , pnetcdf .NC_FLOAT , (dx , dy ))
126
- # initialize variable values
125
+
126
+ # leaving define mode
127
127
f .enddef ()
128
- for i in range (num_reqs * 2 ):
128
+
129
+ # initialize variable values
130
+ for i in range (num_vars ):
129
131
v = f .variables [f'data{ i } ' ]
130
132
v [:] = np .full ((xdim , ydim ), - 1 , dtype = np .float32 )
131
133
@@ -137,41 +139,41 @@ def setUp(self):
137
139
req_id = v .iput_var (data , start = starts , count = counts , num = num_subarrays )
138
140
# track the reqeust ID for each write reqeust
139
141
req_ids .append (req_id )
140
- f . end_indep ()
142
+
141
143
# all processes commit those 10 requests to the file at once using wait_all (collective i/o)
142
144
req_errs = [None ] * num_reqs
143
145
f .wait_all (num_reqs , req_ids , req_errs )
146
+
144
147
# check request error msg for each unsuccessful requests
145
148
for i in range (num_reqs ):
146
149
if strerrno (req_errs [i ]) != "NC_NOERR" :
147
150
print (f"Error on request { i } :" , strerror (req_errs [i ]))
148
151
149
- # post 10 requests to write an array of values for the last 10 variables w/o tracking req ids
150
- for i in range (num_reqs , num_reqs * 2 ):
151
- v = f .variables [f'data{ i } ' ]
152
+ # post 10 requests to write an array of values for the last 10 variables w/o tracking req ids
153
+ for i in range (num_reqs ):
154
+ v = f .variables [f'data{ num_reqs + i } ' ]
152
155
# post the request to write an array of values
153
156
v .iput_var (data , start = starts , count = counts , num = num_subarrays )
154
157
155
158
# all processes commit all pending requests to the file at once using wait_all (collective i/o)
156
159
f .wait_all (num = pnetcdf .NC_PUT_REQ_ALL )
157
160
f .close ()
158
- assert validate_nc_file (os .environ .get ('PNETCDF_DIR' ), self .file_path ) == 0 if os .environ .get ('PNETCDF_DIR' ) is not None else True
159
161
162
+ if rank == 0 :
163
+ if os .environ .get ('PNETCDF_DIR' ) is not None :
164
+ assert validate_nc_file (os .environ .get ('PNETCDF_DIR' ), self .file_path ) == 0
160
165
161
- def tearDown (self ):
162
- # remove the temporary files if the test file ouptut directory not specified
163
- comm .Barrier ()
164
- if (rank == 0 ) and not ((len (sys .argv ) == 2 ) and os .path .isdir (sys .argv [1 ])):
165
- os .remove (self .file_path )
166
-
167
166
def runTest (self ):
168
- """testing variable iput varn for CDF-5/CDF-2/CDF-1 file format"""
169
-
170
- f = pnetcdf .File (self .file_path , 'r' )
171
- # test iput_varn and collective i/o wait_all
172
- for i in range (num_reqs * 2 ):
173
- v = f .variables [f'data{ i } ' ]
174
- assert_array_equal (v [:], dataref )
167
+ if nprocs < 4 :
168
+ return
169
+
170
+ if rank == 0 :
171
+ # check contents of the output file
172
+ f = pnetcdf .File (self .file_path , 'r' , comm = MPI .COMM_SELF )
173
+ for i in range (num_vars ):
174
+ v = f .variables [f'data{ i } ' ]
175
+ assert_array_equal (v [:], dataref )
176
+ f .close
175
177
176
178
if __name__ == '__main__' :
177
179
suite = unittest .TestSuite ()
0 commit comments