简体   繁体   English

如何使用指针在 Numba 中包装 CFFI 函数

[英]How to wrap a CFFI function in Numba taking Pointers

It should be a easy task, but I can't find a way how to pass a pointer of a scalar value to a CFFI function within a Numba function.这应该是一项简单的任务,但我找不到如何将标量值的指针传递给 Numba 函数中的 CFFI 函数的方法。 Passing a pointer to an array works without problems using ffi.from_buffer .使用ffi.from_buffer传递指向数组的指针没有问题。

Example function示例函数

import cffi

ffi = cffi.FFI()
defs="void foo_f(int a,double *b);"
ffi.cdef(defs, override=True)
source="""
#include <stdio.h>;
void foo_f(int a,double *b){
  printf("%i",a);
  printf("   ");
  printf("%f",b[0]);
  }

"""
ffi.set_source(module_name="foo",source=source)
ffi.compile()

Passing a pointer to an array传递指向数组的指针

import numpy as np
import numba as nb
import cffi
ffi = cffi.FFI()
import numpy as np
import ctypes
import foo
nb.cffi_support.register_module(foo)
foo_f = foo.lib.foo_f

@nb.njit()
def Test(a,b):
  a_wrap=np.int32(a)
  #This works for an array
  b_wrap=ffi.from_buffer(b.astype(np.float64))
  foo_f(a_wrap,b_wrap)


a=64.
b=np.ones(5)
Test(a,b)

This works without problems, but how can I modify the Test function to take a scalar value b=5.这没有问题,但我如何修改Test函数以采用标量值b=5. without modifying the CFFI-function itself?不修改 CFFI 函数本身?

Pass scalar values by reference using Numba使用 Numba 通过引用传递标量值

To get useful timings I have modified the wrapped function a bit.为了获得有用的时间,我稍微修改了包装函数。 The function simply adds a scalar (passed by value) to a scalar b (passed by reference).该函数只是将一个标量(通过值传递)添加到一个标量 b(通过引用传递)。

Pros and cons of the approach using intrinsics使用内在函数的方法的优缺点

  • Only working in nopython mode仅在 nopython 模式下工作
  • Faster for C or Fortran functions with short runtime ( real-world example )运行时间短的 C 或 Fortran 函数更快( 实际示例

Example function示例函数

import cffi

ffi = cffi.FFI()
defs="void foo_f(double a,double *b);"
ffi.cdef(defs, override=True)
source="""
void foo_f(double a,double *b){
  b[0]+=a;
  }
"""
ffi.set_source(module_name="foo",source=source)
ffi.compile()

Wrapper using a temporary array使用临时数组的包装器

This is quite straight forward, but requires to allocate an array of size one, which is quite slow.这很简单,但需要分配一个大小为 1 的数组,这很慢。

import numpy as np
import numba as nb
from numba import cffi_support
import cffi
ffi = cffi.FFI()
import foo

nb.cffi_support.register_module(foo)
foo_f = foo.lib.foo_f

@nb.njit("float64(float64,float64)")
def method_using_arrays(a,b):
    b_arr=np.empty(1,dtype=np.float64)
    b_arr[0]=b
    b_arr_ptr=b_wrap=ffi.from_buffer(b_arr)
    foo_f(a,b_arr_ptr)
    return b_arr[0]

Wrapper using intrinsics使用内在函数的包装器

from numba import types
from numba.extending import intrinsic
from numba import cgutils

@intrinsic
def ptr_from_val(typingctx, data):
    def impl(context, builder, signature, args):
        ptr = cgutils.alloca_once_value(builder,args[0])
        return ptr
    sig = types.CPointer(data)(data)
    return sig, impl

@intrinsic
def val_from_ptr(typingctx, data):
    def impl(context, builder, signature, args):
        val = builder.load(args[0])
        return val
    sig = data.dtype(data)
    return sig, impl

@nb.njit("float64(float64,float64)")
def method_using_intrinsics(a,b):
    b_ptr=ptr_from_val(b)
    foo_f(a,b_ptr)
    return val_from_ptr(b_ptr)

Timings时间安排

#Just call the wrapped function a few times
@nb.njit()
def timing_method_using_intrinsics(a,b):
    for i in range(1000):
        b=method_using_intrinsics(a,b)
    return b

#Just call the wrapped function a few times
@nb.njit()
def timing_method_using_arrays(a,b):
    for i in range(1000):
        b=method_using_arrays(a,b)
    return b

a=1.
b=1.

%timeit timing_method_using_intrinsics(a,b)
#5.15 µs ± 33.9 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
%timeit timing_method_using_arrays(a,b)
#121 µs ± 601 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM