<div dir="ltr"><div dir="ltr">On Wed, Jan 6, 2021 at 2:41 AM Roland Richter <<a href="mailto:roland.richter@ntnu.no">roland.richter@ntnu.no</a>> wrote:<br></div><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<div>
<p>Hei,</p>
<p>I added one additional function to the code:</p>
<p><i>void test_scaling_petsc_pointer(const Mat &in_mat,</i><i><br>
</i><i> Mat &out_mat,</i><i><br>
</i><i> const PetscScalar
&scaling_factor) {</i><i><br>
</i><i> MatCopy (in_mat, out_mat, SAME_NONZERO_PATTERN);</i><i><br>
</i><i> PetscScalar *mat_ptr;</i><i><br>
</i><i> MatDenseGetArray (out_mat, &mat_ptr);</i><i><br>
</i><i> PetscInt r_0, r_1;</i><i><br>
</i><i> MatGetLocalSize (out_mat, &r_0, &r_1);</i><i><br>
</i><i> for(int i = 0; i < r_0 * r_1; ++i)</i><i><br>
</i><i> *(mat_ptr + i) = (*(mat_ptr + i) * scaling_factor);</i><i><br>
</i><i><br>
</i><i> MatAssemblyBegin (out_mat, MAT_FINAL_ASSEMBLY);</i><i><br>
</i><i> MatAssemblyEnd (out_mat, MAT_FINAL_ASSEMBLY);</i><i><br>
</i><i>}</i></p>
<p>When replacing test function <i>test_scaling_petsc()</i> with <i>test_scaling_petsc_pointer()</i>
everything works as it should, but I do not understand why.</p>
<p>Do you have any suggestions?</p></div></blockquote><div>The easiest explanation is that you have a memory overwrite in the code somewhere. Barry's suggestion to use</div><div>valgrind is good.</div><div><br></div><div> Matt </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div>
<p>Thanks!<br>
<b><i></i></b></p>
<p><br>
</p>
<div>Am 05.01.21 um 15:24 schrieb Roland
Richter:<br>
</div>
<blockquote type="cite">
<p>Hei,</p>
<p>the code I attached to the original mail should work out of the
box, but requires armadillo and PETSc to compile/run. Armadillo
stores the data in column-major order, and therefore I am
transposing the matrices before and after transferring using
.st().</p>
<p>Thank you for your help!</p>
<p>Regards,</p>
<p>Roland<br>
</p>
<div>Am 05.01.21 um 15:21 schrieb Matthew
Knepley:<br>
</div>
<blockquote type="cite">
<div dir="ltr">
<div dir="ltr">On Tue, Jan 5, 2021 at 7:57 AM Roland Richter
<<a href="mailto:roland.richter@ntnu.no" target="_blank">roland.richter@ntnu.no</a>>
wrote:<br>
</div>
<div class="gmail_quote">
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Hei,<br>
<br>
I would like to scale a given matrix with a fixed scalar
value, and<br>
therefore would like to use MatScale(). Nevertheless, I
observed an<br>
interesting behavior depending on the size of the matrix,
and currently<br>
I am not sure why.<br>
<br>
When running the attached code, I intend to divide all
elements in the<br>
matrix by a constant factor of 10. If I have three or
fewer rows and<br>
1024 columns, I get the expected result. If I have four or
more rows<br>
(with the same number of columns), suddenly my scaling
factor seems to<br>
be 0.01 instead of 0.1 for the PETSc-matrix. The
armadillo-based matrix<br>
still behaves as expected.<br>
</blockquote>
<div><br>
</div>
<div>1) It looks like you assume the storage in your
armadillo matrix is row major. I would be surprised if
this was true.</div>
<div><br>
</div>
<div>2) I think it is unlikely that there is a problem with
MatScale, so I would guess either you have a memory
overwrite</div>
<div>or are misinterpreting your output. If you send
something I can run, I will figure out which it is.</div>
<div><br>
</div>
<div> Thanks,</div>
<div><br>
</div>
<div> Matt</div>
<div> </div>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"> I currently do not
understand that behavior, but do not see any problems<br>
with the code either. Are there any possible explanations
for that behavior?<br>
<br>
Thank you very much,<br>
<br>
regards,<br>
<br>
Roland Richter<br>
<br>
</blockquote>
</div>
<br clear="all">
<div><br>
</div>
-- <br>
<div dir="ltr">
<div dir="ltr">
<div>
<div dir="ltr">
<div>
<div dir="ltr">
<div>What most experimenters take for granted
before they begin their experiments is
infinitely more interesting than any results to
which their experiments lead.<br>
-- Norbert Wiener</div>
<div><br>
</div>
<div><a href="http://www.cse.buffalo.edu/~knepley/" target="_blank">https://www.cse.buffalo.edu/~knepley/</a><br>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</blockquote>
</blockquote>
</div>
</blockquote></div><br clear="all"><div><br></div>-- <br><div dir="ltr" class="gmail_signature"><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><div>What most experimenters take for granted before they begin their experiments is infinitely more interesting than any results to which their experiments lead.<br>-- Norbert Wiener</div><div><br></div><div><a href="http://www.cse.buffalo.edu/~knepley/" target="_blank">https://www.cse.buffalo.edu/~knepley/</a><br></div></div></div></div></div></div></div></div>