Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add image GPU loading in preprocimage #44

Merged
merged 13 commits into from
Aug 16, 2022
2 changes: 1 addition & 1 deletion src/imfilter.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function imfilter_gpu(img, krn)
padkrn = padarray(krn, Fill(zero(T), ntuple(i->0, N), padsize))

# perform ifft(fft(img) .* conj.(fft(krn)))
fftimg = img |> CuArray |> CUFFT.fft
fftimg = img |> CUFFT.fft
fftkrn = padkrn |> CuArray |> CUFFT.fft
juliohm marked this conversation as resolved.
Show resolved Hide resolved
result = (fftimg .* conj.(fftkrn)) |> CUFFT.ifft

Expand Down
2 changes: 1 addition & 1 deletion src/iqsim.jl
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ function iqsim(trainimg::AbstractArray{T,N}, tilesize::Dims{N},
rtile = CartesianIndex(start):CartesianIndex(finish)

# selected training image dataevent
TIdev = view(TI, rtile)
TIdev = view_kernel(TI, rtile)

# boundary cut mask
cutmask .= false
Expand Down
20 changes: 19 additions & 1 deletion src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,18 @@ function activation(hard, tile)
buff
end

array_cpu(array) = array

array_gpu(array) = CuArray(array)

const array_kernel = CUDA.functional() ? array_gpu : array_cpu

view_cpu(array, I) = view(array, I)

view_gpu(array, I) = Array(array[I])

const view_kernel = CUDA.functional() ? view_gpu : view_cpu

function imagepreproc(trainimg, soft, geoconfig)
padsize = geoconfig.padsize

Expand All @@ -73,7 +85,13 @@ function imagepreproc(trainimg, soft, geoconfig)
AUX, AUXTI
end

TI, SOFT
# load array to GPU, when available
TI_kernel = array_kernel(TI)
SOFT_kernel = map(SOFT) do (AUX, AUXTI)
AUX, array_kernel(AUXTI)
juliohm marked this conversation as resolved.
Show resolved Hide resolved
end

TI_kernel, SOFT_kernel
end

function finddisabled(trainimg, geoconfig)
Expand Down
4 changes: 2 additions & 2 deletions test/lowapi.jl
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ if CUDA.functional()
krn = rand(30, 10)

result_cpu = ImageQuilting.imfilter_cpu(img, krn)
result_gpu = ImageQuilting.imfilter_gpu(img, krn)
result_gpu = ImageQuilting.imfilter_gpu(CuArray(img), krn)
@test size(result_cpu) == size(result_gpu)
@test isapprox(result_cpu[:], result_gpu[:], atol=tolerance)

Expand All @@ -160,7 +160,7 @@ if CUDA.functional()
krn = rand(10, 20, 30)

result_cpu = ImageQuilting.imfilter_cpu(img, krn)
result_gpu = ImageQuilting.imfilter_gpu(img, krn)
result_gpu = ImageQuilting.imfilter_gpu(CuArray(img), krn)
@test size(result_cpu) == size(result_gpu)
@test isapprox(result_cpu[:], result_gpu[:], atol=tolerance)
end
Expand Down