是否可以向量化这个 python 函数以在 GPU 上运行?

Rad*_*rus 5 python parallel-processing gpu vectorization numba

我有这个函数(见下文),它接受一个点坐标元组,如果该点位于形状多边形(全局变量)内,则返回一个布尔值。我想将其向量化以获取元组数组并返回等效的 bool 数组。

它与 numpy.vectorize 配合得很好,但速度很慢(6-7s),特别是因为我在 for 循环中调用它。

我读到在 GPU 上运行函数可能比在单核上运行更快。这是我根据互联网上的示例尝试的,但我使用了TypeError: invalid signature: 'str' instance not allowed函数装饰器。

函数签名有什么问题以及如何修复它?

import shapely.geometry as sp
import numba as nb
import numpy as np

#the matrix of tuples:
obj_location = np.empty((800,800), object)
for idx in np.ndindex(maxY,maxX):
    obj_location[idx] = idx

#the polygon
poly=sp.Polygon([(200,200),(200,600),(600,600),(600,200)])

#function definition
@nb.vectorize(['bool(tuple)'], target='cuda')
def inside(point_coordinates):
    (y,x) = point_coordinates
    return poly.contains(sp.Point(x,y))

#call function
inside(obj_location)
Run Code Online (Sandbox Code Playgroud)