[petsc-users] How to efficiently fill in, in parallel, a PETSc matrix from a COO sparse matrix?

Stefano Zampini stefano.zampini at gmail.com
Tue Jun 20 10:18:03 CDT 2023


The loop should iterate on the number of entries of the array, not the
number of local rows

On Tue, Jun 20, 2023, 17:07 Matthew Knepley <knepley at gmail.com> wrote:

> On Tue, Jun 20, 2023 at 10:55 AM Diego Magela Lemos via petsc-users <
> petsc-users at mcs.anl.gov> wrote:
>
>> Considering, for instance, the following COO sparse matrix format, with
>> repeated indices:
>>
>> std::vector<size_t> rows{0, 0, 1, 2, 3, 4};
>> std::vector<size_t> cols{0, 0, 1, 2, 3, 4};
>> std::vector<double> values{2, -1, 2, 3, 4, 5};
>>
>> that represents a 5x5 diagonal matrix A.
>>
>> So far, the code that I have is:
>>
>> // fill_in_matrix.cc
>> static char help[] = "Fill in a parallel COO format sparse matrix.";
>> #include <petsc.h>#include <vector>
>> int main(int argc, char **args){
>>     Mat A;
>>     PetscInt m = 5, i, Istart, Iend;
>>
>>     PetscCall(PetscInitialize(&argc, &args, NULL, help));
>>
>>     PetscCall(MatCreate(PETSC_COMM_WORLD, &A));
>>     PetscCall(MatSetSizes(A, PETSC_DECIDE, PETSC_DECIDE, m, m));
>>     PetscCall(MatSetFromOptions(A));
>>     PetscCall(MatSetUp(A));
>>     PetscCall(MatGetOwnershipRange(A, &Istart, &Iend));
>>
>>     std::vector<PetscInt> II{0, 0, 1, 2, 3, 4};
>>     std::vector<PetscInt> JJ{0, 0, 1, 2, 3, 4};
>>     std::vector<PetscScalar> XX{2, -1, 2, 3, 4, 5};
>>
>>     for (i = Istart; i < Iend; i++)
>>         PetscCall(MatSetValues(A, 1, &II.at(i), 1, &JJ.at(i), &XX.at(i), ADD_VALUES));
>>
>>     PetscCall(MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY));
>>     PetscCall(MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY));
>>     PetscCall(MatView(A, PETSC_VIEWER_STDERR_WORLD));
>>
>>     PetscCall(MatDestroy(&A));
>>     PetscCall(PetscFinalize());
>>     return 0;
>> }
>>
>> When running it with
>>
>> petscmpiexec -n 4 ./fill_in_matrix
>>
>>
>> I get
>>
>>
>>  Mat Object: 4 MPI processes
>>
>>   type: mpiaij
>> row 0: (0, 1.)
>> row 1: (1, 2.)
>> row 2: (2, 3.)
>> row 3: (3, 4.)
>> row 4:
>>
>>
>> Which is missing the entry of the last row.
>>
>> What am I missing? Even better, which would be the best way to fill in this matrix?
>>
>> We have a new interface for this:
>
>   https://petsc.org/main/manualpages/Mat/MatSetValuesCOO/
>
>   Thanks,
>
>      Matt
>
> --
> 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://www.cse.buffalo.edu/~knepley/
> <http://www.cse.buffalo.edu/~knepley/>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mcs.anl.gov/pipermail/petsc-users/attachments/20230620/d8fa1575/attachment-0001.html>


More information about the petsc-users mailing list