python - __init__() 得到了一个意外的关键字参数“py_func”
问题描述
我们将为要在 GPU 上计算的函数使用 numba.jit 装饰器。装饰器有几个参数,但我们只使用目标参数。Target 告诉 jit 为哪个源(“CPU”或“Cuda”)编译代码。“Cuda”对应GPU。但是,如果 CPU 作为参数传递,那么 jit 会尝试优化代码在 CPU 上运行得更快,并提高速度。但我得到了错误——init()得到了一个意外的关键字参数'py_func'
from numba import jit, cuda
import numpy as np
# to measure exec time
from timeit import default_timer as timer
# normal function to run on cpu
def func(a):
for i in range(10000000):
a[i]+= 1
# function optimized to run on gpu
@jit(target ="cuda")
def func2(a):
for i in range(10000000):
a[i]+= 1
if __name__=="__main__":
n = 10000000
a = np.ones(n, dtype = np.float64)
b = np.ones(n, dtype = np.float32)
start = timer()
func(a)
print("without GPU:", timer()-start)
start = timer()
func2(a)
print("with GPU:", timer()-start)
解决方案
我遇到了同样的问题并尝试了@Tim Roberts 提供的解决方案。但是,我收到以下错误:
ValueError:未指定内核启动配置。使用语法:
kernel_function[blockspergrid, threadsperblock](arg0, arg1, ..., argn)
请参阅 https://numba.pydata.org/numba-doc/latest/cuda/kernels.html#kernel-invocation 寻求帮助。
推荐阅读
- python - Tkinter 将多个滚动条绑定到 MouseWheel
- python - 加入 DataFrames,它们是包含在不同字典列表中的字典中的值
- sql-server - T-SQL While 无限循环
- python - 标准化已用 np.nan 填充的 3D NumPy 数组
- c - 为什么 ARMv7-A 在从缓存中刷新堆栈指针时会崩溃
- c# - 在 lambda 中具有 await 的异步方法中缺少 await 警告
- typescript - 从树结构创建节点结构
- c# - 使用 npgsql 连接到 RDS 中的 PostGres
- jupyter-notebook - 如何在出现特定字符串时拆分单元格
- python - 给定曲面方程的形式,如何找到“最佳拟合”曲面的方程?