Skip to content

Commit eab903a

Browse files
Merge pull request #27 from SciML/format
format SciML Style
2 parents 1dcdaaa + d4fd27a commit eab903a

File tree

9 files changed

+258
-197
lines changed

9 files changed

+258
-197
lines changed

.JuliaFormatter.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
style = "sciml"

.github/workflows/FormatCheck.yml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
name: format-check
2+
3+
on:
4+
push:
5+
branches:
6+
- 'master'
7+
- 'release-'
8+
tags: '*'
9+
pull_request:
10+
11+
jobs:
12+
build:
13+
runs-on: ${{ matrix.os }}
14+
strategy:
15+
matrix:
16+
julia-version: [1]
17+
julia-arch: [x86]
18+
os: [ubuntu-latest]
19+
steps:
20+
- uses: julia-actions/setup-julia@latest
21+
with:
22+
version: ${{ matrix.julia-version }}
23+
24+
- uses: actions/checkout@v1
25+
- name: Install JuliaFormatter and format
26+
# This will use the latest version by default but you can set the version like so:
27+
#
28+
# julia -e 'using Pkg; Pkg.add(PackageSpec(name="JuliaFormatter", version="0.13.0"))'
29+
run: |
30+
julia -e 'using Pkg; Pkg.add(PackageSpec(name="JuliaFormatter"))'
31+
julia -e 'using JuliaFormatter; format(".", verbose=true)'
32+
- name: Format check
33+
run: |
34+
julia -e '
35+
out = Cmd(`git diff --name-only`) |> read |> String
36+
if out == ""
37+
exit(0)
38+
else
39+
@error "Some files have not been formatted !!!"
40+
write(stdout, out)
41+
exit(1)
42+
end'

src/PreallocationTools.jl

Lines changed: 106 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -1,100 +1,106 @@
1-
module PreallocationTools
2-
3-
using ForwardDiff, ArrayInterfaceCore, LabelledArrays, Adapt
4-
5-
struct DiffCache{T<:AbstractArray, S<:AbstractArray}
6-
du::T
7-
dual_du::S
8-
end
9-
10-
function DiffCache(u::AbstractArray{T}, siz, chunk_sizes) where {T}
11-
x = adapt(ArrayInterfaceCore.parameterless_type(u), zeros(T, prod(chunk_sizes .+ 1)*prod(siz)))
12-
DiffCache(u, x)
13-
end
14-
15-
"""
16-
17-
`dualcache(u::AbstractArray, N::Int = ForwardDiff.pickchunksize(length(u)); levels::Int = 1)`
18-
`dualcache(u::AbstractArray; N::AbstractArray{<:Int})`
19-
20-
Builds a `DualCache` object that stores both a version of the cache for `u`
21-
and for the `Dual` version of `u`, allowing use of pre-cached vectors with
22-
forward-mode automatic differentiation. Supports nested AD via keyword `levels`
23-
or specifying an array of chunk_sizes.
24-
25-
"""
26-
dualcache(u::AbstractArray, N::Int=ForwardDiff.pickchunksize(length(u)); levels::Int = 1) = DiffCache(u, size(u), N*ones(Int, levels))
27-
dualcache(u::AbstractArray, N::AbstractArray{<:Int}) = DiffCache(u, size(u), N)
28-
dualcache(u::AbstractArray, ::Type{Val{N}}; levels::Int = 1) where N = dualcache(u,N;levels)
29-
dualcache(u::AbstractArray, ::Val{N}; levels::Int = 1) where N = dualcache(u,N;levels)
30-
31-
"""
32-
33-
`get_tmp(dc::DiffCache, u)`
34-
35-
Returns the `Dual` or normal cache array stored in `dc` based on the type of `u`.
36-
37-
"""
38-
function get_tmp(dc::DiffCache, u::T) where T<:ForwardDiff.Dual
39-
nelem = div(sizeof(T), sizeof(eltype(dc.dual_du)))*length(dc.du)
40-
if nelem > length(dc.dual_du)
41-
enlargedualcache!(dc, nelem)
42-
end
43-
ArrayInterfaceCore.restructure(dc.du, reinterpret(T, view(dc.dual_du, 1:nelem)))
44-
end
45-
46-
function get_tmp(dc::DiffCache, u::AbstractArray{T}) where T<:ForwardDiff.Dual
47-
nelem = div(sizeof(T), sizeof(eltype(dc.dual_du)))*length(dc.du)
48-
if nelem > length(dc.dual_du)
49-
enlargedualcache!(dc, nelem)
50-
end
51-
ArrayInterfaceCore.restructure(dc.du, reinterpret(T, view(dc.dual_du, 1:nelem)))
52-
end
53-
54-
function get_tmp(dc::DiffCache, u::LabelledArrays.LArray{T,N,D,Syms}) where {T,N,D,Syms}
55-
nelem = div(sizeof(T), sizeof(eltype(dc.dual_du)))*length(dc.du)
56-
if nelem > length(dc.dual_du)
57-
enlargedualcache!(dc, nelem)
58-
end
59-
_x = ArrayInterfaceCore.restructure(dc.du, reinterpret(T, view(dc.dual_du, 1:nelem)))
60-
LabelledArrays.LArray{T,N,D,Syms}(_x)
61-
end
62-
63-
get_tmp(dc::DiffCache, u::Number) = dc.du
64-
get_tmp(dc::DiffCache, u::AbstractArray) = dc.du
65-
66-
function enlargedualcache!(dc, nelem) #warning comes only once per dualcache.
67-
chunksize = div(nelem, length(dc.du)) - 1
68-
@warn "The supplied dualcache was too small and was enlarged. This incurrs allocations
69-
on the first call to get_tmp. If few calls to get_tmp occur and optimal performance is essential,
70-
consider changing 'N'/chunk size of this dualcache to $chunksize."
71-
resize!(dc.dual_du, nelem)
72-
end
73-
74-
75-
"""
76-
b = LazyBufferCache(f=identity)
77-
78-
A lazily allocated buffer object. Given an array `u`, `b[u]` returns an array of the
79-
same type and size `f(size(u))` (defaulting to the same size), which is allocated as
80-
needed and then cached within `b` for subsequent usage.
81-
82-
"""
83-
struct LazyBufferCache{F<:Function}
84-
bufs::Dict # a dictionary mapping types to buffers
85-
sizemap::F
86-
LazyBufferCache(f::F=identity) where {F<:Function} = new{F}(Dict(), f) # start with empty dict
87-
end
88-
89-
# override the [] method
90-
function Base.getindex(b::LazyBufferCache, u::T) where {T<:AbstractArray}
91-
s = b.sizemap(size(u)) # required buffer size
92-
buf = get!(b.bufs, (T, s)) do
93-
similar(u, s) # buffer to allocate if it was not found in b.bufs
94-
end::T # declare type since b.bufs dictionary is untyped
95-
return buf
96-
end
97-
98-
export dualcache, get_tmp, LazyBufferCache
99-
100-
end
1+
module PreallocationTools
2+
3+
using ForwardDiff, ArrayInterfaceCore, LabelledArrays, Adapt
4+
5+
struct DiffCache{T <: AbstractArray, S <: AbstractArray}
6+
du::T
7+
dual_du::S
8+
end
9+
10+
function DiffCache(u::AbstractArray{T}, siz, chunk_sizes) where {T}
11+
x = adapt(ArrayInterfaceCore.parameterless_type(u),
12+
zeros(T, prod(chunk_sizes .+ 1) * prod(siz)))
13+
DiffCache(u, x)
14+
end
15+
16+
"""
17+
18+
`dualcache(u::AbstractArray, N::Int = ForwardDiff.pickchunksize(length(u)); levels::Int = 1)`
19+
`dualcache(u::AbstractArray; N::AbstractArray{<:Int})`
20+
21+
Builds a `DualCache` object that stores both a version of the cache for `u`
22+
and for the `Dual` version of `u`, allowing use of pre-cached vectors with
23+
forward-mode automatic differentiation. Supports nested AD via keyword `levels`
24+
or specifying an array of chunk_sizes.
25+
26+
"""
27+
function dualcache(u::AbstractArray, N::Int = ForwardDiff.pickchunksize(length(u));
28+
levels::Int = 1)
29+
DiffCache(u, size(u), N * ones(Int, levels))
30+
end
31+
dualcache(u::AbstractArray, N::AbstractArray{<:Int}) = DiffCache(u, size(u), N)
32+
function dualcache(u::AbstractArray, ::Type{Val{N}}; levels::Int = 1) where {N}
33+
dualcache(u, N; levels)
34+
end
35+
dualcache(u::AbstractArray, ::Val{N}; levels::Int = 1) where {N} = dualcache(u, N; levels)
36+
37+
"""
38+
39+
`get_tmp(dc::DiffCache, u)`
40+
41+
Returns the `Dual` or normal cache array stored in `dc` based on the type of `u`.
42+
43+
"""
44+
function get_tmp(dc::DiffCache, u::T) where {T <: ForwardDiff.Dual}
45+
nelem = div(sizeof(T), sizeof(eltype(dc.dual_du))) * length(dc.du)
46+
if nelem > length(dc.dual_du)
47+
enlargedualcache!(dc, nelem)
48+
end
49+
ArrayInterfaceCore.restructure(dc.du, reinterpret(T, view(dc.dual_du, 1:nelem)))
50+
end
51+
52+
function get_tmp(dc::DiffCache, u::AbstractArray{T}) where {T <: ForwardDiff.Dual}
53+
nelem = div(sizeof(T), sizeof(eltype(dc.dual_du))) * length(dc.du)
54+
if nelem > length(dc.dual_du)
55+
enlargedualcache!(dc, nelem)
56+
end
57+
ArrayInterfaceCore.restructure(dc.du, reinterpret(T, view(dc.dual_du, 1:nelem)))
58+
end
59+
60+
function get_tmp(dc::DiffCache,
61+
u::LabelledArrays.LArray{T, N, D, Syms}) where {T, N, D, Syms}
62+
nelem = div(sizeof(T), sizeof(eltype(dc.dual_du))) * length(dc.du)
63+
if nelem > length(dc.dual_du)
64+
enlargedualcache!(dc, nelem)
65+
end
66+
_x = ArrayInterfaceCore.restructure(dc.du, reinterpret(T, view(dc.dual_du, 1:nelem)))
67+
LabelledArrays.LArray{T, N, D, Syms}(_x)
68+
end
69+
70+
get_tmp(dc::DiffCache, u::Number) = dc.du
71+
get_tmp(dc::DiffCache, u::AbstractArray) = dc.du
72+
73+
function enlargedualcache!(dc, nelem) #warning comes only once per dualcache.
74+
chunksize = div(nelem, length(dc.du)) - 1
75+
@warn "The supplied dualcache was too small and was enlarged. This incurrs allocations
76+
on the first call to get_tmp. If few calls to get_tmp occur and optimal performance is essential,
77+
consider changing 'N'/chunk size of this dualcache to $chunksize."
78+
resize!(dc.dual_du, nelem)
79+
end
80+
81+
"""
82+
b = LazyBufferCache(f=identity)
83+
84+
A lazily allocated buffer object. Given an array `u`, `b[u]` returns an array of the
85+
same type and size `f(size(u))` (defaulting to the same size), which is allocated as
86+
needed and then cached within `b` for subsequent usage.
87+
88+
"""
89+
struct LazyBufferCache{F <: Function}
90+
bufs::Dict # a dictionary mapping types to buffers
91+
sizemap::F
92+
LazyBufferCache(f::F = identity) where {F <: Function} = new{F}(Dict(), f) # start with empty dict
93+
end
94+
95+
# override the [] method
96+
function Base.getindex(b::LazyBufferCache, u::T) where {T <: AbstractArray}
97+
s = b.sizemap(size(u)) # required buffer size
98+
buf = get!(b.bufs, (T, s)) do
99+
similar(u, s) # buffer to allocate if it was not found in b.bufs
100+
end::T # declare type since b.bufs dictionary is untyped
101+
return buf
102+
end
103+
104+
export dualcache, get_tmp, LazyBufferCache
105+
106+
end

test/core_dispatch.jl

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
using LinearAlgebra, OrdinaryDiffEq, Test, PreallocationTools, ForwardDiff, LabelledArrays, RecursiveArrayTools
1+
using LinearAlgebra, OrdinaryDiffEq, Test, PreallocationTools, ForwardDiff, LabelledArrays,
2+
RecursiveArrayTools
23

34
#Base Array tests
45
chunk_size = 5
56
u0_B = ones(5, 5)
6-
dual_B = zeros(ForwardDiff.Dual{ForwardDiff.Tag{typeof(something), Float64}, Float64, chunk_size}, 2, 2)
7+
dual_B = zeros(ForwardDiff.Dual{ForwardDiff.Tag{typeof(something), Float64}, Float64,
8+
chunk_size}, 2, 2)
79
cache_B = dualcache(u0_B, chunk_size)
810
tmp_du_BA = get_tmp(cache_B, u0_B)
911
tmp_dual_du_BA = get_tmp(cache_B, dual_B)
@@ -14,8 +16,8 @@ tmp_dual_du_BN = get_tmp(cache_B, dual_B[1])
1416
@test eltype(tmp_du_BA) == eltype(u0_B)
1517
@test size(tmp_dual_du_BA) == size(u0_B)
1618
@test typeof(tmp_dual_du_BA) == typeof(dual_B)
17-
@test eltype(tmp_dual_du_BA) == eltype(dual_B)
18-
@test size(tmp_du_BN) == size(u0_B)
19+
@test eltype(tmp_dual_du_BA) == eltype(dual_B)
20+
@test size(tmp_du_BN) == size(u0_B)
1921
@test typeof(tmp_du_BN) == typeof(u0_B)
2022
@test eltype(tmp_du_BN) == eltype(u0_B)
2123
@test size(tmp_dual_du_BN) == size(u0_B)
@@ -24,9 +26,10 @@ tmp_dual_du_BN = get_tmp(cache_B, dual_B[1])
2426

2527
#LArray tests
2628
chunk_size = 4
27-
u0_L = LArray((2,2); a=1.0, b=1.0, c=1.0, d=1.0)
28-
zerodual = zero(ForwardDiff.Dual{ForwardDiff.Tag{typeof(something), Float64}, Float64, chunk_size})
29-
dual_L = LArray((2,2); a=zerodual, b=zerodual, c=zerodual, d=zerodual)
29+
u0_L = LArray((2, 2); a = 1.0, b = 1.0, c = 1.0, d = 1.0)
30+
zerodual = zero(ForwardDiff.Dual{ForwardDiff.Tag{typeof(something), Float64}, Float64,
31+
chunk_size})
32+
dual_L = LArray((2, 2); a = zerodual, b = zerodual, c = zerodual, d = zerodual)
3033
cache_L = dualcache(u0_L, chunk_size)
3134
tmp_du_LA = get_tmp(cache_L, u0_L)
3235
tmp_dual_du_LA = get_tmp(cache_L, dual_L)
@@ -37,19 +40,21 @@ tmp_dual_du_LN = get_tmp(cache_L, dual_L[1])
3740
@test eltype(tmp_du_LA) == eltype(u0_L)
3841
@test size(tmp_dual_du_LA) == size(u0_L)
3942
@test typeof(tmp_dual_du_LA) == typeof(dual_L)
40-
@test eltype(tmp_dual_du_LA) == eltype(dual_L)
41-
@test size(tmp_du_LN) == size(u0_L)
43+
@test eltype(tmp_dual_du_LA) == eltype(dual_L)
44+
@test size(tmp_du_LN) == size(u0_L)
4245
@test typeof(tmp_du_LN) == typeof(u0_L)
4346
@test eltype(tmp_du_LN) == eltype(u0_L)
4447
@test size(tmp_dual_du_LN) == size(u0_L)
4548
@test typeof(tmp_dual_du_LN) == typeof(dual_L)
46-
@test eltype(tmp_dual_du_LN) == eltype(dual_L)
49+
@test eltype(tmp_dual_du_LN) == eltype(dual_L)
4750

4851
#ArrayPartition tests
49-
u0_AP = ArrayPartition(ones(2,2), ones(3,3))
50-
dual_a = zeros(ForwardDiff.Dual{ForwardDiff.Tag{typeof(something), Float64}, Float64, chunk_size}, 2, 2)
51-
dual_b = zeros(ForwardDiff.Dual{ForwardDiff.Tag{typeof(something), Float64}, Float64, chunk_size}, 3, 3)
52-
dual_AP = ArrayPartition(dual_a, dual_b)
52+
u0_AP = ArrayPartition(ones(2, 2), ones(3, 3))
53+
dual_a = zeros(ForwardDiff.Dual{ForwardDiff.Tag{typeof(something), Float64}, Float64,
54+
chunk_size}, 2, 2)
55+
dual_b = zeros(ForwardDiff.Dual{ForwardDiff.Tag{typeof(something), Float64}, Float64,
56+
chunk_size}, 3, 3)
57+
dual_AP = ArrayPartition(dual_a, dual_b)
5358
cache_AP = dualcache(u0_AP, chunk_size)
5459
tmp_du_APA = get_tmp(cache_AP, u0_AP)
5560
tmp_dual_du_APA = get_tmp(cache_AP, dual_AP)
@@ -60,10 +65,10 @@ tmp_dual_du_APN = get_tmp(cache_AP, dual_AP[1])
6065
@test eltype(tmp_du_APA) == eltype(u0_AP)
6166
@test size(tmp_dual_du_APA) == size(u0_AP)
6267
@test typeof(tmp_dual_du_APA) == typeof(dual_AP)
63-
@test eltype(tmp_dual_du_APA) == eltype(dual_AP)
64-
@test size(tmp_du_APN) == size(u0_AP)
68+
@test eltype(tmp_dual_du_APA) == eltype(dual_AP)
69+
@test size(tmp_du_APN) == size(u0_AP)
6570
@test typeof(tmp_du_APN) == typeof(u0_AP)
6671
@test eltype(tmp_du_APN) == eltype(u0_AP)
6772
@test size(tmp_dual_du_APN) == size(u0_AP)
6873
@test typeof(tmp_dual_du_APN) == typeof(dual_AP)
69-
@test eltype(tmp_dual_du_APN) == eltype(dual_AP)
74+
@test eltype(tmp_dual_du_APN) == eltype(dual_AP)

0 commit comments

Comments
 (0)