Skip to content

Commit 379e75e

Browse files
authored
enabled numba.cuda.jit in hotspots cupy (#691)
1 parent aac6834 commit 379e75e

File tree

1 file changed

+35
-30
lines changed

1 file changed

+35
-30
lines changed

xrspatial/focal.py

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from math import isnan
44

55
import dask.array as da
6+
import numba as nb
67
import numpy as np
78
import pandas as pd
89
import xarray as xr
@@ -611,40 +612,42 @@ def _hotspots_dask_numpy(raster, kernel):
611612
return out
612613

613614

614-
def _calc_hotspots_cupy(z_array):
615-
out = cupy.zeros_like(z_array, dtype=cupy.int8)
616-
rows, cols = z_array.shape
615+
@nb.cuda.jit(device=True)
616+
def _gpu_hotspots(data):
617+
zscore = data[0, 0]
617618

618-
for y in prange(rows):
619-
for x in prange(cols):
620-
zscore = z_array[y, x]
619+
# find p value
620+
p_value = 1.0
621+
if abs(zscore) >= 2.33:
622+
p_value = 0.0099
623+
elif abs(zscore) >= 1.65:
624+
p_value = 0.0495
625+
elif abs(zscore) >= 1.29:
626+
p_value = 0.0985
621627

622-
# find p value
623-
p_value = 1.0
624-
if abs(zscore) >= 2.33:
625-
p_value = 0.0099
626-
elif abs(zscore) >= 1.65:
627-
p_value = 0.0495
628-
elif abs(zscore) >= 1.29:
629-
p_value = 0.0985
628+
# confidence
629+
confidence = 0
630+
if abs(zscore) > 2.58 and p_value < 0.01:
631+
confidence = 99
632+
elif abs(zscore) > 1.96 and p_value < 0.05:
633+
confidence = 95
634+
elif abs(zscore) > 1.65 and p_value < 0.1:
635+
confidence = 90
630636

631-
# confidence
632-
confidence = 0
633-
if abs(zscore) > 2.58 and p_value < 0.01:
634-
confidence = 99
635-
elif abs(zscore) > 1.96 and p_value < 0.05:
636-
confidence = 95
637-
elif abs(zscore) > 1.65 and p_value < 0.1:
638-
confidence = 90
637+
hot_cold = 0
638+
if zscore > 0:
639+
hot_cold = 1
640+
elif zscore < 0:
641+
hot_cold = -1
639642

640-
hot_cold = 0
641-
if zscore > 0:
642-
hot_cold = 1
643-
elif zscore < 0:
644-
hot_cold = -1
643+
return hot_cold * confidence
645644

646-
out[y, x] = hot_cold * confidence
647-
return out
645+
646+
@nb.cuda.jit
647+
def _run_gpu_hotspots(data, out):
648+
i, j = nb.cuda.grid(2)
649+
if i >= 0 and i < out.shape[0] and j >= 0 and j < out.shape[1]:
650+
out[i, j] = _gpu_hotspots(data[i:i + 1, j:j + 1])
648651

649652

650653
def _hotspots_cupy(raster, kernel):
@@ -666,7 +669,9 @@ def _hotspots_cupy(raster, kernel):
666669
)
667670
z_array = (mean_array - global_mean) / global_std
668671

669-
out = _calc_hotspots_cupy(z_array)
672+
out = cupy.zeros_like(z_array, dtype=cupy.int8)
673+
griddim, blockdim = cuda_args(z_array.shape)
674+
_run_gpu_hotspots[griddim, blockdim](z_array, out)
670675
return out
671676

672677

0 commit comments

Comments
 (0)