[petsc-users] Help initializing matrix to a constant

Junchao Zhang junchao.zhang at gmail.com
Tue Dec 14 14:55:07 CST 2021


MatDenseGetArrayWrite
<https://petsc.org/release/docs/manualpages/Mat/MatDenseGetArrayWrite.html#MatDenseGetArrayWrite>()
only returns the pointer to the local array, so it does not need any
extra memory.

2130: *PetscErrorCode
<https://petsc.org/release/docs/manualpages/Sys/PetscErrorCode.html#PetscErrorCode>
MatDenseGetArray_SeqDense(Mat
<https://petsc.org/release/docs/manualpages/Mat/Mat.html#Mat>
A,PetscScalar <https://petsc.org/release/docs/manualpages/Sys/PetscScalar.html#PetscScalar>
**array)*

2131: {2132:   Mat_SeqDense *mat = (Mat_SeqDense*)A->data;
2135:   if (mat->matinuse) SETERRQ
<https://petsc.org/release/docs/manualpages/Sys/SETERRQ.html#SETERRQ>(PETSC_COMM_SELF
<https://petsc.org/release/docs/manualpages/Sys/PETSC_COMM_SELF.html#PETSC_COMM_SELF>,PETSC_ERR_ORDER,"Need
to call MatDenseRestoreSubMatrix
<https://petsc.org/release/docs/manualpages/Mat/MatDenseRestoreSubMatrix.html#MatDenseRestoreSubMatrix>()
first");2136:   *array = mat->v;2137:   return(0);2138: }


--Junchao Zhang


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

> Thanks Mark, I will try that -- that seems to be what I want.
>
> Junchao, that excerpt seems like it runs into the same problem as above
> right? If every rank tries to get the whole matrix then the process will
> surely OOM.
>
> Rohan
>
> On Tue, Dec 14, 2021 at 3:27 PM Junchao Zhang <junchao.zhang at gmail.com>
> wrote:
>
>> From https://petsc.org/release/src/ksp/ksp/tutorials/ex77.c.html
>>
>> 114:       MatDenseGetArrayWrite <https://petsc.org/release/docs/manualpages/Mat/MatDenseGetArrayWrite.html#MatDenseGetArrayWrite>(B,&x);115:       for (i=0; i<m*N; ++i) x[i] = 1.0;116:       MatDenseRestoreArrayWrite <https://petsc.org/release/docs/manualpages/Mat/MatDenseRestoreArrayWrite.html#MatDenseRestoreArrayWrite>(B,&x);
>>
>> --Junchao Zhang
>>
>>
>> On Tue, Dec 14, 2021 at 1: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;
>>>
>>>   MatCreateDense(PETSC_COMM_WORLD, PETSC_DECIDE, PETSC_DECIDE, k, j,
>>> 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/aef52ccc/attachment-0001.html>


More information about the petsc-users mailing list