<div dir="ltr"><pre width="80" style="color:rgb(0,0,0)"><a name="line2130"></a><pre width="80" style="white-space:pre-wrap;color:rgb(0,0,0)"><a name="line2130"></a><a href="https://petsc.org/release/docs/manualpages/Mat/MatDenseGetArrayWrite.html#MatDenseGetArrayWrite" target="_blank">MatDenseGetArrayWrite</a>() only returns the pointer to the local array, so it does not need any extra memory. </pre><pre width="80" style="white-space:pre-wrap;color:rgb(0,0,0)"><a name="line2130" style="font-family:Arial,Helvetica,sans-serif">2130: </a><strong style="font-family:Arial,Helvetica,sans-serif"><font color="#4169E1"><a name="MatDenseGetArray_SeqDense"></a><a href="https://petsc.org/release/docs/manualpages/Sys/PetscErrorCode.html#PetscErrorCode">PetscErrorCode</a> MatDenseGetArray_SeqDense(<a href="https://petsc.org/release/docs/manualpages/Mat/Mat.html#Mat">Mat</a> A,<a href="https://petsc.org/release/docs/manualpages/Sys/PetscScalar.html#PetscScalar">PetscScalar</a> **array)</font></strong><br></pre></pre><pre width="80" style="color:rgb(0,0,0)"><a name="line2131">2131: </a>{
<a name="line2132">2132: </a> Mat_SeqDense *mat = (Mat_SeqDense*)A->data;
<a name="line2135">2135: </a> <font color="#4169E1">if</font> (mat->matinuse) <a href="https://petsc.org/release/docs/manualpages/Sys/SETERRQ.html#SETERRQ">SETERRQ</a>(<a href="https://petsc.org/release/docs/manualpages/Sys/PETSC_COMM_SELF.html#PETSC_COMM_SELF">PETSC_COMM_SELF</a>,PETSC_ERR_ORDER,<font color="#666666">"Need to call <a href="https://petsc.org/release/docs/manualpages/Mat/MatDenseRestoreSubMatrix.html#MatDenseRestoreSubMatrix">MatDenseRestoreSubMatrix</a>() first"</font>);
<a name="line2136">2136: </a> *array = mat->v;
<a name="line2137">2137: </a> <font color="#4169E1">return</font>(0);
<a name="line2138">2138: </a>}</pre><pre width="80" style="color:rgb(0,0,0)"><br></pre><div><div dir="ltr" class="gmail_signature" data-smartmail="gmail_signature"><div dir="ltr">--Junchao Zhang</div></div></div><br></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Tue, Dec 14, 2021 at 2:29 PM Rohan Yadav <<a href="mailto:rohany@alumni.cmu.edu">rohany@alumni.cmu.edu</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr">Thanks Mark, I will try that -- that seems to be what I want.<div><br></div><div>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.</div><div><br></div><div>Rohan</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Tue, Dec 14, 2021 at 3:27 PM Junchao Zhang <<a href="mailto:junchao.zhang@gmail.com" target="_blank">junchao.zhang@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><pre width="80" style="color:rgb(0,0,0)"><a name="m_1929831222985232328_m_676069990667621645_line114">From https://petsc.org/release/src/ksp/ksp/tutorials/ex77.c.html</a></pre><pre width="80" style="color:rgb(0,0,0)"><a name="m_1929831222985232328_m_676069990667621645_line114">114: </a> <a href="https://petsc.org/release/docs/manualpages/Mat/MatDenseGetArrayWrite.html#MatDenseGetArrayWrite" target="_blank">MatDenseGetArrayWrite</a>(B,&x);
<a name="m_1929831222985232328_m_676069990667621645_line115">115: </a> <font color="#4169E1">for</font> (i=0; i<m*N; ++i) x[i] = 1.0;
<a name="m_1929831222985232328_m_676069990667621645_line116">116: </a> <a href="https://petsc.org/release/docs/manualpages/Mat/MatDenseRestoreArrayWrite.html#MatDenseRestoreArrayWrite" target="_blank">MatDenseRestoreArrayWrite</a>(B,&x);</pre><div><div dir="ltr"><div dir="ltr">--Junchao Zhang</div></div></div><br></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Tue, Dec 14, 2021 at 1:05 PM Rohan Yadav <<a href="mailto:rohany@alumni.cmu.edu" target="_blank">rohany@alumni.cmu.edu</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr">Hi,<div><br></div><div>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.</div><div><br></div><div>```</div><div> Mat C;</div><div>
<p style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;font-size:12px;line-height:normal;font-family:"Helvetica Neue""><span> </span>MatCreateDense(PETSC_COMM_WORLD, PETSC_DECIDE, PETSC_DECIDE, k, j, NULL, &C);</p>
<p style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;font-size:12px;line-height:normal;font-family:"Helvetica Neue""><span> </span>for (int kk = 0; kk < k; kk++) {<br></p>
<p style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;font-size:12px;line-height:normal;font-family:"Helvetica Neue""><span> </span>for (int jj = 0; jj < j; jj++) {</p>
<p style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;font-size:12px;line-height:normal;font-family:"Helvetica Neue""><span> </span>MatSetValue(C, kk, jj, 1, INSERT_VALUES);</p>
<p style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;font-size:12px;line-height:normal;font-family:"Helvetica Neue""><span> </span>}</p>
<p style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;font-size:12px;line-height:normal;font-family:"Helvetica Neue""><span> </span>}</p>
<p style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;font-size:12px;line-height:normal;font-family:"Helvetica Neue""><span> </span>MatAssemblyBegin(C, MAT_FINAL_ASSEMBLY);</p>
<p style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;font-size:12px;line-height:normal;font-family:"Helvetica Neue""><span> </span>MatAssemblyEnd(C, MAT_FINAL_ASSEMBLY);</p></div><div>```</div><div><br></div><div>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.</div><div><br></div><div>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?</div><div><br></div><div>Thanks,</div><div><br></div><div>Rohan</div></div>
</blockquote></div>
</blockquote></div>
</blockquote></div>