Skip to content

simplify examples #42

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 23 additions & 54 deletions examples/collective_write.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = (
Expand All @@ -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]
Expand All @@ -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 = []
Expand All @@ -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)

Expand All @@ -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
Expand Down
13 changes: 8 additions & 5 deletions examples/create_open.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand All @@ -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:
Expand Down
17 changes: 14 additions & 3 deletions examples/fill_mode.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand All @@ -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:
Expand Down
47 changes: 29 additions & 18 deletions examples/flexible_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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()
Expand All @@ -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:
Expand Down
15 changes: 10 additions & 5 deletions examples/get_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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:
Expand Down
Loading
Loading