[PYTHON MATRIX-SIG] A related idea

P. Dubois dubois@kristen.llnl.gov
Thu, 9 Nov 1995 11:10:44 -0800


Jim Hugunin replied, in response to my suggestion for providing an
"adoption" method for compiled arrays:
   > Suppose we have the matrix class working.

   What do you mean suppose ;)  ?

Hey, I'm a mathematician, all my sentences start with suppose!

   > Suppose there is a Fortran
   > or C array in the program which you would like to "adopt" as a
   > matrix in Python. The only difference between this and a matrix created a
   > normal way is that you have to remember never to free the memory.
   > So it seems you might be able to do this if you had a method
   > of creation (from C) which took a pointer,size,type kind of
   > description. You could then increment the reference counter one extra, so
   > that the memory is never freed, or a "don't free" flag could be in the
   > header.

   I agree that there should be a function to create a new matrix object from  
   an existing array in C (or FORTRAN).  What we disagree about is the  
   semantics.  I played around a while with the idea of "sharing" an array  
   between python and C.  I found that as a general rule I ran into memory leak  
   problems (or worse).  Using your scheme, python is never responsible for  
   freeing the allocated memory, this means that the C code must be responsible  
   for it, but the C code can't know when it is safe to free the memory  
   because even when it's done with it there might still be a python reference  
   to the memory.

   Let me know if I'm completely missing something here, and this memory leak  
   issue isn't really a problem.

   <snip>

This is a good point which I hadn't thought of because the way I intend to
use this "adopting" the arrays are the permanent state of a calculation and
are never "released" by the compiled code. I was aware that if I wanted to
ever change size etc. I would somehow need to tell Python about it. Some
careful thinking is required in that case.

The point isn't that I want to save memory but that I want to MODIFY the
array the compiled code is using, such as an array in a Fortran common block.
I don't have a problem copying it when I'm going to do something with it.
But to use it as a method of input to a code, I need to be able to do
something like:

import phys1 #load up physics code
phys1.x = <matrix expression>
# or
phys1.x[3] = 1.

Here I'm assuming x is created by the initialization of the module such
that it has the "real" x in the compiled code as a data area.

Obviously, one could instead provide methods (e.g., set_x, set_y, set_z)
but it dispels the illusion that x is really a matrix attribute of a physics
package.

Maybe I'm not taking the right approach here. Suggestions appreciated.














=================
MATRIX-SIG  - SIG on Matrix Math for Python

send messages to: matrix-sig@python.org
administrivia to: matrix-sig-request@python.org
=================