inducer / pyopencl

OpenCL integration for Python, plus shiny features
http://mathema.tician.de/software/pyopencl
Other
1.04k stars 238 forks source link

Expose create_buffer_gc in a header #680

Open isuruf opened 1 year ago

isuruf commented 1 year ago

Is your feature request related to a problem? Please describe. When using pyvkfft, vkfft calls clCreateBuffer which fails sometimes because it doesn't run the garbage collector.

Describe the solution you'd like If we could have a header like below, we can include that in pyvkfft.

#include <CL/cl.h>

static inline pyopenclCreateBuffer(cl_context ctx,
      cl_mem_flags flags,
      size_t size,
      void *host_ptr,
      cl_int *status_code) {
    PYOPENCL_RETRY_IF_MEM_ERROR(
       clCreateBuffer(ctx, flags, size, host_ptr, status_code);
       if (*status_code != CL_SUCCESS)
           throw pyopencl::error("pyopenclCreateBuffer", *status_code);
    );
}

#define clCreateBuffer pyopenclCreateBuffer

Describe alternatives you've considered A clear and concise description of any alternative solutions or features you've considered.

Additional context Add any other context or screenshots about the feature request here.

inducer commented 1 year ago

Not sure about this, specifically because I'm hesitant to have PyOpenCL pick up a C-level API (which is more complexity that needs managing).

I think you can do the same just from Python, and the extra overhead should not really matter given that memory allocation is high-cost anyhow.

isuruf commented 1 year ago

The issue is that allocation might happen in C side, for eg: in VkFFT where phase vectors are allocated and there's no way to plug in a custom allocator.

inducer commented 1 year ago

But wouldn't a call to a presumptive PyOpenCL C API stick out like sore thumb in such a setting just as much as a bunch of Python C API "stuff" to call the Python side?

isuruf commented 1 year ago

Taking the example of pyvkfft, I was thinking of changing the line https://github.com/vincefn/pyvkfft/blob/5b51a7bcb0d93581c5ea8dcf7edbe77d1b191f2f/src/vkfft_opencl.cpp#L15 from

#include "vkFFT.h"

to

#include <pyopencl-allocate.h>
#include "vkFFT.h"

and that should work without any other changes.