Skip to content

Parallelize read-only unchop neighbor checks - #111

Draft
yeeus wants to merge 3 commits into
vgteam:masterfrom
yeeus:unchop-parallel-neighbor-analysis
Draft

yeeus wants to merge 3 commits into
vgteam:masterfrom
yeeus:unchop-parallel-neighbor-analysis

Conversation

@yeeus

@yeeus yeeus commented Nov 24, 2026 •

Copy link
Copy Markdown

Expose the optional thread count without changing the existing serial entry point. Workers only check whether path neighbors can be merged. Component processing and all graph and path rewrites are serial. The caller may request more than one thread only on a backend that supports concurrent const path and edge traversal. The cache uses memory proportional to the node count.

The pinned chr21 implementation requested four threads. The sampled peak was approximately four cores and the unchop phase averaged approximately 1.4 cores. Phase wall time was 8 h 48 min on the earlier single-thread run and 6 h 13 min on the later four-thread run. Other conditions also varied, so the comparison does not isolate thread count.

Built the shared and static libraries. A local PackedGraph differential test produced identical serialized output at 1, 2, 4, and 8 threads on linear, cyclic, and partial traversals. The unused path-buffer removal is reviewed separately in #110.

The integrated chr21 comparison covered 1,319 human haplotypes and passed 37/37 final-output comparisons against each of the paired control and the historical outputs in scope. See ComparativeGenomicsToolkit/cactus#2096 for test coverage and benchmark context.

@adamnovak adamnovak 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.

I think this is a worthwhile idea, but to match the other vg ecosystem libraries (and make vgteam/vg#5128 simpler) this probably should be using OpenMP for parallelism.

Comment thread src/chop.cpp Outdated

// Read-only candidate analysis is parallel; all graph edits below remain serial.
// Each eligible oriented edge owns its two reverse-complement cache entries.
std::vector<uint8_t> perfect_neighbors;

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.

This is holding 8 times as much data as it would need if we could figure out how to use a vector of bools safely. I think if we're going in batches of 64, we already are going to be doing whole blocks of 64 ranks together.

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.

The cache now uses one bit per oriented-handle slot. Each worker writes full 64-bit words. Both orientations look up the same canonical entry, so a worker never has to update a word owned by another worker. The output comparison passed at 1, 2, 4, and 8 threads, including edges whose two orientations fall in different words.

Comment thread src/chop.cpp Resolved
Comment thread src/chop.cpp Outdated
std::vector<std::future<void>> futures;
futures.reserve(workers - 1);
for (size_t i = 1; i < workers; ++i) {
futures.emplace_back(std::async(std::launch::async, worker));

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.

SonarQube has strong opinions and thinks std::async is too terrible to use.

I'm not sure it's right, but everywhere else in the vg ecosystem we use OpenMP for parallelism. So if we stole the long piece of code from libbdsg that finds the OpenMP libraries, or figured out the right way to find OpenMP on modern Macs with modern CMake, we could do this as a #pragma omp parallel for.

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.

Switched the read-only checks to #pragma omp parallel for in fe1b3b5. Graph edits stay serial. Linux builds and consumers of the installed CMake package passed with OpenMP on and off. The AppleClang libomp discovery path still needs a macOS check.

@adamnovak adamnovak 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.

I think the OMP stuff is good, but there could be more comments around the caching-perfect-path-neighbor-ness idea.

Comment thread src/chop.cpp
Comment on lines +696 to +698
std::vector<uint64_t> perfect_neighbors;
std::function<bool(handle_t, handle_t)> neighbor_check;
#ifdef _OPENMP

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.

I had a little trouble seeing how the control flow comes back together between OpenMP and non-OpenMP mode and still gets the right result, since there isn't a comment explaining the idea here and there's a bit OpenMP-only block with no else branch. So it might want explaining.

Suggested change
std::vector<uint64_t> perfect_neighbors;
std::function<bool(handle_t, handle_t)> neighbor_check;
#ifdef _OPENMP
// This is a bit vector of whether a handle and the one to its right are
// perfect path neighbors (i.e. participate in exactly the same paths)
// and can be merged. If we fill it in in parallel now, we don't have
// to look at the paths in a single thread later.
std::vector<uint64_t> perfect_neighbors;
// We can fill this in with a function to quickly answer the question
// of whether nodes are perfect path neighbors. If left unset, the
// graph will be checked.
std::function<bool(handle_t, handle_t)> neighbor_check;
#ifdef _OPENMP
// Do a parallel pass now to fill in the neighbor information cache.

Looking more closely, it looks like we check both degree and path constraints here to fill the cache, and then in simple_components() we do all the degree checks/edge-following again and then consult the cache just for the path check. Ideally we'd do something clever with linked lists or a big table of next-perfect-neighbor ranks that we could fill in parallel, and then turn into a vector<deque<handle_t>> without any more need to touch the edges. But that might be overkill.

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.

Thanks. In 9b02f6c I documented how both paths reach the same check. The OpenMP pass caches path compatibility for the unbranched pairs. When that pass is skipped, simple_components() checks the path steps on demand. The component walk still checks edges and degrees. I’d revisit the successor table if profiling shows that the repeated edge walk is costly.

Comment thread src/chop.cpp
Comment on lines +347 to +348
vector<deque<handle_t>> simple_components(const PathHandleGraph& graph, int min_size = 1,
const std::function<bool(handle_t, handle_t)>* neighbor_check = nullptr) {

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.

There probably should be documentation for what question exactly neighbor_check() is expected to answer.

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.

Documented the callback contract in 9b02f6c. The edge walk has already checked that the pair is unbranched. neighbor_check(left, right) then answers whether every path visit to either handle has a matching adjacent visit to the other in the expected orientation, including repeated visits. Without a callback, the same check runs directly on the path steps.

@sonarqubecloud

sonarqubecloud Bot commented Dec 1, 2026

Copy link
Copy Markdown

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.

2 participants