[petsc-users] Question about MATMPIAIJ and column indexes ordering
Barry Smith
bsmith at petsc.dev
Mon Jan 8 09:49:39 CST 2024
> On Jan 8, 2024, at 4:31 AM, Deuse, Mathieu via petsc-users <petsc-users at mcs.anl.gov> wrote:
>
> Hello,
>
> I have a piece of code which generates a matrix in CSR format, but the without sorting the column indexes in increasing order within each row. This seems not to be 100% compatible with the MATMPIAIJ format: the documentation of MatCreateMPIAIJWithArrays indeed mentions 'row-major ordering'.
>
> For example, consider the 2x2 matrix (1 2; 3 4), which in my code could be stored as i=[0, 2, 4], j=[1, 0, 0, 1], v=[2, 1, 3, 4]. I can generate the matrix as follows (on 1 proc): MatCreateMPIAIJWithArrays(PETSC_COMM_SELF, 2, 2, 2, 2, i, j, v, &matrix). This appears to work fine, and I can then use the matrix in a KSP for example. However, if I try to update the entry values (same order and values v=[2, 1, 3, 4]) with MatUpdateMPIAIJWithArray(matrix, v), it seems that PETSc does not memorize the order of the column indexes and the matrix that I get now is (2 1; 3 4). I get the same result with MatUpdateMPIAIJWithArrays(matrix, 2, 2, 2, 2, i, j, v). On the other hand, if the column indexes are sorted within each row (i=[0, 2, 4], j=[0, 1, 0, 1], v=[1, 2, 3, 4]), then it works fine. I have attached a minimal working example (C++).
>
> Can I safely rely on MatCreateMPIAIJWithArrays working fine with unsorted column indexes as long as I do not use MatUpdateMPIAIJWithArray(s)?
Yes, this is correct. The column indices do not need to be sorted if you never call MatUpdateMPIAIJWithArray().
> Or should I do the sorting myself before calling MatCreateMPIAIJWithArrays? (or alternatively use another matrix format).
>
> Thanks in advance for the help.
>
> Kind regards,
>
> Mathieu Deuse
> <main.cpp>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mcs.anl.gov/pipermail/petsc-users/attachments/20240108/500b837c/attachment.html>
More information about the petsc-users
mailing list