From a30632f7af2abdd105eadd120113d6f980938c99 Mon Sep 17 00:00:00 2001 From: Yichao Yu <yyc1992@gmail.com> Date: Mon, 23 Jun 2014 09:45:58 +0800 Subject: [PATCH] fix typo in MemoryObject.get_host_array, disable get_host_array for MemoryObject without USE_HOST_PTR since the returned array may not be valid --- pyopencl/cffi_cl.py | 11 ++++++----- src/c_wrapper/memory_object.cpp | 7 +++++++ 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/pyopencl/cffi_cl.py b/pyopencl/cffi_cl.py index 9263287a..179a6c12 100644 --- a/pyopencl/cffi_cl.py +++ b/pyopencl/cffi_cl.py @@ -641,10 +641,10 @@ def _norm_shape_dtype(shape, dtype, order="C", strides=None, name=""): except: shape = (shape,) if strides is None: - if order == "cC": - strides = c_contigous_strides(byte_size, shape) - elif order == "cF": - strides = f_contigous_strides(byte_size, shape) + if order in "cC": + strides = c_contiguous_strides(dtype.itemsize, shape) + elif order in "fF": + strides = f_contiguous_strides(dtype.itemsize, shape) else: raise RuntimeError("unrecognized order specifier %s" % order, status_code.INVALID_VALUE, name) @@ -671,7 +671,8 @@ class MemoryObjectHolder(_Common): shape, dtype, order, None, 'MemoryObjectHolder.get_host_array') _hostptr = _ffi.new('void**') _size = _ffi.new('size_t*') - _handle_error(memory_object__get_host_array(self.ptr, _hostptr, _size)) + _handle_error(_lib.memory_object__get_host_array(self.ptr, _hostptr, + _size)) ary = cffi_array(_ffi.buffer(_hostptr[0], _size[0]), shape, dtype, strides, self) if ary.nbytes > _size[0]: diff --git a/src/c_wrapper/memory_object.cpp b/src/c_wrapper/memory_object.cpp index 4670a4a5..72e442f4 100644 --- a/src/c_wrapper/memory_object.cpp +++ b/src/c_wrapper/memory_object.cpp @@ -84,6 +84,13 @@ memory_object__get_host_array(clobj_t _obj, void **hostptr, size_t *size) { auto obj = static_cast<memory_object*>(_obj); return c_handle_error([&] { + cl_mem_flags flags; + pyopencl_call_guarded(clGetMemObjectInfo, obj, CL_MEM_FLAGS, + size_arg(flags), nullptr); + if (!(flags & CL_MEM_USE_HOST_PTR)) + throw clerror("MemoryObject.get_host_array", CL_INVALID_VALUE, + "Only MemoryObject with USE_HOST_PTR " + "is supported."); pyopencl_call_guarded(clGetMemObjectInfo, obj, CL_MEM_HOST_PTR, size_arg(*hostptr), nullptr); pyopencl_call_guarded(clGetMemObjectInfo, obj, CL_MEM_SIZE, -- GitLab