1

I got a simple C++ class that holds a C array together with its shape for convenience:

template <typename T> class GpuBuffer {
public:
  GpuBuffer(mydim4 shape = {1, 1, 1, 1}) : data(0) {
    resize(shape);
  }

  inline operator T*() { return data; }
  inline T* operator ->() { return data; }
  inline T& operator[](const idx_t& at) { return data[at]; }
  
  // …

  mydim4 shape;
  T *data = 0;
};

using BUF = GpuBuffer<DT_CUDA>;

I am using SWIG to generate python code to access this from python. It works well, but I struggle with getting the contained array converted back to numpy. Currently, I use this code in my flambeau.i:

%pythoncode %{
import numpy

def as_numpy(buf):
  e = numpy.empty(buf.length(), dtype="float32")
  for i in range(0, buf.length()):
      e[i] = buf[i]
  e = e.reshape((buf.shape.batches, buf.shape.channels, buf.shape.height, buf.shape.width))
  
  return e
%}

I can then call flambeau.as_numpy(buffer) and get a numpy array back. It works, but it is, of course, excruciatingly slow. How do I best go about the other direction? Do I use typemaps? How would I do that? How can I make sure there won't be memory leaks?

phdoerfler
  • 470
  • 6
  • 19
  • A small improvement would be to use `numpy.frombuffer`. – Jens Munk Jan 18 '22 at 22:11
  • @JensMunk How would I do that? I can't just do `numpy.frombuffer(buf.data)` because `a bytes-like object is required, not 'SwigPyObject'`. – phdoerfler Jan 19 '22 at 15:17
  • You are right. You can do it with c-types, but it gets pretty ugly. My recommendation is to create the numpy array using a typemap. Look into `numpy.i` which is shipped with numpy. It contains numerous conversion from C++ to NumPy shallow and deep. – Jens Munk Jan 20 '22 at 19:23
  • I'd probably favour a memory view over ctypes: https://stackoverflow.com/a/16998687/168175 is an example I put together a while back. (I can't quite pull together a complete example right now because there's some stuff missing from your example currently, e.g. `DT_CUDA`, `mydim4` definitions) – Flexo Jan 21 '22 at 19:53
  • You can probably cut out a bunch of faff though and just use https://github.com/numpy/numpy/blob/main/tools/swig/numpy.i directly – Flexo Jan 21 '22 at 19:57

0 Answers0