[petsc-users] When to perform PETSc.Vec assembly with petsc4py

Matthew Knepley knepley at gmail.com
Tue Jul 15 07:18:04 CDT 2025


On Tue, Jul 15, 2025 at 8:13 AM SCOTTO Alexandre via petsc-users <
petsc-users at mcs.anl.gov> wrote:

> Hello Jose,
>
> Thanks for your answer. Then it seems that I have under the hood usages of
> setValues() in my code since I do not explicitly make use of it but still
> has problems when not assembling my vector. Do I need to assemble vectors
> after Mat.mult or Scatter.scatter?
>

No. The purpose of VecAsseblyBegin/End() is to move values from processes
that do not own them to the processes that do. PETSc does this
automatically for MatMult() and VecScatter routines because we know exactly
where values are headed. However, when users call VecSetValues(), they may
set locations that are owned by other processes. We could communicate these
immediately, but that might be expensive for a series of VecSetValues()
calls, so we wait until you call VecAssembly().

Note that direct assignment to the array can only set local values. This is
equivalent to VecGetArray().

  Thanks,

     Matt


> By the way, I did not know that PETSc.Vec objects supported direct
> assignments like x[i] = ..., so I rather use assignments of the form
> x.array = ...
>
> From what I understand from the GitHub page (
> https://urldefense.us/v3/__https://gitlab.com/petsc/petsc/-/blob/main/src/binding/petsc4py/src/petsc4py/PETSc/Vec.pyx__;!!G_uCfscf7eWS!f3M5SmfhSqew4tgfvFDjdLnd3q3kT_KzcCitJTJC0HRl1YvbSNBr5Uuzv6OWP1pVQBgbi_WJlekerylpGcBHQ9vsbzL1suFBQHkoPIwhdw$
> ) it seems, that doing so, we only access the local portion of the array
> which should not require any assemble() am I right?
>
> Best regards,
> Alexandre.
>
>
> -----Message d'origine-----
> De : Jose E. Roman <jroman at dsic.upv.es>
> Envoyé : mardi 15 juillet 2025 12:21
> À : SCOTTO Alexandre <alexandre.scotto at irt-saintexupery.com>
> Cc : petsc-users at mcs.anl.gov
> Objet : Re: [petsc-users] When to perform PETSc.Vec assembly with petsc4py
>
> Assembly is needed after a call to x.setValues() or any of its variants.
>
> https://urldefense.us/v3/__https://petsc.org/main/petsc4py/reference/petsc4py.PETSc.Vec.html*petsc4py.PETSc.Vec.setValue__;Iw!!G_uCfscf7eWS!f3M5SmfhSqew4tgfvFDjdLnd3q3kT_KzcCitJTJC0HRl1YvbSNBr5Uuzv6OWP1pVQBgbi_WJlekerylpGcBHQ9vsbzL1suFBQHlKEPOyQg$
> Take into account that in python the notation x[i] = ...  with call
> x.setValues() under the hood.
>
> Jose
>
>
> > El 15 jul 2025, a las 12:02, SCOTTO Alexandre via petsc-users <
> petsc-users at mcs.anl.gov> escribió:
> >
> > Dear PETSc community,
> >  As a beginner in the MPI world and with the PETSc library, I come with
> a possibly very naive question.
> > I know from the documentation that assembling vectors must be done, but
> it is not clear to me when to perform this operation.
> > Is there a simple way to know when a vector need be assembled and when
> it is not?
> >  Thanks in advance.
> > Regards,
> > Alexandre.
>
>

-- 
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://urldefense.us/v3/__https://www.cse.buffalo.edu/*knepley/__;fg!!G_uCfscf7eWS!fx-I-mCGkHBLHWAaiP_nkFN7915JhuHJsjJm8Qpi3_otAh9GDsilnbnEQsr0-PfcqLdM5RdSw58rYsASqEIL$  <https://urldefense.us/v3/__http://www.cse.buffalo.edu/*knepley/__;fg!!G_uCfscf7eWS!fx-I-mCGkHBLHWAaiP_nkFN7915JhuHJsjJm8Qpi3_otAh9GDsilnbnEQsr0-PfcqLdM5RdSw58rYq7e9dsG$ >
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mcs.anl.gov/pipermail/petsc-users/attachments/20250715/c7185aa8/attachment.html>


More information about the petsc-users mailing list