Skip to content

Commit

Permalink
Merge pull request #33 from JuliaAlgebra/st/inbounds
Browse files Browse the repository at this point in the history
Add inbounds statements where possible (while adding @BoundsCheck)
  • Loading branch information
saschatimme authored Mar 20, 2019
2 parents 098d988 + 6ca8ee0 commit b7b50bb
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 29 deletions.
23 changes: 17 additions & 6 deletions src/evaluation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,15 @@ function evaluate_impl(f::Type{Polynomial{T, E, P}}) where {T, E, P}
access_input(i) = i n ? :(p[$i]) : :(x[$(i-n)])
end

boundschecks = [(:@boundscheck length(x) $(size(E)[1]))]
if P != Nothing
push!(boundschecks, (:@boundscheck length(p) $(size(P)[1])))
end

quote
Base.@_propagate_inbounds_meta
@boundscheck length(x) $(size(E)[1])
$(boundschecks...)
c = coefficients(f)
@inbounds out = begin
out = @inbounds begin
$(generate_evaluate(exponents(P, E), T, access_input))
end
out
Expand All @@ -53,10 +57,15 @@ function _val_gradient_impl(f::Type{Polynomial{T, E, P}}) where {T, E, P}
n = size(P, 1)
access_input(i) = i n ? :(p[$i]) : :(x[$(i-n)])
end
boundschecks = [(:@boundscheck length(x) $(size(E)[1]))]
if P != Nothing
push!(boundschecks, :(@boundscheck length(p) $(size(P)[1])))
end

quote
@boundscheck length(x) $(size(E)[1])
$(boundschecks...)
c = coefficients(f)
val, grad = begin
val, grad = @inbounds begin
$(generate_gradient(exponents(E), exponents(P), T, access_input))
end
val, grad
Expand Down Expand Up @@ -180,7 +189,9 @@ function _differentiate_parameters_impl(f::Type{Polynomial{T, E, P}}) where {T,
@boundscheck length(x) $(size(E, 1))
@boundscheck length(p) $(size(P, 1))
c = coefficients(f)
$(generate_differentiate_parameters(exponents(E), exponents(P), T, access_input))
@inbounds begin
$(generate_differentiate_parameters(exponents(E), exponents(P), T, access_input))
end
end
end

Expand Down
60 changes: 37 additions & 23 deletions src/polynomial_system.jl
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,10 @@ Evaluate the polynomial system `F` at `x` with parameters `p`.

@generated function _evaluate!(u, F::PolynomialSystem{N}, x...) where {N}
quote
$((:(u[$i] = evaluate(F.polys[$i], x...)) for i=1:N)...)
@boundscheck checkbounds(u, 1:$N)
@inbounds begin
$((:(u[$i] = evaluate(F.polys[$i], x...)) for i=1:N)...)
end
u
end
end
Expand All @@ -190,14 +193,17 @@ Evaluate the polynomial system `F` at `x` with parameters `p` and store its resu
###########
@generated function _jacobian!(U, F::PolynomialSystem{N, NVars}, x...) where {N, NVars}
quote
$(map(1:N) do i
quote
= _gradient(F.polys[$i], x...)
for j=1:$NVars
U[$i, j] = ∇[j]
@boundscheck checkbounds(U, 1:$N, 1:$NVars)
@inbounds begin
$(map(1:N) do i
quote
= _gradient(F.polys[$i], x...)
for j=1:$NVars
U[$i, j] = ∇[j]
end
end
end
end...)
end...)
end
U
end
end
Expand Down Expand Up @@ -260,15 +266,20 @@ Evaluate the Jacobian of the polynomial system `F` at `x` with parameters `p`.

@generated function _evaluate_and_jacobian!(u, U, F::PolynomialSystem{N, NVars}, x...) where {N, NVars}
quote
$(map(1:N) do i
quote
val, ∇ = _val_gradient(F.polys[$i], x...)
u[$i] = val
for j=1:$NVars
U[$i, j] = ∇[j]
@boundscheck checkbounds(u, 1:$N)
@boundscheck checkbounds(U, 1:$N, 1:$NVars)

@inbounds begin
$(map(1:N) do i
quote
val, ∇ = _val_gradient(F.polys[$i], x...)
u[$i] = val
for j=1:$NVars
U[$i, j] = ∇[j]
end
end
end
end...)
end...)
end
nothing
end
end
Expand Down Expand Up @@ -329,14 +340,17 @@ Evaluate the system `F` and its Jacobian at `x` with parameters `p`.

@generated function _differentiate_parameters!(U, F::PolynomialSystem{N, NVars, NParams}, x, p) where {N, NVars, NParams}
quote
$(map(1:N) do i
quote
= _differentiate_parameters(F.polys[$i], x, p)
for j=1:$NParams
U[$i, j] = ∇[j]
@boundscheck checkbounds(U, 1:$N, 1:$NParams)
@inbounds begin
$(map(1:N) do i
quote
= _differentiate_parameters(F.polys[$i], x, p)
for j=1:$NParams
U[$i, j] = ∇[j]
end
end
end
end...)
end...)
end
U
end
end
Expand Down

0 comments on commit b7b50bb

Please sign in to comment.