numba用户手册 7.@cfunc
发布日期:2021-06-29 14:43:59
浏览次数:3
分类:技术文章
本文共 3229 字,大约阅读时间需要 10 分钟。
numba用户手册
5.1.@cfunc # 创建C/C++回调函数;还公开了ctypes指向该回调的回调对象-------------------------------------------------------------------------------------------------------------- #实例1.1:from numba import cfunc@cfunc("float64(float64, float64)")def add(x, y): return x + yprint(add.ctypes(4.0, 5.0)) # prints "9.0"-------------------------------------------------------------------------------------------------------------- #实例1.2:import numpy as npfrom numba import cfuncdef fun(t): # 纯Python函数 return np.exp(-t) / t**2nb_fun = cfunc("float64(float64)")(fun)# 将其编译为C回调# 将nb_fun对象的ctypes回调传递给scipy.integrate.quad并检查结果是否与纯Python函数相同import scipy.integrate as sidef run_fun(func): return si.quad(func, 1, np.inf) # 接受常规Python回调或包装在ctypes回调对象中的C回调run_fun(fun) # (0.14849550677592208, 3.8736750296130505e-10)run_fun(nb_fun.ctypes) # (0.14849550677592208, 3.8736750296130505e-10)-------------------------------------------------------------------------------------------------------------- 5.2.指针和阵列存储器处理# 实例2.1:实现回调:void(double *input, double *output, int m, int n)调用传递C数组from numba import cfunc, types, carrayc_sig = types.void(types.CPointer(types.double),types.CPointer(types.double),types.intc, types.intc)@cfunc(c_sig)def my_callback(in_, out_, m, n): in_array = carray(in_, (m, n)) out_array = carray(out_, (m, n)) for i in range(m): for j in range(n): out_array[i, j] = 2 * in_array[i, j]-------------------------------------------------------------------------------------------------------------- # 实例2.2:处理C结构# numba可将cffi类型转换为numba Record类型 numba.cffi_support.map_type:from numba import cffi_supportfrom cffi import FFIsrc = """/* Define the C struct */typedef struct my_struct { int i1; float f2; double d3; float af4[7]; // arrays are supported} my_struct;/* Define a callback function */typedef double (*my_func)(my_struct*, size_t);"""ffi = FFI()ffi.cdef(src)# Get the function signature from *my_func*sig = cffi_support.map_type(ffi.typeof('my_func'), use_record_dtype=True)#True是必需否则返回指向C结构的指针作为void指针# Make the cfuncfrom numba import cfunc, carray@cfunc(sig)def foo(ptr, n): base = carray(ptr, n) # view pointer as an array of my_struct tmp = 0 for i in range(n): tmp += base[i].i1 * base[i].f2 / base[i].d3 tmp += base[i].af4.sum() # nested arrays are like normal numpy array return tmp-------------------------------------------------------------------------------------------------------------- # 实例2.3: # 用nb.types.Record.make_c_struct可手动创建遵循C-结构的布局my_struct = types.Record.make_c_struct([ # Provides a sequence of 2-tuples i.e. (name:str, type:Type) ('i1', types.int32), ('f2', types.float32), ('d3', types.float64), ('af4', types.NestedArray(dtype=types.float32, shape=(7,))),])--------------------------------------------------------------------------------------------------------------
转载地址:https://chunyou.blog.csdn.net/article/details/103963064 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
关注你微信了!
[***.104.42.241]2024年04月05日 21时43分06秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
awk 的内置变量 NF、NR、FNR、FS、OFS、RS、ORS
2019-04-29
CentOS系统内核升级攻略
2019-04-29
linux系统时区修改(Debian的主机和docker)
2019-04-29
docker-compose 安装
2019-04-29
crontab 定时任务
2019-04-29
查看docker veth pair与宿主机上网卡的对应关系
2019-04-29
使用 GitLab CI 进行持续集成的一些踩坑
2019-04-29
企业云盘给贸易业带来新的效益
2019-04-29
Linux入门常用命令
2019-04-29
Spring整理
2019-04-29
SpringMvc加强
2019-04-29
初识Vue全家桶 Nuxt.js(一)
2019-04-29
基本路由及动态路由(二)
2019-04-29
视图:默认模板+默认布局(自定义布局)+nuxt.js页面(三)
2019-04-29
基于nuxt下asyncData,fetch发送axios请求(四)
2019-04-29
插件机制+自定义axios(五)
2019-04-29
Redis的学习之路
2019-04-29
Windows下Redies+GUI安装,使用Jedis与spring boot 整合
2019-04-29