(This is a move of my and Kurt's discussion into the public; I'll give 
some context but not that much.)

Kurt: This will go into a broader discussion of buffers in Cython as such
and not be directly Fortran-relevant. I want to have Fortran support
integrated in a nice way into Cython, so I think this is necesarry to some
degree, but I'll try not to overdo it.

So, you wanted to target contiguous buffer passing first. And I was 
saying that fixing

http://trac.cython.org/cython_trac/ticket/177

would for the most part amount to what we need. Adding support for

   cdef void myfunc(object[int, mode="c"] buf)

which would have the C signature

   void myfunc(Py_buffer* buf) // + a contract on int/contiguous

and have the caller be responsible for getting the buffer, gets us far. 
It would mean that you could do

   myfunc(myobject)

and it would turn into

   Py_buffer buf = acquire buffer from myobject
   raise exception if buf is not contiguous
   myfunc(&buf)
   release buf

I first thought that adding one component more would get us all the way: 
Automatic copying into contiguous buffers. So assuming #177 is
implemented, one would then move on to having it instead turn into:

   Py_buffer buf = acquire buffer from myobject
   if (buf is contiguous) {
     myfunc(&buf);
   } else {
     Py_buffer buf2 = make contiguous copy of buf
     myfunc(&buf2)
   }
   release buf

However, there's a (big) problem here: What Python object does buf2 refer to?

Using the one of "buf" would be too confusing as they point to different
memory areas. One solution is just setting it to None, perhaps. However
once #177 is solved one will expect to be able to do

   cdef void myfunc(np.ndarray[int, mode="c"] buf)

and work simultaneously with NumPy functions/operators and buffer access,
and that works nice in itself with only #177, but with the addition of
copy-in/copy-out one then has a problem.

All of this makes me think about pushing the "new buffer syntax" a bit
harder and get it started on in your GSoC. With that,

   cdef void myfunc(int[:] buf)

could easily give non-surprising effects for #177 and copy-in/copy-out, as
the Python object is not "part of the deal".

Your GSoC would then Cython-side consist more or less of

a) #177, with new syntax
b) A generic mechanism for automatic coercion between buffers of different
modes. That is:

cdef int[::contiguous] buf # new syntax for mode="c" w/o Python obj?
buf = some_object

Here, if the buffer of some_object is not contiguous, a contiguous copy
will be made! And when releasing buf, it would be copied back.

This would make the parts necesarry for Fortran support tremendously
useful elsewhere for what I believe will not be much extra effort.

(Though I could help out with the parts of those not needed for Fortran
support in order to not derail your project.)

I should probably describe some of this better, let me know which part!

-- 
Dag Sverre






_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev

Reply via email to