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

Conversation

rdeits
Copy link
Contributor

@rdeits rdeits commented Jun 20, 2018

Fixes #207 (at least for the particular case i showed there).

I also changed the equivalent code in hessian(), although i think that code may have been broken already (Array{TH should have been Array{$TH). I'll see what the test coverage says.

Anyway, please let me know if you think this is the right thing to do. If so, I should be able to construct a test to make sure #207 is really fixed.

@rdeits rdeits closed this Jul 3, 2018
@rdeits rdeits reopened this Jul 3, 2018
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.

@rdeits
Copy link
Contributor Author

rdeits commented Jul 10, 2018

Rebased. Good to go?

@rdeits
Copy link
Contributor Author

rdeits commented Jul 10, 2018

Re-rebased after #197 🙂

@timholy timholy merged commit 01835b0 into JuliaMath:master Jul 10, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Non-pure @generated functions cause hard-to-fix MethodErrors
3 participants