6666import pnetcdf
6767
6868
69- def parse_help ():
70- help_flag = "-h" in sys .argv or "--help" in sys .argv
71- if help_flag and rank == 0 :
72- help_text = (
73- "Usage: {} [-h] | [-q] [file_name]\n "
74- " [-h] Print help\n "
75- " [-q] Quiet mode (reports when fail)\n "
76- " [-k format] file format: 1 for CDF-1, 2 for CDF-2, 5 for CDF-5\n "
77- " [-l len] size of each dimension of the local array\n "
78- " [filename] (Optional) output netCDF file name\n "
79- ).format (sys .argv [0 ])
80- print (help_text )
81- return help_flag
82-
83-
8469def pnetcdf_io (filename , file_format , length ):
8570
8671 count = [length , length + 1 ]
8772 psizes = MPI .Compute_dims (nprocs , 2 )
8873
74+ nghosts = 2
75+
8976 if verbose and rank == 0 :
90- print ("psizes=" , psizes [0 ], psizes [1 ])
77+ print ("number of MPI processes =" , nprocs )
78+ print ("number of ghost cells =" , nghosts )
79+ print ("psizes=" , psizes )
9180
9281 # set global array sizes
9382 gsizes = np .zeros (2 , dtype = np .int64 )
9483 gsizes [0 ] = length * psizes [0 ] # global array size
9584 gsizes [1 ] = (length + 1 ) * psizes [1 ]
9685 if verbose and rank == 0 :
97- print ("global variable shape:" , gsizes [ 0 ], gsizes [ 1 ] )
86+ print ("global variable shape:" , gsizes )
9887
9988 # find its local rank IDs along each dimension
10089 local_rank = np .zeros (2 , dtype = np .int32 )
10190 local_rank [0 ] = rank // psizes [1 ]
10291 local_rank [1 ] = rank % psizes [1 ]
10392 if verbose :
104- print ("rank {}: dim rank= {} {}" . format ( rank , local_rank [ 0 ], local_rank [ 1 ]) )
93+ print ("rank " , rank , ": local_rank = " , local_rank )
10594
10695 # set subarray access pattern
10796 count = np .array ([length , length + 1 ], dtype = np .int64 )
10897 start = np .array ([local_rank [0 ] * count [0 ], local_rank [1 ] * count [1 ]],
10998 dtype = np .int64 )
11099 if verbose :
111- print ("start= {} {} count= {} {}" .format (start [0 ], start [1 ], count [0 ], count [1 ]))
112-
113- # allocate and initialize buffer with ghost cells on both ends of each dim
114- nghosts = 2
115- bufsize = (count [0 ] + 2 * nghosts ) * (count [1 ] + 2 * nghosts )
116- buf = np .empty (bufsize , dtype = np .int32 )
117- for i in range (count [0 ] + 2 * nghosts ):
118- for j in range (count [1 ] + 2 * nghosts ):
119- if nghosts <= i < count [0 ] + nghosts and \
120- nghosts <= j < count [1 ] + nghosts :
121- buf [i * (count [1 ] + 2 * nghosts ) + j ] = rank
122- else :
123- # set values of all ghost cells to -8
124- buf [i * (count [1 ] + 2 * nghosts ) + j ] = - 8
100+ print ("rank " ,rank ,": start = " ,start ," count =" , count )
125101
126102 # Create the file
127103 f = pnetcdf .File (filename = filename ,
@@ -140,19 +116,39 @@ def pnetcdf_io(filename, file_format, length):
140116 # Exit the define mode
141117 f .enddef ()
142118
143- # set imap pattern for local buffer
144- imap = np .zeros (2 , dtype = np .int64 )
145- imap [1 ] = 1
146- imap [0 ] = count [1 ] + 2 * nghosts
147- buf_ptr = buf [nghosts * (count [1 ] + 2 * nghosts + 1 ):]
119+ # allocate and initialize buffer with ghost cells on both ends of each dim
120+ buf = np .empty ([2 * nghosts + count [0 ], 2 * nghosts + count [1 ]], dtype = np .int32 )
121+ buf .fill (- 8 )
122+
123+ # keep contents of ghost cells to -8, all others 'rank'
124+ buf [nghosts :nghosts + count [0 ], nghosts :nghosts + count [1 ]].fill (rank )
148125
149126 # Write data to the variable
150- var .put_var_all (buf_ptr , start = start , count = count , imap = imap )
127+ end = np .add (start , count )
128+ var [start [0 ]:end [0 ], start [1 ]:end [1 ]] = buf [nghosts :nghosts + count [0 ], nghosts :nghosts + count [1 ]]
129+
130+ # Equivalently, below uses function call
131+ var .put_var_all (buf [nghosts :nghosts + count [0 ], nghosts :nghosts + count [1 ]], start = start , count = count )
151132
152133 # Close the file
153134 f .close ()
154135
155136
137+ def parse_help ():
138+ help_flag = "-h" in sys .argv or "--help" in sys .argv
139+ if help_flag and rank == 0 :
140+ help_text = (
141+ "Usage: {} [-h] | [-q] [file_name]\n "
142+ " [-h] Print help\n "
143+ " [-q] Quiet mode (reports when fail)\n "
144+ " [-k format] file format: 1 for CDF-1, 2 for CDF-2, 5 for CDF-5\n "
145+ " [-l len] size of each dimension of the local array\n "
146+ " [filename] (Optional) output netCDF file name\n "
147+ ).format (sys .argv [0 ])
148+ print (help_text )
149+ return help_flag
150+
151+
156152if __name__ == "__main__" :
157153 comm = MPI .COMM_WORLD
158154 rank = comm .Get_rank ()
0 commit comments