生成器理解和列表理解迭代不同

时间:2016-03-22 15:17:58

标签: python c numpy pypy python-cffi

我编写了一个函数,它使用CFFI将numpy数组传递给C代码。它利用缓冲协议和内存视图有效地传递数据而无需复制。但是,这意味着您需要传递C-contiguous数组并确保使用正确的类型。 Numpy提供了一个函数numpy.ascontiguous,来执行此操作。所以我迭代参数,并应用此函数。下面的实现有效,并且可能是普遍感兴趣的。但是,考虑到它被调用的次数,它很慢。 (关于如何加快速度的任何一般性评论都会有所帮助。)

但是,实际的问题是当你用生成器理解替换第一个列表推导时,或者如果重构代码以便在第二个中调用np.ascontigous,则不再将指针传递给C代码指向numpy数组的开头。我认为它没有被调用。我正在迭代理解并只使用返回值,为什么使用列表理解或生成器理解会改变任何东西?

def cffi_wrap(cffi_func, ndarray_params, pod_params, return_shapes=None):
    """
    Wraps a cffi function to allow it to be called on numpy arrays.

    It uss the numpy buffer protocol and and the cffi buffer protocol to pass the 
    numpy array into the c function without copying any of the parameters. 
    You will need to pass dimensions into the C function, which you can do using 
    the pod_params.

    Parameters
    ----------
    cffi_func : c function
        This is a c function declared using cffi. It must take double pointers and 
        plain old data types. The arguments must be in the form of numpy arrays, 
        plain old data types, and then the returned numpy arrays.
    ndarray_params : iterable of ndarrays
         The numpy arrays to pass into the function.
    pod_params : tuple of plain old data
        This plain old data objects to pass in.  This may include for example 
        dimensions.
    return_shapes : iterable of tuples of positive ints
          The shapes of the returned objects.

    Returns
    -------
    return_vals : ndarrays of doubles.
        The objects to be calculated by the cffi_func.

    """

    arr_param_buffers = [np.ascontiguousarray(param, np.float64) 
         if np.issubdtype(param.dtype, np.float)
         else np.ascontiguousarray(param, np.intc) for param in ndarray_params]
    arr_param_ptrs = [ffi.cast("double *", ffi.from_buffer(memoryview(param))) 
        if np.issubdtype(param.dtype, np.float)
        else ffi.cast("int *", ffi.from_buffer(memoryview(param))) 
        for param in arr_param_buffers]

    if return_shapes is not None:

        return_vals_ptrs = tuple(ffi.new("double[" + str(np.prod(shape)) + "]") 
            for shape in return_shapes)
        returned_val = cffi_func(*arr_param_ptrs, *pod_params, *return_vals_ptrs)
        return_vals = tuple(np.frombuffer(ffi.buffer(
              return_val))[:np.prod(shape)].reshape(shape)
              for shape, return_val in zip(return_shapes, return_vals_ptrs))
    else:
        returned_val = cffi_func(*arr_param_ptrs, *pod_params)
        return_vals = None

    if returned_val is not None and return_vals is not None:
        return_vals = return_vals + (returned_val,)
    elif return_vals is None:
       return_vals = (returned_val,)

    if len(return_vals) == 1:
        return return_vals[0]
    else:
       return return_vals

1 个答案:

答案 0 :(得分:1)

我只是猜测,但错误可能来自keepalive:arr_param_buffers列表理解,就像你发布的代码一样,只要这个局部变量存在(即整个持续时间) cffi_wrap()),所有创建的numpy数组都是活着的。这允许您在下一行执行ffi.from_buffer(memoryview(...))并确保它们都是指向有效数据的指针。

如果用生成器表达式替换arr_param_buffers,它将逐个生成新的numpy数组,在它们上面调用ffi.from_buffer(memoryview(param)),然后将它们丢弃。 ffi.from_buffer(x)返回一个应该保持x活着的对象,但是对于我所知道的所有内容,x == memoryview(nd)本身并不会保留numpy数组nd