我能够使用这个:
import numpy as np
from numba import cuda
USE_64 = True
if USE_64:
bits = 64
np_type = np.float64
else:
bits = 32
np_type = np.float32
@cuda.jit("void(float{}[:, :], float{}[:, :])".format(bits, bits))
def distance_matrix(mat, out):
m = mat.shape[0]
n = mat.shape[1]
i, j = cuda.grid(2)
d = 0
if i < m and j < m:
for k in range(n):
tmp = mat[i, k] - mat[j, k]
d += tmp * tmp
out[i, j] = d
def gpu_dist_matrix(mat):
rows = mat.shape[0]
block_dim = (16, 16)
grid_dim = (int(rows/block_dim[0] + 1), int(rows/block_dim[1] + 1))
stream = cuda.stream()
mat2 = cuda.to_device(np.asarray(mat, dtype=np_type), stream=stream)
out2 = cuda.device_array((rows, rows))
distance_matrix[grid_dim, block_dim](mat2, out2)
out = out2.copy_to_host(stream=stream)
return out