[petsc-users] Clarification for use of MatMPIBAIJSetPreallocationCSR

Junchao Zhang junchao.zhang at gmail.com
Fri Mar 1 12:51:45 CST 2024


On Fri, Mar 1, 2024 at 11:10 AM Fabian Wermelinger <fab4100 at posteo.ch>
wrote:

> On Fri, 01 Mar 2024 10:09:17 -0600, Junchao Zhang wrote:
> >No. MatMPIBAIJSetPreallocationCSR() copies the data, but
> >VecCreateMPIWithArray() does not copy (only use pointers user provided).
>
> OK, my understanding was that MatMPIBAIJSetPreallocationCSR DOES NOT copy
> since
> it has direct access to matrix entries in the required PETSc layout ->
> thank you
>
> >I think your intention is to avoid memory copies.
>
> Yes. (not only copies but also avoid unnecessary increase of memory
> footprint)
>
> >If it is MATMPIAIJ, we can do
> >    MatMPIAIJSetPreallocationCSR(A, i, j, v); // let petsc copy i, j v
> >
> >    // To quickly update A when you have an updated v[]
> >
> >    MatUpdateMPIAIJWithArray(A, v); // copy v[], but faster than
> MatSetValues()
>
> Thanks for this input!
>
> >But it seems we currently do not have a MatUpdateMPIBAIJWithArray()  :(
>
> So when my v[] changed during iterations (only the v[] used during the
> MatMPIBAIJSetPreallocationCSR call, the matrix structure/connectivity has
> NOT
> changed), it is not sufficient to just call KSPSetOperators() before
> calling
> KSPSolve()?

Correct, your v[] is not shared with petsc


> I must copy the updated values in v[] explicitly into the PETSc
> matrix, then call KSPSetOperators() followed by KSPSolve()? (for every
> iteration in my application)
>
Yes

>
> The preferred method then is to use MatSetValues() for the copies (or
> possibly
> MatSetValuesRow() or MatSetValuesBlocked())?
>
 MatSetValuesRow()

>
> The term "Preallocation" is confusing to me.  For example,
> MatCreateMPIBAIJWithArrays clearly states in the doc that arrays are copied
> (https://urldefense.us/v3/__https://petsc.org/release/manualpages/Mat/MatCreateMPIBAIJWithArrays/__;!!G_uCfscf7eWS!YLXb4luirYVduVkmxILIpET7hYcMAPTHUxHG7tR7os-mxUGGqYt_XTB0xPp5y5CU2gWNtgNEmD8gu3rmtqcqNfLfc2Oe$ ),
> I would
> then assume PETSc maintains internal storage for it.  If something is
> preallocated, I would not make that assumption.
>
"Preallocation" in petsc means "tell petsc sizes of rows in a matrix",  so
that petsc can preallocate the memory before you do MatSetValues().
This is clearer in
https://urldefense.us/v3/__https://petsc.org/release/manualpages/Mat/MatMPIAIJSetPreallocation/__;!!G_uCfscf7eWS!YLXb4luirYVduVkmxILIpET7hYcMAPTHUxHG7tR7os-mxUGGqYt_XTB0xPp5y5CU2gWNtgNEmD8gu3rmtqcqNdzTPnSf$ 


> Thank you for your time, I appreciate the inputs!
>
> All best,
>
> --
> fabs
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mcs.anl.gov/pipermail/petsc-users/attachments/20240301/c22c7d0f/attachment.html>


More information about the petsc-users mailing list