44#
55
66"""
7- This example program is intended to illustrate the use of the pnetCDF python API.
8- The program runs in non-blocking mode and makes a request to read a list of subarray of
9- a netCDF variable of an opened netCDF file using iget_var method of `Variable` class. The
10- library will internally invoke ncmpi_iget_varn in C.
7+ This program tests iget_varn() method of `Variable` class, by making a
8+ nonblocking request to read a list of subarrays of a netCDF variable of an
9+ opened netCDF file. The library will internally invoke ncmpi_iget_varn() in C.
1110"""
11+
1212import pnetcdf
13- from numpy .random import seed , randint
14- from numpy .testing import assert_array_equal , assert_equal , assert_array_almost_equal
15- import tempfile , unittest , os , random , sys
13+ from numpy .random import seed
14+ from numpy .testing import assert_array_equal
15+ import unittest , os , random , sys
1616import numpy as np
1717from mpi4py import MPI
1818from pnetcdf import strerror , strerrno
9090 # - - - - - - - 3 3 3
9191else :
9292 num_subarray_reqs = 0
93+
9394# obtain the buffer size of returned array
9495buf_len = 0
9596for i in range (num_subarray_reqs ):
9697 w_req_len = np .prod (counts [i ,:])
9798 buf_len += w_req_len
99+
98100# generate reference array for comparing at the testing phase
99101dataref = np .full ((buf_len ,), rank , np .float32 )
100- # total number of iget_var requests for this test program
102+ # total number of subarray requests for this test program
101103num_reqs = 10
102104# initialize a list to store references of variable values
103105v_datas = []
@@ -110,58 +112,71 @@ def setUp(self):
110112 else :
111113 self .file_path = file_name
112114 self ._file_format = file_formats .pop (0 )
113- f = pnetcdf .File (filename = self .file_path , mode = 'w' , format = self ._file_format , comm = comm , info = None )
115+
116+ f = pnetcdf .File (filename = self .file_path , mode = 'w' ,
117+ format = self ._file_format , comm = comm , info = None )
118+
114119 dx = f .def_dim ('x' ,xdim )
115120 dy = f .def_dim ('y' ,ydim )
116121
117122 # define 20 netCDF variables
118123 for i in range (num_reqs * 2 ):
119124 v = f .def_var (f'data{ i } ' , pnetcdf .NC_FLOAT , (dx , dy ))
120- # initialize variable values
125+
121126 f .enddef ()
127+
128+ # initialize and write variable values
122129 for i in range (num_reqs * 2 ):
123130 v = f .variables [f'data{ i } ' ]
124131 v [:] = data
125- f .close ()
126- assert validate_nc_file (os .environ .get ('PNETCDF_DIR' ), self .file_path ) == 0 if os .environ .get ('PNETCDF_DIR' ) is not None else True
127132
133+ f .close ()
134+ if os .environ .get ('PNETCDF_DIR' ) is not None :
135+ assert validate_nc_file (os .environ .get ('PNETCDF_DIR' ), self .file_path ) == 0
128136
129137
130138 f = pnetcdf .File (self .file_path , 'r' )
139+
131140 # each process post 10 requests to read a list of subarrays from the variable
132141 req_ids = []
133142 v_datas .clear ()
143+
134144 for i in range (num_reqs ):
135145 v = f .variables [f'data{ i } ' ]
136146 buff = np .empty (shape = buf_len , dtype = v .datatype )
137147 # post the request to read multiple slices (subarrays) of the variable
138- req_id = v .iget_var (buff , start = starts , count = counts , num = num_subarray_reqs )
148+ req_id = v .iget_varn (buff , num_subarray_reqs , starts , counts )
139149 # track the reqeust ID for each read reqeust
140150 req_ids .append (req_id )
141151 # store the reference of variable values
142152 v_datas .append (buff )
143- f . end_indep ()
153+
144154 # commit those 10 requests to the file at once using wait_all (collective i/o)
145155 req_errs = [None ] * num_reqs
146156 f .wait_all (num_reqs , req_ids , req_errs )
157+
147158 # check request error msg for each unsuccessful requests
148159 for i in range (num_reqs ):
149160 if strerrno (req_errs [i ]) != "NC_NOERR" :
150161 print (f"Error on request { i } :" , strerror (req_errs [i ]))
151162
152- # post 10 requests to read an array of values for the last 10 variables w/o tracking req ids
163+ # post 10 requests to read an array of values for the last 10
164+ # variables w/o tracking req ids
153165 for i in range (num_reqs , num_reqs * 2 ):
154166 v = f .variables [f'data{ i } ' ]
155167 buff = np .empty (buf_len , dtype = v .datatype )
156168 # post the request to read a list of subarrays from the variable
157- v .iget_var (buff , start = starts , count = counts , num = num_subarray_reqs )
169+ v .iget_varn (buff , num_subarray_reqs , starts , counts )
158170 # store the reference of variable values
159171 v_datas .append (buff )
160172
161- # commit all pending get requests to the file at once using wait_all (collective i/o)
173+ # commit all pending get requests to the file at once using wait_all
174+ # (collective i/o)
162175 req_errs = f .wait_all (num = pnetcdf .NC_GET_REQ_ALL )
176+
163177 f .close ()
164- assert validate_nc_file (os .environ .get ('PNETCDF_DIR' ), self .file_path ) == 0 if os .environ .get ('PNETCDF_DIR' ) is not None else True
178+ if os .environ .get ('PNETCDF_DIR' ) is not None :
179+ assert validate_nc_file (os .environ .get ('PNETCDF_DIR' ), self .file_path ) == 0
165180
166181
167182 def tearDown (self ):
@@ -188,3 +203,4 @@ def runTest(self):
188203 if not result .wasSuccessful ():
189204 print (output .getvalue ())
190205 sys .exit (1 )
206+
0 commit comments