Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

move promote_type call inside the generated function body #208

Merged
merged 4 commits into from
Jul 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,8 @@ julia:
matrix:
allow_failures:
- julia: nightly
after_success:
# push coverage results to Coveralls
- julia -e 'cd(Pkg.dir("WebIO")); Pkg.add("Coverage"); using Coverage; Coveralls.submit(Coveralls.process_folder())'
# push coverage results to Codecov
- julia -e 'cd(Pkg.dir("WebIO")); Pkg.add("Coverage"); using Coverage; Codecov.submit(Codecov.process_folder())'
12 changes: 8 additions & 4 deletions src/b-splines/indexing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,11 @@ end
for R in [:Real, :Any]
@eval @generated function gradient(itp::AbstractInterpolation{T,N}, xs::$R...) where {T,N}
n = count_interp_dims(itp, N)
Tg = promote_type(T, [x <: AbstractArray ? eltype(x) : x for x in xs]...)
xargs = [:(xs[$d]) for d in 1:length(xs)]
:(gradient!(Array{$Tg, 1}($n), itp, $(xargs...)))
quote
Tg = $(Expr(:call, :promote_type, T, [x <: AbstractArray ? eltype(x) : x for x in xs]...))
gradient!(Array{Tg, 1}($n), itp, $(xargs...))
end
end
end

Expand Down Expand Up @@ -158,9 +160,11 @@ end

@generated function hessian(itp::AbstractInterpolation{T,N}, xs...) where {T,N}
n = count_interp_dims(itp,N)
TH = promote_type(T, [x <: AbstractArray ? eltype(x) : x for x in xs]...)
xargs = [:(xs[$d]) for d in 1:length(xs)]
:(hessian!(Array{TH, 2}($n,$n), itp, $(xargs...)))
quote
TH = $(Expr(:call, :promote_type, T, [x <: AbstractArray ? eltype(x) : x for x in xs]...))
Copy link
Member

@stevengj stevengj Jul 3, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem is that this constructs an array and does the type computations at runtime. It may not be type-stable, either. It seems like you'd be better off doing something like TH = hessian_type(T, xs...) with

hessian_type(T) = T
hessian_type(T, x::AbstractArray, xs...) = promote_type(eltype(x), hessian_type(T, xs...))
hessian_type(T, x, xs...) = promote_type(typeof(x), hessian_type(T, xs...))

that can get evaluated at compile time.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure? That array is constructed inside the generator, not in the run-time code, and all the branching likewise happens inside the generator. I'm a fan of the lisp-y recursion trick anyway, but I'm pretty sure this case will work as-is.

Copy link
Member

@stevengj stevengj Jul 5, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, right, I misread your code — you're right, there should continue to be no runtime cost here.

hessian!(Array{TH, 2}($n,$n), itp, $(xargs...))
end
end

hessian1(itp::AbstractInterpolation{T,1}, x) where {T} = hessian(itp, x)[1,1]