Repository navigation
Plans: totals in pieces with no slot a member, counts, choices, deep expressions - #100
Merged
Merged
Conversation
…expressions Totals in pieces. A total was one node of a plan: on a GPU each scenario added its members in one thread, and every member's value kept a slot until the total read it (8 MB a scenario at a million rows). Now a large total is cut into whole subtrees of the same number of members (a power of two, as many as keep a piece under about 512 instructions, at most 256), each a node, and those again; the total combines the top ones. Each piece is a whole subtree of the graph's tree, so the order of combining, and the result, are the graph's bit for bit. The pieces of a level are one shape and short enough for a kernel each, so a GPU works them out side by side. Members worked out in their piece. A node read only by totals (a column read by its sum, min and max, say), and at most 24 instructions, gets no slot: each total works it out inside its piece. On a 1,000,000-row collection, a plan of the sum and the maximum now compiles in 0.8 s (0.9 s before), needs 254 KB a scenario (8 MB before) and runs a scenario in 4.6 ms on the CPU on four cores (6 ms before), with the same results. Counts. A count adds 1 for each member present, exact in an f64 up to 2^53, and comes out as the f64 of the same number. A whole number can be compared and the smaller or larger of two taken; arithmetic on whole numbers is still refused (the graph's does not round, and can overflow). An output may now be a whole number. Choices. choose(index, options) is a chain of conditionals on index == k. The index must be the same in every scenario and in range, or the plan refuses the node by name (the graph would hold an error); then the choice folds to the option it picks. Constant folding. A part of a node's program that reads nothing varied or shared is worked out when the plan is made, by running it, so it gives what running it would; a constant condition keeps only the branch taken. Nodes with nothing to fold are passed through without being copied. Deep expressions. A program that needs a stack deeper than 32 was refused (TooDeep). Now the operand that needs most is made a node of its own until every piece fits, so a lookup written as hundreds of nested conditionals, as the spreadsheet example writes VLOOKUP, runs. TooDeep is no longer returned. No new instructions: kernels and the plan file format are unchanged. Tests: random totals now include counts, and passed with every total cut into pieces of two members (checked by hand). New: a 100,000-row total in pieces with no slot a row, read back from a file; counts of groups whose rows change with the varied input, also in clusters; choices of each option, out of range, and by a number that varies; a 300-row lookup split into nodes; on the GPU, the large total (also in clusters), the counts and the lookup, replacing the test of a total too long for its own kernel; unit tests that every piece is short and few shapes, and of constant folding. Every example in watershed-gpu, at a small size, gives the same output as before. Docs: scenario-plans.md (compilation, totals, pieces), limitations.md, the README's limits (which still said totals were incremental only), the crate README and lib.rs, testing.md. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AdzBXcxXfE7iKtPMVoTTfr
matthewjones372
pushed a commit
that referenced
this pull request
Nov 16, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This fixes the two totals limits left open by #98 and lets plans run more of what they used to refuse. It adds no new instructions, so the CUDA kernels and the plan file format are unchanged.
1. Totals: pieces, and no slot per member
1,000,000-row collection, a plan of sum + max, same machine, old code built in a worktree for comparison:
2. What plans refused that they now run
f64up to 2^53, and it comes out as thef64of the same number. A whole number can be compared, and the smaller or larger of two taken. Arithmetic on whole numbers is still refused, because the graph's integer arithmetic doesn't round and can overflow. Outputs may now be whole numbers.choose. It's lowered to a chain of conditionals onindex == k. The index must be the same in every scenario and in range; otherwise the plan refuses the node by name (asHoldsError), because the graph would hold an error. The constant folding below then leaves only the chosen option. Achoosewhose index varies (only possible via a count) is refused by name.TooDeep. Now the operand that needs the most stack becomes a node of its own until every piece fits. A 300-row lookup written as nested conditionals (how the spreadsheet example writesVLOOKUP) runs as about 20 nodes.TooDeepstays in the enum but is no longer returned.Tests
choosefor each option, out of range, and by a varying index;--features cuda):cargo test --workspace: 238 passed.cargo clippy --workspace --all-targetsand with--features cudaare clean.cargo fmt --checkpasses.The GPU tests haven't been run here: there's no GPU in this environment. They need running on a CUDA machine.
Docs
docs/scenario-plans.md: compilation steps, totals, pieces, and members inside their piece.docs/limitations.md.lib.rs.docs/testing.md.Generated by Claude Code