Repository navigation
Parallelize read-only unchop neighbor checks #111
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| find_package(OpenMP QUIET COMPONENTS CXX) | ||
| if(NOT OpenMP_CXX_FOUND AND CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang" | ||
| AND NOT CMAKE_DISABLE_FIND_PACKAGE_OpenMP) | ||
| # AppleClang needs the separately installed libomp from Homebrew or MacPorts. | ||
| find_path(HANDLEGRAPH_OMP_INCLUDE omp.h | ||
| HINTS "$ENV{HOMEBREW_PREFIX}/opt/libomp/include" /opt/homebrew/opt/libomp/include /usr/local/opt/libomp/include /opt/local/include/libomp) | ||
| find_library(HANDLEGRAPH_OMP_LIBRARY omp | ||
| HINTS "$ENV{HOMEBREW_PREFIX}/opt/libomp/lib" /opt/homebrew/opt/libomp/lib /usr/local/opt/libomp/lib /opt/local/lib/libomp) | ||
| if(HANDLEGRAPH_OMP_INCLUDE AND HANDLEGRAPH_OMP_LIBRARY) | ||
| set(OpenMP_CXX_FLAGS "-Xpreprocessor -fopenmp") | ||
| set(OpenMP_CXX_INCLUDE_DIR "${HANDLEGRAPH_OMP_INCLUDE}") | ||
| set(OpenMP_CXX_LIB_NAMES omp) | ||
| set(OpenMP_omp_LIBRARY "${HANDLEGRAPH_OMP_LIBRARY}") | ||
| find_package(OpenMP QUIET COMPONENTS CXX) | ||
| endif() | ||
| endif() |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,9 @@ | ||
| if("@OpenMP_CXX_FOUND@") | ||
| include("${CMAKE_CURRENT_LIST_DIR}/HandlegraphOpenMP.cmake") | ||
| if(NOT OpenMP_CXX_FOUND) | ||
| set(libhandlegraph_FOUND FALSE) | ||
| set(libhandlegraph_NOT_FOUND_MESSAGE "This libhandlegraph build requires C++ OpenMP") | ||
| return() | ||
| endif() | ||
| endif() | ||
| include("${CMAKE_CURRENT_LIST_DIR}/libhandlegraphTargets.cmake") | ||
|
|
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -16,6 +16,10 @@ | |||||||||||||||||||||||||||||
| #include <cassert> | ||||||||||||||||||||||||||||||
| #include <algorithm> | ||||||||||||||||||||||||||||||
| #include <stdexcept> | ||||||||||||||||||||||||||||||
| #include <exception> | ||||||||||||||||||||||||||||||
| #ifdef _OPENMP | ||||||||||||||||||||||||||||||
| #include <omp.h> | ||||||||||||||||||||||||||||||
| #endif | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| namespace handlegraph { | ||||||||||||||||||||||||||||||
| namespace algorithms { | ||||||||||||||||||||||||||||||
|
|
@@ -340,7 +344,13 @@ bool nodes_are_perfect_path_neighbors(const PathHandleGraph& graph, handle_t lef | |||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| vector<deque<handle_t>> simple_components(const PathHandleGraph& graph, int min_size = 1) { | ||||||||||||||||||||||||||||||
| // The edge walk checks that the left handle's only outgoing edge is also | ||||||||||||||||||||||||||||||
| // the right handle's only incoming edge. The optional neighbor_check(left, | ||||||||||||||||||||||||||||||
| // right) tests only whether each visit to either handle has a matching | ||||||||||||||||||||||||||||||
| // adjacent visit to the other in the expected orientation, including | ||||||||||||||||||||||||||||||
| // repeated visits. Without the callback, check the path steps directly. | ||||||||||||||||||||||||||||||
| vector<deque<handle_t>> simple_components(const PathHandleGraph& graph, int min_size = 1, | ||||||||||||||||||||||||||||||
| const std::function<bool(handle_t, handle_t)>* neighbor_check = nullptr) { | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| // go around and establish groupings | ||||||||||||||||||||||||||||||
| unordered_set<nid_t> seen; | ||||||||||||||||||||||||||||||
|
|
@@ -391,7 +401,7 @@ vector<deque<handle_t>> simple_components(const PathHandleGraph& graph, int min_ | |||||||||||||||||||||||||||||
| // Move over left to that node | ||||||||||||||||||||||||||||||
| l = prev.front(); | ||||||||||||||||||||||||||||||
| // avoid merging if it breaks stored paths | ||||||||||||||||||||||||||||||
| if (!nodes_are_perfect_path_neighbors(graph, l, last)) { | ||||||||||||||||||||||||||||||
| if (!(neighbor_check ? (*neighbor_check)(l, last) : nodes_are_perfect_path_neighbors(graph, l, last))) { | ||||||||||||||||||||||||||||||
| #ifdef debug | ||||||||||||||||||||||||||||||
| cerr << "\tNot perfect neighbors!" << endl; | ||||||||||||||||||||||||||||||
| #endif | ||||||||||||||||||||||||||||||
|
|
@@ -454,7 +464,7 @@ vector<deque<handle_t>> simple_components(const PathHandleGraph& graph, int min_ | |||||||||||||||||||||||||||||
| // Move over right to that node | ||||||||||||||||||||||||||||||
| r = next.front(); | ||||||||||||||||||||||||||||||
| // avoid merging if it breaks stored paths | ||||||||||||||||||||||||||||||
| if (!nodes_are_perfect_path_neighbors(graph, last, r)) { | ||||||||||||||||||||||||||||||
| if (!(neighbor_check ? (*neighbor_check)(last, r) : nodes_are_perfect_path_neighbors(graph, last, r))) { | ||||||||||||||||||||||||||||||
| #ifdef debug | ||||||||||||||||||||||||||||||
| cerr << "\tNot perfect neighbors!" << endl; | ||||||||||||||||||||||||||||||
| #endif | ||||||||||||||||||||||||||||||
|
|
@@ -681,13 +691,79 @@ void unchop(MutablePathDeletableHandleGraph& graph) { | |||||||||||||||||||||||||||||
| #ifdef debug | ||||||||||||||||||||||||||||||
| std::cerr << "Running unchop" << std::endl; | ||||||||||||||||||||||||||||||
| #endif | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| std::unordered_map<nid_t, uint64_t> node_rank; | ||||||||||||||||||||||||||||||
| uint64_t rank = 0; | ||||||||||||||||||||||||||||||
| graph.for_each_handle([&](const handle_t& h) { | ||||||||||||||||||||||||||||||
| node_rank[graph.get_id(h)] = rank++; | ||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||
|
adamnovak marked this conversation as resolved.
Resolved
|
||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| // The OpenMP pass caches path compatibility for unbranched oriented | ||||||||||||||||||||||||||||||
| // pairs so simple_components() can use those answers without rescanning paths. | ||||||||||||||||||||||||||||||
| // The component walk still follows edges and checks degrees. If OpenMP | ||||||||||||||||||||||||||||||
| // is unavailable or the pass is skipped, neighbor_check is left unset | ||||||||||||||||||||||||||||||
| // and the same path compatibility check runs on demand. | ||||||||||||||||||||||||||||||
| std::vector<uint64_t> perfect_neighbors; | ||||||||||||||||||||||||||||||
| std::function<bool(handle_t, handle_t)> neighbor_check; | ||||||||||||||||||||||||||||||
| #ifdef _OPENMP | ||||||||||||||||||||||||||||||
|
Comment on lines
+706
to
+708
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Looking more closely, it looks like we check both degree and path constraints here to fill the cache, and then in
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks. In |
||||||||||||||||||||||||||||||
| std::vector<handle_t> original_handles; | ||||||||||||||||||||||||||||||
| const auto* ranked = dynamic_cast<const RankedHandleGraph*>(&graph); | ||||||||||||||||||||||||||||||
| const auto& ranks = node_rank; | ||||||||||||||||||||||||||||||
| auto cache_index = [&](handle_t h) { | ||||||||||||||||||||||||||||||
| return ranked ? ranked->handle_to_rank(h) - 1 | ||||||||||||||||||||||||||||||
| : 2 * ranks.at(graph.get_id(h)) + graph.get_is_reverse(h); | ||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||
| if (omp_get_max_threads() > 1 && !omp_in_parallel() && graph.get_node_count() != 0) { | ||||||||||||||||||||||||||||||
| if (!ranked) { | ||||||||||||||||||||||||||||||
| original_handles.resize(graph.get_node_count()); | ||||||||||||||||||||||||||||||
| graph.for_each_handle([&](const handle_t& h) { | ||||||||||||||||||||||||||||||
| original_handles[ranks.at(graph.get_id(h))] = h; | ||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| // Store only the canonical direction of each edge. A worker writes one | ||||||||||||||||||||||||||||||
| // complete word, even if the other orientation ranks in another block. | ||||||||||||||||||||||||||||||
| const size_t orientations = 2 * graph.get_node_count(); | ||||||||||||||||||||||||||||||
| constexpr size_t word_bits = 64; | ||||||||||||||||||||||||||||||
| perfect_neighbors.resize((orientations - 1) / word_bits + 1); | ||||||||||||||||||||||||||||||
| std::exception_ptr failure; | ||||||||||||||||||||||||||||||
| #pragma omp parallel for schedule(dynamic, 8) | ||||||||||||||||||||||||||||||
| for (size_t word = 0; word < perfect_neighbors.size(); ++word) { | ||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||
| uint64_t bits = 0; | ||||||||||||||||||||||||||||||
| size_t start = word * word_bits; | ||||||||||||||||||||||||||||||
| size_t end = std::min(start + word_bits, orientations); | ||||||||||||||||||||||||||||||
| for (size_t i = start; i < end; ++i) { | ||||||||||||||||||||||||||||||
| handle_t left = ranked ? ranked->rank_to_handle(i + 1) | ||||||||||||||||||||||||||||||
| : (i % 2 ? graph.flip(original_handles[i / 2]) : original_handles[i / 2]); | ||||||||||||||||||||||||||||||
| if (graph.get_degree(left, false) != 1) continue; | ||||||||||||||||||||||||||||||
| handle_t right; | ||||||||||||||||||||||||||||||
| graph.follow_edges(left, false, [&](const handle_t& h) { | ||||||||||||||||||||||||||||||
| right = h; | ||||||||||||||||||||||||||||||
| return false; | ||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||
| if (graph.get_degree(right, true) != 1) continue; | ||||||||||||||||||||||||||||||
| if (as_integer(left) > as_integer(graph.flip(right))) continue; | ||||||||||||||||||||||||||||||
| if (nodes_are_perfect_path_neighbors(graph, left, right)) { | ||||||||||||||||||||||||||||||
| bits |= uint64_t{1} << (i - start); | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| perfect_neighbors[word] = bits; | ||||||||||||||||||||||||||||||
| } catch (...) { | ||||||||||||||||||||||||||||||
| #pragma omp critical(handlegraph_unchop_failure) | ||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||
| if (!failure) failure = std::current_exception(); | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| if (failure) std::rethrow_exception(failure); | ||||||||||||||||||||||||||||||
| neighbor_check = [&](handle_t left, handle_t right) { | ||||||||||||||||||||||||||||||
| handle_t opposite = graph.flip(right); | ||||||||||||||||||||||||||||||
| size_t i = cache_index(as_integer(left) <= as_integer(opposite) ? left : opposite); | ||||||||||||||||||||||||||||||
| return ((perfect_neighbors[i / word_bits] >> (i % word_bits)) & 1) != 0; | ||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| #endif | ||||||||||||||||||||||||||||||
| // if possible, don't hold these in memory | ||||||||||||||||||||||||||||||
| // meanwhile, we exploit the it for parallelizing the paths' validation | ||||||||||||||||||||||||||||||
| std::vector<std::string> path_names; | ||||||||||||||||||||||||||||||
|
|
@@ -708,7 +784,14 @@ void unchop(MutablePathDeletableHandleGraph& graph) { | |||||||||||||||||||||||||||||
| ++rank; | ||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| auto components = simple_components(graph, 2); | ||||||||||||||||||||||||||||||
| auto components = simple_components(graph, 2, neighbor_check ? &neighbor_check : nullptr); | ||||||||||||||||||||||||||||||
| // No parallel reader may survive into graph mutation. | ||||||||||||||||||||||||||||||
| perfect_neighbors.clear(); | ||||||||||||||||||||||||||||||
| perfect_neighbors.shrink_to_fit(); | ||||||||||||||||||||||||||||||
| #ifdef _OPENMP | ||||||||||||||||||||||||||||||
| original_handles.clear(); | ||||||||||||||||||||||||||||||
| original_handles.shrink_to_fit(); | ||||||||||||||||||||||||||||||
| #endif | ||||||||||||||||||||||||||||||
| std::unordered_set<nid_t> to_merge; | ||||||||||||||||||||||||||||||
| for (auto& comp : components) { | ||||||||||||||||||||||||||||||
| for (auto& handle : comp) { | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
There was a problem hiding this comment.
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.There was a problem hiding this comment.
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.