From 6ca8ee04bbfd3bc80bdacdaa47a20db668bb3863 Mon Sep 17 00:00:00 2001 From: Sascha Timme Date: Wed, 20 Mar 2019 19:24:21 +0100 Subject: [PATCH] Add inbounds statements where possible (while adding @boundscheck) --- src/evaluation.jl | 23 +++++++++++---- src/polynomial_system.jl | 60 +++++++++++++++++++++++++--------------- 2 files changed, 54 insertions(+), 29 deletions(-) diff --git a/src/evaluation.jl b/src/evaluation.jl index 86588ab..61146c0 100644 --- a/src/evaluation.jl +++ b/src/evaluation.jl @@ -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 @@ -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 @@ -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 diff --git a/src/polynomial_system.jl b/src/polynomial_system.jl index 59f2a4b..f3f0c8a 100644 --- a/src/polynomial_system.jl +++ b/src/polynomial_system.jl @@ -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 @@ -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 @@ -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 @@ -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