From 184c4cf1de1635b9a2d81a917fff29346ceecce4 Mon Sep 17 00:00:00 2001 From: wkliao Date: Fri, 9 Aug 2024 16:39:47 -0500 Subject: [PATCH] simplify examples --- examples/collective_write.py | 77 +++++++++---------------------- examples/create_open.py | 13 ++++-- examples/fill_mode.py | 17 +++++-- examples/flexible_api.py | 47 +++++++++++-------- examples/get_info.py | 15 ++++-- examples/get_vara.py | 25 ++++++---- examples/ghost_cell.py | 29 +++++++----- examples/global_attribute.py | 21 +++++---- examples/hints.py | 26 +++++++---- examples/nonblocking_write.py | 57 +++++------------------ examples/nonblocking_write_def.py | 53 +++++---------------- examples/put_vara.py | 23 +++++---- examples/put_varn_int.py | 23 ++++++--- examples/transpose.py | 52 ++++++++++++--------- examples/transpose2D.py | 20 +++++--- 15 files changed, 243 insertions(+), 255 deletions(-) diff --git a/examples/collective_write.py b/examples/collective_write.py index b0bd236..2a2cb5e 100644 --- a/examples/collective_write.py +++ b/examples/collective_write.py @@ -31,16 +31,12 @@ 32 400 x 400 x 200 6.67 45.72 """ -import sys, os, argparse, inspect +import sys, os, argparse import numpy as np from mpi4py import MPI import pnetcdf -NDIMS = 3 -NUM_VARS = 10 - -def parse_help(comm): - rank = comm.Get_rank() +def parse_help(): help_flag = "-h" in sys.argv or "--help" in sys.argv if help_flag and rank == 0: help_text = ( @@ -54,21 +50,22 @@ def parse_help(comm): print(help_text) return help_flag -def print_info(info_used): - print("MPI hint: cb_nodes =", info_used.Get("cb_nodes")) - print("MPI hint: cb_buffer_size =", info_used.Get("cb_buffer_size")) - print("MPI hint: striping_factor =", info_used.Get("striping_factor")) - print("MPI hint: striping_unit =", info_used.Get("striping_unit")) +def pnetcdf_io(filename, file_format, length): + # number of dimensions + NDIMS = 3 + # number of variables + NUM_VARS = 10 -def pnetcdf_io(comm, filename, file_format, length): - rank = comm.Get_rank() - nprocs = comm.Get_size() + if verbose and rank == 0: + print("Number of variables = ", NUM_VARS) + print("Number of dimensions = ", NDIMS) starts = np.zeros(NDIMS, dtype=np.int32) counts = np.zeros(NDIMS, dtype=np.int32) gsizes = np.zeros(NDIMS, dtype=np.int32) buf = [] + # calculate local subarray access pattern psizes = MPI.Compute_dims(nprocs, NDIMS) starts[0] = rank % psizes[0] starts[1] = (rank // psizes[1]) % psizes[1] @@ -87,20 +84,12 @@ def pnetcdf_io(comm, filename, file_format, length): for j in range(bufsize): buf[i][j] = rank * i + 123 + j - comm.Barrier() - write_timing = MPI.Wtime() - # Create the file using file clobber mode - try: - f = pnetcdf.File(filename = filename, \ - mode = 'w', \ - format = file_format, \ - comm = comm, \ - info = None) - except OSError as e: - print("Error at {}:{} ncmpi_create() file {} ({})".format(__file__,inspect.currentframe().f_back.f_lineno, filename, e)) - comm.Abort() - exit(1) + f = pnetcdf.File(filename = filename, + mode = 'w', + format = file_format, + comm = comm, + info = None) # Define dimensions dims = [] @@ -127,34 +116,14 @@ def pnetcdf_io(comm, filename, file_format, length): # Close the file f.close() - write_timing = MPI.Wtime() - write_timing - - # calculate write amount across all processes in total - write_size = bufsize * NUM_VARS * np.dtype(np.int32).itemsize - sum_write_size = comm.reduce(write_size, MPI.SUM, root=0) - max_write_timing = comm.reduce(write_timing, MPI.MAX, root=0) - - if rank == 0 and verbose: - subarray_size = (bufsize * np.dtype(np.int32).itemsize) / 1048576.0 - print_info(info_used) - print("Local array size {} x {} x {} integers, size = {:.2f} MB".format(length, length, length, subarray_size)) - sum_write_size /= 1048576.0 - print("Global array size {} x {} x {} integers, write size = {:.2f} GB".format(gsizes[0], gsizes[1], gsizes[2], sum_write_size/1024.0)) - - write_bw = sum_write_size / max_write_timing - print(" procs Global array size exec(sec) write(MB/s)") - print("------- ------------------ --------- -----------") - 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)) - if __name__ == "__main__": - verbose = True comm = MPI.COMM_WORLD rank = comm.Get_rank() nprocs = comm.Get_size() - if parse_help(comm): + if parse_help(): MPI.Finalize() sys.exit(1) @@ -168,25 +137,25 @@ def pnetcdf_io(comm, filename, file_format, length): parser.add_argument("-l", help="Size of each dimension of the local array\n") args = parser.parse_args() - file_format = None - length = 10 - - if args.q: verbose = False + verbose = False if args.q else True + file_format = None if args.k: - kind_dict = {'1':None, '2':"NETCDF3_64BIT_OFFSET", '5':"NETCDF3_64BIT_DATA"} + kind_dict = {'1':None, '2':"NC_64BIT_OFFSET", '5':"NC_64BIT_DATA"} file_format = kind_dict[args.k] + length = 10 if args.l and int(args.l) > 0: length = int(args.l) filename = args.dir + if verbose and rank == 0: print("{}: example of collective writes".format(os.path.basename(__file__))) # Run I/O try: - pnetcdf_io(comm, filename, file_format, length) + pnetcdf_io(filename, file_format, length) except BaseException as err: print("Error: type:", type(err), str(err)) raise diff --git a/examples/create_open.py b/examples/create_open.py index fd6ebe9..e64e2e8 100644 --- a/examples/create_open.py +++ b/examples/create_open.py @@ -34,11 +34,12 @@ def parse_help(): return help_flag def pnetcdf_io(filename): - if verbose and rank == 0: - print("{}: example of file create and open".format(os.path.basename(__file__))) # create a new file using file clobber mode, i.e. flag "-w" - f = pnetcdf.File(filename=filename, mode = 'w', comm=comm, info=None) + f = pnetcdf.File(filename = filename, + mode = 'w', + comm = comm, + info = None) # close the file f.close() @@ -51,7 +52,6 @@ def pnetcdf_io(filename): if __name__ == "__main__": - verbose = True comm = MPI.COMM_WORLD rank = comm.Get_rank() nprocs = comm.Get_size() @@ -68,10 +68,13 @@ def pnetcdf_io(filename): parser.add_argument("-q", help="Quiet mode (reports when fail)", action="store_true") args = parser.parse_args() - if args.q: verbose = False + verbose = False if args.q else True filename = args.dir + if verbose and rank == 0: + print("{}: example of file create and open".format(os.path.basename(__file__))) + try: pnetcdf_io(filename) except BaseException as err: diff --git a/examples/fill_mode.py b/examples/fill_mode.py index 95541fb..647a481 100644 --- a/examples/fill_mode.py +++ b/examples/fill_mode.py @@ -63,10 +63,14 @@ def pnetcdf_io(filename): NX = 4 if verbose and rank == 0: - print("{}: example of setting fill mode".format(os.path.basename(__file__))) + print("Y dimension size = ", NY) + print("X dimension size = ", NX) # create a new file using clobber "w" mode - f = pnetcdf.File(filename=filename, mode = 'w', comm=comm, info=None) + f = pnetcdf.File(filename = filename, + mode = 'w', + comm = comm, + info = None) # the global array is NY * (NX * nprocs) global_ny = NY @@ -131,8 +135,11 @@ def pnetcdf_io(filename): # write to the 2nd record rec_var.put_var_all(buf, start = starts, count = counts) + + # close file f.close() + if __name__ == "__main__": verbose = True @@ -152,10 +159,14 @@ def pnetcdf_io(filename): parser.add_argument("-q", help="Quiet mode (reports when fail)", action="store_true") args = parser.parse_args() - if args.q: verbose = False + + verbose = False if args.q else True filename = args.dir + if verbose and rank == 0: + print("{}: example of setting fill mode".format(os.path.basename(__file__))) + try: pnetcdf_io(filename) except BaseException as err: diff --git a/examples/flexible_api.py b/examples/flexible_api.py index 854f97d..379d056 100644 --- a/examples/flexible_api.py +++ b/examples/flexible_api.py @@ -89,14 +89,21 @@ def pnetcdf_io(filename, file_format): NY = 5 NX = 5 NZ = 5 - ghost_len = 3 if verbose and rank == 0: - print("{}: example of using flexible APIs".format(os.path.basename(__file__))) + print("Z dimension size = ", NZ) + print("Y dimension size = ", NY) + print("X dimension size = ", NX) + # number of cells at both end of each dimension + ghost_len = 3 # Create the file - f = pnetcdf.File(filename=filename, mode = 'w', format = file_format, comm=comm, info=None) + f = pnetcdf.File(filename = filename, + mode = 'w', + format = file_format, + comm = comm, + info = None) # Define dimensions dim_z = f.def_dim("Z", NZ*nprocs) @@ -146,9 +153,9 @@ def pnetcdf_io(filename, file_format): buf_zy.fill(-1) # read using flexible API - var_zy.get_var_all(buf_zy, start = starts, \ - count = counts, \ - bufcount = 1, \ + var_zy.get_var_all(buf_zy, start = starts, + count = counts, + bufcount = 1, buftype = subarray) # check contents of the get buffer @@ -171,7 +178,10 @@ def pnetcdf_io(filename, file_format): array_of_sizes = np.array([NY + 2 * ghost_len, NX + 2 * ghost_len]) array_of_subsizes = np.array([NY, NX]) array_of_starts = np.array([ghost_len, ghost_len]) - subarray = MPI.DOUBLE.Create_subarray(array_of_sizes, array_of_subsizes, array_of_starts, order=MPI.ORDER_C) + subarray = MPI.DOUBLE.Create_subarray(array_of_sizes, + array_of_subsizes, + array_of_starts, + order=MPI.ORDER_C) subarray.Commit() # initialize write user buffer @@ -181,9 +191,9 @@ def pnetcdf_io(filename, file_format): counts = np.array([NY, NX]) # calling a blocking flexible write API - req_id = var_yx.iput_var(buf_yx, start = starts, \ - count = counts, \ - bufcount = 1, \ + req_id = var_yx.iput_var(buf_yx, start = starts, + count = counts, + bufcount = 1, buftype = subarray) # commit posted pending nonblocking requests @@ -198,9 +208,9 @@ def pnetcdf_io(filename, file_format): buf_yx.fill(-1) # calling a blocking flexible read API - req_id = var_yx.iget_var(buf_yx, start = starts, \ - count = counts, \ - bufcount = 1, \ + req_id = var_yx.iget_var(buf_yx, start = starts, + count = counts, + bufcount = 1, buftype=subarray) # commit posted pending nonblocking requests @@ -227,7 +237,6 @@ def pnetcdf_io(filename, file_format): if __name__ == "__main__": - verbose = True comm = MPI.COMM_WORLD rank = comm.Get_rank() nprocs = comm.Get_size() @@ -245,16 +254,18 @@ def pnetcdf_io(filename, file_format): parser.add_argument("-k", help="File format: 1 for CDF-1, 2 for CDF-2, 5 for CDF-5") args = parser.parse_args() - file_format = None - - if args.q: verbose = False + verbose = False if args.q else True + file_format = None if args.k: - kind_dict = {'1':None, '2':"NETCDF3_64BIT_OFFSET", '5':"NETCDF3_64BIT_DATA"} + kind_dict = {'1':None, '2':"NC_64BIT_OFFSET", '5':"NC_64BIT_DATA"} file_format = kind_dict[args.k] filename = args.dir + if verbose and rank == 0: + print("{}: example of using flexible APIs".format(os.path.basename(__file__))) + try: pnetcdf_io(filename, file_format) except BaseException as err: diff --git a/examples/get_info.py b/examples/get_info.py index 75b4447..8b67349 100644 --- a/examples/get_info.py +++ b/examples/get_info.py @@ -50,6 +50,7 @@ def parse_help(): print(help_text) return help_flag + def print_info(info_used): nkeys = info_used.Get_nkeys() print("MPI File Info: nkeys =", nkeys) @@ -60,11 +61,13 @@ def print_info(info_used): def pnetcdf_io(filename): - if verbose and rank == 0: - print("{}: example of getting MPI-IO hints".format(os.path.basename(__file__))) # create a new file using clobber "w" mode - f = pnetcdf.File(filename=filename, mode = 'w', file_format = "NETCDF3_64BIT_DATA", comm=comm, info=None) + f = pnetcdf.File(filename=filename, + mode = 'w', + file_format = "NC_64BIT_DATA", + comm=comm, + info=None) # exit the define mode f.enddef() @@ -82,7 +85,6 @@ def pnetcdf_io(filename): if __name__ == "__main__": - verbose = True comm = MPI.COMM_WORLD rank = comm.Get_rank() nprocs = comm.Get_size() @@ -99,10 +101,13 @@ def pnetcdf_io(filename): parser.add_argument("-q", help="Quiet mode (reports when fail)", action="store_true") args = parser.parse_args() - if args.q: verbose = False + verbose = False if args.q else True filename = args.dir + if verbose and rank == 0: + print("{}: example of getting MPI-IO hints".format(os.path.basename(__file__))) + try: pnetcdf_io(filename) except BaseException as err: diff --git a/examples/get_vara.py b/examples/get_vara.py index bbb8f3b..84d5094 100644 --- a/examples/get_vara.py +++ b/examples/get_vara.py @@ -68,11 +68,11 @@ def parse_help(): def pnetcdf_io(filename, file_format): - if verbose and rank == 0: - print("{}: reading file ".format(os.path.basename(__file__)), filename) - # Open an existing file for reading - f = pnetcdf.File(filename=filename, mode = 'r', comm=comm, info=None) + f = pnetcdf.File(filename = filename, + mode = 'r', + comm = comm, + info = None) # Get global attribute named "history" str_att = f.get_att("history") @@ -83,6 +83,10 @@ def pnetcdf_io(filename, file_format): global_ny = len(f.dimensions['Y']) global_nx = len(f.dimensions['X']) + if verbose and rank == 0: + print("Y dimension size = ", global_ny) + print("X dimension size = ", global_nx) + # get the variable of a 2D variable of integer type v = f.variables['var'] @@ -101,14 +105,14 @@ def pnetcdf_io(filename, file_format): counts = [local_ny, local_nx] # Read a subarray in collective mode - buff = np.empty(tuple(counts), v.dtype) - v.get_var_all(buff, start = starts, count = counts) + r_buf = np.empty(tuple(counts), v.dtype) + v.get_var_all(r_buf, start = starts, count = counts) # close the file f.close() + if __name__ == "__main__": - verbose = True comm = MPI.COMM_WORLD rank = comm.Get_rank() nprocs = comm.Get_size() @@ -129,14 +133,17 @@ def pnetcdf_io(filename, file_format): file_format = None length = 10 - if args.q: verbose = False + verbose = False if args.q else True if args.k: - kind_dict = {'1':None, '2':"NETCDF3_64BIT_OFFSET", '5':"NETCDF3_64BIT_DATA"} + kind_dict = {'1':None, '2':"NC_64BIT_OFFSET", '5':"NC_64BIT_DATA"} file_format = kind_dict[args.k] filename = args.dir + if verbose and rank == 0: + print("{}: reading file ".format(os.path.basename(__file__)), filename) + try: pnetcdf_io(filename, file_format) except BaseException as err: diff --git a/examples/ghost_cell.py b/examples/ghost_cell.py index 14de88d..d9f664b 100644 --- a/examples/ghost_cell.py +++ b/examples/ghost_cell.py @@ -83,9 +83,6 @@ def parse_help(): def pnetcdf_io(filename, file_format, length): - if verbose and rank == 0: - print("{}: example of using buffers with ghost cells".format(os.path.basename(__file__))) - counts = [length, length + 1] psizes = MPI.Compute_dims(nprocs, 2) @@ -108,7 +105,8 @@ def pnetcdf_io(filename, file_format, length): # set subarray access pattern counts = np.array([length, length + 1], dtype=np.int64) - starts = np.array([local_rank[0] * counts[0], local_rank[1] * counts[1]], dtype=np.int64) + starts = np.array([local_rank[0] * counts[0], local_rank[1] * counts[1]], + dtype=np.int64) if verbose: print("starts= {} {} counts= {} {}".format(starts[0], starts[1], counts[0], counts[1])) @@ -118,13 +116,19 @@ def pnetcdf_io(filename, file_format, length): buf = np.empty(bufsize, dtype=np.int32) for i in range(counts[0] + 2 * nghosts): for j in range(counts[1] + 2 * nghosts): - if nghosts <= i < counts[0] + nghosts and nghosts <= j < counts[1] + nghosts: + if nghosts <= i < counts[0] + nghosts and \ + nghosts <= j < counts[1] + nghosts: buf[i * (counts[1] + 2 * nghosts) + j] = rank else: - buf[i * (counts[1] + 2 * nghosts) + j] = -8 # all ghost cells have value -8 + # set values of all ghost cells to -8 + buf[i * (counts[1] + 2 * nghosts) + j] = -8 # Create the file - f = pnetcdf.File(filename=filename, mode = 'w', format = file_format, comm=comm, info=None) + f = pnetcdf.File(filename = filename, + mode = 'w', + format = file_format, + comm = comm, + info = None) # Define dimensions dim_y = f.def_dim("Y", gsizes[0]) @@ -150,7 +154,6 @@ def pnetcdf_io(filename, file_format, length): if __name__ == "__main__": - verbose = True comm = MPI.COMM_WORLD rank = comm.Get_rank() nprocs = comm.Get_size() @@ -169,19 +172,21 @@ def pnetcdf_io(filename, file_format, length): parser.add_argument("-l", help="Size of each dimension of the local array\n") args = parser.parse_args() - if args.q: verbose = False + verbose = False if args.q else True file_format = None if args.k: - kind_dict = {'1':None, '2':"NETCDF3_64BIT_OFFSET", '5':"NETCDF3_64BIT_DATA"} + kind_dict = {'1':None, '2':"NC_64BIT_OFFSET", '5':"NC_64BIT_DATA"} file_format = kind_dict[args.k] length = 4 - if args.l: length = int(args.l) + if args.l and int(args.l) > 0: + length = int(args.l) filename = args.dir - length = 4 if length <= 0 else length + if verbose and rank == 0: + print("{}: example of using buffers with ghost cells".format(os.path.basename(__file__))) try: pnetcdf_io(filename, file_format, length) diff --git a/examples/global_attribute.py b/examples/global_attribute.py index 35b1cde..0c75a57 100644 --- a/examples/global_attribute.py +++ b/examples/global_attribute.py @@ -48,13 +48,14 @@ def parse_help(): def pnetcdf_io(filename, file_format): digit = np.int16([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) - if verbose and rank == 0: - print("{}: example of put/get global attributes".format(os.path.basename(__file__))) - # Run pnetcdf i/o # Create the file - f = pnetcdf.File(filename=filename, mode = 'w', format = file_format, comm=comm, info=None) + f = pnetcdf.File(filename = filename, + mode = 'w', + format = file_format, + comm = comm, + info = None) if rank == 0: ltime = time.localtime() @@ -107,7 +108,6 @@ def pnetcdf_io(filename, file_format): if __name__ == "__main__": - verbose = True comm = MPI.COMM_WORLD rank = comm.Get_rank() nprocs = comm.Get_size() @@ -125,15 +125,18 @@ def pnetcdf_io(filename, file_format): parser.add_argument("-k", help="File format: 1 for CDF-1, 2 for CDF-2, 5 for CDF-5") args = parser.parse_args() - if args.q: verbose = False - - filename = args.dir + verbose = False if args.q else True file_format = None if args.k: - kind_dict = {'1':None, '2':"NETCDF3_64BIT_OFFSET", '5':"NETCDF3_64BIT_DATA"} + kind_dict = {'1':None, '2':"NC_64BIT_OFFSET", '5':"NC_64BIT_DATA"} file_format = kind_dict[args.k] + filename = args.dir + + if verbose and rank == 0: + print("{}: example of put/get global attributes".format(os.path.basename(__file__))) + try: pnetcdf_io(filename, file_format) except BaseException as err: diff --git a/examples/hints.py b/examples/hints.py index 4df0162..d917fcf 100644 --- a/examples/hints.py +++ b/examples/hints.py @@ -94,16 +94,22 @@ def pnetcdf_io(filename): NZ = 5 if verbose and rank == 0: - print("{}: example of set/get PnetCDF hints".format(os.path.basename(__file__))) + print("Z dimension size = ", NZ) + print("Y dimension size = ", NY) + print("X dimension size = ", NX) # create MPI info object and set a few hints - info1 = MPI.Info.Create() - info1.Set("nc_header_align_size", "1024") - info1.Set("nc_var_align_size", "512") - info1.Set("nc_header_read_chunk_size", "256") + info = MPI.Info.Create() + info.Set("nc_header_align_size", "1024") + info.Set("nc_var_align_size", "512") + info.Set("nc_header_read_chunk_size", "256") # create a new file for writing - f = pnetcdf.File(filename=filename, mode = 'w', file_format = "NETCDF3_64BIT_DATA", comm=comm, info=info1) + f = pnetcdf.File(filename = filename, + mode = 'w', + format = "NC_64BIT_DATA", + comm = comm, + info = info) # define dimensions dim_z = f.def_dim('Z', NZ*nprocs) @@ -144,14 +150,13 @@ def pnetcdf_io(filename): if verbose and rank == 0: print_hints(f, var_zy, var_yx) - info1.Free() + info.Free() # close the file f.close() if __name__ == "__main__": - verbose = True comm = MPI.COMM_WORLD rank = comm.Get_rank() nprocs = comm.Get_size() @@ -168,10 +173,13 @@ def pnetcdf_io(filename): parser.add_argument("-q", help="Quiet mode (reports when fail)", action="store_true") args = parser.parse_args() - if args.q: verbose = False + verbose = False if args.q else True filename = args.dir + if verbose and rank == 0: + print("{}: example of set/get PnetCDF hints".format(os.path.basename(__file__))) + try: pnetcdf_io(filename) except BaseException as err: diff --git a/examples/nonblocking_write.py b/examples/nonblocking_write.py index 5395883..55bfe9a 100644 --- a/examples/nonblocking_write.py +++ b/examples/nonblocking_write.py @@ -49,18 +49,14 @@ def parse_help(): print(help_text) return help_flag -def print_info(info_used): - print("MPI hint: cb_nodes =", info_used.Get("cb_nodes")) - print("MPI hint: cb_buffer_size =", info_used.Get("cb_buffer_size")) - print("MPI hint: striping_factor =", info_used.Get("striping_factor")) - print("MPI hint: striping_unit =", info_used.Get("striping_unit")) def pnetcdf_io(filename, length): NDIMS = 3 NUM_VARS = 10 if verbose and rank == 0: - print("{}: example of calling nonblocking write APIs".format(os.path.basename(__file__))) + print("Number of variables = ", NUM_VARS) + print("Number of dimensions = ", NDIMS) # set up subarray access pattern starts = np.zeros(NDIMS, dtype=np.int32) @@ -86,16 +82,12 @@ def pnetcdf_io(filename, length): for j in range(bufsize): buf[i][j] = rank * i + 123 + j - comm.Barrier() - write_timing = MPI.Wtime() - # Create the file - try: - f = pnetcdf.File(filename=filename, mode = 'w', format = "NETCDF3_64BIT_DATA", comm=comm, info=None) - except OSError as e: - print("Error at {}:{} ncmpi_create() file {} ({})".format(__file__,inspect.currentframe().f_back.f_lineno, filename, e)) - comm.Abort() - sys.exit(1) + f = pnetcdf.File(filename = filename, + mode = 'w', + format = "NC_64BIT_DATA", + comm = comm, + info = None) # Define dimensions dims = [] @@ -112,9 +104,6 @@ def pnetcdf_io(filename, length): # Exit the define mode f.enddef() - # Get all the hints used - info_used = f.inq_info() - # Write one variable at a time, using iput APIs reqs = [] for i in range(NUM_VARS): @@ -153,38 +142,11 @@ def pnetcdf_io(filename, length): # detach the temporary buffer f.detach_buff() - # obtain write amount yet to now - put_size = f.inq_put_size() - put_size = comm.allreduce(put_size, op=MPI.SUM) - # Close the file f.close() - write_timing = MPI.Wtime() - write_timing - - write_size = bufsize * NUM_VARS * np.dtype(np.int32).itemsize - sum_write_size = comm.reduce(write_size, MPI.SUM, root=0) - max_write_timing = comm.reduce(write_timing, MPI.MAX, root=0) - - if rank == 0 and verbose: - print() - print("Total amount writes to variables only (exclude header) = {} bytes".format(sum_write_size)) - print("Total amount writes reported by pnetcdf (include header) = {} bytes".format(put_size)) - print() - subarray_size = (bufsize * np.dtype(np.int32).itemsize) / 1048576.0 - print_info(info_used) - print("Local array size {} x {} x {} integers, size = {:.2f} MB".format(length, length, length, subarray_size)) - sum_write_size /= 1048576.0 - print("Global array size {} x {} x {} integers, write size = {:.2f} GB".format(gsizes[0], gsizes[1], gsizes[2], sum_write_size/1024.0)) - - write_bw = sum_write_size / max_write_timing - print(" procs Global array size exec(sec) write(MB/s)") - print("------- ------------------ --------- -----------") - 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)) - if __name__ == "__main__": - verbose = True comm = MPI.COMM_WORLD rank = comm.Get_rank() nprocs = comm.Get_size() @@ -202,13 +164,16 @@ def pnetcdf_io(filename, length): parser.add_argument("-l", help="Size of each dimension of the local array\n") args = parser.parse_args() - if args.q: verbose = False + verbose = False if args.q else True length = 10 if args.l and int(args.l) > 0: length = int(args.l) filename = args.dir + if verbose and rank == 0: + print("{}: example of calling nonblocking write APIs".format(os.path.basename(__file__))) + try: pnetcdf_io(filename, length) except BaseException as err: diff --git a/examples/nonblocking_write_def.py b/examples/nonblocking_write_def.py index 23c83d4..8ebb860 100644 --- a/examples/nonblocking_write_def.py +++ b/examples/nonblocking_write_def.py @@ -50,19 +50,14 @@ def parse_help(): print(help_text) return help_flag -def print_info(info_used): - print("MPI hint: cb_nodes =", info_used.Get("cb_nodes")) - print("MPI hint: cb_buffer_size =", info_used.Get("cb_buffer_size")) - print("MPI hint: striping_factor =", info_used.Get("striping_factor")) - print("MPI hint: striping_unit =", info_used.Get("striping_unit")) def pnetcdf_io(file_name, length): NDIMS = 3 NUM_VARS = 10 if verbose and rank == 0: - print("{}: example of nonblocking APIs in define mode".format(os.path.basename(__file__))) - + print("Number of variables = ", NUM_VARS) + print("Number of dimensions = ", NDIMS) # set subarray access pattern starts = np.zeros(NDIMS, dtype=np.int32) @@ -88,11 +83,12 @@ def pnetcdf_io(file_name, length): for j in range(bufsize): buf[i][j] = rank * i + 123 + j - comm.Barrier() - write_timing = MPI.Wtime() - # Create the file - f = pnetcdf.File(filename=filename, mode = 'w', format = "NETCDF3_64BIT_DATA", comm=comm, info=None) + f = pnetcdf.File(filename = filename, + mode = 'w', + format = "NC_64BIT_DATA", + comm = comm, + info = None) # Define dimensions dims = [] @@ -140,41 +136,11 @@ def pnetcdf_io(file_name, length): # detach the temporary buffer f.detach_buff() - # Get all the hints used - info_used = f.inq_info() - - # check write amount - put_size = f.inq_put_size() - put_size = comm.allreduce(put_size, op=MPI.SUM) - # close the file f.close() - write_timing = MPI.Wtime() - write_timing - - write_size = bufsize * NUM_VARS * np.dtype(np.int32).itemsize - sum_write_size = comm.reduce(write_size, MPI.SUM, root=0) - max_write_timing = comm.reduce(write_timing, MPI.MAX, root=0) - - if rank == 0 and verbose: - print() - print("Total amount writes to variables only (exclude header) = {} bytes".format(sum_write_size)) - print("Total amount writes reported by pnetcdf (include header) = {} bytes".format(put_size)) - print() - subarray_size = (bufsize * np.dtype(np.int32).itemsize) / 1048576.0 - print_info(info_used) - print("Local array size {} x {} x {} integers, size = {:.2f} MB".format(length, length, length, subarray_size)) - sum_write_size /= 1048576.0 - print("Global array size {} x {} x {} integers, write size = {:.2f} GB".format(gsizes[0], gsizes[1], gsizes[2], sum_write_size/1024.0)) - - write_bw = sum_write_size / max_write_timing - print(" procs Global array size exec(sec) write(MB/s)") - print("------- ------------------ --------- -----------") - 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)) - if __name__ == "__main__": - verbose = True comm = MPI.COMM_WORLD rank = comm.Get_rank() nprocs = comm.Get_size() @@ -192,7 +158,7 @@ def pnetcdf_io(file_name, length): parser.add_argument("-l", help="Size of each dimension of the local array\n") args = parser.parse_args() - if args.q: verbose = False + verbose = False if args.q else True length = 10 if args.l and int(args.l) > 0: @@ -200,6 +166,9 @@ def pnetcdf_io(file_name, length): filename = args.dir + if verbose and rank == 0: + print("{}: example of nonblocking APIs in define mode".format(os.path.basename(__file__))) + try: pnetcdf_io(filename, length) except BaseException as err: diff --git a/examples/put_vara.py b/examples/put_vara.py index 1d4e431..61b7add 100644 --- a/examples/put_vara.py +++ b/examples/put_vara.py @@ -71,9 +71,6 @@ def parse_help(): def pnetcdf_io(filename, file_format): - if verbose and rank == 0: - print("{}: example of writing subarrays".format(os.path.basename(__file__))) - NY = 10 NX = 4 global_ny = NY @@ -81,8 +78,16 @@ def pnetcdf_io(filename, file_format): starts = [0, NX * rank] counts = [NY, NX] + if verbose and rank == 0: + print("Y dimension size = ", NY) + print("X dimension size = ", NX) + # Create the file - f = pnetcdf.File(filename=filename, mode = 'w', format = file_format, comm=comm, info=None) + f = pnetcdf.File(filename = filename, + mode = 'w', + format = file_format, + comm = comm, + info = None) # Add a global attribute: a time stamp at rank 0 if rank == 0: @@ -113,7 +118,7 @@ def pnetcdf_io(filename, file_format): short_att = np.int16(1000) var.put_att("short_att_name", short_att) - # Exit the define mode + # Exit the define mode f.enddef() # initialize write buffer @@ -127,7 +132,6 @@ def pnetcdf_io(filename, file_format): if __name__ == "__main__": - verbose = True comm = MPI.COMM_WORLD rank = comm.Get_rank() nprocs = comm.Get_size() @@ -145,15 +149,18 @@ def pnetcdf_io(filename, file_format): parser.add_argument("-k", help="File format: 1 for CDF-1, 2 for CDF-2, 5 for CDF-5") args = parser.parse_args() - if args.q: verbose = False + verbose = False if args.q else True file_format = None if args.k: - kind_dict = {'1':None, '2':"NETCDF3_64BIT_OFFSET", '5':"NETCDF3_64BIT_DATA"} + kind_dict = {'1':None, '2':"NC_64BIT_OFFSET", '5':"NC_64BIT_DATA"} file_format = kind_dict[args.k] filename = args.dir + if verbose and rank == 0: + print("{}: example of writing subarrays".format(os.path.basename(__file__))) + try: pnetcdf_io(filename, file_format) except BaseException as err: diff --git a/examples/put_varn_int.py b/examples/put_varn_int.py index 39026a4..7193e98 100644 --- a/examples/put_varn_int.py +++ b/examples/put_varn_int.py @@ -55,16 +55,23 @@ def parse_help(): print(help_text) return help_flag + def pnetcdf_io(file_name, file_format): NY = 4 NX = 10 NDIMS = 2 if verbose and rank == 0: - print("{}: example of writing multiple variables in a call".format(os.path.basename(__file__))) + print("Number of dimensions = ", NDIMS) + print("Y dimension size = ", NY) + print("X dimension size = ", NX) # create a new file - f = pnetcdf.File(filename=filename, mode = 'w', format=file_format, comm=comm, info=None) + f = pnetcdf.File(filename = filename, + mode = 'w', + format = file_format, + comm = comm, + info = None) # define dimensions dimx = f.def_dim('x',NX) @@ -143,17 +150,16 @@ def pnetcdf_io(file_name, file_format): # allocate I/O buffer and initialize its contents w_len = np.sum(np.prod(counts, axis=1)) - buffer = np.full(w_len, rank, dtype=np.int32) + w_buf = np.full(w_len, rank, dtype=np.int32) # set the buffer pointers to different offsets to the I/O buffe - v.put_var_all(buffer, start = starts, count = counts, num = num_reqs) + v.put_var_all(w_buf, start = starts, count = counts, num = num_reqs) # close the file f.close() if __name__ == "__main__": - verbose = True comm = MPI.COMM_WORLD rank = comm.Get_rank() nprocs = comm.Get_size() @@ -171,15 +177,18 @@ def pnetcdf_io(file_name, file_format): parser.add_argument("-k", help="File format: 1 for CDF-1, 2 for CDF-2, 5 for CDF-5") args = parser.parse_args() - if args.q: verbose = False + verbose = False if args.q else True file_format = None if args.k: - kind_dict = {'1':None, '2':"NETCDF3_64BIT_OFFSET", '5':"NETCDF3_64BIT_DATA"} + kind_dict = {'1':None, '2':"NC_64BIT_OFFSET", '5':"NC_64BIT_DATA"} file_format = kind_dict[args.k] filename = args.dir + if verbose and rank == 0: + print("{}: example of writing multiple variables in a call".format(os.path.basename(__file__))) + try: pnetcdf_io(filename, file_format) except BaseException as err: diff --git a/examples/transpose.py b/examples/transpose.py index 5473c27..516b03b 100644 --- a/examples/transpose.py +++ b/examples/transpose.py @@ -43,17 +43,17 @@ def parse_help(): print(help_text) return help_flag -def pnetcdf_io(filename, file_format, length): - if verbose and rank == 0: - print("{}: example of put/get 3D transposed arrays".format(os.path.basename(__file__))) +def pnetcdf_io(filename, file_format, length): NDIMS = 3 + if verbose and rank == 0: + print("Number of dimensions = ", NDIMS) - gsizes = np.zeros(NDIMS, dtype=np.int64) - starts = np.zeros(NDIMS, dtype=np.int64) - counts = np.zeros(NDIMS, dtype=np.int64) - imap = np.zeros(NDIMS, dtype=np.int64) + gsizes = np.zeros(NDIMS, dtype=np.int64) + starts = np.zeros(NDIMS, dtype=np.int64) + counts = np.zeros(NDIMS, dtype=np.int64) + imap = np.zeros(NDIMS, dtype=np.int64) startsT = np.zeros(NDIMS, dtype=np.int64) countsT = np.zeros(NDIMS, dtype=np.int64) @@ -73,10 +73,10 @@ def pnetcdf_io(filename, file_format, length): # set up subarray access pattern bufsize = 1 for i in range(NDIMS): - gsizes[i] = (length + i) * psizes[i] # global array size + gsizes[i] = (length + i) * psizes[i] # global array size starts[i] *= (length + i) # start indices - counts[i] = (length + i) # array elements - bufsize *= (length + i) + counts[i] = (length + i) # array elements + bufsize *= (length + i) # allocate buffer and initialize with contiguous numbers buf = np.empty(bufsize, dtype=int) @@ -84,11 +84,17 @@ def pnetcdf_io(filename, file_format, length): for k in range(counts[0]): for j in range(counts[1]): for i in range(counts[2]): - buf[index] = (starts[0]+k)*gsizes[1]*gsizes[2] + (starts[1]+j)*gsizes[2] + (starts[2]+i) + buf[index] = (starts[0]+k)*gsizes[1]*gsizes[2] + \ + (starts[1]+j)*gsizes[2] + \ + (starts[2]+i) index += 1 # Create the file - f = pnetcdf.File(filename=filename, mode = 'w', format = file_format, comm=comm, info=None) + f = pnetcdf.File(filename = filename, + mode = 'w', + format = file_format, + comm = comm, + info = None) # Define dimensions dim_z = f.def_dim("Z", gsizes[0]) @@ -99,29 +105,31 @@ def pnetcdf_io(filename, file_format, length): var_zyx = f.def_var("ZYX_var", pnetcdf.NC_INT, (dim_z, dim_y, dim_x)) # Define variable with transposed file layout: ZXY - var_zxy = f.def_var("ZXY_var", pnetcdf.NC_INT, (dim_z, dim_x, dim_y)) + var_zxy = f.def_var("ZXY_var", pnetcdf.NC_INT, (dim_z, dim_x, dim_y)) # Define variable with transposed file layout: YZX - var_yzx = f.def_var("YZX_var", pnetcdf.NC_INT, (dim_y, dim_z, dim_x)) + var_yzx = f.def_var("YZX_var", pnetcdf.NC_INT, (dim_y, dim_z, dim_x)) # Define variable with transposed file layout: YXZ - var_yxz = f.def_var("YXZ_var", pnetcdf.NC_INT, (dim_y, dim_x, dim_z)) + var_yxz = f.def_var("YXZ_var", pnetcdf.NC_INT, (dim_y, dim_x, dim_z)) # Define variable with transposed file layout: XZY - var_xzy = f.def_var("XZY_var", pnetcdf.NC_INT, (dim_x, dim_z, dim_y)) + var_xzy = f.def_var("XZY_var", pnetcdf.NC_INT, (dim_x, dim_z, dim_y)) # Define variable with transposed file layout: XYZ - var_xyz = f.def_var("XYZ_var", pnetcdf.NC_INT, (dim_x, dim_y, dim_z)) + var_xyz = f.def_var("XYZ_var", pnetcdf.NC_INT, (dim_x, dim_y, dim_z)) # Exit the define mode f.enddef() # Write the whole variable in file: ZYX var_zyx.put_var_all(buf, start=starts, count=counts) + # ZYX -> ZXY: - imap[1] = 1; imap[2] = counts[2]; imap[0] = counts[1]*counts[2] + imap[1] = 1; imap[2] = counts[2]; imap[0] = counts[1]*counts[2] startsT[0] = starts[0]; startsT[1] = starts[2]; startsT[2] = starts[1] countsT[0] = counts[0]; countsT[1] = counts[2]; countsT[2] = counts[1] var_zxy.put_var_all(buf, start = startsT, count = countsT, imap = imap) + # ZYX -> ZXY: imap[1] = 1; imap[2] = counts[2]; imap[0] = counts[1]*counts[2] startsT[0] = starts[0]; startsT[1] = starts[2]; startsT[2] = starts[1] @@ -157,7 +165,6 @@ def pnetcdf_io(filename, file_format, length): if __name__ == "__main__": - verbose = True comm = MPI.COMM_WORLD rank = comm.Get_rank() nprocs = comm.Get_size() @@ -177,11 +184,11 @@ def pnetcdf_io(filename, file_format, length): args = parser.parse_args() - if args.q: verbose = False + verbose = False if args.q else True file_format = None if args.k: - kind_dict = {'1':None, '2':"NETCDF3_64BIT_OFFSET", '5':"NETCDF3_64BIT_DATA"} + kind_dict = {'1':None, '2':"NC_64BIT_OFFSET", '5':"NC_64BIT_DATA"} file_format = kind_dict[args.k] length = 10 @@ -189,6 +196,9 @@ def pnetcdf_io(filename, file_format, length): filename = args.dir + if verbose and rank == 0: + print("{}: example of put/get 3D transposed arrays".format(os.path.basename(__file__))) + try: pnetcdf_io(filename, file_format, length) except BaseException as err: diff --git a/examples/transpose2D.py b/examples/transpose2D.py index e5b1db1..63ebcc4 100644 --- a/examples/transpose2D.py +++ b/examples/transpose2D.py @@ -66,11 +66,11 @@ def parse_help(): return help_flag def pnetcdf_io(filename, file_format, length): - if verbose and rank == 0: - print("{}: example of put/get 2D transposed arrays".format(os.path.basename(__file__))) - NDIMS = 2 + if verbose and rank == 0: + print("Number of dimensions = ", NDIMS) + gsizes = np.zeros(NDIMS, dtype=np.int64) starts = np.zeros(NDIMS, dtype=np.int64) counts = np.zeros(NDIMS, dtype=np.int64) @@ -114,7 +114,11 @@ def pnetcdf_io(filename, file_format, length): # Create the file - f = pnetcdf.File(filename=filename, mode = 'w', format = file_format, comm=comm, info=None) + f = pnetcdf.File(filename = filename, + mode = 'w', + format = file_format, + comm = comm, + info = None) # Define dimensions dim_y = f.def_dim("Y", gsizes[0]) @@ -144,7 +148,6 @@ def pnetcdf_io(filename, file_format, length): if __name__ == "__main__": - verbose = True comm = MPI.COMM_WORLD rank = comm.Get_rank() nprocs = comm.Get_size() @@ -163,11 +166,11 @@ def pnetcdf_io(filename, file_format, length): parser.add_argument("-l", help="size of each dimension of the local array") args = parser.parse_args() - if args.q: verbose = False + verbose = False if args.q else True file_format = None if args.k: - kind_dict = {'1':None, '2':"NETCDF3_64BIT_OFFSET", '5':"NETCDF3_64BIT_DATA"} + kind_dict = {'1':None, '2':"NC_64BIT_OFFSET", '5':"NC_64BIT_DATA"} file_format = kind_dict[args.k] length = 2 @@ -175,6 +178,9 @@ def pnetcdf_io(filename, file_format, length): filename = args.dir + if verbose and rank == 0: + print("{}: example of put/get 2D transposed arrays".format(os.path.basename(__file__))) + try: pnetcdf_io(filename, file_format, length) except BaseException as err: