Skip to content

Refact ImageQuilting code #1

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

Draft
wants to merge 21 commits into
base: platform-aware-iqsim-main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ CUDA = "052768ef-5323-5732-b1bb-66c8b64840ba"
CpuId = "adafc99b-e345-5852-983c-f28acb93d879"
FFTW = "7a1cc6ca-52ef-59f5-83cd-3a7055c09341"
GeoStatsBase = "323cb8eb-fbf6-51c0-afd0-f8fba70507b2"
GeoStatsImages = "7cd16168-b42c-5e7d-a585-4f59d326662d"
Graphs = "86223c79-3864-5bf0-83f7-82e725a168b6"
GraphsFlows = "06909019-6f44-4949-96fc-b9d9aaa02889"
ImageFiltering = "6a3955dd-da59-5b1f-98d4-e7296123deb5"
Expand All @@ -33,12 +32,10 @@ Tables = "bd369af6-aec1-5ad0-b16a-f7cc5008161c"
CUDA = "3.11"
CpuId = "0.2, 0.3"
FFTW = "1.0"
GeoStatsBase = "0.26"
Graphs = "1.4"
GraphsFlows = "0.1"
ImageFiltering = "0.6, 0.7"
ImageMorphology = "0.2, 0.3, 0.4"
Meshes = "0.22"
ProgressMeter = "1.1"
RecipesBase = "1.0"
StatsBase = "0.32, 0.33"
Expand Down
11 changes: 11 additions & 0 deletions run_experiment_app.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/bash

for turn in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
do
echo $turn.adhoc
git switch iq-sim-refact-adhoc
PATH_IQ="." $JULIA_PATH/julia ./run_sample_app.jl $turn >> output.iq.app.adhoc
echo $turn.structured
git switch iq-sim-refact
PATH_IQ="." $JULIA_PATH/julia ./run_sample_app.jl $turn >> output.iq.app.structured
done
17 changes: 17 additions & 0 deletions run_experiment_kernel.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/bash



for size in 64 128 256
do
for version in 2
do
for turn in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
do
echo $size.$version.$turn.adhoc
$JULIA_PATH/julia ./run_sample.jl $version $size $turn >> output.iq.adhoc.$version.$size
echo $size.$version.$turn.structured
PLATFORM_DESCRIPTION=Platform.$version.toml $JULIA_PATH/julia ./run_sample.jl -$version $size $turn >> output.iq.structured.$version.$size
done
done
done
37 changes: 37 additions & 0 deletions run_sample_app.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
path_iq = get(ENV,"PATH_IQ",".")
import Pkg; Pkg.activate(path_iq)
using GeoStats
using GeoStatsImages
using ImageQuilting

function main(args)

@info args

i = parse(Int64,args[1])

@info "small $i"

# small
TIₛ = geostatsimage("WalkerLake")
iqsim(asarray(TIₛ, :Z), (30, 30))
@time iqsim(asarray(TIₛ, :Z), (30, 30))

@info "medium $i"

# medium
TIₘ = geostatsimage("StanfordV")
iqsim(asarray(TIₘ, :K), (30, 30, 30))
@time iqsim(asarray(TIₘ, :K), (30, 30, 30))

@info "large $i"

# large
TIₗ = geostatsimage("Fluvsim")
iqsim(asarray(TIₗ, :facies), (30, 30, 30))
@time iqsim(asarray(TIₗ, :facies), (30, 30, 30))

end

main(ARGS)

48 changes: 48 additions & 0 deletions run_sample_kernel.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import Pkg; Pkg.activate(".")
using ImageQuilting
using CUDA

function main(args)

@info args

v = parse(Int64,args[1])
size = parse(Int64,args[2])
i = parse(Int64,args[3])

img = rand(Float32,(size,size,size))
krn = rand(Float32,(10,10,10))

if (v < 0)
@info "$i: structured / $v"
img = ImageQuilting.array_kernel(img)
ImageQuilting.init_imfilter_kernel()
@time ImageQuilting.imfilter_kernel(img,krn)
ImageQuilting.imfilter_kernel(img,krn)
ImageQuilting.imfilter_kernel(img,krn)
@time ImageQuilting.imfilter_kernel(img,krn)
elseif (v == 1)
@info "$i: ad-hoc / default"
@time ImageQuilting.imfilter_cpu(img, krn)
@time ImageQuilting.imfilter_cpu(img, krn)
elseif (v == 2)
@info "$i: ad-hoc / CUDA"
img = CuArray(img)
@time ImageQuilting.imfilter_cuda(img, krn)
ImageQuilting.imfilter_cuda(img, krn)
ImageQuilting.imfilter_cuda(img, krn)
@time ImageQuilting.imfilter_cuda(img, krn)
elseif (v == 3)
@info "$i: ad-hoc / OpenCL"
ImageQuilting.init_opencl_context()
@time ImageQuilting.imfilter_opencl(img, krn)
ImageQuilting.imfilter_opencl(img, krn)
ImageQuilting.imfilter_opencl(img, krn)
@time ImageQuilting.imfilter_opencl(img, krn)
else
@info "wrong selection"
end
end

main(ARGS)

21 changes: 9 additions & 12 deletions src/ImageQuilting.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,20 @@ using ProgressMeter: Progress, next!
using FFTW: set_num_threads
using CpuId: cpucores
using RecipesBase
using Primes

using CUDA
using OpenCL
using CLFFT
const clfft = CLFFT
using PlatformAware

using Base: @nexprs, @nloops, @nref
using SparseArrays: spzeros
using Statistics: mean, std
using Random

using PlatformAware

import GeoStatsBase: preprocess, solvesingle

include("utils.jl")
include("utils_gpu.jl")
include("plotrecipes.jl")
include("relaxation.jl")
include("taumodel.jl")
Expand All @@ -42,15 +39,15 @@ include("iqsim.jl")
include("voxelreuse.jl")
include("geostats.jl")

include("test_imfilter.jl")

function __init__()
include(pkgdir(@__MODULE__) * "/src/kernels.jl")
include(pkgdir(@__MODULE__) * "/src/imfilter.default.jl")
include(pkgdir(@__MODULE__) * "/src/imfilter.cuda.jl")
include(pkgdir(@__MODULE__) * "/src/imfilter.opencl.jl")
function __init__()
include("src/kernels.jl")
include("src/kernel/imfilter_default.jl")
include("src/kernel/imfilter_cuda.jl")
include("src/kernel/imfilter_opencl.jl")
end

include("test_imfilter.jl")

export
# functions
iqsim,
Expand Down
34 changes: 0 additions & 34 deletions src/imfilter.cuda.jl

This file was deleted.

111 changes: 0 additions & 111 deletions src/imfilter.opencl.jl

This file was deleted.

44 changes: 44 additions & 0 deletions src/kernel/imfilter_cuda.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# ------------------------------------------------------------------
# Licensed under the MIT License. See LICENCE in the project root.
# ------------------------------------------------------------------

@platform aware function init_imfilter_kernel({accelerator_count::(@atleast 1),
accelerator_manufacturer::NVIDIA,
accelerator_api::(@api CUDA)})
@info "Running on NVIDIA/CUDA GPU"
end

@platform aware array_kernel({accelerator_count::(@atleast 1),
accelerator_manufacturer::NVIDIA,
accelerator_api::(@api CUDA)}, array) =CuArray{Float32}(array)

@platform aware view_kernel({accelerator_count::(@atleast 1),
accelerator_manufacturer::NVIDIA,
accelerator_api::(@api CUDA)}, array, I) = Array(array[I])

counter = Ref{Int}(0)


@platform aware function imfilter_kernel({accelerator_count::(@atleast 1),
accelerator_manufacturer::NVIDIA,
accelerator_api::(@api CUDA)}, img, krn)

counter[] = counter[] + 1
imfilter_cuda(img,krn)
end

function imfilter_cuda(img, krn)

# pad kernel to common size with image
padkrn = CUDA.zeros(Float32, size(img))
copyto!(padkrn, CartesianIndices(krn), CuArray{Float32}(krn), CartesianIndices(krn))

# perform ifft(fft(img) .* conj.(fft(krn)))
fftimg = img |> CUFFT.fft
fftkrn = padkrn |> CUFFT.fft
result = (fftimg .* conj.(fftkrn)) |> CUFFT.ifft

# recover result
finalsize = size(img) .- (size(krn) .- 1)
real.(result[CartesianIndices(finalsize)]) |> Array
end
Loading