Skip to content

use starts and counts only in varn APIs #50

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
Sep 4, 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
16 changes: 8 additions & 8 deletions examples/collective_write.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,22 +60,22 @@ def pnetcdf_io(filename, file_format, length):
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)
start = np.zeros(NDIMS, dtype=np.int32)
count = 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]
starts[2] = (rank // (psizes[0] * psizes[1])) % psizes[2]
start[0] = rank % psizes[0]
start[1] = (rank // psizes[1]) % psizes[1]
start[2] = (rank // (psizes[0] * psizes[1])) % psizes[2]

bufsize = 1
for i in range(NDIMS):
gsizes[i] = length * psizes[i]
starts[i] *= length
counts[i] = length
start[i] *= length
count[i] = length
bufsize *= length

# Allocate buffer and initialize with non-zero numbers
Expand Down Expand Up @@ -111,7 +111,7 @@ def pnetcdf_io(filename, file_format, length):

# Collectively write one variable at a time
for i in range(NUM_VARS):
vars[i].put_var_all(buf[i], start = starts, count = counts)
vars[i].put_var_all(buf[i], start = start, count = count)

# Close the file
f.close()
Expand Down
20 changes: 10 additions & 10 deletions examples/fill_mode.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,34 +107,34 @@ def pnetcdf_io(filename):
f.enddef()

# set subarray access pattern
starts = np.array([0, NX * rank])
counts = np.array([NY, NX])
start = np.array([0, NX * rank])
count = np.array([NY, NX])

# allocate user buffer
buf = np.array([[rank] * NX] * NY).astype('i4')

# do not write the variable in full
counts[1] -= 1
fix_var.put_var_all(buf, start = starts, count = counts)
count[1] -= 1
fix_var.put_var_all(buf, start = start, count = count)

# check fill value
no_fill, fill_value = fix_var.inq_fill()
assert(no_fill == 0)
assert(fill_value == pnetcdf.NC_FILL_INT)

# fill the 1st record of the record variable
counts[0] = 1
rec_var.fill_rec(starts[0])
count[0] = 1
rec_var.fill_rec(start[0])

# write to the 1st record
rec_var.put_var_all(buf, start = starts, count = counts)
rec_var.put_var_all(buf, start = start, count = count)

# fill the 2nd record of the record variable
starts[0] = 1
rec_var.fill_rec(starts[0])
start[0] = 1
rec_var.fill_rec(start[0])

# write to the 2nd record
rec_var.put_var_all(buf, start = starts, count = counts)
rec_var.put_var_all(buf, start = start, count = count)

# close file
f.close()
Expand Down
32 changes: 16 additions & 16 deletions examples/flexible_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,11 @@ def pnetcdf_io(filename, file_format):
# create an MPI derived datatype to exclude ghost cells
array_of_sizes = np.array([NZ + 2 * ghost_len, NY + 2 * ghost_len])
array_of_subsizes = np.array([NZ, NY])
array_of_starts = np.array([ghost_len, ghost_len])
array_of_start = np.array([ghost_len, ghost_len])

subarray = MPI.INT.Create_subarray(array_of_sizes, \
array_of_subsizes, \
array_of_starts, \
array_of_start, \
order=MPI.ORDER_C)
subarray.Commit()

Expand All @@ -135,12 +135,12 @@ def pnetcdf_io(filename, file_format):
buf_zy = np.full(buffer_len, rank, dtype=np.int32)

# set the subarray access pattern
starts = np.array([NZ * rank, 0])
counts = np.array([NZ, NY])
start = np.array([NZ * rank, 0])
count = np.array([NZ, NY])

# calling a blocking flexible API using put_var_all()
var_zy.put_var_all(buf_zy, start = starts, \
count = counts, \
var_zy.put_var_all(buf_zy, start = start, \
count = count, \
bufcount = 1, \
buftype = subarray)

Expand All @@ -153,8 +153,8 @@ 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,
var_zy.get_var_all(buf_zy, start = start,
count = count,
bufcount = 1,
buftype = subarray)

Expand All @@ -177,22 +177,22 @@ def pnetcdf_io(filename, file_format):
# var_yx is partitioned along X dimension
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])
array_of_start = np.array([ghost_len, ghost_len])
subarray = MPI.DOUBLE.Create_subarray(array_of_sizes,
array_of_subsizes,
array_of_starts,
array_of_start,
order=MPI.ORDER_C)
subarray.Commit()

# initialize write user buffer
buffer_len = (NY + 2 * ghost_len) * (NX + 2 * ghost_len)
buf_yx = np.full(buffer_len, rank, dtype=np.float64)
starts = np.array([0, NX * rank])
counts = np.array([NY, NX])
start = np.array([0, NX * rank])
count = np.array([NY, NX])

# calling a blocking flexible write API
req_id = var_yx.iput_var(buf_yx, start = starts,
count = counts,
req_id = var_yx.iput_var(buf_yx, start = start,
count = count,
bufcount = 1,
buftype = subarray)

Expand All @@ -208,8 +208,8 @@ 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,
req_id = var_yx.iget_var(buf_yx, start = start,
count = count,
bufcount = 1,
buftype=subarray)

Expand Down
8 changes: 4 additions & 4 deletions examples/get_vara.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,12 @@ def pnetcdf_io(filename, file_format):
# set access pattern for reading subarray
local_ny = global_ny
local_nx = global_nx // nprocs
starts = [0, local_nx * rank]
counts = [local_ny, local_nx]
start = [0, local_nx * rank]
count = [local_ny, local_nx]

# Read a subarray in collective mode
r_buf = np.empty(tuple(counts), v.dtype)
v.get_var_all(r_buf, start = starts, count = counts)
r_buf = np.empty(tuple(count), v.dtype)
v.get_var_all(r_buf, start = start, count = count)

# close the file
f.close()
Expand Down
28 changes: 14 additions & 14 deletions examples/ghost_cell.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def parse_help():

def pnetcdf_io(filename, file_format, length):

counts = [length, length + 1]
count = [length, length + 1]
psizes = MPI.Compute_dims(nprocs, 2)

if verbose and rank == 0:
Expand All @@ -104,24 +104,24 @@ def pnetcdf_io(filename, file_format, length):
print("rank {}: dim rank= {} {}".format(rank, local_rank[0], local_rank[1]))

# 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]],
count = np.array([length, length + 1], dtype=np.int64)
start = np.array([local_rank[0] * count[0], local_rank[1] * count[1]],
dtype=np.int64)
if verbose:
print("starts= {} {} counts= {} {}".format(starts[0], starts[1], counts[0], counts[1]))
print("start= {} {} count= {} {}".format(start[0], start[1], count[0], count[1]))

# allocate and initialize buffer with ghost cells on both ends of each dim
nghosts = 2
bufsize = (counts[0] + 2 * nghosts) * (counts[1] + 2 * nghosts)
bufsize = (count[0] + 2 * nghosts) * (count[1] + 2 * nghosts)
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:
buf[i * (counts[1] + 2 * nghosts) + j] = rank
for i in range(count[0] + 2 * nghosts):
for j in range(count[1] + 2 * nghosts):
if nghosts <= i < count[0] + nghosts and \
nghosts <= j < count[1] + nghosts:
buf[i * (count[1] + 2 * nghosts) + j] = rank
else:
# set values of all ghost cells to -8
buf[i * (counts[1] + 2 * nghosts) + j] = -8
buf[i * (count[1] + 2 * nghosts) + j] = -8

# Create the file
f = pnetcdf.File(filename = filename,
Expand All @@ -143,11 +143,11 @@ def pnetcdf_io(filename, file_format, length):
# set imap pattern for local buffer
imap = np.zeros(2, dtype=np.int64)
imap[1] = 1
imap[0] = counts[1] + 2 * nghosts
buf_ptr = buf[nghosts * (counts[1] + 2 * nghosts + 1):]
imap[0] = count[1] + 2 * nghosts
buf_ptr = buf[nghosts * (count[1] + 2 * nghosts + 1):]

# Write data to the variable
var.put_var_all(buf_ptr, start = starts, count = counts, imap = imap)
var.put_var_all(buf_ptr, start = start, count = count, imap = imap)

# Close the file
f.close()
Expand Down
18 changes: 9 additions & 9 deletions examples/nonblocking_write.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,21 +59,21 @@ def pnetcdf_io(filename, length):
print("Number of dimensions = ", NDIMS)

# set up subarray access pattern
starts = np.zeros(NDIMS, dtype=np.int32)
counts = np.zeros(NDIMS, dtype=np.int32)
start = np.zeros(NDIMS, dtype=np.int32)
count = np.zeros(NDIMS, dtype=np.int32)
gsizes = np.zeros(NDIMS, dtype=np.int32)
buf = []

psizes = MPI.Compute_dims(nprocs, NDIMS)
starts[0] = rank % psizes[0]
starts[1] = (rank // psizes[1]) % psizes[1]
starts[2] = (rank // (psizes[0] * psizes[1])) % psizes[2]
start[0] = rank % psizes[0]
start[1] = (rank // psizes[1]) % psizes[1]
start[2] = (rank // (psizes[0] * psizes[1])) % psizes[2]

bufsize = 1
for i in range(NDIMS):
gsizes[i] = length * psizes[i]
starts[i] *= length
counts[i] = length
start[i] *= length
count[i] = length
bufsize *= length

# Allocate buffer and initialize with non-zero numbers
Expand Down Expand Up @@ -107,7 +107,7 @@ def pnetcdf_io(filename, length):
# Write one variable at a time, using iput APIs
reqs = []
for i in range(NUM_VARS):
req_id = vars[i].iput_var(buf[i], start = starts, count = counts)
req_id = vars[i].iput_var(buf[i], start = start, count = count)
reqs.append(req_id)

# commit posted noblocking requests
Expand All @@ -126,7 +126,7 @@ def pnetcdf_io(filename, length):
# Write one variable at a time, using bput APIs
reqs = []
for i in range(NUM_VARS):
req_id = vars[i].bput_var(buf[i], start = starts, count = counts)
req_id = vars[i].bput_var(buf[i], start = start, count = count)
reqs.append(req_id)
# can safely change contents or free up the buf[i] here

Expand Down
18 changes: 9 additions & 9 deletions examples/nonblocking_write_def.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,21 +60,21 @@ def pnetcdf_io(file_name, length):
print("Number of dimensions = ", NDIMS)

# set subarray access pattern
starts = np.zeros(NDIMS, dtype=np.int32)
counts = np.zeros(NDIMS, dtype=np.int32)
start = np.zeros(NDIMS, dtype=np.int32)
count = np.zeros(NDIMS, dtype=np.int32)
gsizes = np.zeros(NDIMS, dtype=np.int32)
buf = []

psizes = MPI.Compute_dims(nprocs, NDIMS)
starts[0] = rank % psizes[0]
starts[1] = (rank // psizes[1]) % psizes[1]
starts[2] = (rank // (psizes[0] * psizes[1])) % psizes[2]
start[0] = rank % psizes[0]
start[1] = (rank // psizes[1]) % psizes[1]
start[2] = (rank // (psizes[0] * psizes[1])) % psizes[2]

bufsize = 1
for i in range(NDIMS):
gsizes[i] = length * psizes[i]
starts[i] *= length
counts[i] = length
start[i] *= length
count[i] = length
bufsize *= length

# Allocate buffer and initialize with non-zero numbers
Expand Down Expand Up @@ -104,7 +104,7 @@ def pnetcdf_io(file_name, length):

# Write one variable at a time
for i in range(NUM_VARS):
vars[i].iput_var(buf[i], start = starts, count = counts)
vars[i].iput_var(buf[i], start = start, count = count)

# exit define mode and enter data mode
f.enddef()
Expand All @@ -120,7 +120,7 @@ def pnetcdf_io(file_name, length):
# call bput for writing to one variable at a time
reqs = []
for i in range(NUM_VARS):
req_id = vars[i].bput_var(buf[i], start = starts, count = counts)
req_id = vars[i].bput_var(buf[i], start = start, count = count)
reqs.append(req_id)
# can safely change contents or free up the buf[i] here

Expand Down
7 changes: 4 additions & 3 deletions examples/put_vara.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ def pnetcdf_io(filename, file_format):
NX = 4
global_ny = NY
global_nx = NX * nprocs
starts = [0, NX * rank]
counts = [NY, NX]
start = [0, NX * rank]
count = [NY, NX]

if verbose and rank == 0:
print("Y dimension size = ", NY)
Expand Down Expand Up @@ -125,7 +125,8 @@ def pnetcdf_io(filename, file_format):
buf = np.zeros(shape = (NY, NX), dtype = "i4") + rank

# Write data to the variable
var.put_var_all(buf, start = starts, count = counts)
# var.put_var_all(buf, start = starts, count = counts)
var[0:NY, NX*rank:NX*rank+NX] = buf

# Close the file
f.close()
Expand Down
Loading
Loading