要實現NumPy數組與GPU加速計算,可以使用NumPy庫中的CUDA加速模塊,例如Numba和CuPy。下面是如何使用這些模塊進行GPU加速計算的步驟:
import numpy as np
from numba import cuda
@cuda.jit
def add_gpu(a, b, c):
idx = cuda.grid(1)
if idx < c.size:
c[idx] = a[idx] + b[idx]
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
c = np.zeros_like(a)
threads_per_block = 32
blocks_per_grid = (a.size + (threads_per_block - 1)) // threads_per_block
add_gpu[blocks_per_grid, threads_per_block](a, b, c)
print(c)
import cupy as cp
a = cp.array([1, 2, 3])
b = cp.array([4, 5, 6])
c = a + b
print(c)
通過使用Numba和CuPy這兩個庫,可以在GPU上加速計算NumPy數組,提高計算性能。