From 1d11993653450a6efe93dcd1b6b4647ffc7ddc46 Mon Sep 17 00:00:00 2001 From: Andreas Kloeckner Date: Fri, 14 Aug 2015 22:03:21 -0500 Subject: [PATCH] Try using numpy specific array interface before falling back to generic buffer interface --- pyopencl/cffi_cl.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/pyopencl/cffi_cl.py b/pyopencl/cffi_cl.py index f1dcf81d..dbbe762a 100644 --- a/pyopencl/cffi_cl.py +++ b/pyopencl/cffi_cl.py @@ -849,6 +849,20 @@ elif sys.version_info >= (2, 7): def _c_buffer_from_obj(obj, writable=False, retain=False): # {{{ fall back to the old CPython buffer protocol API + # {{{ try the numpy array interface first + + # avoid slow ctypes-based buffer interface wrapper + + ary_intf = getattr(obj, "__array_interface__", None) + if ary_intf is not None: + buf_base, is_read_only = ary_intf["data"] + return ( + _ffi.cast('void*', buf_base + ary_intf.get("offset", 0)), + obj.nbytes, + obj) + + # }}} + from pyopencl._buffers import Py_buffer, PyBUF_ANY_CONTIGUOUS, PyBUF_WRITABLE flags = PyBUF_ANY_CONTIGUOUS -- GitLab