Skip to content

Instantly share code, notes, and snippets.

@QuantumFractal
Created November 29, 2020 20:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save QuantumFractal/dabc2967d6f70f7a76ee4518b523d01f to your computer and use it in GitHub Desktop.
Save QuantumFractal/dabc2967d6f70f7a76ee4518b523d01f to your computer and use it in GitHub Desktop.
function gauss(A, i, j, ui, uj)
nw, n, ne = 1.0f0/9.0f0, 1.0f0/9.0f0, 1.0f0/9.0f0
w, c, e = 1.0f0/9.0f0, 1.0f0/9.0f0, 1.0f0/9.0f0
sw, s, se = 1.0f0/9.0f0, 1.0f0/9.0f0, 1.0f0/9.0f0
top , bottom = j - 1, uj - j
left, right = i - 1, ui - i
nw *= clamp(top * left, 0, 1)
n *= clamp(top, 0, 1)
ne *= clamp(top * right, 0, 1)
w *= clamp(left, 0, 1)
e *= clamp(right, 0, 1)
sw *= clamp(bottom * left, 0, 1)
s *= clamp(bottom, 0, 1)
se *= clamp(bottom * right, 0, 1)
# Clamp local neighborhood
x1, x2 = max(i-1, 1), min(i+1, ui)
y1, y2 = max(j-1, 1), min(j+1, uj)
# Sum over local neighborhood
lapl = A[x1, y1] * nw + A[i, y1] * n + A[x2, y1] * ne
lapl += A[x1, j] * w + A[i,j] * c + A[x2, j] * e
lapl += A[x1, y2] * sw + A[i, y2] * s + A[x2, y2] * se
return Float32(lapl)
end
function gauss_conv!(A, w, h)
# Get block index.
i = (blockIdx().x-UInt32(1)) * blockDim().x + threadIdx().x
j = (blockIdx().y-UInt32(1)) * blockDim().y + threadIdx().y
A[i,j] = gauss(A, i, j, w, h)
nothing
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment