Perform topological sort to order components - #428
cmacmackin wants to merge 24 commits into
Conversation
|
This is nearly done. Remaining tasks:
|
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## master #428 +/- ##
==========================================
+ Coverage 59.73% 60.26% +0.53%
==========================================
Files 98 98
Lines 10270 10420 +150
Branches 1482 1507 +25
==========================================
+ Hits 6135 6280 +145
- Misses 3488 3490 +2
- Partials 647 650 +3 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
a75b2ce to
8d049e2
Compare
c31490e to
e650c1c
Compare
e650c1c to
9dbeed5
Compare
9dbeed5 to
04a8977
Compare
04a8977 to
8fc17e0
Compare
|
I missed this in #421, but I see that you added a long-needed feature - a better way to determine species type which is based on the charge: hermes-3/src/component_scheduler.cxx Lines 339 to 352 in 8fc17e0 There is already a function that does the same in hermes-3/include/hermes_utils.hxx Lines 61 to 70 in 053c9bb Ideally there should be only one tool to do this, and your new one seems more robust. Is there any reason not to replace the one in |
Probably not. I just wasn't sure if people would be happy about me changing that bit of code. Note that this will require changing the function signature of |
|
I went through the sorting algorithm and I see that you have left a good amount of comments on the individual bits. However, I found it difficult to get my head around what's going on just because of the amount of steps involved. It would be very useful to have a paragraph describing how the algorithm works step-by-step, either in the docs or in the comments (or both) |
|
There are some places (e.g., when writing tests) where it was convenient to just be able to have a list of species names and use the old heuristics to categorise them. Probably not a good enough reason to keep that though. |
ZedThree
left a comment
There was a problem hiding this comment.
LGTM, thanks @cmacmackin!
There's some trivial bits that I'm happy to fix myself
8fc17e0 to
73593b7
Compare
Use CRTP to get component name for registration.
This required some changes to how options are accessed, so that we can more easily get/set only the domain or only the bounds.
aacebfa to
4572264
Compare
| // Shouldn't change if called again | ||
| ASSERT_EQ(isSetFinalBoundary(option["test"]), false); | ||
| } | ||
|
|
There was a problem hiding this comment.
GPT suggests a test to cover the GuardedOptions overload of IsSetFinalBoundary
| /// isSetFinalBoundary should succeed without needing interior access, | |
| /// lock boundary writes, and still allow interior writes. | |
| TEST(ComponentTest, IsSetFinalBoundaryGuardedBoundaryOnly) { | |
| Options options; | |
| options["test"] = 1; | |
| Permissions permissions({readOnly("test", Regions::Boundaries)}); | |
| GuardedOptions guarded(&options, &permissions); | |
| ASSERT_TRUE(isSetFinalBoundary(guarded["test"])); | |
| #if CHECKLEVEL >= 1 | |
| // Boundary is now assumed final | |
| ASSERT_THROW(setBoundary<int>(options["test"], 2), BoutException); | |
| // Interior/domain is still allowed | |
| ASSERT_NO_THROW(setNoBoundary<int>(options["test"], 3)); | |
| ASSERT_EQ(getNoBoundary<int>(options["test"]), 3); | |
| #endif | |
| } |
There was a problem hiding this comment.
I think there are some other functions which should be tested like this too.
mikekryjak
left a comment
There was a problem hiding this comment.
I gave another serious go at going through everything and left a few comments. My preference would be that we merge this with a flag to disable the sort if needed (enabled by default is fine) as well as a facility to print the as-written and as-sorted component list to console on initialisation.
We also definitely need to make a release before merging this in.
| /// permissions on the boundaries and read permissions for the | ||
| /// interior. | ||
| /// | ||
| /// FIXME: Currently these permissiosn are not expressed properly, due |
There was a problem hiding this comment.
is this FIXME still active? what does it take to fix it and what are the possible risks of leaving it unfixed?
| sorted.push_back(item); | ||
| } | ||
|
|
||
| /// Get all the parent sections of a variable "path". Sections are |
There was a problem hiding this comment.
Can you clarify that the path is just the hierarchy of the state object.
| std::set_intersection(unconditional_names.begin(), unconditional_names.end(), | ||
| unconditional_sections.begin(), unconditional_sections.end(), | ||
| std::inserter(sections_present, sections_present.begin())); | ||
| /// Assemble the set of all variable names which are definitlye |
| section defines the list of components used. The component order | ||
| matters, as the components are executed in order. | ||
| section defines the list of components used. The component order doesn't | ||
| matters, as they will be sorted to ensure state variables are set |
There was a problem hiding this comment.
| matters, as they will be sorted to ensure state variables are set | |
| matter, as they will be sorted to ensure state variables are set |
| /// permissions on the boundaries and read permissions for the | ||
| /// interior. | ||
| /// | ||
| /// FIXME: Currently these permissiosn are not expressed properly, due |
There was a problem hiding this comment.
| /// FIXME: Currently these permissiosn are not expressed properly, due | |
| /// FIXME: Currently these permissions are not expressed properly, due |
| for (const auto& component : components) { | ||
| const Permissions& permissions = component->getPermissions(); | ||
| for (const auto& [varname, _] : | ||
| permissions.getVariablesWithPermission(PermissionTypes::ReadIfSet, true)) { |
There was a problem hiding this comment.
I found this section hard to understand because of the bool. Can you split it into getVariablesWithMinimumPermission and getVaraiblesWithPermission instead?
| std::set_intersection(unconditional_names.begin(), unconditional_names.end(), | ||
| unconditional_sections.begin(), unconditional_sections.end(), | ||
| std::inserter(sections_present, sections_present.begin())); | ||
| /// Assemble the set of all variable names which are definitly |
There was a problem hiding this comment.
| /// Assemble the set of all variable names which are definitly | |
| /// Assemble the set of all variable names which are definitely |
| result[name] = {name}; | ||
| } | ||
| // Sections map to those variables which they contain | ||
| for (const auto& section : sections_present) { |
There was a problem hiding this comment.
Claude flagged an issue here and I've spent quite a long time trying to figure out whether it's a bug or not. I've spent a couple of hours trying to untangle it but I'm still not 100% sure. I think this function would benefit from a clearer docstring at the top going through the algorithm more clearly and in a bit more detail. It's good you have comments at each of the step but I'm finding it tricky to put them all together in my head even though I know what the function is for.
I can see that you are making a tree of all the permissions. The complaint is that you are making the tree by initially looking at paths of unconditional_names only. Then you make sure to account for conditional_names as well, but you fill the map with blanks which are only filled if the right permissions exist in unconditional_names.
See a summary from Claude:
The hierarchy is built from `unconditional_names` only (L177-199). `conditional_names`
get an entry at L206-208, but an empty one — nothing in this function fills those. They
end up populated only if the same path independently arrives via the unconditional pile
at L212/L220.
So a `readIfSet` claim resolves correctly only when some component claims that exact
same path with `readOnly` or stronger. Whenever reader and writer have to be matched
through the tree, it fails — in both directions:
Reader readIfSet("a:b") Writer readWrite("a:b:c")
Reader readIfSet("a:b:c") Writer readWrite("a:b")
In each case the `readIfSet` name keeps its empty entry, `expandVariableName` returns
nothing, no dependency is created, and the reader may sort before the writer — silently.
Swap the reader to `readOnly` and both order correctly, which is what makes this a bug
rather than a limitation.
Fix: run the parent scan and the section/variable split over
`conditional_names ∪ unconditional_names`, then drop the L206-208 loop — every
conditional name then gets its entry from one of the other two loops, which
`expandVariableName`'s `.at()` requires.
There was a problem hiding this comment.
I'll have a think about what the correct behaviour should be here. To be honest, I don't really like it if people give write permission for whole sections, because you can't actually guarantee they'll have written every variable in them that another component might use. I think the only reason I allowed permissions to be set for whole sections was so that components could read all the collision rates for a species without having to work out in advance exactly which ones were being computed.
There was a problem hiding this comment.
I think this function would benefit from a clearer docstring at the top going through the algorithm more clearly and in a bit more detail.
Note that the overall algorithm is set out before the sortComponents function, as well as in the documentation.
| component->declareAllSpecies(species); | ||
| } | ||
|
|
||
| ::sortComponents(components); |
There was a problem hiding this comment.
We definitely need to have a flag to enable or disable the component sorting, as well as a console print at construction to print the input file component order and then the sorted list of components. I know you said this was in a separate PR - I'm assuming it's #445? How come it's not in this PR? It opens a risk of issues between this and the next one being merged.
I'm sure you've explained this to me before, apologies for not remembering all of this. It's definitely good to get this merged soon.
| {{"components", "a"}, | ||
| {"a", {{"type", "orderchecker"}, {"permissions", toString(Permissions())}}}}, | ||
| {"a"}), | ||
| // Single component with only read permissions |
There was a problem hiding this comment.
| // Single component with only read permissions | |
| // Single component with only write permissions |
Using the access control information introduced in #421, this PR makes it possible for Hermes-3 to work out the order of components at run-time. This will make things far simpler and more robust for users. It will also fail faster if there is an unsatisfiable or circular dependency.
Closes #384.