[petsc-dev] xlocal = da.getLocalVec() .. da.restoreLocalVec(xlocal) skip the restore
Barry Smith
bsmith at petsc.dev
Thu Feb 2 13:53:27 CST 2023
Jed,
Thanks for the pointer, I got it working.
> On Feb 2, 2023, at 1:11 PM, Jed Brown <jed at jedbrown.org> wrote:
>
> Barry Smith <bsmith at petsc.dev> writes:
>
>>> On Feb 1, 2023, at 9:17 PM, Matthew Knepley <knepley at gmail.com> wrote:
>>>
>>> On Wed, Feb 1, 2023 at 9:06 PM Barry Smith <bsmith at petsc.dev <mailto:bsmith at 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.
>>
>> Thanks. I am assuming you are talking about the getLocalVec() case. So it appears I need to dynamically attach an __exit__() method to the Vec in da.getLocalVec() so that restoreLocalVec() gets called https://www.geeksforgeeks.org/with-statement-in-python/ when the local vector goes out of scope of the with.
>>
>> I assume I can have multiple independent objects created with a with or I guess I could nest them when needed for local representations of u and du for TS.IFunction.
>
> Yes.
>
> See `with v.localForm() as loc` in src/binding/petsc4py/test/test_vec.py and the corresponding implementation. There are a few other similar context handlers.
>
> https://gitlab.com/petsc/petsc/-/commit/afbe0322dffa
More information about the petsc-dev
mailing list