-
Notifications
You must be signed in to change notification settings - Fork 0
Add rpq #1
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
Merged
Add rpq #1
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| name: Compile, run tests and check code style | ||
|
|
||
| on: | ||
| push: | ||
| branches: | ||
| - '**' | ||
| pull_request: | ||
| branches: | ||
| - '**' | ||
|
|
||
| jobs: | ||
| build: | ||
| runs-on: ${{ matrix.os }} | ||
|
|
||
| strategy: | ||
| fail-fast: false | ||
|
|
||
| matrix: | ||
| os: [ubuntu-24.04] | ||
| c_compiler: [gcc-14] | ||
| cpp_compiler: [g++-14] | ||
| build_type: [Release, Debug] | ||
|
|
||
| steps: | ||
| - uses: actions/checkout@v2 | ||
| with: | ||
| submodules: recursive | ||
|
|
||
| - name: Setup enviroment | ||
| run: | | ||
| sudo apt update | ||
| sudo apt install gcc-14 | ||
| sudo apt install g++-14 | ||
| sudo apt install cmake | ||
| sudo apt install clang-format | ||
|
|
||
| - name: Configure CMake | ||
| run: | | ||
| cd ${{ github.workspace }} | ||
| cmake -B build -S . \ | ||
| -DCMAKE_CXX_COMPILER=${{ matrix.cpp_compiler }} \ | ||
| -DCMAKE_C_COMPILER=${{ matrix.c_compiler }} \ | ||
| -DCMAKE_BUILD_TYPE=${{ matrix.build_type }} \ | ||
| -DCUBOOL_GRAPH_ENABLE_TESTING=ON \ | ||
| -DCUBOOL_WITH_CUDA=OFF \ | ||
| -DCUBOOL_WITH_SEQUENTIAL=ON | ||
|
|
||
| - name: Build | ||
| run: | | ||
| cmake --build build -j10 | ||
|
|
||
| - name: Run tests | ||
| run: | | ||
| ./build/tests/cuboolgraph_tests |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -30,3 +30,7 @@ | |
| *.exe | ||
| *.out | ||
| *.app | ||
|
|
||
| build/* | ||
| .cache/* | ||
| .vscode/* | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| [submodule "deps/googletest"] | ||
| path = deps/googletest | ||
| url = https://github.com/google/googletest | ||
| [submodule "deps/fast_matrix_market"] | ||
| path = deps/fast_matrix_market | ||
| url = https://github.com/alugowski/fast_matrix_market | ||
| [submodule "deps/cuBool"] | ||
| path = deps/cuBool | ||
| url = https://github.com/mitya-y/cuBool |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| cmake_minimum_required(VERSION 3.25) | ||
|
|
||
| set(CMAKE_CXX_STANDARD 20) | ||
|
|
||
| set(CUBOOL_GRAPH_LIB_NAME cuboolgraph) | ||
| project(${CUBOOL_GRAPH_LIB_NAME} LANGUAGES CXX) | ||
|
|
||
| add_library(${CUBOOL_GRAPH_LIB_NAME} SHARED "") | ||
|
|
||
| set(CUBOOL_COPY_TO_PY_PACKAGE OFF) | ||
| set(CUBOOL_BUILD_TESTS OFF) | ||
| add_subdirectory(deps/cuBool) | ||
|
|
||
| # cubool is a name of CMakeTarget cmake target | ||
| target_link_libraries(${CUBOOL_GRAPH_LIB_NAME} PUBLIC cubool) | ||
|
|
||
| target_include_directories(${CUBOOL_GRAPH_LIB_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include) | ||
|
|
||
| add_subdirectory(src) | ||
|
|
||
| option(CUBOOL_GRAPH_ENABLE_TESTING "Compile tests for algorithms" OFF) | ||
|
|
||
| if(CUBOOL_GRAPH_ENABLE_TESTING) | ||
| add_subdirectory(deps/googletest) | ||
| add_subdirectory(deps/fast_matrix_market) | ||
| add_subdirectory(tests) | ||
| endif() | ||
|
|
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,18 @@ | ||
| # cuBoolGraph | ||
| cuBool based graph analysis algorithms | ||
|
|
||
| ## List of algorithms | ||
|
|
||
| regular_path_query - Single-Source Regular Path Query Algorithm in Terms of Linear Algebra | ||
|
|
||
| # Run tests | ||
mitya-y marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| cuBoolGraph supports Linux-based OS (tested on Ubuntu 24.04 and Manjaro 6.19). | ||
| For building project are required gcc version 14 and later, CMake with version 3.25 and later, CUDA developing toolkint version 12.0 and later. | ||
|
|
||
| For build and run tests execute commands | ||
| ``` | ||
| git submodule update --init --recursive | ||
| cmake -B build -DCUBOOL_GRAPH_ENABLE_TESTING=ON | ||
| cmake --build build | ||
| ./build/tests/cuboolgraph_tests | ||
| ``` | ||
Submodule fast_matrix_market
added at
b6172c
Submodule googletest
added at
35b75a
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| #include <vector> | ||
|
|
||
| #include <cubool/cubool.h> | ||
|
|
||
| cuBool_Matrix regular_path_query_with_transposed( | ||
| // vector of sparse graph matrices for each label | ||
| const std::vector<cuBool_Matrix> &graph, const std::vector<cuBool_Index> &source_vertices, | ||
| // vector of sparse automaton matrices for each label | ||
| const std::vector<cuBool_Matrix> &automaton, const std::vector<cuBool_Index> &start_states, | ||
| // transposed matrices for graph and automaton | ||
| const std::vector<cuBool_Matrix> &graph_transposed, | ||
| const std::vector<cuBool_Matrix> &automaton_transposed, | ||
|
|
||
| const std::vector<bool> &inversed_labels = {}, bool all_labels_are_inversed = false); | ||
|
|
||
| cuBool_Matrix regular_path_query( | ||
| // vector of sparse graph matrices for each label | ||
| const std::vector<cuBool_Matrix> &graph, const std::vector<cuBool_Index> &source_vertices, | ||
| // vector of sparse automaton matrices for each label | ||
| const std::vector<cuBool_Matrix> &automaton, const std::vector<cuBool_Index> &start_states, | ||
|
|
||
| const std::vector<bool> &inversed_labels = {}, bool all_labels_are_inversed = false); |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| target_sources(${CUBOOL_GRAPH_LIB_NAME} PRIVATE regular_path_query.cpp) |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,193 @@ | ||
| #include <cassert> | ||
| #include <cstdint> | ||
| #include <iostream> | ||
| #include <print> | ||
| #include <set> | ||
|
|
||
| #include "regular_path_query.hpp" | ||
|
|
||
| cuBool_Matrix regular_path_query_with_transposed( | ||
| // vector of sparse graph matrices for each label | ||
| const std::vector<cuBool_Matrix> &graph, const std::vector<cuBool_Index> &source_vertices, | ||
| // vector of sparse automaton matrices for each label | ||
| const std::vector<cuBool_Matrix> &automaton, const std::vector<cuBool_Index> &start_states, | ||
| // transposed matrices for graph and automaton | ||
| const std::vector<cuBool_Matrix> &graph_transposed, | ||
| const std::vector<cuBool_Matrix> &automaton_transposed, | ||
|
|
||
| const std::vector<bool> &inversed_labels_input, bool all_labels_are_inversed) { | ||
| cuBool_Status status; | ||
|
|
||
| auto inversed_labels = inversed_labels_input; | ||
| inversed_labels.resize(std::max(graph.size(), automaton.size())); | ||
|
|
||
| for (uint32_t i = 0; i < inversed_labels.size(); i++) { | ||
| bool is_inverse = inversed_labels[i]; | ||
| is_inverse ^= all_labels_are_inversed; | ||
| inversed_labels[i] = is_inverse; | ||
| } | ||
|
|
||
| cuBool_Index graph_nodes_number = 0; | ||
| cuBool_Index automaton_nodes_number = 0; | ||
|
|
||
| // get number of graph nodes | ||
| for (auto label_matrix : graph) { | ||
| if (label_matrix != nullptr) { | ||
| cuBool_Matrix_Nrows(label_matrix, &graph_nodes_number); | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| // get number of automaton nodes | ||
| for (auto label_matrix : automaton) { | ||
| if (label_matrix != nullptr) { | ||
| cuBool_Matrix_Nrows(label_matrix, &automaton_nodes_number); | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| // this will be answer | ||
| cuBool_Matrix reacheble {}; | ||
| status = cuBool_Matrix_New(&reacheble, automaton_nodes_number, graph_nodes_number); | ||
| assert(status == CUBOOL_STATUS_SUCCESS); | ||
|
|
||
| // allocate neccessary for algorithm matrices | ||
| cuBool_Matrix frontier {}, symbol_frontier {}, next_frontier {}; | ||
| status = cuBool_Matrix_New(&next_frontier, automaton_nodes_number, graph_nodes_number); | ||
| assert(status == CUBOOL_STATUS_SUCCESS); | ||
| status = cuBool_Matrix_New(&frontier, automaton_nodes_number, graph_nodes_number); | ||
| assert(status == CUBOOL_STATUS_SUCCESS); | ||
| status = cuBool_Matrix_New(&symbol_frontier, automaton_nodes_number, graph_nodes_number); | ||
| assert(status == CUBOOL_STATUS_SUCCESS); | ||
|
|
||
| // init start values of algorithm matricies | ||
| for (const auto state : start_states) { | ||
| for (const auto vert : source_vertices) { | ||
| assert(state < automaton_nodes_number); | ||
| assert(vert < graph_nodes_number); | ||
| cuBool_Matrix_SetElement(next_frontier, state, vert); | ||
| cuBool_Matrix_SetElement(reacheble, state, vert); | ||
| } | ||
| } | ||
|
|
||
| cuBool_Index states = source_vertices.size(); | ||
|
|
||
| // temporary matrix for write result of cubool functions | ||
| cuBool_Matrix result; | ||
| status = cuBool_Matrix_New(&result, automaton_nodes_number, graph_nodes_number); | ||
| assert(status == CUBOOL_STATUS_SUCCESS); | ||
|
|
||
| const auto label_number = std::min(graph.size(), automaton.size()); | ||
| while (states > 0) { | ||
| std::swap(frontier, next_frontier); | ||
|
|
||
| // clear next_frontier | ||
| status = cuBool_Matrix_Build(next_frontier, nullptr, nullptr, 0, CUBOOL_HINT_NO); | ||
| assert(status == CUBOOL_STATUS_SUCCESS); | ||
|
|
||
| for (int i = 0; i < label_number; i++) { | ||
| if (graph[i] == nullptr || automaton[i] == nullptr) { | ||
| continue; | ||
| } | ||
|
|
||
| cuBool_Matrix automaton_matrix = all_labels_are_inversed ? automaton[i] : automaton_transposed[i]; | ||
| status = cuBool_MxM(symbol_frontier, automaton_matrix, frontier, CUBOOL_HINT_NO); | ||
| assert(status == CUBOOL_STATUS_SUCCESS); | ||
|
|
||
| // next_frontier += (symbol_frontier * graph[i]) & (!reachible) | ||
| // multiply 2 matrices | ||
| cuBool_Matrix graph_matrix = inversed_labels[i] ? graph_transposed[i] : graph[i]; | ||
| status = cuBool_MxM(next_frontier, symbol_frontier, graph_matrix, CUBOOL_HINT_ACCUMULATE); | ||
| assert(status == CUBOOL_STATUS_SUCCESS); | ||
| // apply invert mask | ||
| status = cuBool_Matrix_EWiseMulInverted(result, next_frontier, reacheble, CUBOOL_HINT_NO); | ||
| assert(status == CUBOOL_STATUS_SUCCESS); | ||
| std::swap(result, next_frontier); | ||
| } | ||
|
|
||
| // this must be accumulate with mask and save old value: reacheble += next_frontier & reacheble | ||
| status = cuBool_Matrix_EWiseAdd(result, reacheble, next_frontier, CUBOOL_HINT_NO); | ||
| assert(status == CUBOOL_STATUS_SUCCESS); | ||
| std::swap(result, reacheble); | ||
|
|
||
| cuBool_Matrix_Nvals(next_frontier, &states); | ||
| } | ||
|
|
||
| // free matrix necessary for algorithm | ||
| cuBool_Matrix_Free(next_frontier); | ||
| cuBool_Matrix_Free(frontier); | ||
| cuBool_Matrix_Free(symbol_frontier); | ||
| cuBool_Matrix_Free(result); | ||
|
|
||
| return reacheble; | ||
| } | ||
|
|
||
|
|
||
| cuBool_Matrix regular_path_query( | ||
| // vector of sparse graph matrices for each label | ||
| const std::vector<cuBool_Matrix> &graph, const std::vector<cuBool_Index> &source_vertices, | ||
| // vector of sparse automaton matrices for each label | ||
| const std::vector<cuBool_Matrix> &automaton, const std::vector<cuBool_Index> &start_states, | ||
| // work with inverted labels | ||
| const std::vector<bool> &inversed_labels_input, bool all_labels_are_inversed) { | ||
| cuBool_Status status; | ||
|
|
||
| // transpose graph matrices | ||
| std::vector<cuBool_Matrix> graph_transposed; | ||
| graph_transposed.reserve(graph.size()); | ||
| for (uint32_t i = 0; i < graph.size(); i++) { | ||
| graph_transposed.emplace_back(); | ||
|
|
||
| auto label_matrix = graph[i]; | ||
| if (label_matrix == nullptr) { | ||
| continue; | ||
| } | ||
|
|
||
| cuBool_Index nrows, ncols; | ||
| cuBool_Matrix_Nrows(label_matrix, &nrows); | ||
| cuBool_Matrix_Ncols(label_matrix, &ncols); | ||
|
|
||
| status = cuBool_Matrix_New(&graph_transposed.back(), ncols, nrows); | ||
| assert(status == CUBOOL_STATUS_SUCCESS); | ||
| status = cuBool_Matrix_Transpose(graph_transposed.back(), label_matrix, CUBOOL_HINT_NO); | ||
| assert(status == CUBOOL_STATUS_SUCCESS); | ||
| } | ||
|
|
||
| // transpose automaton matrices | ||
| std::vector<cuBool_Matrix> automaton_transposed; | ||
| automaton_transposed.reserve(automaton.size()); | ||
| for (auto label_matrix : automaton) { | ||
| automaton_transposed.emplace_back(); | ||
| if (label_matrix == nullptr) { | ||
| continue; | ||
| } | ||
|
|
||
| cuBool_Index nrows, ncols; | ||
| cuBool_Matrix_Nrows(label_matrix, &nrows); | ||
| cuBool_Matrix_Ncols(label_matrix, &ncols); | ||
|
|
||
| status = cuBool_Matrix_New(&automaton_transposed.back(), ncols, nrows); | ||
| assert(status == CUBOOL_STATUS_SUCCESS); | ||
| status = cuBool_Matrix_Transpose(automaton_transposed.back(), label_matrix, CUBOOL_HINT_NO); | ||
| assert(status == CUBOOL_STATUS_SUCCESS); | ||
| } | ||
|
|
||
| auto result = regular_path_query_with_transposed( | ||
| graph, source_vertices, | ||
| automaton, start_states, | ||
| graph_transposed, automaton_transposed, | ||
| inversed_labels_input, all_labels_are_inversed); | ||
|
|
||
| for (cuBool_Matrix matrix : graph_transposed) { | ||
| if (matrix != nullptr) { | ||
| cuBool_Matrix_Free(matrix); | ||
| } | ||
| } | ||
| for (cuBool_Matrix matrix : automaton_transposed) { | ||
| if (matrix != nullptr) { | ||
| cuBool_Matrix_Free(matrix); | ||
| } | ||
| } | ||
|
|
||
| return result; | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| set(TESTS_TARGET cuboolgraph_tests) | ||
|
|
||
| add_executable(${TESTS_TARGET} "main.cpp") | ||
|
|
||
| target_link_libraries(${TESTS_TARGET} PUBLIC ${CUBOOL_GRAPH_LIB_NAME}) | ||
| target_link_libraries(${TESTS_TARGET} PUBLIC gtest) | ||
| target_link_libraries(${TESTS_TARGET} PUBLIC fast_matrix_market) | ||
|
|
||
| target_compile_definitions(${TESTS_TARGET} PUBLIC | ||
| TESTS_DATA_PATH="${CMAKE_CURRENT_SOURCE_DIR}/test_data/") | ||
|
|
||
| # tests files | ||
| target_sources(${TESTS_TARGET} PUBLIC "rpq_tests.cpp") |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| #include <gtest/gtest.h> | ||
|
|
||
| int main(int argc, char **argv) { | ||
| ::testing::InitGoogleTest(&argc, argv); | ||
| return RUN_ALL_TESTS(); | ||
| } |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.