3131 32 400 x 400 x 200 6.67 45.72
3232"""
3333
34- import sys , os , argparse , inspect
34+ import sys , os , argparse
3535import numpy as np
3636from mpi4py import MPI
3737import pnetcdf
3838
39- NDIMS = 3
40- NUM_VARS = 10
41-
42- def parse_help (comm ):
43- rank = comm .Get_rank ()
39+ def parse_help ():
4440 help_flag = "-h" in sys .argv or "--help" in sys .argv
4541 if help_flag and rank == 0 :
4642 help_text = (
@@ -54,21 +50,22 @@ def parse_help(comm):
5450 print (help_text )
5551 return help_flag
5652
57- def print_info ( info_used ):
58- print ( "MPI hint: cb_nodes =" , info_used . Get ( "cb_nodes" ))
59- print ( "MPI hint: cb_buffer_size =" , info_used . Get ( "cb_buffer_size" ))
60- print ( "MPI hint: striping_factor =" , info_used . Get ( "striping_factor" ))
61- print ( "MPI hint: striping_unit =" , info_used . Get ( "striping_unit" ))
53+ def pnetcdf_io ( filename , file_format , length ):
54+ # number of dimensions
55+ NDIMS = 3
56+ # number of variables
57+ NUM_VARS = 10
6258
63- def pnetcdf_io ( comm , filename , file_format , length ) :
64- rank = comm . Get_rank ( )
65- nprocs = comm . Get_size ( )
59+ if verbose and rank == 0 :
60+ print ( "Number of variables = " , NUM_VARS )
61+ print ( "Number of dimensions = " , NDIMS )
6662
6763 starts = np .zeros (NDIMS , dtype = np .int32 )
6864 counts = np .zeros (NDIMS , dtype = np .int32 )
6965 gsizes = np .zeros (NDIMS , dtype = np .int32 )
7066 buf = []
7167
68+ # calculate local subarray access pattern
7269 psizes = MPI .Compute_dims (nprocs , NDIMS )
7370 starts [0 ] = rank % psizes [0 ]
7471 starts [1 ] = (rank // psizes [1 ]) % psizes [1 ]
@@ -87,20 +84,12 @@ def pnetcdf_io(comm, filename, file_format, length):
8784 for j in range (bufsize ):
8885 buf [i ][j ] = rank * i + 123 + j
8986
90- comm .Barrier ()
91- write_timing = MPI .Wtime ()
92-
9387 # Create the file using file clobber mode
94- try :
95- f = pnetcdf .File (filename = filename , \
96- mode = 'w' , \
97- format = file_format , \
98- comm = comm , \
99- info = None )
100- except OSError as e :
101- print ("Error at {}:{} ncmpi_create() file {} ({})" .format (__file__ ,inspect .currentframe ().f_back .f_lineno , filename , e ))
102- comm .Abort ()
103- exit (1 )
88+ f = pnetcdf .File (filename = filename ,
89+ mode = 'w' ,
90+ format = file_format ,
91+ comm = comm ,
92+ info = None )
10493
10594 # Define dimensions
10695 dims = []
@@ -127,34 +116,14 @@ def pnetcdf_io(comm, filename, file_format, length):
127116 # Close the file
128117 f .close ()
129118
130- write_timing = MPI .Wtime () - write_timing
131-
132- # calculate write amount across all processes in total
133- write_size = bufsize * NUM_VARS * np .dtype (np .int32 ).itemsize
134- sum_write_size = comm .reduce (write_size , MPI .SUM , root = 0 )
135- max_write_timing = comm .reduce (write_timing , MPI .MAX , root = 0 )
136-
137- if rank == 0 and verbose :
138- subarray_size = (bufsize * np .dtype (np .int32 ).itemsize ) / 1048576.0
139- print_info (info_used )
140- print ("Local array size {} x {} x {} integers, size = {:.2f} MB" .format (length , length , length , subarray_size ))
141- sum_write_size /= 1048576.0
142- print ("Global array size {} x {} x {} integers, write size = {:.2f} GB" .format (gsizes [0 ], gsizes [1 ], gsizes [2 ], sum_write_size / 1024.0 ))
143-
144- write_bw = sum_write_size / max_write_timing
145- print (" procs Global array size exec(sec) write(MB/s)" )
146- print ("------- ------------------ --------- -----------" )
147- print (" {:4d} {:4d} x {:4d} x {:4d} {:8.2f} {:10.2f}\n " .format (nprocs , gsizes [0 ], gsizes [1 ], gsizes [2 ], max_write_timing , write_bw ))
148-
149119
150120if __name__ == "__main__" :
151121
152- verbose = True
153122 comm = MPI .COMM_WORLD
154123 rank = comm .Get_rank ()
155124 nprocs = comm .Get_size ()
156125
157- if parse_help (comm ):
126+ if parse_help ():
158127 MPI .Finalize ()
159128 sys .exit (1 )
160129
@@ -168,25 +137,25 @@ def pnetcdf_io(comm, filename, file_format, length):
168137 parser .add_argument ("-l" , help = "Size of each dimension of the local array\n " )
169138 args = parser .parse_args ()
170139
171- file_format = None
172- length = 10
173-
174- if args .q : verbose = False
140+ verbose = False if args .q else True
175141
142+ file_format = None
176143 if args .k :
177- kind_dict = {'1' :None , '2' :"NETCDF3_64BIT_OFFSET " , '5' :"NETCDF3_64BIT_DATA " }
144+ kind_dict = {'1' :None , '2' :"NC_64BIT_OFFSET " , '5' :"NC_64BIT_DATA " }
178145 file_format = kind_dict [args .k ]
179146
147+ length = 10
180148 if args .l and int (args .l ) > 0 :
181149 length = int (args .l )
182150
183151 filename = args .dir
152+
184153 if verbose and rank == 0 :
185154 print ("{}: example of collective writes" .format (os .path .basename (__file__ )))
186155
187156 # Run I/O
188157 try :
189- pnetcdf_io (comm , filename , file_format , length )
158+ pnetcdf_io (filename , file_format , length )
190159 except BaseException as err :
191160 print ("Error: type:" , type (err ), str (err ))
192161 raise
0 commit comments