Jacky Tsao
2017-04-13 02:23:32 UTC
tHi,
I'm developing a memory structure for python based deeplearning
framework. I thought it will be so nice if it support Python buffer
protocol, like zero-copy its buffer to NUMPY for calculation.
So I ran a lot of options in SWIG currently, none quite fit my need.
So I came up with a hack way to get it done. But I think if done it
right, it will benefit more that it is currently, and I also want to
know if there is a better way to do it.
For convenient, I start using a class template inherited from
std::vector, named carray (I know, and I'll work on the name later).
Implemented a method called 'getbuffer(PyObject *self, Py_buffer
*view, int flags)', and I'm using '-builtin' compilation flag to make
sure SWIG generate a built-in type. All I need is the built-in type of
this class can dispatch 'getbufferproc' to it, then I can fill in the
protocol with vector's data(). Attached file is a preliminary concept
work.
I just new to SWIG and use it for like weeks, so I need opinions if
this is a feasible way to do this, or can this work be generalized to
help others.
I'm developing a memory structure for python based deeplearning
framework. I thought it will be so nice if it support Python buffer
protocol, like zero-copy its buffer to NUMPY for calculation.
So I ran a lot of options in SWIG currently, none quite fit my need.
So I came up with a hack way to get it done. But I think if done it
right, it will benefit more that it is currently, and I also want to
know if there is a better way to do it.
For convenient, I start using a class template inherited from
std::vector, named carray (I know, and I'll work on the name later).
Implemented a method called 'getbuffer(PyObject *self, Py_buffer
*view, int flags)', and I'm using '-builtin' compilation flag to make
sure SWIG generate a built-in type. All I need is the built-in type of
this class can dispatch 'getbufferproc' to it, then I can fill in the
protocol with vector's data(). Attached file is a preliminary concept
work.
I just new to SWIG and use it for like weeks, so I need opinions if
this is a feasible way to do this, or can this work be generalized to
help others.