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

Fix extra allocations when benchmarking with no motion #483

Merged
merged 6 commits into from
Sep 20, 2024
Merged
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
2 changes: 1 addition & 1 deletion KomaMRICore/src/KomaMRICore.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module KomaMRICore
module KomaMRICore

# General
import Base.*, Base.abs
Expand Down
37 changes: 23 additions & 14 deletions KomaMRICore/src/simulation/Flow.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,10 @@ function outflow_spin_reset!(
idx = KomaMRIBase.get_indexing_range(spin_span)
spin_state_matrix = @view(spin_state_matrix[idx, :])
# Obtain mask
itp = KomaMRIBase.interpolate(action.spin_reset, KomaMRIBase.Gridded(KomaMRIBase.Constant{KomaMRIBase.Previous}()), Val(size(action.spin_reset, 1)), t)
mask = KomaMRIBase.resample(itp, ts)
mask .= (cumsum(mask; dims=2) .== 0)
mask = get_mask(action.spin_reset, ts)
# Modify spin state: reset and replace by initial value
spin_state_matrix .*= mask
spin_state_matrix .+= replace_by .* (1 .- mask)
spin_state_matrix .*= (1 .- mask)
spin_state_matrix .+= replace_by .* mask
return nothing
end

Expand All @@ -56,20 +54,31 @@ function outflow_spin_reset!(
idx = KomaMRIBase.get_indexing_range(spin_span)
M = @view(M[idx])
# Obtain mask
itp = KomaMRIBase.interpolate(action.spin_reset, KomaMRIBase.Gridded(KomaMRIBase.Constant{KomaMRIBase.Previous}()), Val(size(action.spin_reset, 1)), t)
mask = KomaMRIBase.resample(itp, ts)
mask .= (cumsum(mask; dims=2) .== 0)
mask = get_mask(action.spin_reset, ts)
mask = @view(mask[:, end])
# Modify spin state: reset and replace by initial value
M.xy .*= mask
M.z .*= mask
M.xy .+= 0 .* (1 .- mask)
M.z .+= replace_by .* (1 .- mask)
M.xy .*= (1 .- mask)
M.z .*= (1 .- mask)
M.xy .+= 0 .* mask
M.z .+= replace_by .* mask
return nothing
end

init_time(t, seq_t, add_t0) = t
init_time(t, seq_t::AbstractArray, add_t0) = begin
function init_time(t, seq_t::AbstractArray, add_t0)
t1 = @view(seq_t[1])
return add_t0 ? [t1 (t1 .+ t)] : t1 .+ t
end
function init_time(t, seq_t, add_t0)
return t
end

function get_mask(spin_reset, t::Real)
itp = KomaMRIBase.interpolate(spin_reset, KomaMRIBase.Gridded(KomaMRIBase.Constant{KomaMRIBase.Previous}()), Val(size(spin_reset, 1)), t)
return KomaMRIBase.resample(itp, t)
end
function get_mask(spin_reset, t::AbstractArray)
itp = KomaMRIBase.interpolate(spin_reset, KomaMRIBase.Gridded(KomaMRIBase.Constant{KomaMRIBase.Previous}()), Val(size(spin_reset, 1)), t)
mask = KomaMRIBase.resample(itp, t)
mask .= (cumsum(mask; dims=2) .== 1)
return mask
end
6 changes: 3 additions & 3 deletions KomaMRICore/src/simulation/SimMethods/Bloch/BlochCPU.jl
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ function run_spin_precession!(
) where {T<:Real}
#Simulation
#Motion
x, y, z = get_spin_coords(p.motion, p.x, p.y, p.z, seq.t[1,:]')
x, y, z = get_spin_coords(p.motion, p.x, p.y, p.z, seq.t[1])

#Initialize arrays
Bz_old = prealloc.Bz_old
Expand All @@ -76,7 +76,7 @@ function run_spin_precession!(

t_seq = zero(T) # Time
for seq_idx=2:length(seq.t)
x, y, z = get_spin_coords(p.motion, p.x, p.y, p.z, seq.t[seq_idx,:]')
x, y, z = get_spin_coords(p.motion, p.x, p.y, p.z, seq.t[seq_idx])
t_seq += seq.Δt[seq_idx-1]

#Effective Field
Expand All @@ -90,7 +90,7 @@ function run_spin_precession!(
@. Mxy = exp(-t_seq / p.T2) * M.xy * cis(ϕ)

#Reset Spin-State (Magnetization). Only for FlowPath
outflow_spin_reset!(Mxy, seq.t[seq_idx,:]', p.motion)
outflow_spin_reset!(Mxy, seq.t[seq_idx], p.motion)

sig[ADC_idx] = sum(Mxy)
ADC_idx += 1
Expand Down
Loading