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

Matthew Knepley knepley at gmail.com
Tue Jul 15 09:05:44 CDT 2025


We check that the x argument is assembled in MatMultTranspose(), but not y.
It seems possible that y could have stashed values that get communicated
when the operation is performed. Why do we not check that y is assembled?

  Thanks,

     Matt

On Tue, Jul 15, 2025 at 9:36 AM Jose E. Roman <jroman at dsic.upv.es> wrote:

> It is better if you can provide a full python script that we can run to
> reproduce the problem.
>
> Jose
>
>
> > El 15 jul 2025, a las 14:49, SCOTTO Alexandre <
> alexandre.scotto at irt-saintexupery.com> escribió:
> >
> > Ok I get it, Vec.assemble() is mandatory whenever MPI communications are
> required to get the values to appropriate processes.
> >  To provide more information, I am in a situation where I perform a
> A.multTranspose(x, y), where y is a vector that has been filled in with
> values earlier in the process. At this stage, I no longer care of these
> values and I expect the multTranspose() to override the values. But what I
> get is:
> > ·         if I do y.assemble() before the transpose multiplication, then
> y is filled-in with the correct result.
> > ·         if I do not perform y.assemble() before the transpose
> multiplication, I actually get y = y + A^T(x), i.e. a result rather of the
> form multTransposeAdd().
> > ·         If I do A.multTranspose(x, y) twice, then I get the correct
> result y = A^T(x).
> >  This makes me think that I am misusing something at some points, but it
> is not clear what. If someone has a hint to explain this behaviour that
> would help me better understand how to properly use PETSc!
> >  Regards,
> > Alexandre.
> >  De : Matthew Knepley <knepley at gmail.com>
> > Envoyé : mardi 15 juillet 2025 14:18
> > À : SCOTTO Alexandre <alexandre.scotto at irt-saintexupery.com>
> > Cc : Jose E. Roman <jroman at dsic.upv.es>; petsc-users at mcs.anl.gov
> > Objet : Re: [petsc-users] When to perform PETSc.Vec assembly with
> petsc4py
> >  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!dSyL6vBiLAFtj65CVYTcdDSdHEss-RYYzDlW5AXqReVAaV4ND9G4hKjVv2091d4dmLUf7tF09u5_6hVbw5fP$ 
>
>
>

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


More information about the petsc-users mailing list