@@ -2,7 +2,6 @@ using SciMLBase
2
2
3
3
export IndependentlyLinearizedSolution
4
4
5
-
6
5
"""
7
6
CachePool(T, alloc; thread_safe = true)
8
7
@@ -40,11 +39,11 @@ mutable struct CachePool{T, THREAD_SAFE}
40
39
num_acquired:: Int
41
40
42
41
function CachePool (T, alloc:: F ; thread_safe:: Bool = true ) where {F}
43
- return new {T,Val{thread_safe}} (T[], alloc, ReentrantLock (), 0 , 0 )
42
+ return new {T, Val{thread_safe}} (T[], alloc, ReentrantLock (), 0 , 0 )
44
43
end
45
44
end
46
- const ThreadSafeCachePool{T} = CachePool{T,Val{true }}
47
- const ThreadUnsafeCachePool{T} = CachePool{T,Val{false }}
45
+ const ThreadSafeCachePool{T} = CachePool{T, Val{true }}
46
+ const ThreadUnsafeCachePool{T} = CachePool{T, Val{false }}
48
47
49
48
"""
50
49
acquire!(cache::CachePool)
78
77
# Thread-safe versions just sub out to the other methods, using `_dummy` to force correct dispatch
79
78
acquire! (cache:: ThreadSafeCachePool ) = @lock cache. lock acquire! (cache, nothing )
80
79
release! (cache:: ThreadSafeCachePool , val) = @lock cache. lock release! (cache, val, nothing )
81
- is_fully_released (cache:: ThreadSafeCachePool ) = @lock cache. lock is_fully_released (cache, nothing )
80
+ function is_fully_released (cache:: ThreadSafeCachePool )
81
+ @lock cache. lock is_fully_released (cache, nothing )
82
+ end
82
83
83
84
macro with_cache (cache, name, body)
84
85
return quote
@@ -91,20 +92,20 @@ macro with_cache(cache, name, body)
91
92
end
92
93
end
93
94
94
-
95
- struct IndependentlyLinearizedSolutionChunksCache{T,S}
95
+ struct IndependentlyLinearizedSolutionChunksCache{T, S}
96
96
t_chunks:: ThreadUnsafeCachePool{Vector{T}}
97
97
u_chunks:: ThreadUnsafeCachePool{Matrix{S}}
98
98
time_masks:: ThreadUnsafeCachePool{BitMatrix}
99
99
100
- function IndependentlyLinearizedSolutionChunksCache {T,S} (num_us:: Int , num_derivatives:: Int , chunk_size:: Int ) where {T,S}
100
+ function IndependentlyLinearizedSolutionChunksCache {T, S} (
101
+ num_us:: Int , num_derivatives:: Int , chunk_size:: Int ) where {T, S}
101
102
t_chunks_alloc = () -> Vector {T} (undef, chunk_size)
102
- u_chunks_alloc = () -> Matrix {S} (undef, num_derivatives+ 1 , chunk_size)
103
+ u_chunks_alloc = () -> Matrix {S} (undef, num_derivatives + 1 , chunk_size)
103
104
time_masks_alloc = () -> BitMatrix (undef, num_us, chunk_size)
104
105
return new (
105
- CachePool (Vector{T}, t_chunks_alloc; thread_safe= false ),
106
- CachePool (Matrix{S}, u_chunks_alloc; thread_safe= false ),
107
- CachePool (BitMatrix, time_masks_alloc; thread_safe= false ),
106
+ CachePool (Vector{T}, t_chunks_alloc; thread_safe = false ),
107
+ CachePool (Matrix{S}, u_chunks_alloc; thread_safe = false ),
108
+ CachePool (BitMatrix, time_masks_alloc; thread_safe = false )
108
109
)
109
110
end
110
111
end
@@ -130,16 +131,19 @@ mutable struct IndependentlyLinearizedSolutionChunks{T, S, N}
130
131
131
132
cache:: IndependentlyLinearizedSolutionChunksCache
132
133
133
- function IndependentlyLinearizedSolutionChunks {T, S} (num_us:: Int , num_derivatives:: Int = 0 ,
134
+ function IndependentlyLinearizedSolutionChunks {T, S} (
135
+ num_us:: Int , num_derivatives:: Int = 0 ,
134
136
chunk_size:: Int = 512 ,
135
- cache:: IndependentlyLinearizedSolutionChunksCache = IndependentlyLinearizedSolutionChunksCache {T,S} (num_us, num_derivatives, chunk_size)) where {T, S}
137
+ cache:: IndependentlyLinearizedSolutionChunksCache = IndependentlyLinearizedSolutionChunksCache{
138
+ T, S}(num_us, num_derivatives, chunk_size)) where {T, S}
136
139
t_chunks = [acquire! (cache. t_chunks)]
137
140
u_chunks = [[acquire! (cache. u_chunks)] for _ in 1 : num_us]
138
141
time_masks = [acquire! (cache. time_masks)]
139
142
last_chunks = [u_chunks[u_idx][1 ] for u_idx in 1 : num_us]
140
143
u_offsets = [1 for _ in 1 : num_us]
141
144
t_offset = 1
142
- return new {T,S,num_derivatives} (t_chunks, u_chunks, time_masks, last_chunks, u_offsets, t_offset, cache)
145
+ return new {T, S, num_derivatives} (
146
+ t_chunks, u_chunks, time_masks, last_chunks, u_offsets, t_offset, cache)
143
147
end
144
148
end
145
149
@@ -158,8 +162,7 @@ function num_us(ilsc::IndependentlyLinearizedSolutionChunks)
158
162
end
159
163
return length (ilsc. u_chunks)
160
164
end
161
- num_derivatives (ilsc:: IndependentlyLinearizedSolutionChunks{T,S,N} ) where {T,S,N} = N
162
-
165
+ num_derivatives (ilsc:: IndependentlyLinearizedSolutionChunks{T, S, N} ) where {T, S, N} = N
163
166
164
167
function Base. isempty (ilsc:: IndependentlyLinearizedSolutionChunks )
165
168
return length (ilsc. t_chunks) == 1 && ilsc. t_offset == 1
@@ -187,7 +190,7 @@ function get_chunks(ilsc::IndependentlyLinearizedSolutionChunks{T, S}) where {T,
187
190
return (
188
191
ilsc. t_chunks[end ],
189
192
ilsc. time_masks[end ],
190
- ilsc. last_chunks,
193
+ ilsc. last_chunks
191
194
)
192
195
end
193
196
@@ -283,37 +286,43 @@ mutable struct IndependentlyLinearizedSolution{T, S, N}
283
286
time_mask:: BitMatrix
284
287
285
288
# Temporary object used during construction, will be set to `nothing` at the end.
286
- ilsc:: Union{Nothing,IndependentlyLinearizedSolutionChunks{T,S,N}}
287
- ilsc_cache_pool:: Union{Nothing,ThreadSafeCachePool{IndependentlyLinearizedSolutionChunksCache{T,S}}}
289
+ ilsc:: Union{Nothing, IndependentlyLinearizedSolutionChunks{T, S, N}}
290
+ ilsc_cache_pool:: Union {
291
+ Nothing, ThreadSafeCachePool{IndependentlyLinearizedSolutionChunksCache{T, S}}}
288
292
end
289
293
# Helper function to create an ILS wrapped around an in-progress ILSC
290
- function IndependentlyLinearizedSolution (ilsc:: IndependentlyLinearizedSolutionChunks{T,S,N} , cache_pool = nothing ) where {T,S,N}
291
- return IndependentlyLinearizedSolution {T,S,N} (
294
+ function IndependentlyLinearizedSolution (
295
+ ilsc:: IndependentlyLinearizedSolutionChunks{T, S, N} ,
296
+ cache_pool = nothing ) where {T, S, N}
297
+ return IndependentlyLinearizedSolution {T, S, N} (
292
298
T[],
293
299
Matrix{S}[],
294
- BitMatrix (undef, 0 ,0 ),
300
+ BitMatrix (undef, 0 , 0 ),
295
301
ilsc,
296
- cache_pool,
302
+ cache_pool
297
303
)
298
304
end
299
305
# Automatically create an ILS wrapped around an ILSC from a `prob`
300
- function IndependentlyLinearizedSolution (prob:: SciMLBase.AbstractDEProblem , num_derivatives = 0 ;
301
- cache_pool = nothing ,
302
- chunk_size:: Int = 512 )
306
+ function IndependentlyLinearizedSolution (
307
+ prob:: SciMLBase.AbstractDEProblem , num_derivatives = 0 ;
308
+ cache_pool = nothing ,
309
+ chunk_size:: Int = 512 )
303
310
T = eltype (prob. tspan)
304
311
S = eltype (prob. u0)
305
312
U = isnothing (prob. u0) ? Float64 : eltype (prob. u0)
306
313
num_us = isnothing (prob. u0) ? 0 : length (prob. u0)
307
314
if cache_pool === nothing
308
- cache = IndependentlyLinearizedSolutionChunksCache {T,S} (num_us, num_derivatives, chunk_size)
315
+ cache = IndependentlyLinearizedSolutionChunksCache {T, S} (
316
+ num_us, num_derivatives, chunk_size)
309
317
else
310
318
cache = acquire! (cache_pool)
311
319
end
312
- chunks = IndependentlyLinearizedSolutionChunks {T,U} (num_us, num_derivatives, chunk_size, cache)
320
+ chunks = IndependentlyLinearizedSolutionChunks {T, U} (
321
+ num_us, num_derivatives, chunk_size, cache)
313
322
return IndependentlyLinearizedSolution (chunks, cache_pool)
314
323
end
315
324
316
- num_derivatives (:: IndependentlyLinearizedSolution{T,S, N} ) where {T,S, N} = N
325
+ num_derivatives (:: IndependentlyLinearizedSolution{T, S, N} ) where {T, S, N} = N
317
326
num_us (ils:: IndependentlyLinearizedSolution ) = length (ils. us)
318
327
Base. size (ils:: IndependentlyLinearizedSolution ) = size (ils. time_mask)
319
328
Base. length (ils:: IndependentlyLinearizedSolution ) = length (ils. ts)
@@ -344,33 +353,34 @@ function finish!(ils::IndependentlyLinearizedSolution{T, S}, return_code) where
344
353
chunk_len (chunk) = size (chunk, ndims (chunk))
345
354
function chunks_len (chunks:: Vector , offset)
346
355
len = 0
347
- for chunk_idx in 1 : length (chunks)- 1
356
+ for chunk_idx in 1 : ( length (chunks) - 1 )
348
357
len += chunk_len (chunks[chunk_idx])
349
358
end
350
359
return len + offset - 1
351
360
end
352
361
353
- function copy_chunk! (out:: Vector , in:: Vector , out_offset:: Int , len= chunk_len (in))
362
+ function copy_chunk! (out:: Vector , in:: Vector , out_offset:: Int , len = chunk_len (in))
354
363
for idx in 1 : len
355
- out[idx+ out_offset] = in[idx]
364
+ out[idx + out_offset] = in[idx]
356
365
end
357
366
end
358
- function copy_chunk! (out:: AbstractMatrix , in:: AbstractMatrix , out_offset:: Int , len= chunk_len (in))
367
+ function copy_chunk! (out:: AbstractMatrix , in:: AbstractMatrix ,
368
+ out_offset:: Int , len = chunk_len (in))
359
369
for zdx in 1 : size (in, 1 )
360
370
for idx in 1 : len
361
- out[zdx, idx+ out_offset] = in[zdx, idx]
371
+ out[zdx, idx + out_offset] = in[zdx, idx]
362
372
end
363
373
end
364
374
end
365
375
366
376
function collapse_chunks! (out, chunks, offset:: Int )
367
377
write_offset = 0
368
- for chunk_idx in 1 : (length (chunks)- 1 )
378
+ for chunk_idx in 1 : (length (chunks) - 1 )
369
379
chunk = chunks[chunk_idx]
370
380
copy_chunk! (out, chunk, write_offset)
371
381
write_offset += chunk_len (chunk)
372
382
end
373
- copy_chunk! (out, chunks[end ], write_offset, offset- 1 )
383
+ copy_chunk! (out, chunks[end ], write_offset, offset - 1 )
374
384
end
375
385
376
386
# Collapse t_chunks
@@ -380,11 +390,13 @@ function finish!(ils::IndependentlyLinearizedSolution{T, S}, return_code) where
380
390
# Collapse u_chunks
381
391
us = Vector {Matrix{S}} (undef, length (ilsc. u_chunks))
382
392
for u_idx in 1 : length (ilsc. u_chunks)
383
- 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]))
393
+ us[u_idx] = Matrix {S} (undef, size (ilsc. u_chunks[u_idx][1 ], 1 ),
394
+ chunks_len (ilsc. u_chunks[u_idx], ilsc. u_offsets[u_idx]))
384
395
collapse_chunks! (us[u_idx], ilsc. u_chunks[u_idx], ilsc. u_offsets[u_idx])
385
396
end
386
397
387
- time_mask = BitMatrix (undef, size (ilsc. time_masks[1 ], 1 ), chunks_len (ilsc. time_masks, ilsc. t_offset))
398
+ time_mask = BitMatrix (
399
+ undef, size (ilsc. time_masks[1 ], 1 ), chunks_len (ilsc. time_masks, ilsc. t_offset))
388
400
collapse_chunks! (time_mask, ilsc. time_masks, ilsc. t_offset)
389
401
end
390
402
0 commit comments