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

More types for nodes #26

Merged
merged 10 commits into from
May 8, 2018
Merged

More types for nodes #26

merged 10 commits into from
May 8, 2018

Conversation

rafaqz
Copy link
Contributor

@rafaqz rafaqz commented Apr 25, 2018

I needed parametric types on nodes, with different type parameters on each node. With Vectors that gives Union types.

Not for merging yet - I'll follow up with some tests. Just checking this is a reasonable idea.

@ChrisRackauckas
Copy link
Member

Wouldn't this make all of the indexing not type-stable (if used)? It could be useful but it would be much slower.

@rafaqz
Copy link
Contributor Author

rafaqz commented Apr 25, 2018

Accessing the parametric fields with haskell-style head/tail recursive function application is type-stable in this version. But you're right - array access is now unstable! creating a big problem to solve a small one. I'll have a look, but I imagine it would require making everything recursive to fix it.

It's also possible 0.7 union types will be fast enough to fix this for small problems.

@ChrisRackauckas
Copy link
Member

ChrisRackauckas commented Apr 26, 2018

I'll have a look, but I imagine it would require making everything recursive to fix it.

That's fine if broadcast is defined appropriately. And I think it's parametrically-unstable: for the standard vector types it's still type-stable, but only when you put something odd in there it will not be.

It's also possible 0.7 union types will be fast enough to fix this for small problems.

Yeah, that should be fine.

This needs passing tests though.

@rafaqz
Copy link
Contributor Author

rafaqz commented Apr 26, 2018

I think it will still be a net gain for me because I'll have unstable types everywhere using an union vector anyway, my problem is basically the same as discussed in #5. And yes I hoped this change just makes it easier to do something slow, but should work as usual if you just use a typed Vector.

I'll get the tests passing, was saving time in case this was just a bad idea.

@rafaqz
Copy link
Contributor Author

rafaqz commented Apr 26, 2018

Seems this is failing on master as well - but not the same commit on tag v.0.6?

Master fails locally for me on 0.6.2.

@ChrisRackauckas
Copy link
Member

Interesting. I'll have to find time to take a look at it. The code you have looks fine.

@@ -25,7 +58,7 @@ end

@inline function getindex(m::AbstractMultiScaleArray, i, I...)
if isempty(m.values) || i < length(m.end_idxs)
length(I) == 1 ? m.nodes[i].nodes[I[1]] : m.nodes[i][I...]
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure what this line does? Seems like it can error in a couple of ways... The tuple version is not type stable currently

Copy link
Member

Choose a reason for hiding this comment

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

It handles the case where I is a scalar since then it cannot splat I think.

@rafaqz
Copy link
Contributor Author

rafaqz commented Apr 26, 2018

getindex for parametric typed tuples seems to be pretty much type stable now, except for the line mentioned above. I'll do some more comprehensive tests of type stability and some general tests and push them in the next few days.

Maybe it could be optimised more too, I'm not that clear on when @inline etc should be used.

Also there's probably a much more julian way of doing this. It looks pretty weird.

@rafaqz
Copy link
Contributor Author

rafaqz commented May 1, 2018

Indexing type stability turned out to be easier than I thought.

But the real problem turns out to be that running an ODE triggers add_node!. That means the struct would always need to be mutable, and all the nodes would need to be packed into a new tuple. I'm not sure what add_node! does in the solver?

@ChrisRackauckas
Copy link
Member

But the real problem turns out to be that running an ODE triggers add_node!. That means the struct would always need to be mutable, and all the nodes would need to be packed into a new tuple. I'm not sure what add_node! does in the solver?

One of the tests is about resizing. Resizing and mutability is one of the possible behaviors. However, it's only done if the user makes an event that triggers it. It shouldn't be triggered on its own.

@@ -89,7 +89,7 @@ growing_cb = DiscreteCallback(condition, affect!)

println("ODE with tuple nodes")

prob = ODEProblem(f, scenario, (0.0, 1.0))
prob = ODEProblem(f, dscenario, scenario, (0.0, 1.0))
Copy link
Member

Choose a reason for hiding this comment

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

This syntax doesn't make sense. The previous one was correct.

@ChrisRackauckas
Copy link
Member

The test problem you're using has an event:

tstop = [0.5]
condition = function (u, t, integrator)
    t  tstop
end
affect! = function (integrator)
    add_node!(integrator, integrator.u[1, 1, 1], 1, 1)
end
growing_cb = DiscreteCallback(condition, affect!)

that tries to add a new plant at t=0.5. That's not compatible with the tuples. I assume you didn't mean to add this event?

@@ -7,7 +10,7 @@ Base.IndexStyle(::Type{<:AbstractMultiScaleArray}) = IndexLinear()
@inline function getindex(m::AbstractMultiScaleArray, i::Int)
idx = bisect_search(m.end_idxs, i)
idx > 1 && (i -= m.end_idxs[idx-1]) # also works with values
(isempty(m.values) || idx < length(m.end_idxs)) ? m.nodes[idx][i] : m.values[i]
(isempty(m.values) || idx < length(m.end_idxs)) ? nodeselect(m.nodes, idx, i) : m.values[i]
Copy link
Member

Choose a reason for hiding this comment

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

this is no different?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry turned out that wasn't even needed after all, something else fixed the type stability. I'm still scratching my head...

@rafaqz
Copy link
Contributor Author

rafaqz commented May 1, 2018

Ok that makes sense with the event, I just copied some of your tests for arrays.

But it's good to know where using tuples will break. I will look at writing an add_node! method that replaces the whole tuple so you can use events if you use a mutable type.

@rafaqz
Copy link
Contributor Author

rafaqz commented May 2, 2018

I think my tests are all passing, the test that failing is line 67 of dinamic_diffeq.jl:

sol = solve(prob, Rosenbrock23(), callback=growing_cb, tstops=tstop)

LoadError: MethodError: no method matching resize!(::Embryo{Tissue{Population{Cell{ForwardDiff.Dual{ForwardDiff.Tag{DiffEqDiffTools.UJacobianWrapper{##27#28,Float64,Void},Float64},Float64,10}},ForwardDiff.Dual{ForwardDiff.Tag{DiffEqDiffTools.UJacobianWrapper{##27#28,Float64,Void},Float64},Float64,10}},ForwardDiff.Dual{ForwardDiff.Tag{DiffEqDiffTools.UJacobianWrapper{##27#28,Float64,Void},Float64},Float64,10}},ForwardDiff.Dual{ForwardDiff.Tag{DiffEqDiffTools.UJacobianWrapper{##27#28,Float64,Void},Float64},Float64,10}}, ::Int64)

And that's also failing on master.

@ChrisRackauckas
Copy link
Member

Rebased.

@ChrisRackauckas
Copy link
Member

Passing. Can this get docs?

@coveralls
Copy link

coveralls commented May 8, 2018

Coverage Status

Coverage increased (+0.1%) to 80.357% when pulling 9c569a1 on rafaqz:loose_typed_nodes into 8f2a994 on JuliaDiffEq:master.

@codecov
Copy link

codecov bot commented May 8, 2018

Codecov Report

Merging #26 into master will not change coverage.
The diff coverage is n/a.

Impacted file tree graph

@@           Coverage Diff           @@
##           master      #26   +/-   ##
=======================================
  Coverage   80.23%   80.23%           
=======================================
  Files           7        7           
  Lines         167      167           
=======================================
  Hits          134      134           
  Misses         33       33

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 8f2a994...b14833b. Read the comment docs.

@rafaqz
Copy link
Contributor Author

rafaqz commented May 8, 2018

Yes that's a good idea. It might not be for a week or so with my current workload.

@ChrisRackauckas
Copy link
Member

I went ahead and added the docs. Cheers!

@ChrisRackauckas ChrisRackauckas merged commit c7bb8fe into SciML:master May 8, 2018
@rafaqz
Copy link
Contributor Author

rafaqz commented May 9, 2018 via email

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.

3 participants