Files
toropovv-pp_122-mko-lab2/lec5/numba_example.py
T
2026-11-28 10:07:24 +03:00

15 lines
545 B
Python

from numba import cuda
@cuda.jit
def heat_step(u, v):
j, i = cuda.grid(2) # x → столбец j, y → строка i
if 1 <= i < u.shape[0] - 1 and 1 <= j < u.shape[1] - 1:
v[i, j] = 0.25 * (u[i - 1, j] + u[i + 1, j]
+ u[i, j - 1] + u[i, j + 1])
threads = (16, 16)
blocks = ((u.shape[1] + 15) // 16, (u.shape[0] + 15) // 16)
d_u = cuda.to_device(u)
d_v = cuda.to_device(u) # границы уже равны u
heat_step[blocks, threads](d_u, d_v)
cuda.synchronize()