[petsc-users] When to perform PETSc.Vec assembly with petsc4py
Stefano Zampini
stefano.zampini at gmail.com
Tue Jul 15 08:17:07 CDT 2025
A.multTranspose(x, y) produces y = A^t * x
If it is not the case, you are doing something wrong
I minimal working example will help us debug your approach.
You can take a look at the test folder
https://urldefense.us/v3/__https://gitlab.com/petsc/petsc/-/tree/main/src/binding/petsc4py/test__;!!G_uCfscf7eWS!dhEH-iztM8EuysrQ-SA3_P74GVXmb3CXNESdTF-hHBHpAa-fQbeM0Lf27pHtwK5mW_VFNYIFPHdQbhUpPxZdxitcNaZSyTY$ , the
demo folder
https://urldefense.us/v3/__https://gitlab.com/petsc/petsc/-/tree/main/src/binding/petsc4py/demo__;!!G_uCfscf7eWS!dhEH-iztM8EuysrQ-SA3_P74GVXmb3CXNESdTF-hHBHpAa-fQbeM0Lf27pHtwK5mW_VFNYIFPHdQbhUpPxZdxitcupJqo4Q$
There are also Python examples here
https://urldefense.us/v3/__https://gitlab.com/stefanozampini/petscexamples__;!!G_uCfscf7eWS!dhEH-iztM8EuysrQ-SA3_P74GVXmb3CXNESdTF-hHBHpAa-fQbeM0Lf27pHtwK5mW_VFNYIFPHdQbhUpPxZdxitcntk_xfM$
Il giorno mar 15 lug 2025 alle ore 14:49 SCOTTO Alexandre via petsc-users <
petsc-users at mcs.anl.gov> ha scritto:
> 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$
> <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$
> <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!dhEH-iztM8EuysrQ-SA3_P74GVXmb3CXNESdTF-hHBHpAa-fQbeM0Lf27pHtwK5mW_VFNYIFPHdQbhUpPxZdxitcsLpPCdI$
> <https://urldefense.us/v3/__http://www.cse.buffalo.edu/*knepley/__;fg!!G_uCfscf7eWS!cspsHbAhId4QoslCKt3i_holgL7kvzmVSbBmROUEJ9HuA2YEjyfDurQ0XiBEpAaqohjglNK31xfzm4zAiM3wDxpywkRKnJnXtifrP1eVXw$>
>
--
Stefano
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mcs.anl.gov/pipermail/petsc-users/attachments/20250715/8760c208/attachment.html>
More information about the petsc-users
mailing list