Skip to content

Commit

Permalink
Merge pull request #3 from velocitatem/parallelize
Browse files Browse the repository at this point in the history
Introduced HPC into relevant parts
  • Loading branch information
velocitatem authored Jan 13, 2025
2 parents 72e2644 + 221f831 commit 76289e2
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
6 changes: 6 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ add_library(symphony SHARED

target_include_directories(symphony PUBLIC include)

# add openmp
find_package(OpenMP)
if(OpenMP_CXX_FOUND)
target_link_libraries(symphony PUBLIC OpenMP::OpenMP_CXX)
endif()

enable_testing()

add_subdirectory(tests)
Expand Down
2 changes: 2 additions & 0 deletions src/search.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <algorithm>
#include <vector>
#include <functional>
#include <omp.h>


Search *create_search(SearchAlgorithmIndex search_algorithm_index, Problem *problem) {
Expand Down Expand Up @@ -60,6 +61,7 @@ std::shared_ptr<Node> BreadthFirstSearch::search() {
if (problem->goal_test(state)) {
return node;
}
#pragma omp parallel for shared(frontier) schedule(dynamic) if (frontier.size() > 1000)
for (const auto &action : problem->actions(node->state)) {
auto child = std::make_shared<Node>(
std::shared_ptr<Node>(node),
Expand Down
17 changes: 12 additions & 5 deletions tests/tests.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <gtest/gtest.h>
#include "definitions.h"
#include "search.h"
#include <omp.h>

class TestState : public State {
public:
Expand All @@ -18,7 +19,7 @@ class TestProblem : public Problem {
}
bool goal_test(State *state) override {
auto *test_state = dynamic_cast<TestState *>(state);
return test_state->value == 10;
return test_state->value == 10010;
}
std::vector<std::shared_ptr<Action>> actions(std::shared_ptr<State> state) override {
auto test_state = std::dynamic_pointer_cast<TestState>(state);
Expand All @@ -28,7 +29,7 @@ class TestProblem : public Problem {
}
double heuristic(State *state) override {
auto *test_state = dynamic_cast<TestState *>(state);
return 10 - test_state->value;
return 10010 - test_state->value;
}
};

Expand All @@ -40,18 +41,24 @@ TEST(Definitions, State) {
TEST(Search, AStarSearch) {
TestProblem problem;
Search *search = create_search(SearchAlgorithmIndex::A_STAR, &problem);
double start = omp_get_wtime();
std::shared_ptr<Node> node = search->search();
double end = omp_get_wtime();
std::cout << "Time: " << end - start << std::endl;
ASSERT_NE(node, nullptr);
EXPECT_EQ(dynamic_cast<TestState *>(node->state.get())->value, 10);
EXPECT_EQ(dynamic_cast<TestState *>(node->state.get())->value, 10010);
delete search;
}

TEST(Search, BreadthFirstSearch) {
TestProblem problem;
Search *search = create_search(SearchAlgorithmIndex::BREADTH_FIRST_SEARCH, &problem);
double start = omp_get_wtime();
std::shared_ptr<Node> node = search->search();
double end = omp_get_wtime();
std::cout << "Time: " << end - start << std::endl;
ASSERT_NE(node, nullptr);
EXPECT_EQ(dynamic_cast<TestState *>(node->state.get())->value, 10);
EXPECT_EQ(dynamic_cast<TestState *>(node->state.get())->value, 10010);
delete search;
}

Expand All @@ -60,7 +67,7 @@ TEST(Search, BeamSearch) {
Search *search = create_search(SearchAlgorithmIndex::BEAM_SEARCH, &problem);
std::shared_ptr<Node> node = search->search();
ASSERT_NE(node, nullptr);
EXPECT_EQ(dynamic_cast<TestState *>(node->state.get())->value, 10);
EXPECT_EQ(dynamic_cast<TestState *>(node->state.get())->value, 10010);
delete search;
}

Expand Down

0 comments on commit 76289e2

Please sign in to comment.