On Wed, Feb 1, 2023 at 9:06 PM Barry Smith <bsm...@petsc.dev> wrote: > > Hmm, When I do > > def somePythonfunction(): > ... > x = da.createLocalVec() > .... > return > > The x seems to be properly "garbage collected" at some point; I was > guessing it was at the return or some time later than the return, but it > did not wait until PetscFinalize(). > > So does the x above HAVE to exist until PetscFinalize()? If not then > somehow Python realizes x is no longer referenced and VecDestroy() is > called on it when some garbage collection takes place. I would like the > same thing happen with the x = da.getLocalVec() but calling > x.da.restoreLocalVec() instead of x.destroy(). Note specifically > parallelism doesn't play a role here because x is local. >
It can happen at a random time, so it is unsafe to structure without a "with" since it could hold the local vector maybe after the user tries to destroy the DA. Matt > Barry > > > On Feb 1, 2023, at 8:07 PM, Matthew Knepley <knep...@gmail.com> wrote: > > On Wed, Feb 1, 2023 at 6:33 PM Barry Smith <bsm...@petsc.dev> wrote: > >> >> Would it be possible to rig the xlocal obtained from >> self.da.getLocalVec() to automatically trigger the da.restoreLocalVec() >> when the xlocal goes out of scope so that the user does not need to >> explicitly make the call. If so, how should I go about implementing it? >> > > This would work in C++ where the object has a defined lifetime, but Python > is garbage collected. The way it does this > pattern is using the "with" statement. > > Matt > > >> Barry >> > -- > What most experimenters take for granted before they begin their > experiments is infinitely more interesting than any results to which their > experiments lead. > -- Norbert Wiener > > https://www.cse.buffalo.edu/~knepley/ > <http://www.cse.buffalo.edu/~knepley/> > > > -- What most experimenters take for granted before they begin their experiments is infinitely more interesting than any results to which their experiments lead. -- Norbert Wiener https://www.cse.buffalo.edu/~knepley/ <http://www.cse.buffalo.edu/~knepley/>