Skip to content

DNMY: Exposed variable new_x in C_wrapper. Maintains backward comp. - #313

Closed
sohailreddy wants to merge 3 commits into
jump-dev:masterfrom
sohailreddy:expose_new_x
Closed

DNMY: Exposed variable new_x in C_wrapper. Maintains backward comp.#313
sohailreddy wants to merge 3 commits into
jump-dev:masterfrom
sohailreddy:expose_new_x

Conversation

@sohailreddy

Copy link
Copy Markdown

Updated C_wrapper to exposed the variable new_x from Ipopt routines to Julia functions. Maintains backward compatibility for cases where new_x is not used by routines.

…o Julia functions. Maintains backward compatibility for cases where new_x is not used by routines.

@odow odow left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

What's the purpose of this?

We can't merge this as-is, because it's going to force all current models to throw an error on every function call, which will cause a massive performance problem.

Do you want to know if you need to recompute the function? You can do this in a closure.

function memoize_f(f)
    last_x, last_fx = nothing, NaN
    return (x) -> begin
        if x == last_x
            return last_fx
        end
        last_x = x
        return f(x)
    end
end

@codecov

codecov Bot commented Apr 7, 2022

Copy link
Copy Markdown

Codecov Report

Merging #313 (3cce6c5) into master (2016f03) will decrease coverage by 0.63%.
The diff coverage is 74.07%.

@@            Coverage Diff             @@
##           master     #313      +/-   ##
==========================================
- Coverage   87.71%   87.07%   -0.64%     
==========================================
  Files           3        3              
  Lines         692      712      +20     
==========================================
+ Hits          607      620      +13     
- Misses         85       92       +7     
Impacted Files Coverage Δ
src/C_wrapper.jl 83.70% <74.07%> (-3.26%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 2016f03...3cce6c5. Read the comment docs.

@odow odow changed the title Exposed variable new_x in C_wrapper. Maintains backward comp. DNMY: Exposed variable new_x in C_wrapper. Maintains backward comp. Apr 7, 2022
…ion, maintains backward compatibility and passes all test
@sohailreddy

Copy link
Copy Markdown
Author

Understood, I updated it to remove the exceptions. The update shouldn't affect performance much and it should be faster to use Ipopt's xnew rather than recomputing the norm/checking equivalence outside every time eval_* are called.

@odow

odow commented Apr 21, 2022

Copy link
Copy Markdown
Member

What's the purpose of this? Do you have a benchmark where using a closure is a performance bottleneck?

Comment thread src/C_wrapper.jl Outdated
prob.x .= x
end
new_obj = convert(Float64, prob.eval_f(x))::Float64
new_x = convert(Bool, x_new)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

In the spirit of keeping closer to the C interface, why not just pass x_new in as the Cint?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

On the Julia side, we might want to keep it closer to the Julia types?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It's better to keep closer to the C types. Having differences between Julia and C is a pain (hence the reason you opened this PR).

Comment thread src/C_wrapper.jl
new_obj = convert(Float64, prob.eval_f(x))::Float64
new_x = convert(Bool, x_new)
new_obj = nothing
if prob.expose_xnew

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

You can also write

new_obj = if prob.expose_xnew
    convert(Float64, ...)
else
    convert(Float64, ...)
end

That avoids the need for new_obj = nothing which creates a type instability.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Then would this need to be done for the other routines? eval_f, eval_grad_f, eval_g, eval_jac_g, eval_h?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yes

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yes

Comment thread src/C_wrapper.jl Outdated
Comment thread src/C_wrapper.jl
m::Cint,
lambda_ptr::Ptr{Float64},
::Cint,
new_lambda::Cint,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
new_lambda::Cint,
::Cint,

We don't name arguments if they aren't used. But if we're going th x_new route, should we not include new_lambda as well?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Yes to be consistent, we should also expose new_lambda but I can't imagine many users using the exact hessian routines

Comment thread src/C_wrapper.jl Outdated
@odow

odow commented Jul 18, 2022

Copy link
Copy Markdown
Member

I've updated this in #322, and added some basic tests. I'd still like to see a benchmark where this makes a difference. You'd have to cache the old solutions, so why not just cache and check the x vector as well. It should be a pretty minor check in comparison to the full optimization, I can't see it being a bottleneck.

@odow

odow commented Aug 17, 2022

Copy link
Copy Markdown
Member

Closing in favor of #322. I'm still not convinced, but we can continue to discuss it in that PR. I'd like to see a practical application where this makes a difference.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants