pepijndevos / futhark-pycffi

Python library using the Futhark C backend via CFFI
MIT License
23 stars 7 forks source link

Matching the PyOpenCL API: adding a .get() method. #2

Closed athas closed 6 years ago

athas commented 6 years ago

When code produced by futhark-pyopencl returns an array, it returns a PyOpenCL array. These have a .get() method for turning them into Numpy arrays. futhark-pycffi has the same functionality, but by calling the to_futhark() method on the Futhark object. Would it be possible to wrap the raw cffi objects in another object that provides a .get() method (and maybe other things in the future)?

My hope is to write Python programs that automatically pick either a futhark-pyopencl or a futhark-pycffi version at runtime, based on what is available. This would allow things to Just Work for most users, and the ones who are able can get more performance by also installing and running futhark-pycffi.

pepijndevos commented 6 years ago

I'm not a huge fan, for two reasons:

For automatic compatibility, it would probably be best to always convert to a ndarray subclass with a no-op .get() method. Or find a way to turn the C types into actual PyOpenCL arrays.

Maybe it'd be best to add a compat module that exposes a slower PyOpenCL compatible interface, and keep the main one as fast as possible. But maybe some actual profiling is needed to determine what's actually fast and what not. Maybe a simple wrapper is negligible.

athas commented 6 years ago

I don't need general PyOpenCL compatibility. By looking at my own Python/Futhark programs (about ten), the vast majority need only the .get() method. Adding this would merely ease the writing of programs that work with either backend.

pepijndevos commented 6 years ago

Alright, there is a compat module now, which I think is ugly AF, but it serves the purpose you want.