Skip to content

Commit

Permalink
implement threshold smapling with ips4o sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
Dominik Rosch committed Jan 9, 2025
1 parent 567e19f commit a34124d
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 17 deletions.
5 changes: 4 additions & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,7 @@
url = https://github.com/kamping-site/bakward-mpi.git
[submodule "networkit"]
path = networkit
url = https://github.com/networkit/networkit
url = https://github.com/networkit/networkit
[submodule "ips4o"]
path = ips4o
url = https://github.com/ips4o/ips4o.git
5 changes: 4 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,10 @@ include_directories(${PROJECT_SOURCE_DIR})
include_directories(${PROJECT_SOURCE_DIR}/networkit/include ${PROJECT_SOURCE_DIR}/networkit/extlibs/tlx ${PROJECT_SOURCE_DIR}/networkit/extlibs/ttmath ${JL_SHARE}/../../include/julia/)
add_subdirectory(networkit)

# ips4o
add_subdirectory(ips4o)
add_definitions(-D_REENTRANT)


# Shared memory components
add_subdirectory(kaminpar-common)
Expand Down Expand Up @@ -435,4 +439,3 @@ if (KAMINPAR_BUILD_DISTRIBUTED)
endif ()



1 change: 1 addition & 0 deletions ips4o
Submodule ips4o added at 9ddfa6
2 changes: 1 addition & 1 deletion kaminpar-common/datastructures/static_array.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ template <typename T> class StaticArray {
public:
template <bool is_const> class StaticArrayIterator {
public:
using iterator_category = std::contiguous_iterator_tag;
using iterator_category = std::random_access_iterator_tag;
using value_type = T;
using reference = std::conditional_t<is_const, const T &, T &>;
using pointer = std::conditional_t<is_const, const T *, T *>;
Expand Down
2 changes: 1 addition & 1 deletion kaminpar-shm/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ add_library(kaminpar_shm ${KAMINPAR_SHM_SOURCE_FILES}
coarsening/sparsification/WeightedForestFireScore.h
coarsening/sparsification/IndependentRandomSampler.cpp)
target_include_directories(kaminpar_shm PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/../" ${JL_SHARE}/../../include/julia/)
target_link_libraries(kaminpar_shm PUBLIC kaminpar_common networkit ${JL_SHARE}/../../lib/libjulia.so)
target_link_libraries(kaminpar_shm PUBLIC kaminpar_common networkit ${JL_SHARE}/../../lib/libjulia.so ips4o)
target_compile_options(kaminpar_shm PRIVATE ${KAMINPAR_WARNING_FLAGS})

if (Sparsehash_FOUND)
Expand Down
32 changes: 19 additions & 13 deletions kaminpar-shm/coarsening/sparsification/ThresholdSampler.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#pragma once
#include <oneapi/tbb/parallel_sort.h>
#include <utils/output.hpp>

#include "Sampler.h"
#include "ScoreBacedSampler.h"
#include "ips4o.hpp"
#include "sparsification_utils.h"

#include "kaminpar-common/timer.h"
Expand All @@ -25,12 +27,12 @@ template <typename Score> class ThresholdSampler : public ScoreBacedSampler<Scor

utils::K_SmallestInfo<Score> threshold;
{
SCOPED_TIMER("Find Threshold with qselect");
SCOPED_TIMER("Find Threshold with iso4o sort");
threshold =
utils::quickselect_k_smallest<Score>(target_edge_amount, scores.begin(), scores.end());
find_threshold(scores,target_edge_amount);
}

double inclusion_probaility_if_equal = (target_edge_amount / 2 - threshold.number_of_elements_smaller) / threshold.number_of_elemtns_equal;
double inclusion_probaility_if_equal = (target_edge_amount / 2 - threshold.number_of_elements_smaller) / static_cast<double>(threshold.number_of_elemtns_equal);
utils::parallel_for_upward_edges(g, [&](EdgeID e) {
if (scores[e] < threshold.value || (scores[e] == threshold.value && Random::instance().random_bool(inclusion_probaility_if_equal))) {
sample[e] = g.edge_weight(e);
Expand All @@ -41,20 +43,24 @@ template <typename Score> class ThresholdSampler : public ScoreBacedSampler<Scor
}

private:
std::pair<EdgeID, EdgeID>
utils::K_SmallestInfo<Score>
find_threshold(const StaticArray<Score> &scores, EdgeID target_edge_amount) {
SCOPED_TIMER("Find Threshold");
std::vector<Score> sorted_scores(scores.size());
tbb::parallel_for(0ul, scores.size(), [&](auto e) { sorted_scores[e] = scores[e]; });
tbb::parallel_sort(sorted_scores.begin(), sorted_scores.end());
utils::K_SmallestInfo<Score> output;
StaticArray<Score> sorted_scores(scores.begin(), scores.end());
ips4o::parallel::sort(sorted_scores.begin(), sorted_scores.end());

EdgeID indexOfThreshold = sorted_scores.size() - target_edge_amount;
Score threshold = sorted_scores[indexOfThreshold];
EdgeID indexOfFirstLagerScore =
std::upper_bound(sorted_scores.begin(), sorted_scores.end(), threshold) -
output.value = sorted_scores[indexOfThreshold];
EdgeID indexOfFirstLargerScore =
std::upper_bound(sorted_scores.begin(), sorted_scores.end(), output.value) -
sorted_scores.begin();
EdgeID numEdgesAtThresholdScoreToInclude = indexOfFirstLagerScore - indexOfThreshold / 2;
return std::make_pair(threshold, numEdgesAtThresholdScoreToInclude);
EdgeID indexOfFirstEqualScore =
std::lower_bound(sorted_scores.begin(), sorted_scores.end(), output.value) -
sorted_scores.begin();
EdgeID numEdgesAtThresholdScoreToInclude = (indexOfFirstLargerScore - indexOfThreshold) / 2;
output.number_of_elemtns_equal = (indexOfFirstLargerScore - indexOfFirstEqualScore)/2;
output.number_of_elements_smaller = indexOfFirstEqualScore/2;
return output;
};
};
} // namespace kaminpar::shm::sparsification

0 comments on commit a34124d

Please sign in to comment.