<head><!-- BaNnErBlUrFlE-HeAdEr-start -->
<style>
  #pfptBannern7x90aq { all: revert !important; display: block !important; 
    visibility: visible !important; opacity: 1 !important; 
    background-color: #D0D8DC !important; 
    max-width: none !important; max-height: none !important }
  .pfptPrimaryButtonn7x90aq:hover, .pfptPrimaryButtonn7x90aq:focus {
    background-color: #b4c1c7 !important; }
  .pfptPrimaryButtonn7x90aq:active {
    background-color: #90a4ae !important; }
</style>

<!-- BaNnErBlUrFlE-HeAdEr-end -->
</head><!-- BaNnErBlUrFlE-BoDy-start -->
<!-- Preheader Text : BEGIN -->
<div style="display:none !important;display:none;visibility:hidden;mso-hide:all;font-size:1px;color:#ffffff;line-height:1px;height:0px;max-height:0px;opacity:0;overflow:hidden;">
 On Fri, Mar 1, 2024 at 11: 10 AM Fabian Wermelinger <fab4100@ posteo. ch> wrote: On Fri, 01 Mar 2024 10: 09: 17 -0600, Junchao Zhang wrote: >No. MatMPIBAIJSetPreallocationCSR() copies the data, but >VecCreateMPIWithArray() does not copy
</div>
<!-- Preheader Text : END -->

<!-- Email Banner : BEGIN -->
<div style="display:none !important;display:none;visibility:hidden;mso-hide:all;font-size:1px;color:#ffffff;line-height:1px;height:0px;max-height:0px;opacity:0;overflow:hidden;">ZjQcmQRYFpfptBannerStart</div>

<!--[if ((ie)|(mso))]>
  <table border="0" cellspacing="0" cellpadding="0" width="100%" style="padding: 16px 0px 16px 0px; direction: ltr" ><tr><td>
    <table border="0" cellspacing="0" cellpadding="0" style="padding: 0px 10px 5px 6px; width: 100%; border-radius:4px; border-top:4px solid #90a4ae;background-color:#D0D8DC;"><tr><td valign="top">
      <table align="left" border="0" cellspacing="0" cellpadding="0" style="padding: 4px 8px 4px 8px">
        <tr><td style="color:#000000; font-family: 'Arial', sans-serif; font-weight:bold; font-size:14px; direction: ltr">
          This Message Is From an External Sender
        </td></tr>
        <tr><td style="color:#000000; font-weight:normal; font-family: 'Arial', sans-serif; font-size:12px; direction: ltr">
          This message came from outside your organization.
        </td></tr>

      </table>

    </td></tr></table>
  </td></tr></table>
<![endif]-->

<![if !((ie)|(mso))]>
  <div dir="ltr"  id="pfptBannern7x90aq" style="all: revert !important; display:block !important; text-align: left !important; margin:16px 0px 16px 0px !important; padding:8px 16px 8px 16px !important; border-radius: 4px !important; min-width: 200px !important; background-color: #D0D8DC !important; background-color: #D0D8DC; border-top: 4px solid #90a4ae !important; border-top: 4px solid #90a4ae;">
    <div id="pfptBannern7x90aq" style="all: unset !important; float:left !important; display:block !important; margin: 0px 0px 1px 0px !important; max-width: 600px !important;">
      <div id="pfptBannern7x90aq" style="all: unset !important; display:block !important; visibility: visible !important; background-color: #D0D8DC !important; color:#000000 !important; color:#000000; font-family: 'Arial', sans-serif !important; font-family: 'Arial', sans-serif; font-weight:bold !important; font-weight:bold; font-size:14px !important; line-height:18px !important; line-height:18px">
        This Message Is From an External Sender
      </div>
      <div id="pfptBannern7x90aq" style="all: unset !important; display:block !important; visibility: visible !important; background-color: #D0D8DC !important; color:#000000 !important; color:#000000; font-weight:normal; font-family: 'Arial', sans-serif !important; font-family: 'Arial', sans-serif; font-size:12px !important; line-height:18px !important; line-height:18px; margin-top:2px !important;">
This message came from outside your organization.
      </div>

    </div>

    <div style="clear: both !important; display: block !important; visibility: hidden !important; line-height: 0 !important; font-size: 0.01px !important; height: 0px"> </div>
  </div>
<![endif]>

<div style="display:none !important;display:none;visibility:hidden;mso-hide:all;font-size:1px;color:#ffffff;line-height:1px;height:0px;max-height:0px;opacity:0;overflow:hidden;">ZjQcmQRYFpfptBannerEnd</div>
<!-- Email Banner : END -->

<!-- BaNnErBlUrFlE-BoDy-end -->
<div dir="ltr"><div dir="ltr"><br><br></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Fri, Mar 1, 2024 at 11:10 AM Fabian Wermelinger <<a href="mailto:fab4100@posteo.ch">fab4100@posteo.ch</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">On Fri, 01 Mar 2024 10:09:17 -0600, Junchao Zhang wrote:<br>
>No. MatMPIBAIJSetPreallocationCSR() copies the data, but<br>
>VecCreateMPIWithArray() does not copy (only use pointers user provided).<br>
<br>
OK, my understanding was that MatMPIBAIJSetPreallocationCSR DOES NOT copy since<br>
it has direct access to matrix entries in the required PETSc layout -> thank you<br>
<br>
>I think your intention is to avoid memory copies.<br>
<br>
Yes. (not only copies but also avoid unnecessary increase of memory footprint)<br>
<br>
>If it is MATMPIAIJ, we can do<br>
>    MatMPIAIJSetPreallocationCSR(A, i, j, v); // let petsc copy i, j v<br>
><br>
>    // To quickly update A when you have an updated v[]<br>
><br>
>    MatUpdateMPIAIJWithArray(A, v); // copy v[], but faster than MatSetValues()<br>
<br>
Thanks for this input!<br>
<br>
>But it seems we currently do not have a MatUpdateMPIBAIJWithArray()  :(<br>
<br>
So when my v[] changed during iterations (only the v[] used during the<br>
MatMPIBAIJSetPreallocationCSR call, the matrix structure/connectivity has NOT<br>
changed), it is not sufficient to just call KSPSetOperators() before calling<br>
KSPSolve()?  </blockquote><div>Correct, your v[] is not shared with petsc</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 must copy the updated values in v[] explicitly into the PETSc<br>
matrix, then call KSPSetOperators() followed by KSPSolve()? (for every<br>
iteration in my application)<br></blockquote><div>Yes </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<br>
The preferred method then is to use MatSetValues() for the copies (or possibly<br>
MatSetValuesRow() or MatSetValuesBlocked())?<br></blockquote><div> MatSetValuesRow()</div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<br>
The term "Preallocation" is confusing to me.  For example,<br>
MatCreateMPIBAIJWithArrays clearly states in the doc that arrays are copied<br>
(<a href="https://urldefense.us/v3/__https://petsc.org/release/manualpages/Mat/MatCreateMPIBAIJWithArrays/__;!!G_uCfscf7eWS!YLXb4luirYVduVkmxILIpET7hYcMAPTHUxHG7tR7os-mxUGGqYt_XTB0xPp5y5CU2gWNtgNEmD8gu3rmtqcqNfLfc2Oe$" rel="noreferrer" target="_blank">https://petsc.org/release/manualpages/Mat/MatCreateMPIBAIJWithArrays/</a>), I would<br>
then assume PETSc maintains internal storage for it.  If something is<br>
preallocated, I would not make that assumption.<br></blockquote><div>"Preallocation" in petsc means "tell petsc sizes of rows in a matrix",  so that petsc can preallocate the memory before you do MatSetValues().</div><div>This is clearer in <a href="https://urldefense.us/v3/__https://petsc.org/release/manualpages/Mat/MatMPIAIJSetPreallocation/__;!!G_uCfscf7eWS!YLXb4luirYVduVkmxILIpET7hYcMAPTHUxHG7tR7os-mxUGGqYt_XTB0xPp5y5CU2gWNtgNEmD8gu3rmtqcqNdzTPnSf$">https://petsc.org/release/manualpages/Mat/MatMPIAIJSetPreallocation/</a> </div><div> <br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
Thank you for your time, I appreciate the inputs!<br>
<br>
All best,<br>
<br>
-- <br>
fabs<br>
</blockquote></div></div>