[petsc-users] Help initializing matrix to a constant

Mark Adams mfadams at lbl.gov
Tue Dec 14 14:21:52 CST 2021

This should work on one process if you have enough memory in parallel you
are having every processor set the whole matrix.
If this code runs it should fail in MatAssemblyEnd because you are
inserting into the same place (ADD_VALUES would give you a matrix with np
in each entry).
See this for the function and examples of doing what you want (just do your
local rows):



On Tue, Dec 14, 2021 at 2:05 PM Rohan Yadav <rohany at alumni.cmu.edu> wrote:

> Hi,
> I'm having trouble setting all entries of a matrix to a constant value,
> similar to the `VecSet` method on vectors. I have a dense matrix that I
> want to initialize all entries to 1. The only related method I see on the
> `Mat` interface is `MatZeroEntries`, which sets all entries to 0. The
> obvious first attempt is to use the `MatSetValue` function to set all
> entries to the constant.
> ```
>   Mat C;
> NULL, &C);
>   for (int kk = 0; kk < k; kk++) {
>     for (int jj = 0; jj < j; jj++) {
>       MatSetValue(C, kk, jj, 1, INSERT_VALUES);
>     }
>   }
>   MatAssemblyBegin(C, MAT_FINAL_ASSEMBLY);
>   MatAssemblyEnd(C, MAT_FINAL_ASSEMBLY);
> ```
> However, when run with a relatively large matrix C (5GB) and a
> rank-per-core on my 40-core machine this code OOMs and crashes. It does not
> OOM with only 1 and 10 rank, leading me to believe that this API call is
> somehow causing the entire matrix to be replicated on each rank.
> Despite looking through the documentation, I could not find another API
> call that would allow me to set all the values in the matrix to a constant.
> What should I do here?
> Thanks,
> Rohan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mcs.anl.gov/pipermail/petsc-users/attachments/20211214/fdf74cd8/attachment.html>

More information about the petsc-users mailing list