Skip to content

Commit

Permalink
Merge pull request #34 from MagneticParticleImaging/nh/gpuSupport
Browse files Browse the repository at this point in the history
Add GPU support
  • Loading branch information
nHackel authored Jul 12, 2024
2 parents f168899 + 13b977a commit e964253
Show file tree
Hide file tree
Showing 25 changed files with 506 additions and 310 deletions.
37 changes: 37 additions & 0 deletions .buildkite/pipeline.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
steps:
- label: "Nvidia GPUs -- MPIReco.jl"
plugins:
- JuliaCI/julia#v1:
version: "1.10"
agents:
queue: "juliagpu"
cuda: "*"
command: |
julia --color=yes --project -e '
using Pkg
Pkg.add("TestEnv")
using TestEnv
TestEnv.activate();
Pkg.add("CUDA")
Pkg.instantiate()
include("test/gpu/cuda.jl")'
timeout_in_minutes: 30

- label: "AMD GPUs -- MPIReco.jl"
plugins:
- JuliaCI/julia#v1:
version: "1.10"
agents:
queue: "juliagpu"
rocm: "*"
rocmgpu: "*"
command: |
julia --color=yes --project -e '
using Pkg
Pkg.add("TestEnv")
using TestEnv
TestEnv.activate();
Pkg.add("AMDGPU")
Pkg.instantiate()
include("test/gpu/rocm.jl")'
timeout_in_minutes: 30
12 changes: 7 additions & 5 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "MPIReco"
uuid = "e4246700-6248-511e-8146-a1d1f47669d2"
authors = ["Tobias Knopp <tobias@knoppweb.de>"]
version = "0.5.4"
version = "0.6.0"

[deps]
DSP = "717857b8-e6f2-59f4-9121-6e50c889abd2"
Expand Down Expand Up @@ -31,13 +31,14 @@ DistributedArrays = "0.6"
FFTW = "1.3"
ImageUtils = "0.2"
IniFile = "0.5"
JLArrays = "0.1"
LinearAlgebra = "1"
LinearOperators = "2.3.3"
LinearOperatorCollection = "1.2"
LinearOperators = "2.3"
LinearOperatorCollection = "2"
MPIFiles = "0.13, 0.14, 0.15, 0.16"
ProgressMeter = "1.2"
Reexport = "1.0"
RegularizedLeastSquares = "0.14"
RegularizedLeastSquares = "0.16"
SparseArrays = "1"
Statistics = "1"
ThreadPools = "2.1.1"
Expand All @@ -47,6 +48,7 @@ julia = "1.9"
[extras]
FileIO = "5789e2e9-d7fb-5bc7-8068-2c6fae9b9549"
HTTP = "cd3eb016-35fb-5094-929b-558a96fad6f3"
JLArrays = "27aeb0d3-9eb9-45fb-866b-73c2ecf80fcb"
LazyArtifacts = "4af54fe1-eca0-43a8-85a7-787d91b784e3"
Scratch = "6c6a2e73-6563-6170-7368-637461726353"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
Expand All @@ -55,4 +57,4 @@ ImageQualityIndexes = "2996bd0c-7a13-11e9-2da2-2f5ce47296a9"
Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d"

[targets]
test = ["Test", "HTTP", "FileIO", "LazyArtifacts", "Scratch", "ImageMagick", "ImageQualityIndexes", "Unitful"]
test = ["Test", "HTTP", "FileIO", "LazyArtifacts", "Scratch", "ImageMagick", "ImageQualityIndexes", "Unitful", "JLArrays"]
File renamed without changes.
File renamed without changes.
98 changes: 98 additions & 0 deletions config/SinglePatch.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
_module = "MPIReco"
_type = "RecoPlan{SinglePatchReconstructionAlgorithm}"

[parameter]
_module = "MPIReco"
_type = "RecoPlan{SinglePatchParameters}"

[parameter.reco]
_module = "MPIReco"
_type = "RecoPlan{SinglePatchReconstructionParameter}"

[parameter.reco.sfLoad]
_module = "AbstractImageReconstruction"
_type = "RecoPlan{ProcessResultCache}"

[parameter.reco.sfLoad.param]
_module = "MPIReco"
_type = "RecoPlan{DenseSystemMatixLoadingParameter}"

[parameter.reco.sfLoad.param.freqFilter]
_module = "MPIReco"
_type = "RecoPlan{SNRThresholdFrequencyFilterParameter}"

[parameter.reco.sfLoad.param.gridding]
_module = "MPIReco"
_type = "RecoPlan{SystemMatrixGriddingParameter}"


[[parameter.reco._listener.sf]]
field = "fov"
_module = "AbstractImageReconstruction"
_type = "LinkedFieldListener"
plan = ["parameter", "reco", "sfLoad", "param", "gridding"]

[parameter.reco._listener.sf.fn]
_module = "MPIReco"
_type = "defaultParameterCalibFov"
[[parameter.reco._listener.sf]]
field = "center"
_module = "AbstractImageReconstruction"
_type = "LinkedFieldListener"
plan = ["parameter", "reco", "sfLoad", "param", "gridding"]

[parameter.reco._listener.sf.fn]
_module = "MPIReco"
_type = "defaultParameterCalibCenter"
[[parameter.reco._listener.sf]]
field = "gridsize"
_module = "AbstractImageReconstruction"
_type = "LinkedFieldListener"
plan = ["parameter", "reco", "sfLoad", "param", "gridding"]

[parameter.reco._listener.sf.fn]
_module = "MPIReco"
_type = "defaultParameterGridSize"
[[parameter.reco._listener.sf]]
field = "minFreq"
_module = "AbstractImageReconstruction"
_type = "LinkedFieldListener"
plan = ["parameter", "reco", "sfLoad", "param", "freqFilter"]

[parameter.reco._listener.sf.fn]
_module = "MPIReco"
_type = "defaultParameterMinFreq"
[[parameter.reco._listener.sf]]
field = "maxFreq"
_module = "AbstractImageReconstruction"
_type = "LinkedFieldListener"
plan = ["parameter", "reco", "sfLoad", "param", "freqFilter"]

[parameter.reco._listener.sf.fn]
_module = "MPIReco"
_type = "defaultParameterMaxFreq"
[[parameter.reco._listener.sf]]
field = "recChannels"
_module = "AbstractImageReconstruction"
_type = "LinkedFieldListener"
plan = ["parameter", "reco", "sfLoad", "param", "freqFilter"]

[parameter.reco._listener.sf.fn]
_module = "MPIReco"
_type = "defaultParameterRecChannels"

[parameter.reco.solverParams]
_module = "MPIReco"
_type = "RecoPlan{ElaborateSolverParameters}"

[parameter.pre]
_module = "AbstractImageReconstruction"
_type = "RecoPlan{ProcessResultCache}"

[parameter.pre.param]
_module = "MPIReco"
_type = "RecoPlan{CommonPreProcessingParameters}"

[parameter.post]
_module = "MPIReco"
_type = "RecoPlan{NoPostProcessing}"
70 changes: 70 additions & 0 deletions config/SinglePatchSparse.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
_module = "MPIReco"
_type = "RecoPlan{SinglePatchReconstructionAlgorithm}"

[parameter]
_module = "MPIReco"
_type = "RecoPlan{SinglePatchParameters}"

[parameter.reco]
_module = "MPIReco"
_type = "RecoPlan{SinglePatchReconstructionParameter}"

[parameter.reco.sfLoad]
_module = "AbstractImageReconstruction"
_type = "RecoPlan{ProcessResultCache}"

[parameter.reco.sfLoad.param]
_module = "MPIReco"
_type = "RecoPlan{SparseSystemMatrixLoadingParameter}"

[parameter.reco.sfLoad.param.freqFilter]
_module = "MPIReco"
_type = "RecoPlan{SNRThresholdFrequencyFilterParameter}"

[parameter.reco.sfLoad.param.gridding]
_module = "MPIReco"
_type = "RecoPlan{SystemMatrixGriddingParameter}"

[[parameter.reco._listener.sf]]
field = "minFreq"
_module = "AbstractImageReconstruction"
_type = "LinkedFieldListener"
plan = ["parameter", "reco", "sfLoad", "param", "freqFilter"]

[parameter.reco._listener.sf.fn]
_module = "MPIReco"
_type = "defaultParameterMinFreq"
[[parameter.reco._listener.sf]]
field = "maxFreq"
_module = "AbstractImageReconstruction"
_type = "LinkedFieldListener"
plan = ["parameter", "reco", "sfLoad", "param", "freqFilter"]

[parameter.reco._listener.sf.fn]
_module = "MPIReco"
_type = "defaultParameterMaxFreq"
[[parameter.reco._listener.sf]]
field = "recChannels"
_module = "AbstractImageReconstruction"
_type = "LinkedFieldListener"
plan = ["parameter", "reco", "sfLoad", "param", "freqFilter"]

[parameter.reco._listener.sf.fn]
_module = "MPIReco"
_type = "defaultParameterRecChannels"

[parameter.reco.solverParams]
_module = "MPIReco"
_type = "RecoPlan{ElaborateSolverParameters}"

[parameter.pre]
_module = "AbstractImageReconstruction"
_type = "RecoPlan{ProcessResultCache}"

[parameter.pre.param]
_module = "MPIReco"
_type = "RecoPlan{CommonPreProcessingParameters}"

[parameter.post]
_module = "MPIReco"
_type = "RecoPlan{NoPostProcessing}"
19 changes: 18 additions & 1 deletion src/AlgorithmInterface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,26 @@ struct MixedAlgorithm <: ReconstructionAlgorithmType end
# TODO recoAlgorithmType
# TODO undefined for certain "Algorithm" components
#recoAlgorithmTypes(::Type{ConcreteRecoAlgorithm}) = SystemMatrixBasedAlgorithm()
export plandir
export planpath, plandir
plandir() = abspath(homedir(), ".mpi", "RecoPlans")

function planpath(name::AbstractString)
for dir in [joinpath(@__DIR__, "..", "config"),plandir()]
filename = joinpath(dir, string(name, ".toml"))
if isfile(filename)
return filename
end
end
throw(ArgumentError("Could not find a suitable MPI reconstruction plan with name $name.\nCustom plans can be stored in $(plandir())."))
end

export reconstruct
function reconstruct(name::AbstractString, data::MPIFile; kwargs...)
plan = loadPlan(MPIReco, name, [AbstractImageReconstruction, MPIFiles, MPIReco, RegularizedLeastSquares])
setAll!(plan; kwargs...)
return reconstruct(build(plan), data)
end

# Check if contains
isSystemMatrixBased(::T) where T <: AbstractImageReconstructionAlgorithm = recoAlgorithmTypes(T) isa SystemMatrixBasedAlgorithm
isXSpaceBased(::T) where T <: AbstractImageReconstructionAlgorithm = recoAlgorithmTypes(T) isa XSpaceBasedAlgorithm
Expand Down
3 changes: 2 additions & 1 deletion src/Algorithms/SinglePatchAlgorithms/SinglePatchAlgorithm.jl
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ function process(algo::SinglePatchReconstructionAlgorithm, params::Union{A, Proc
@warn "System matrix and measurement have different element data type. Mapping measurment data to system matrix element type."
result = map(eltype(algo.S),result)
end
result = copyto!(similar(algo.S, size(result)...), result)
return result
end

Expand All @@ -67,5 +68,5 @@ function getLinearOperator(algo::SinglePatchReconstructionAlgorithm, params::Sin
end

function getLinearOperator(algo::SinglePatchReconstructionAlgorithm, params::SinglePatchReconstructionParameter{<:SparseSystemMatrixLoadingParameter, S}) where {S}
return createLinearOperator(params.sfLoad.sparseTrafo, eltype(algo.S); shape=tuple(shape(algo.grid)...))
return process(algo, params.sfLoad, eltype(algo.S), tuple(shape(algo.grid)...))
end
16 changes: 9 additions & 7 deletions src/LeastSquares.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,17 @@ end
export ElaborateSolverParameters
Base.@kwdef mutable struct ElaborateSolverParameters{SL} <: AbstractSolverParameters{SL}
solver::Type{SL}
iterations::Int64=10
enforceReal::Bool=true
enforcePositive::Bool=true
iterations::Int64 = 10
enforceReal::Bool = true
enforcePositive::Bool = true
kwargWarning::Bool = true
# Union of all kwargs
normalizeReg::AbstractRegularizationNormalization = SystemMatrixBasedNormalization()
randomized::Union{Nothing, Bool} = false
randomized::Union{Nothing, Bool, Symbol} = nothing
seed::Union{Nothing, Int64} = nothing
shuffleRows::Union{Nothing, Bool} = false
rho::Union{Nothing, Float64} = nothing
normalize_rho::Union{Nothing, Bool} = nothing
vary_rho::Union{Nothing, Symbol} = nothing
theta::Union{Nothing, Float64} = nothing
restart::Union{Nothing, Symbol} = nothing
regTrafo::Union{Nothing, Vector{Union{AbstractArray, AbstractLinearOperator}}} = nothing
Expand All @@ -51,7 +53,7 @@ Base.propertynames(params::RecoPlan{ElaborateSolverParameters}) = union([:solver

getSolverKwargs(::Type{SL}) where SL <: AbstractLinearSolver = intersect(union(Base.kwarg_decl.(methods(SL))...), fieldnames(ElaborateSolverParameters))

function process(t::Type{<:AbstractMPIRecoAlgorithm}, params::LeastSquaresParameters{SL}, u::Array) where SL
function process(t::Type{<:AbstractMPIRecoAlgorithm}, params::LeastSquaresParameters{SL}, u::AbstractArray) where SL

N = size(params.S, 2)
M = div(length(params.S), N)
Expand Down Expand Up @@ -79,7 +81,7 @@ function process(t::Type{<:AbstractMPIRecoAlgorithm}, params::LeastSquaresParame
if !isnothing(params.op)
d[:] = params.op*d
end
c[:, l] = real(d)
c[:, l] = Array(real(d))
end

return c
Expand Down
4 changes: 2 additions & 2 deletions src/MPIReco.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ module MPIReco
using DistributedArrays
# using TensorDecompositions
using IniFile
import LinearAlgebra: ldiv!, \
import LinearAlgebra: ldiv!, \, mul!
# TODO sort out import for Base and AbstractImageReconstruction to avoid boiler plate
import Base: put!, take!
import AbstractImageReconstruction: process, parameter
import AbstractImageReconstruction: process, parameter, reconstruct
using FFTW
using LinearOperatorCollection

Expand Down
9 changes: 8 additions & 1 deletion src/SystemMatrix/SystemMatrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,12 @@ export AbstractSystemMatrixLoadingParameter
abstract type AbstractSystemMatrixLoadingParameter <: AbstractSystemMatrixParameter end

export DenseSystemMatixLoadingParameter
Base.@kwdef struct DenseSystemMatixLoadingParameter{F<:AbstractFrequencyFilterParameter, G<:AbstractSystemMatrixGriddingParameter} <: AbstractSystemMatrixLoadingParameter
Base.@kwdef struct DenseSystemMatixLoadingParameter{F<:AbstractFrequencyFilterParameter, matT <: AbstractArray, G<:AbstractSystemMatrixGriddingParameter} <: AbstractSystemMatrixLoadingParameter
freqFilter::F
gridding::G
bgCorrection::Bool = false
loadasreal::Bool = false
arrayType::Type{matT} = Array
end
function process(t::Type{<:AbstractMPIRecoAlgorithm}, params::DenseSystemMatixLoadingParameter, sf::MPIFile)
# Construct freqFilter
Expand All @@ -66,6 +67,9 @@ end
function process(t::Type{<:AbstractMPIRecoAlgorithm}, params::DenseSystemMatixLoadingParameter, sf::MPIFile, frequencies::Vector{CartesianIndex{2}})
S, grid = getSF(sf, frequencies, nothing; toKwargs(params)...)
@info "Loading SM"
if !isa(S, params.arrayType)
S = params.arrayType(S)
end
return S, grid
end

Expand All @@ -88,6 +92,9 @@ function process(t::Type{<:AbstractMPIRecoAlgorithm}, params::SparseSystemMatrix
S, grid = getSF(sf, frequencies, params.sparseTrafo; toKwargs(params)...)
return S, grid
end
function process(t::Type{<:AbstractMPIRecoAlgorithm}, params::SparseSystemMatrixLoadingParameter, elType::Type{<:Number}, shape::NTuple{N, Int64}) where N
return createLinearOperator(params.sparseTrafo, elType; shape)
end

function converttoreal(S::AbstractArray{Complex{T}},f) where T
N = prod(calibSize(f))
Expand Down
Loading

2 comments on commit e964253

@nHackel
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/110932

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.6.0 -m "<description of version>" e96425384f36c936002878adacfd53ac0bc5f724
git push origin v0.6.0

Please sign in to comment.