<!-- 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;">
 Hi Stefano, Thanks for looking into this. Since createWithArray calls VecCreateMPIWithArray which, as Matt noted and is documented (https: //petsc. org/main/manualpages/Vec/VecCreateMPIWithArray/) doesn’t free the memory, then there’s a memory
</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="pfptBannerfkjct86" 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="pfptBannerfkjct86" style="all: unset !important; float:left !important; display:block !important; margin: 0px 0px 1px 0px !important; max-width: 600px !important;">
      <div id="pfptBannerfkjct86" 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="pfptBannerfkjct86" 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 -->
<html>
<head><!-- BaNnErBlUrFlE-HeAdEr-start -->
<style>
  #pfptBannerfkjct86 { all: revert !important; display: block !important; 
    visibility: visible !important; opacity: 1 !important; 
    background-color: #D0D8DC !important; 
    max-width: none !important; max-height: none !important }
  .pfptPrimaryButtonfkjct86:hover, .pfptPrimaryButtonfkjct86:focus {
    background-color: #b4c1c7 !important; }
  .pfptPrimaryButtonfkjct86:active {
    background-color: #90a4ae !important; }
</style>

<!-- BaNnErBlUrFlE-HeAdEr-end -->

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body style="overflow-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;">
Hi Stefano,
<div><br>
</div>
<div>Thanks for looking into this. Since createWithArray calls VecCreateMPIWithArray which, as Matt noted and is documented (<a href="https://urldefense.us/v3/__https://petsc.org/main/manualpages/Vec/VecCreateMPIWithArray/__;!!G_uCfscf7eWS!YpH_OGif6W8Ql9KZDYBiRMKyALSRhMZOHRnkCkBHqBcVB4ef_UBHFbT_EuTgx8ivGmDNQU8bRVaOc-B_tqQ2heGjdvoE9tp8ZbPi3QQ$">https://petsc.org/main/manualpages/Vec/VecCreateMPIWithArray/</a>)
 doesn’t free the memory, then there’s a memory leak (and, furthermore, calling del on the original array will have no effect).</div>
<div><br>
</div>
<div>Lisandro: would be great if you can provide some guidance.</div>
<div><br>
</div>
<div>Thanks,</div>
<div><br>
</div>
<div>Samar</div>
<div>
<div><br>
<blockquote type="cite">
<div>On May 3, 2024, at 12:45 PM, Stefano Zampini <stefano.zampini@gmail.com> wrote:</div>
<br class="Apple-interchange-newline">
<div>
<div dir="ltr">While waiting for our Python wizard to shed light on this, I note that, from the documentation of PyArray_FROM_OTF <a href="https://urldefense.us/v3/__https://numpy.org/devdocs/user/c-info.how-to-extend.html*converting-an-arbitrary-sequence-object__;Iw!!G_uCfscf7eWS!YpH_OGif6W8Ql9KZDYBiRMKyALSRhMZOHRnkCkBHqBcVB4ef_UBHFbT_EuTgx8ivGmDNQU8bRVaOc-B_tqQ2heGjdvoE9tp8_QyxtQk$" target="_blank">https://numpy.org/devdocs/user/c-info.how-to-extend.html#converting-an-arbitrary-sequence-object</a>,
 we have
<div><br>
</div>
<div>The object can be any Python object convertible to an ndarray. If the object is already (a subclass of) the ndarray that satisfies the requirements then a new reference is returned.</div>
<div><br>
</div>
<div>I guess we should call "del" on the ndarray returned by iarray_s after having called  self.set_attr('__array__', array) in this case, but let's wait for Lisandro to confirm</div>
<div><br>
</div>
<div><br>
</div>
<div><br>
</div>
</div>
<br>
<div class="gmail_quote">
<div dir="ltr" class="gmail_attr">Il giorno ven 3 mag 2024 alle ore 11:42 Samar Khatiwala <<a href="mailto:samar.khatiwala@earth.ox.ac.uk" target="_blank">samar.khatiwala@earth.ox.ac.uk</a>> ha scritto:<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>
<div style="font-size:1px;color:rgb(255,255,255);line-height:1px;height:0px;max-height:0px;opacity:0;overflow:hidden;display:none">
Hi Matt, Thanks so much for the quick reply! Regarding #2, I put some debug statement in my code and what I find is that when I use createWithArray on my Cython-allocated numpy array, the destructor I set for it is no longer called when I delete
</div>
<div style="font-size:1px;color:rgb(255,255,255);line-height:1px;height:0px;max-height:0px;opacity:0;overflow:hidden;display:none">
ZjQcmQRYFpfptBannerStart</div>
<u></u>
<div dir="ltr" id="m_-3938619114237056735m_-7418861515743674263pfptBannerzljhhp6" style="display:block;text-align:left;margin:16px 0px;padding:8px 16px;border-radius:4px;min-width:200px;background-color:rgb(208,216,220);border-top:4px solid rgb(144,164,174)">
<div id="m_-3938619114237056735m_-7418861515743674263pfptBannerzljhhp6" style="float:left;display:block;margin:0px 0px 1px;max-width:600px">
<div id="m_-3938619114237056735m_-7418861515743674263pfptBannerzljhhp6" style="display: block; background-color: rgb(208, 216, 220); font-family: Arial, sans-serif; font-weight: bold; font-size: 14px; line-height: 18px;">
This Message Is From an External Sender </div>
<div id="m_-3938619114237056735m_-7418861515743674263pfptBannerzljhhp6" style="font-weight: normal; display: block; background-color: rgb(208, 216, 220); font-family: Arial, sans-serif; font-size: 12px; line-height: 18px; margin-top: 2px;">
This message came from outside your organization. </div>
</div>
<div style="height:0px;clear:both;display:block;line-height:0;font-size:0.01px"> </div>
</div>
<u></u>
<div style="font-size:1px;color:rgb(255,255,255);line-height:1px;height:0px;max-height:0px;opacity:0;overflow:hidden;display:none">
ZjQcmQRYFpfptBannerEnd</div>
<div>Hi Matt,
<div><br>
</div>
<div>Thanks so much for the quick reply!</div>
<div><br>
</div>
<div>Regarding #2, I put some debug statement in my code and what I find is that when I use <font face="sans-serif"><span style="white-space:pre-wrap">createWithArray on my Cython-allocated numpy array, the destructor I set for it is no longer called when I
 delete the array. (If I don’t use </span></font><span style="font-family:sans-serif;white-space:pre-wrap">createWithArray then the destructor is triggered.)
</span><font face="sans-serif"><span style="white-space:pre-wrap">I interpret that to suggest that the petsc4py Vec is somehow ’taking over’ management of the numpy array. But I don’t understand where that could be happening. (I don’t think it has to do with
 the actual freeing of memory by PETSc's VecDestroy.) </span></font></div>
<div><span style="font-family:sans-serif;white-space:pre-wrap"><br>
</span></div>
<div><span style="font-family:sans-serif;white-space:pre-wrap">createWithArray calls
</span>iarray_s which in turn calls PyArray_FROM_OTF. Could it be there’s something going on there? The numpy documentation is unclear. </div>
<div><br>
</div>
<div>Lisandro: do you have any thoughts on this?</div>
<div><br>
</div>
<div>Thanks,</div>
<div><font face="sans-serif"><span style="white-space:pre-wrap"><br>
</span></font>
<div>Samar</div>
<div><br>
<blockquote type="cite">
<div>On May 2, 2024, at 11:56 PM, Matthew Knepley <<a href="mailto:knepley@gmail.com" target="_blank">knepley@gmail.com</a>> wrote:</div>
<br>
<div>
<div dir="ltr" style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:400;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;text-decoration:none">
<div dir="ltr">On Thu, May 2, 2024 at 12:53 PM Samar Khatiwala <<a href="mailto:samar.khatiwala@earth.ox.ac.uk" target="_blank">samar.khatiwala@earth.ox.ac.uk</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><u></u><u></u>
<div dir="ltr" id="m_-3938619114237056735m_-7418861515743674263m_1959822169370491940pfptBannerqvl99pk" style="display:block;text-align:left;margin:16px 0px;padding:8px 16px;border-radius:4px;min-width:200px;background-color:rgb(208,216,220);border-top:4px solid rgb(144,164,174)">
<div id="m_-3938619114237056735m_-7418861515743674263m_1959822169370491940pfptBannerqvl99pk" style="float:left;display:block;margin:0px 0px 1px;max-width:600px">
<div id="m_-3938619114237056735m_-7418861515743674263m_1959822169370491940pfptBannerqvl99pk" style="display:block;background-color:rgb(208,216,220);font-family:Arial,sans-serif;font-weight:bold;font-size:14px;line-height:18px">
This Message Is From an External Sender</div>
<div id="m_-3938619114237056735m_-7418861515743674263m_1959822169370491940pfptBannerqvl99pk" style="font-weight:normal;display:block;background-color:rgb(208,216,220);font-family:Arial,sans-serif;font-size:12px;line-height:18px;margin-top:2px">
This message came from outside your organization.</div>
</div>
<div style="height:0px;clear:both;display:block;line-height:0;font-size:0.01px"> </div>
</div>
<u></u>
<div>
<pre style="font-family:sans-serif;font-size:12px;white-space:pre-wrap">Hello,

I have a couple of questions about createWithArray in petsc4py:

1) What is the correct usage for creating a standard MPI Vec with it? Something like this seems to work but is it right?:

On each rank do:
a = np.zeros(localSize)
v = PETSc.Vec().createWithArray(a, comm=PETSc.COMM_WORLD)

Is that all it takes?</pre>
</div>
</div>
</blockquote>
<div><br>
</div>
<div>That looks right to me.</div>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<div>
<div>
<pre style="font-family:sans-serif;font-size:12px;white-space:pre-wrap">2) Who ‘owns’ the underlying memory for a Vec created with the createWithArray method, i.e., who is responsible for managing it and doing garbage collection? In my problem, the numpy array is created in a Cython module where memory is allocated, and a pointer to it is associated with a numpy ndarray via PyArray_SimpleNewFromData and PyArray_SetBaseObject. I have a deallocator method of my own that is called when the numpy array is deleted/goes out of scope/whenever python does garbage collection. All of that works fine. But if I use this array to create a Vec with createWithArray what happens when the Vec is, e.g., destroyed? Will my deallocator be called? </pre>
</div>
</div>
</blockquote>
<div>No. The PETSc struct will be deallocated, but the storage will not be touched. </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>
<div>
<pre style="font-family:sans-serif;font-size:12px;white-space:pre-wrap">Or does petsc4py know that it doesn’t own the memory and won’t attempt to free it? I can’t quite figure out from the petsc4py code what is going on. And help would be appreciated.

Thanks very much.

Samar

</pre>
</div>
</div>
</blockquote>
</div>
<br clear="all">
<div><br>
</div>
<span class="gmail_signature_prefix">--<span> </span></span><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="https://urldefense.us/v3/__http://www.cse.buffalo.edu/*knepley/__;fg!!G_uCfscf7eWS!dXZuKR18IO2hZ4uPEqHaG0Jb0ALPgw1T0dm4HAWfyBfsCvkYTmq7aG7yw3yc-5CVhcFMaUL3WzGM8YHlBTTA_JMs_dYUCTX81-bKyrM$" target="_blank">https://www.cse.buffalo.edu/~knepley/</a></div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</blockquote>
</div>
<br>
</div>
</div>
</div>
</blockquote>
</div>
<br clear="all">
<div><br>
</div>
<span class="gmail_signature_prefix">-- </span><br>
<div dir="ltr" class="gmail_signature">Stefano</div>
</div>
</blockquote>
</div>
<br>
</div>
</body>
</html>