Skip to content

Commit

Permalink
Run formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
staticfloat committed Feb 21, 2024
1 parent 58d4681 commit bd922ba
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 89 deletions.
1 change: 0 additions & 1 deletion ext/DiffEqCallbacksSundialsExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ module DiffEqCallbacksSundialsExt
using Sundials: NVector, IDA
import DiffEqCallbacks: solver_state_alloc, solver_state_type


# Allocator; `U` is typically something like `Vector{Float64}`
solver_state_alloc(solver::IDA, U::DataType, num_us::Int) = () -> NVector(U(undef, num_us))

Check warning on line 7 in ext/DiffEqCallbacksSundialsExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/DiffEqCallbacksSundialsExt.jl#L7

Added line #L7 was not covered by tests

Expand Down
2 changes: 1 addition & 1 deletion src/domain.jl
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ function affect!(integrator, f::AbstractDomainAffect{T, S, uType}) where {T, S,
if dtcache == dt
if integrator.opts.verbose
@warn("Could not restrict values to domain. Iteration was canceled since ",
"proposed time step dt = ", dt," could not be reduced.")
"proposed time step dt = ", dt, " could not be reduced.")
end
break
end
Expand Down
90 changes: 51 additions & 39 deletions src/independentlylinearizedutils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ using SciMLBase

export IndependentlyLinearizedSolution


"""
CachePool(T, alloc; thread_safe = true)
Expand Down Expand Up @@ -40,11 +39,11 @@ mutable struct CachePool{T, THREAD_SAFE}
num_acquired::Int

function CachePool(T, alloc::F; thread_safe::Bool = true) where {F}
return new{T,Val{thread_safe}}(T[], alloc, ReentrantLock(), 0, 0)
return new{T, Val{thread_safe}}(T[], alloc, ReentrantLock(), 0, 0)

Check warning on line 42 in src/independentlylinearizedutils.jl

View check run for this annotation

Codecov / codecov/patch

src/independentlylinearizedutils.jl#L41-L42

Added lines #L41 - L42 were not covered by tests
end
end
const ThreadSafeCachePool{T} = CachePool{T,Val{true}}
const ThreadUnsafeCachePool{T} = CachePool{T,Val{false}}
const ThreadSafeCachePool{T} = CachePool{T, Val{true}}
const ThreadUnsafeCachePool{T} = CachePool{T, Val{false}}

"""
acquire!(cache::CachePool)
Expand Down Expand Up @@ -78,7 +77,9 @@ end
# Thread-safe versions just sub out to the other methods, using `_dummy` to force correct dispatch
acquire!(cache::ThreadSafeCachePool) = @lock cache.lock acquire!(cache, nothing)
release!(cache::ThreadSafeCachePool, val) = @lock cache.lock release!(cache, val, nothing)
is_fully_released(cache::ThreadSafeCachePool) = @lock cache.lock is_fully_released(cache, nothing)
function is_fully_released(cache::ThreadSafeCachePool)
@lock cache.lock is_fully_released(cache, nothing)

Check warning on line 81 in src/independentlylinearizedutils.jl

View check run for this annotation

Codecov / codecov/patch

src/independentlylinearizedutils.jl#L78-L81

Added lines #L78 - L81 were not covered by tests
end

macro with_cache(cache, name, body)
return quote
Expand All @@ -91,20 +92,20 @@ macro with_cache(cache, name, body)
end
end


struct IndependentlyLinearizedSolutionChunksCache{T,S}
struct IndependentlyLinearizedSolutionChunksCache{T, S}
t_chunks::ThreadUnsafeCachePool{Vector{T}}
u_chunks::ThreadUnsafeCachePool{Matrix{S}}
time_masks::ThreadUnsafeCachePool{BitMatrix}

function IndependentlyLinearizedSolutionChunksCache{T,S}(num_us::Int, num_derivatives::Int, chunk_size::Int) where {T,S}
function IndependentlyLinearizedSolutionChunksCache{T, S}(

Check warning on line 100 in src/independentlylinearizedutils.jl

View check run for this annotation

Codecov / codecov/patch

src/independentlylinearizedutils.jl#L100

Added line #L100 was not covered by tests
num_us::Int, num_derivatives::Int, chunk_size::Int) where {T, S}
t_chunks_alloc = () -> Vector{T}(undef, chunk_size)
u_chunks_alloc = () -> Matrix{S}(undef, num_derivatives+1, chunk_size)
u_chunks_alloc = () -> Matrix{S}(undef, num_derivatives + 1, chunk_size)
time_masks_alloc = () -> BitMatrix(undef, num_us, chunk_size)
return new(

Check warning on line 105 in src/independentlylinearizedutils.jl

View check run for this annotation

Codecov / codecov/patch

src/independentlylinearizedutils.jl#L102-L105

Added lines #L102 - L105 were not covered by tests
CachePool(Vector{T}, t_chunks_alloc; thread_safe=false),
CachePool(Matrix{S}, u_chunks_alloc; thread_safe=false),
CachePool(BitMatrix, time_masks_alloc; thread_safe=false),
CachePool(Vector{T}, t_chunks_alloc; thread_safe = false),
CachePool(Matrix{S}, u_chunks_alloc; thread_safe = false),
CachePool(BitMatrix, time_masks_alloc; thread_safe = false)
)
end
end
Expand All @@ -130,16 +131,19 @@ mutable struct IndependentlyLinearizedSolutionChunks{T, S, N}

cache::IndependentlyLinearizedSolutionChunksCache

function IndependentlyLinearizedSolutionChunks{T, S}(num_us::Int, num_derivatives::Int = 0,
function IndependentlyLinearizedSolutionChunks{T, S}(
num_us::Int, num_derivatives::Int = 0,
chunk_size::Int = 512,
cache::IndependentlyLinearizedSolutionChunksCache = IndependentlyLinearizedSolutionChunksCache{T,S}(num_us, num_derivatives, chunk_size)) where {T, S}
cache::IndependentlyLinearizedSolutionChunksCache = IndependentlyLinearizedSolutionChunksCache{
T, S}(num_us, num_derivatives, chunk_size)) where {T, S}
t_chunks = [acquire!(cache.t_chunks)]
u_chunks = [[acquire!(cache.u_chunks)] for _ in 1:num_us]
time_masks = [acquire!(cache.time_masks)]
last_chunks = [u_chunks[u_idx][1] for u_idx in 1:num_us]
u_offsets = [1 for _ in 1:num_us]
t_offset = 1
return new{T,S,num_derivatives}(t_chunks, u_chunks, time_masks, last_chunks, u_offsets, t_offset, cache)
return new{T, S, num_derivatives}(

Check warning on line 145 in src/independentlylinearizedutils.jl

View check run for this annotation

Codecov / codecov/patch

src/independentlylinearizedutils.jl#L139-L145

Added lines #L139 - L145 were not covered by tests
t_chunks, u_chunks, time_masks, last_chunks, u_offsets, t_offset, cache)
end
end

Expand All @@ -158,8 +162,7 @@ function num_us(ilsc::IndependentlyLinearizedSolutionChunks)
end
return length(ilsc.u_chunks)
end
num_derivatives(ilsc::IndependentlyLinearizedSolutionChunks{T,S,N}) where {T,S,N} = N

num_derivatives(ilsc::IndependentlyLinearizedSolutionChunks{T, S, N}) where {T, S, N} = N

Check warning on line 165 in src/independentlylinearizedutils.jl

View check run for this annotation

Codecov / codecov/patch

src/independentlylinearizedutils.jl#L165

Added line #L165 was not covered by tests

function Base.isempty(ilsc::IndependentlyLinearizedSolutionChunks)
return length(ilsc.t_chunks) == 1 && ilsc.t_offset == 1
Expand Down Expand Up @@ -187,7 +190,7 @@ function get_chunks(ilsc::IndependentlyLinearizedSolutionChunks{T, S}) where {T,
return (
ilsc.t_chunks[end],
ilsc.time_masks[end],
ilsc.last_chunks,
ilsc.last_chunks
)
end

Expand Down Expand Up @@ -283,37 +286,43 @@ mutable struct IndependentlyLinearizedSolution{T, S, N}
time_mask::BitMatrix

# Temporary object used during construction, will be set to `nothing` at the end.
ilsc::Union{Nothing,IndependentlyLinearizedSolutionChunks{T,S,N}}
ilsc_cache_pool::Union{Nothing,ThreadSafeCachePool{IndependentlyLinearizedSolutionChunksCache{T,S}}}
ilsc::Union{Nothing, IndependentlyLinearizedSolutionChunks{T, S, N}}
ilsc_cache_pool::Union{
Nothing, ThreadSafeCachePool{IndependentlyLinearizedSolutionChunksCache{T, S}}}
end
# Helper function to create an ILS wrapped around an in-progress ILSC
function IndependentlyLinearizedSolution(ilsc::IndependentlyLinearizedSolutionChunks{T,S,N}, cache_pool = nothing) where {T,S,N}
return IndependentlyLinearizedSolution{T,S,N}(
function IndependentlyLinearizedSolution(

Check warning on line 294 in src/independentlylinearizedutils.jl

View check run for this annotation

Codecov / codecov/patch

src/independentlylinearizedutils.jl#L294

Added line #L294 was not covered by tests
ilsc::IndependentlyLinearizedSolutionChunks{T, S, N},
cache_pool = nothing) where {T, S, N}
return IndependentlyLinearizedSolution{T, S, N}(

Check warning on line 297 in src/independentlylinearizedutils.jl

View check run for this annotation

Codecov / codecov/patch

src/independentlylinearizedutils.jl#L297

Added line #L297 was not covered by tests
T[],
Matrix{S}[],
BitMatrix(undef, 0,0),
BitMatrix(undef, 0, 0),
ilsc,
cache_pool,
cache_pool
)
end
# Automatically create an ILS wrapped around an ILSC from a `prob`
function IndependentlyLinearizedSolution(prob::SciMLBase.AbstractDEProblem, num_derivatives = 0;
cache_pool = nothing,
chunk_size::Int = 512)
function IndependentlyLinearizedSolution(
prob::SciMLBase.AbstractDEProblem, num_derivatives = 0;
cache_pool = nothing,
chunk_size::Int = 512)
T = eltype(prob.tspan)
S = eltype(prob.u0)

Check warning on line 311 in src/independentlylinearizedutils.jl

View check run for this annotation

Codecov / codecov/patch

src/independentlylinearizedutils.jl#L311

Added line #L311 was not covered by tests
U = isnothing(prob.u0) ? Float64 : eltype(prob.u0)
num_us = isnothing(prob.u0) ? 0 : length(prob.u0)
if cache_pool === nothing
cache = IndependentlyLinearizedSolutionChunksCache{T,S}(num_us, num_derivatives, chunk_size)
cache = IndependentlyLinearizedSolutionChunksCache{T, S}(

Check warning on line 315 in src/independentlylinearizedutils.jl

View check run for this annotation

Codecov / codecov/patch

src/independentlylinearizedutils.jl#L313-L315

Added lines #L313 - L315 were not covered by tests
num_us, num_derivatives, chunk_size)
else
cache = acquire!(cache_pool)

Check warning on line 318 in src/independentlylinearizedutils.jl

View check run for this annotation

Codecov / codecov/patch

src/independentlylinearizedutils.jl#L318

Added line #L318 was not covered by tests
end
chunks = IndependentlyLinearizedSolutionChunks{T,U}(num_us, num_derivatives, chunk_size, cache)
chunks = IndependentlyLinearizedSolutionChunks{T, U}(

Check warning on line 320 in src/independentlylinearizedutils.jl

View check run for this annotation

Codecov / codecov/patch

src/independentlylinearizedutils.jl#L320

Added line #L320 was not covered by tests
num_us, num_derivatives, chunk_size, cache)
return IndependentlyLinearizedSolution(chunks, cache_pool)

Check warning on line 322 in src/independentlylinearizedutils.jl

View check run for this annotation

Codecov / codecov/patch

src/independentlylinearizedutils.jl#L322

Added line #L322 was not covered by tests
end

num_derivatives(::IndependentlyLinearizedSolution{T,S,N}) where {T,S,N} = N
num_derivatives(::IndependentlyLinearizedSolution{T, S, N}) where {T, S, N} = N

Check warning on line 325 in src/independentlylinearizedutils.jl

View check run for this annotation

Codecov / codecov/patch

src/independentlylinearizedutils.jl#L325

Added line #L325 was not covered by tests
num_us(ils::IndependentlyLinearizedSolution) = length(ils.us)
Base.size(ils::IndependentlyLinearizedSolution) = size(ils.time_mask)
Base.length(ils::IndependentlyLinearizedSolution) = length(ils.ts)
Expand Down Expand Up @@ -344,33 +353,34 @@ function finish!(ils::IndependentlyLinearizedSolution{T, S}, return_code) where
chunk_len(chunk) = size(chunk, ndims(chunk))
function chunks_len(chunks::Vector, offset)
len = 0
for chunk_idx in 1:length(chunks)-1
for chunk_idx in 1:(length(chunks) - 1)
len += chunk_len(chunks[chunk_idx])
end
return len + offset - 1

Check warning on line 359 in src/independentlylinearizedutils.jl

View check run for this annotation

Codecov / codecov/patch

src/independentlylinearizedutils.jl#L353-L359

Added lines #L353 - L359 were not covered by tests
end

function copy_chunk!(out::Vector, in::Vector, out_offset::Int, len=chunk_len(in))
function copy_chunk!(out::Vector, in::Vector, out_offset::Int, len = chunk_len(in))
for idx in 1:len
out[idx+out_offset] = in[idx]
out[idx + out_offset] = in[idx]
end

Check warning on line 365 in src/independentlylinearizedutils.jl

View check run for this annotation

Codecov / codecov/patch

src/independentlylinearizedutils.jl#L362-L365

Added lines #L362 - L365 were not covered by tests
end
function copy_chunk!(out::AbstractMatrix, in::AbstractMatrix, out_offset::Int, len=chunk_len(in))
function copy_chunk!(out::AbstractMatrix, in::AbstractMatrix,

Check warning on line 367 in src/independentlylinearizedutils.jl

View check run for this annotation

Codecov / codecov/patch

src/independentlylinearizedutils.jl#L367

Added line #L367 was not covered by tests
out_offset::Int, len = chunk_len(in))
for zdx in 1:size(in, 1)
for idx in 1:len
out[zdx, idx+out_offset] = in[zdx, idx]
out[zdx, idx + out_offset] = in[zdx, idx]
end
end

Check warning on line 373 in src/independentlylinearizedutils.jl

View check run for this annotation

Codecov / codecov/patch

src/independentlylinearizedutils.jl#L369-L373

Added lines #L369 - L373 were not covered by tests
end

function collapse_chunks!(out, chunks, offset::Int)
write_offset = 0
for chunk_idx in 1:(length(chunks)-1)
for chunk_idx in 1:(length(chunks) - 1)
chunk = chunks[chunk_idx]
copy_chunk!(out, chunk, write_offset)
write_offset += chunk_len(chunk)
end
copy_chunk!(out, chunks[end], write_offset, offset-1)
copy_chunk!(out, chunks[end], write_offset, offset - 1)

Check warning on line 383 in src/independentlylinearizedutils.jl

View check run for this annotation

Codecov / codecov/patch

src/independentlylinearizedutils.jl#L376-L383

Added lines #L376 - L383 were not covered by tests
end

# Collapse t_chunks
Expand All @@ -380,11 +390,13 @@ function finish!(ils::IndependentlyLinearizedSolution{T, S}, return_code) where
# Collapse u_chunks
us = Vector{Matrix{S}}(undef, length(ilsc.u_chunks))
for u_idx in 1:length(ilsc.u_chunks)
us[u_idx] = Matrix{S}(undef, size(ilsc.u_chunks[u_idx][1],1), chunks_len(ilsc.u_chunks[u_idx], ilsc.u_offsets[u_idx]))
us[u_idx] = Matrix{S}(undef, size(ilsc.u_chunks[u_idx][1], 1),

Check warning on line 393 in src/independentlylinearizedutils.jl

View check run for this annotation

Codecov / codecov/patch

src/independentlylinearizedutils.jl#L391-L393

Added lines #L391 - L393 were not covered by tests
chunks_len(ilsc.u_chunks[u_idx], ilsc.u_offsets[u_idx]))
collapse_chunks!(us[u_idx], ilsc.u_chunks[u_idx], ilsc.u_offsets[u_idx])
end

Check warning on line 396 in src/independentlylinearizedutils.jl

View check run for this annotation

Codecov / codecov/patch

src/independentlylinearizedutils.jl#L395-L396

Added lines #L395 - L396 were not covered by tests

time_mask = BitMatrix(undef, size(ilsc.time_masks[1], 1), chunks_len(ilsc.time_masks, ilsc.t_offset))
time_mask = BitMatrix(

Check warning on line 398 in src/independentlylinearizedutils.jl

View check run for this annotation

Codecov / codecov/patch

src/independentlylinearizedutils.jl#L398

Added line #L398 was not covered by tests
undef, size(ilsc.time_masks[1], 1), chunks_len(ilsc.time_masks, ilsc.t_offset))
collapse_chunks!(time_mask, ilsc.time_masks, ilsc.t_offset)

Check warning on line 400 in src/independentlylinearizedutils.jl

View check run for this annotation

Codecov / codecov/patch

src/independentlylinearizedutils.jl#L400

Added line #L400 was not covered by tests
end

Expand Down
Loading

0 comments on commit bd922ba

Please sign in to comment.