<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  </head>
  <body>
    <p>Hei,</p>
    <p>I removed all dependencies to armadillo and other not directly
      necessary packages, and attached both CMakeLists.txt and
      main-file. Even though I am only having PETSc as main dependence I
      still have the same issues. For a scaling factor of 0.1 and a
      matrix size of [1024, 1024] it works fine, for a scaling factor of
      0.01 on the same matrix the apparent scaling factor is suddenly
      1e-8.</p>
    <p>Thank you for your help!</p>
    <p>Regards,</p>
    <p>Roland<br>
    </p>
    <div class="moz-cite-prefix">Am 06.01.21 um 17:36 schrieb Matthew
      Knepley:<br>
    </div>
    <blockquote type="cite"
cite="mid:CAMYG4G=oHnxt1PhOMKM3SJfxooOesu00M2aX2yjVe-rpBi-pEw@mail.gmail.com">
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <div dir="ltr">
        <div dir="ltr">On Wed, Jan 6, 2021 at 11:05 AM Roland Richter
          <<a href="mailto:roland.richter@ntnu.no"
            moz-do-not-send="true">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 ran the program in both versions using "valgrind
                --tool=memcheck --leak-check=full --show-leak-kinds=all
                <binary> -malloc_debug". I got</p>
              <p>==3059== LEAK SUMMARY:<br>
                ==3059==    definitely lost: 12,916 bytes in 32 blocks<br>
                ==3059==    indirectly lost: 2,415 bytes in 2 blocks<br>
                ==3059==      possibly lost: 0 bytes in 0 blocks<br>
                ==3059==    still reachable: 103,511 bytes in 123 blocks<br>
                ==3059==         suppressed: 0 bytes in 0 blocks<br>
              </p>
              <p>but none of the leaks is related to the
                scaling-function itself.</p>
              <p>Did I miss something here?</p>
            </div>
          </blockquote>
          <div>Here is my analysis. It is certainly the case that
            MatScale() does not mysteriously scale by other numbers.</div>
          <div>It is used all over the place in tests, and in the code.
            Your test requires another package. Thus, it seems</div>
          <div>reasonable to guess that a bad interaction with that
            package (memory overwrite, conflicting layout or format,
            etc.)</div>
          <div>is responsible for the behavior you see.</div>
          <div><br>
          </div>
          <div>  Thanks,</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>
              </p>
              <div>Am 06.01.21 um 15:26 schrieb Matthew Knepley:<br>
              </div>
              <blockquote type="cite">
                <div dir="ltr">
                  <div dir="ltr">On Wed, Jan 6, 2021 at 2:41 AM Roland
                    Richter <<a href="mailto:roland.richter@ntnu.no"
                      target="_blank" moz-do-not-send="true">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>
                        </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" moz-do-not-send="true">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"
                                              moz-do-not-send="true">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">
                    <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" moz-do-not-send="true">https://www.cse.buffalo.edu/~knepley/</a><br>
                              </div>
                            </div>
                          </div>
                        </div>
                      </div>
                    </div>
                  </div>
                </div>
              </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" moz-do-not-send="true">https://www.cse.buffalo.edu/~knepley/</a><br>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </blockquote>
  </body>
</html>