[petsc-users] Question about petsc4py createWithArray function

Samar Khatiwala samar.khatiwala at earth.ox.ac.uk
Fri May 3 03:41:59 CDT 2024


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 the array. (If I don’t use createWithArray then the destructor is triggered.) 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.)

createWithArray calls iarray_s which in turn calls PyArray_FROM_OTF. Could it be there’s something going on there? The numpy documentation is unclear.

Lisandro: do you have any thoughts on this?

Thanks,

Samar

On May 2, 2024, at 11:56 PM, Matthew Knepley <knepley at gmail.com> wrote:

On Thu, May 2, 2024 at 12:53 PM Samar Khatiwala <samar.khatiwala at earth.ox.ac.uk<mailto:samar.khatiwala at earth.ox.ac.uk>> wrote:
This Message Is From an External Sender
This message came from outside your organization.


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?

That looks right to me.

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?

No. The PETSc struct will be deallocated, but the storage will not be touched.

  Thanks,

     Matt

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




--
What most experimenters take for granted before they begin their experiments is infinitely more interesting than any results to which their experiments lead.
-- Norbert Wiener

https://urldefense.us/v3/__https://www.cse.buffalo.edu/*knepley/__;fg!!G_uCfscf7eWS!dXZuKR18IO2hZ4uPEqHaG0Jb0ALPgw1T0dm4HAWfyBfsCvkYTmq7aG7yw3yc-5CVhcFMaUL3WzGM8YHlBTTA_JMs_dYUCTX8wgHdY_Y$ <https://urldefense.us/v3/__http://www.cse.buffalo.edu/*knepley/__;fg!!G_uCfscf7eWS!dXZuKR18IO2hZ4uPEqHaG0Jb0ALPgw1T0dm4HAWfyBfsCvkYTmq7aG7yw3yc-5CVhcFMaUL3WzGM8YHlBTTA_JMs_dYUCTX81-bKyrM$ >

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mcs.anl.gov/pipermail/petsc-users/attachments/20240503/5e5d7e78/attachment.html>


More information about the petsc-users mailing list