<!-- 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, Thank you for this. Your example was invaluable in helping me figure out what is going on. (I did not know about about sys. getrefcount - I’m only just starting to learn python. ) It was indeed the refcount. Until the Vec is destroyed
</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="pfptBanner79exloy" 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="pfptBanner79exloy" style="all: unset !important; float:left !important; display:block !important; margin: 0px 0px 1px 0px !important; max-width: 600px !important;">
      <div id="pfptBanner79exloy" 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="pfptBanner79exloy" 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>
  #pfptBanner79exloy { all: revert !important; display: block !important; 
    visibility: visible !important; opacity: 1 !important; 
    background-color: #D0D8DC !important; 
    max-width: none !important; max-height: none !important }
  .pfptPrimaryButton79exloy:hover, .pfptPrimaryButton79exloy:focus {
    background-color: #b4c1c7 !important; }
  .pfptPrimaryButton79exloy: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>Thank you for this. Your example was invaluable in helping me figure out what is going on. (I did not know about about sys.getrefcount - I’m only just starting to learn python.) It was indeed the refcount. Until the Vec is destroyed it can’t go to zero
 and my array destructor method won’t be triggered (which is not really a problem because the memory is freed elsewhere). I take back what I said about there being a memory leak in petsc4py. Sorry.</div>
<div><br>
</div>
<div>Thanks again for your help!</div>
<div><br>
</div>
<div>Best,</div>
<div><br>
</div>
<div>Samar</div>
<div><br>
</div>
<div>
<div>
<blockquote type="cite">
<div>On May 6, 2024, at 2:09 PM, Stefano Zampini <stefano.zampini@gmail.com> wrote:</div>
<br class="Apple-interchange-newline">
<div>
<div dir="ltr" style="caret-color: rgb(0, 0, 0); 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; -webkit-text-stroke-width: 0px; text-decoration: none;">
Samar
<div><br>
</div>
<div>After a second look, I believe the petsc4py code is correct. You can test it using the script below.</div>
<div>After destroy is called (or del), the reference count of the numpy array is back to its initial state.</div>
<div>Maybe you are not calling del or destroy? a MWE would help to understand your use case</div>
<div>
<div style="margin: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-feature-settings: normal; font-stretch: normal; font-size: 21px; line-height: normal; font-family: Menlo;">
<span class="gmail-s1" style="font-variant-ligatures: no-common-ligatures;"><br>
</span></div>
<div style="margin: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-feature-settings: normal; font-stretch: normal; font-size: 21px; line-height: normal; font-family: Menlo;">
<span class="gmail-s1" style="font-variant-ligatures: no-common-ligatures;">kl-18448:~ szampini$ cat t.py</span></div>
<div style="margin: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-feature-settings: normal; font-stretch: normal; font-size: 21px; line-height: normal; font-family: Menlo;">
<span class="gmail-s1" style="font-variant-ligatures: no-common-ligatures;">import sys</span></div>
<div style="margin: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-feature-settings: normal; font-stretch: normal; font-size: 21px; line-height: normal; font-family: Menlo;">
<span class="gmail-s1" style="font-variant-ligatures: no-common-ligatures;">import numpy as np</span></div>
<div style="margin: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-feature-settings: normal; font-stretch: normal; font-size: 21px; line-height: normal; font-family: Menlo;">
<span class="gmail-s1" style="font-variant-ligatures: no-common-ligatures;">from petsc4py import PETSc</span></div>
<div style="margin: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-feature-settings: normal; font-stretch: normal; font-size: 21px; line-height: normal; font-family: Menlo; min-height: 24px;">
<span class="gmail-s1" style="font-variant-ligatures: no-common-ligatures;"></span><br>
</div>
<div style="margin: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-feature-settings: normal; font-stretch: normal; font-size: 21px; line-height: normal; font-family: Menlo;">
<span class="gmail-s1" style="font-variant-ligatures: no-common-ligatures;">x = np.zeros(4, dtype=PETSc.ScalarType)</span></div>
<div style="margin: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-feature-settings: normal; font-stretch: normal; font-size: 21px; line-height: normal; font-family: Menlo;">
<span class="gmail-s1" style="font-variant-ligatures: no-common-ligatures;">print('initial ref count',sys.getrefcount(x))</span></div>
<div style="margin: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-feature-settings: normal; font-stretch: normal; font-size: 21px; line-height: normal; font-family: Menlo; min-height: 24px;">
<span class="gmail-s1" style="font-variant-ligatures: no-common-ligatures;"></span><br>
</div>
<div style="margin: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-feature-settings: normal; font-stretch: normal; font-size: 21px; line-height: normal; font-family: Menlo;">
<span class="gmail-s1" style="font-variant-ligatures: no-common-ligatures;">v = PETSc.Vec().createWithArray(x)</span></div>
<div style="margin: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-feature-settings: normal; font-stretch: normal; font-size: 21px; line-height: normal; font-family: Menlo;">
<span class="gmail-s1" style="font-variant-ligatures: no-common-ligatures;">print('after create',sys.getrefcount(x))</span></div>
<div style="margin: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-feature-settings: normal; font-stretch: normal; font-size: 21px; line-height: normal; font-family: Menlo; min-height: 24px;">
<span class="gmail-s1" style="font-variant-ligatures: no-common-ligatures;"></span><br>
</div>
<div style="margin: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-feature-settings: normal; font-stretch: normal; font-size: 21px; line-height: normal; font-family: Menlo;">
<span class="gmail-s1" style="font-variant-ligatures: no-common-ligatures;"># check if they share memory</span></div>
<div style="margin: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-feature-settings: normal; font-stretch: normal; font-size: 21px; line-height: normal; font-family: Menlo;">
<span class="gmail-s1" style="font-variant-ligatures: no-common-ligatures;">v.view()</span></div>
<div style="margin: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-feature-settings: normal; font-stretch: normal; font-size: 21px; line-height: normal; font-family: Menlo;">
<span class="gmail-s1" style="font-variant-ligatures: no-common-ligatures;">x[1] = 2</span></div>
<div style="margin: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-feature-settings: normal; font-stretch: normal; font-size: 21px; line-height: normal; font-family: Menlo;">
<span class="gmail-s1" style="font-variant-ligatures: no-common-ligatures;">v.view()</span></div>
<div style="margin: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-feature-settings: normal; font-stretch: normal; font-size: 21px; line-height: normal; font-family: Menlo; min-height: 24px;">
<span class="gmail-s1" style="font-variant-ligatures: no-common-ligatures;"></span><br>
</div>
<div style="margin: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-feature-settings: normal; font-stretch: normal; font-size: 21px; line-height: normal; font-family: Menlo;">
<span class="gmail-s1" style="font-variant-ligatures: no-common-ligatures;"># free</span></div>
<div style="margin: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-feature-settings: normal; font-stretch: normal; font-size: 21px; line-height: normal; font-family: Menlo;">
<span class="gmail-s1" style="font-variant-ligatures: no-common-ligatures;">v.destroy()</span></div>
<div style="margin: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-feature-settings: normal; font-stretch: normal; font-size: 21px; line-height: normal; font-family: Menlo;">
<span class="gmail-s1" style="font-variant-ligatures: no-common-ligatures;"># you can also call del</span></div>
<div style="margin: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-feature-settings: normal; font-stretch: normal; font-size: 21px; line-height: normal; font-family: Menlo;">
<span class="gmail-s1" style="font-variant-ligatures: no-common-ligatures;"># del v</span></div>
<div style="margin: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-feature-settings: normal; font-stretch: normal; font-size: 21px; line-height: normal; font-family: Menlo;">
<span class="gmail-s1" style="font-variant-ligatures: no-common-ligatures;">print('after destroy',sys.getrefcount(x))</span></div>
<div style="margin: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-feature-settings: normal; font-stretch: normal; font-size: 21px; line-height: normal; font-family: Menlo;">
<span class="gmail-s1" style="font-variant-ligatures: no-common-ligatures;"><br>
</span></div>
<div style="margin: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-feature-settings: normal; font-stretch: normal; font-size: 21px; line-height: normal; font-family: Menlo;">
<span style="font-variant-ligatures: no-common-ligatures;">kl-18448:~ szampini$ python t.py</span><span class="gmail-Apple-converted-space" style="font-variant-ligatures: no-common-ligatures;"> </span></div>
<div style="margin: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-feature-settings: normal; font-stretch: normal; font-size: 21px; line-height: normal; font-family: Menlo;">
<span class="gmail-s1" style="font-variant-ligatures: no-common-ligatures;">initial ref count 2</span></div>
<div style="margin: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-feature-settings: normal; font-stretch: normal; font-size: 21px; line-height: normal; font-family: Menlo;">
<span class="gmail-s1" style="font-variant-ligatures: no-common-ligatures;">after create 3</span></div>
<div style="margin: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-feature-settings: normal; font-stretch: normal; font-size: 21px; line-height: normal; font-family: Menlo;">
<span class="gmail-s1" style="font-variant-ligatures: no-common-ligatures;">Vec Object: 1 MPI process</span></div>
<div style="margin: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-feature-settings: normal; font-stretch: normal; font-size: 21px; line-height: normal; font-family: Menlo;">
<span class="gmail-s1" style="font-variant-ligatures: no-common-ligatures;"><span class="gmail-Apple-converted-space"> <span class="Apple-converted-space"> </span></span>type: seq</span></div>
<div style="margin: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-feature-settings: normal; font-stretch: normal; font-size: 21px; line-height: normal; font-family: Menlo;">
<span class="gmail-s1" style="font-variant-ligatures: no-common-ligatures;">0.</span></div>
<div style="margin: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-feature-settings: normal; font-stretch: normal; font-size: 21px; line-height: normal; font-family: Menlo;">
<span class="gmail-s1" style="font-variant-ligatures: no-common-ligatures;">0.</span></div>
<div style="margin: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-feature-settings: normal; font-stretch: normal; font-size: 21px; line-height: normal; font-family: Menlo;">
<span class="gmail-s1" style="font-variant-ligatures: no-common-ligatures;">0.</span></div>
<div style="margin: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-feature-settings: normal; font-stretch: normal; font-size: 21px; line-height: normal; font-family: Menlo;">
<span class="gmail-s1" style="font-variant-ligatures: no-common-ligatures;">0.</span></div>
<div style="margin: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-feature-settings: normal; font-stretch: normal; font-size: 21px; line-height: normal; font-family: Menlo;">
<span class="gmail-s1" style="font-variant-ligatures: no-common-ligatures;">Vec Object: 1 MPI process</span></div>
<div style="margin: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-feature-settings: normal; font-stretch: normal; font-size: 21px; line-height: normal; font-family: Menlo;">
<span class="gmail-s1" style="font-variant-ligatures: no-common-ligatures;"><span class="gmail-Apple-converted-space"> <span class="Apple-converted-space"> </span></span>type: seq</span></div>
<div style="margin: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-feature-settings: normal; font-stretch: normal; font-size: 21px; line-height: normal; font-family: Menlo;">
<span class="gmail-s1" style="font-variant-ligatures: no-common-ligatures;">0.</span></div>
<div style="margin: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-feature-settings: normal; font-stretch: normal; font-size: 21px; line-height: normal; font-family: Menlo;">
<span class="gmail-s1" style="font-variant-ligatures: no-common-ligatures;">2.</span></div>
<div style="margin: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-feature-settings: normal; font-stretch: normal; font-size: 21px; line-height: normal; font-family: Menlo;">
<span class="gmail-s1" style="font-variant-ligatures: no-common-ligatures;">0.</span></div>
<div style="margin: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-feature-settings: normal; font-stretch: normal; font-size: 21px; line-height: normal; font-family: Menlo;">
<span class="gmail-s1" style="font-variant-ligatures: no-common-ligatures;">0.</span></div>
<div style="margin: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-feature-settings: normal; font-stretch: normal; font-size: 21px; line-height: normal; font-family: Menlo;">
<span class="gmail-s1" style="font-variant-ligatures: no-common-ligatures;">after destroy 2</span></div>
</div>
<div><br>
</div>
<div><br>
</div>
</div>
<br style="caret-color: rgb(0, 0, 0); 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; -webkit-text-stroke-width: 0px; text-decoration: none;">
<div class="gmail_quote" style="caret-color: rgb(0, 0, 0); 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; -webkit-text-stroke-width: 0px; text-decoration: none;">
<div dir="ltr" class="gmail_attr">Il giorno lun 6 mag 2024 alle ore 08:49 Samar Khatiwala <<a href="mailto:samar.khatiwala@earth.ox.ac.uk">samar.khatiwala@earth.ox.ac.uk</a>> ha scritto:<br>
</div>
<blockquote class="gmail_quote" style="margin: 0px 0px 0px 0.8ex; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(204, 204, 204); padding-left: 1ex;">
<div>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!fl-7I7xkuEmTFH8M9EaARIZL5vhGFWkIk5bVIGt0bkt_cYcuwPSwgOWaec1-DvYwiKWxA5q0HS0VTLBmD4NnSe1PfG057Y4SBd-8qs8$" target="_blank">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 <<a href="mailto:stefano.zampini@gmail.com" target="_blank">stefano.zampini@gmail.com</a>> wrote:</div>
<br>
<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!fl-7I7xkuEmTFH8M9EaARIZL5vhGFWkIk5bVIGt0bkt_cYcuwPSwgOWaec1-DvYwiKWxA5q0HS0VTLBmD4NnSe1PfG057Y4S1ro4qy0$" 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-width: 1px; border-left-style: solid; border-left-color: rgb(204, 204, 204); padding-left: 1ex;">
<div><u></u>
<div dir="ltr" id="m_4268438130595604253m_-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-width: 4px; border-top-style: solid; border-top-color: rgb(144, 164, 174);">
<div id="m_4268438130595604253m_-3938619114237056735m_-7418861515743674263pfptBannerzljhhp6" style="float: left; display: block; margin: 0px 0px 1px; max-width: 600px;">
<div id="m_4268438130595604253m_-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<span class="Apple-converted-space"> </span></div>
<div id="m_4268438130595604253m_-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.<span class="Apple-converted-space"> </span></div>
</div>
<div style="height: 0px; clear: both; display: block; line-height: 0; font-size: 0.01px;">
 </div>
</div>
<u></u>
<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-width: 1px; border-left-style: solid; border-left-color: rgb(204, 204, 204); padding-left: 1ex;">
<div><u></u><u></u>
<div dir="ltr" id="m_4268438130595604253m_-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-width: 4px; border-top-style: solid; border-top-color: rgb(144, 164, 174);">
<div id="m_4268438130595604253m_-3938619114237056735m_-7418861515743674263m_1959822169370491940pfptBannerqvl99pk" style="float: left; display: block; margin: 0px 0px 1px; max-width: 600px;">
<div id="m_4268438130595604253m_-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_4268438130595604253m_-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-width: 1px; border-left-style: solid; border-left-color: 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-width: 1px; border-left-style: solid; border-left-color: 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 class="Apple-converted-space"> </span></span><br>
<div dir="ltr" class="gmail_signature">Stefano</div>
</div>
</blockquote>
</div>
<br>
</div>
</div>
</blockquote>
</div>
<br clear="all" style="caret-color: rgb(0, 0, 0); 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; -webkit-text-stroke-width: 0px; text-decoration: none;">
<div style="caret-color: rgb(0, 0, 0); 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; -webkit-text-stroke-width: 0px; text-decoration: none;">
<br>
</div>
<span class="gmail_signature_prefix" style="caret-color: rgb(0, 0, 0); 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; -webkit-text-stroke-width: 0px; text-decoration: none;">--<span class="Apple-converted-space"> </span></span><br style="caret-color: rgb(0, 0, 0); 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; -webkit-text-stroke-width: 0px; text-decoration: none;">
<div dir="ltr" class="gmail_signature" style="caret-color: rgb(0, 0, 0); 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; -webkit-text-stroke-width: 0px; text-decoration: none;">
Stefano</div>
</div>
</blockquote>
</div>
<br>
</div>
</body>
</html>