Skip to content

Commit ffc0d8c

Browse files
python - detached borrowed arrays with take_array
Detached the arrays MatMult lends to libCEED with take_array from #1995, matching CeedVectorTakeArray in the C.
1 parent 63dfccc commit ffc0d8c

1 file changed

Lines changed: 10 additions & 7 deletions

File tree

examples/python/bpsraw.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -288,14 +288,18 @@ def set_ceed_array(self, ceed_vec, vec, mode):
288288
ceed_vec.set_array(array, memtype=self.mem_type, cmode=libceed.USE_POINTER)
289289
return handle
290290

291-
def restore_ceed_array(self, vec, handle, mode):
292-
"""Release a device array borrowed by set_ceed_array
291+
def restore_ceed_array(self, ceed_vec, vec, handle, mode):
292+
"""Release an array borrowed by set_ceed_array
293+
294+
C: CeedVectorTakeArray() + VecRestoreArrayAndMemType().
293295
294296
Args:
297+
ceed_vec: CeedVector the array was set on
295298
vec: PETSc Vec whose array was borrowed
296299
handle: CUDA handle returned by set_ceed_array, or None
297300
mode: Access mode used in set_ceed_array
298301
"""
302+
ceed_vec.take_array(self.mem_type)
299303
if handle is not None:
300304
vec.restoreCUDAHandle(handle, mode)
301305

@@ -318,10 +322,9 @@ def mult(self, A, X, Y):
318322
# Apply libCEED operator
319323
self.op_apply.apply(self.x_ceed, self.y_ceed)
320324

321-
# Restore arrays; the C also detaches them with CeedVectorTakeArray, which the Python bindings do not
322-
# wrap, so they are rebound instead
323-
self.restore_ceed_array(self.X_loc, x_handle, "r")
324-
self.restore_ceed_array(self.Y_loc, y_handle, "w")
325+
# Restore arrays
326+
self.restore_ceed_array(self.x_ceed, self.X_loc, x_handle, "r")
327+
self.restore_ceed_array(self.y_ceed, self.Y_loc, y_handle, "w")
325328

326329
# Local-to-global
327330
Y.zeroEntries()
@@ -357,7 +360,7 @@ def compute_error_max(ctx, op_error, X, target, mpi_comm):
357360
op_error.apply(ctx.x_ceed, collocated_error)
358361

359362
# Restore PETSc array
360-
ctx.restore_ceed_array(ctx.X_loc, x_handle, "r")
363+
ctx.restore_ceed_array(ctx.x_ceed, ctx.X_loc, x_handle, "r")
361364

362365
# Reduce max error
363366
with collocated_error.array_read(memtype=libceed.MEM_HOST) as e:

0 commit comments

Comments
 (0)