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

Priority as number #90

Merged
merged 31 commits into from
Aug 3, 2023
Merged

Priority as number #90

merged 31 commits into from
Aug 3, 2023

Conversation

hdavid16
Copy link
Member

@hdavid16 hdavid16 commented Jul 9, 2023

Replaces #77

change type on priority from Int to Number. This allows passing a Float as a priority.

Advantages:
This is a more general expression of priority which may make it easier at some point to use rank functions (#75).
This also avoids having to normalize (and sometimes scaling) priorities so that they are integer in queueing systems.

@codecov-commenter
Copy link

codecov-commenter commented Jul 9, 2023

Codecov Report

Merging #90 (fbbc732) into master (c62a6e5) will increase coverage by 0.02%.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##           master      #90      +/-   ##
==========================================
+ Coverage   98.17%   98.20%   +0.02%     
==========================================
  Files          10       10              
  Lines         274      278       +4     
==========================================
+ Hits          269      273       +4     
  Misses          5        5              
Files Changed Coverage Δ
src/simulations.jl 100.00% <ø> (ø)
src/base.jl 97.91% <100.00%> (ø)
src/events.jl 100.00% <100.00%> (ø)
src/operators.jl 100.00% <100.00%> (ø)
src/processes.jl 100.00% <100.00%> (ø)
src/resources/containers.jl 100.00% <100.00%> (ø)
src/resources/stores.jl 95.45% <100.00%> (+0.21%) ⬆️
src/utils/time.jl 100.00% <100.00%> (ø)

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@hdavid16 hdavid16 marked this pull request as draft July 9, 2023 01:18
@hdavid16 hdavid16 marked this pull request as ready for review July 9, 2023 01:46
@hdavid16
Copy link
Member Author

hdavid16 commented Jul 9, 2023

I'll work on resolving the issues next week.

@@ -63,7 +63,7 @@ function remove_callback(cb::Function, ev::AbstractEvent)
i != 0 && deleteat!(ev.bev.callbacks, i)
end

function schedule(ev::AbstractEvent, delay::Number=zero(Float64); priority::Int=0, value::Any=nothing)
function schedule(ev::AbstractEvent, delay::Number=0; priority::Number=0, value::Any=nothing)
Copy link
Member

Choose a reason for hiding this comment

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

It seems a lot of these type restriction can simply be dropped. Types in function signatures are used only for dispatch, they do not provide performance improvements. And it seems types in kwargs do not even participate in dispatch (see https://discourse.julialang.org/t/force-specialization-on-kwargs/34789/14 ). So this makes types for kwargs (and in particular abstract types) not particularly valuable. In most of these situations (function signatures) you can just delete the ::Int, no need to add ::Number.

TODO: should make order of parameters consistent in both resource types (Containers has it as the first parameter. Stores has it as the second).
@hdavid16
Copy link
Member Author

hdavid16 commented Jul 29, 2023

TODO:

  • should make order of parameters consistent in both resource types (Containers has priority as the first parameter. Stores has it as the second).

@hdavid16
Copy link
Member Author

@Krastanov, I got it to work without introducing a breaking change. One of the tests on GitHub fails on something with makedocs (nightly). Not sure what that is. Let me know what you think of this PR. Thanks

@Krastanov
Copy link
Member

I will look into this by the end of the weekend. The documentation tests are unrelated and probably fixed in #93

@Krastanov
Copy link
Member

This looks good to me. Could you add tests that it works reliably?

E.g.:

  • adding a process with Int priority followed by adding a processes with Float priority
  • Float followed by Int
  • Float followed by Float
  • Int followed by Int

And maybe something with BigInt and BigFloat. Mainly to make sure there is no older code that makes some implicit assumptions and breaks.

That would also test whether we are missing some convert statements.

@Krastanov
Copy link
Member

In the meantime I will fix some of the currently hidden / broken tests. The failure on nightly is unrelated and comes from MacroTools. I will try to fix it upstream sometime soon (see FluxML/MacroTools.jl#195 (feel free to jump in if you feel like it :D )

@Krastanov
Copy link
Member

Merging the other pull request caused conflicts. I think I resolved them all, but please double check my merge commit above (and pull it locally if you need to add something more)

@Krastanov
Copy link
Member

All of the new documentation failures come from a single newly added test. In particular, I think the constructor for Store has a small break in backward compatibility because previously an implicit convert statement. That Store constructor is the only incompatibility I currently see.

@hdavid16
Copy link
Member Author

hdavid16 commented Aug 3, 2023

The error was because that doctest was using the wrong type for capacity. The capacity in a Store has always been UInt. I have fixed this now.

put_queue :: DataStructures.PriorityQueue{Put, StorePutKey{T}}
get_queue :: DataStructures.PriorityQueue{Get, StoreGetKey}
function Store{T}(env::Environment; capacity=typemax(UInt)) where {T}
new(env, UInt(capacity), zero(UInt), Dict{T, UInt}(), zero(UInt), DataStructures.PriorityQueue{Put, StorePutKey{T}}(), DataStructures.PriorityQueue{Get, StoreGetKey}())
Copy link
Member

Choose a reason for hiding this comment

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

The explicit UInt(capacity) cast is removed in the new version. That cast is necessary in order to make capacity=10 work as a keyword argument (which used to be the case until this PR).

Copy link
Member Author

Choose a reason for hiding this comment

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

SimJulia.jl had the UInt signature in the capacity kwarg. I like the idea of using UInt(capacity) instead of imposing this type on the kwarg, making it possible to use capacity=10 as you suggest. However, we need to still convert to UInt so that a Store cannot have a negative capacity.

src/resources/stores.jl Outdated Show resolved Hide resolved
convert to the resource's priority type
"""
put!(sto::Store, item::T)

Put an item into the store. Returns the put event, blocking if the store is full.
"""
function put!(sto::Store{T}, item::T; priority::Int=0) where T
Copy link
Member

Choose a reason for hiding this comment

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

This does not need to be modified, but just FYI, the ::T in the priority kwarg (and the previously existing ::Int) are unnecessary (if I understand Julia's dispatch rules correctly). Basically, type signatures of keyword arguments are not used for dispatch, so they are frequently superfluous.

Copy link
Member

Choose a reason for hiding this comment

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

They are still useful as a type assert though

Copy link
Member Author

Choose a reason for hiding this comment

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

we now convert to the appropriate type given the type of the Store

@Krastanov
Copy link
Member

Wonderful! I will fix the documentation break that was introduced with the merge with the other PR and will merge this as well.

@Krastanov
Copy link
Member

Sorry to be a killjoy... I was going through the rest of the code because I was extremely surprised that the =Inf default values were not triggering UInt(Inf) conversion errors. It took me a bit to find the source of this -- it is not really in anything you had changed.

Rather now that you have made EvenKey a parameterized type, the heap slot in Simulation is not concrete anymore.

It is here:

mutable struct Simulation <: Environment
  time :: Float64
  heap :: DataStructures.PriorityQueue{BaseEvent, EventKey}

I am a bit on the fence about this. BaseEvent is already abstract, so it is not like this is changing much, but it is pushing things in an unpleasant direction.

What I plan to do is to add a few broken tests as a reminder that this is something that should probably be fixed in the future. I will add the tests, create a new issue to have them recorded, and merge this.

@Krastanov Krastanov merged commit bb72add into master Aug 3, 2023
7 of 8 checks passed
@Krastanov
Copy link
Member

Actually, I do not know how to do a recursive isconcretetype check anyway... So I can not write a useful broken test. Given that the heap key is already abstract, I am not too worried about changes in performance, but in the future this would be something to improve.

@hdavid16
Copy link
Member Author

hdavid16 commented Aug 3, 2023

You know, now that I think about this. I don't think we should have priorities be Number for EventKey and other scheduling related methods. The motivation behind this PR was to allow the user to specify floats for the priorities used when queueing resources. I don't think a user would typically be setting float priorities for Events. I would be in favor of restoring the Int priorities for Events and just keeping the Number priorities for resources. What do you think, @Krastanov ?

@Krastanov
Copy link
Member

If you have the bandwidth to do that, I would be very much in favor.

@Krastanov Krastanov deleted the priority_as_number branch June 19, 2024 16:12
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