Skip to content

Commit

Permalink
reduce mutability where possible -> fixed!
Browse files Browse the repository at this point in the history
  • Loading branch information
N-Maas committed Oct 10, 2024
1 parent b5082e0 commit 1d7973e
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 22 deletions.
25 changes: 14 additions & 11 deletions mt-kahypar/partition/evo_partitioner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace mt_kahypar {

template<typename TypeTraits>
typename EvoPartitioner<TypeTraits>::PartitionedHypergraph EvoPartitioner<TypeTraits>::partition(
Hypergraph& hypergraph, Context& context, TargetGraph* target_graph) {
const Hypergraph& hypergraph, Context& context, TargetGraph* target_graph) {
configurePreprocessing(hypergraph, context);
setupContext(hypergraph, context, target_graph);

Expand Down Expand Up @@ -80,7 +80,7 @@ namespace mt_kahypar {
out_stream.close();
}

PartitionedHypergraph partitioned_hypergraph(context.partition.k, hypergraph);
PartitionedHypergraph partitioned_hypergraph(context.partition.k, const_cast<Hypergraph&>(hypergraph));

std::vector<PartitionID> best = best_population.individualAt(best_population.best()).partition();
partitioned_hypergraph.doParallelForAllNodes([&](const HypernodeID& hn) {
Expand Down Expand Up @@ -132,7 +132,7 @@ namespace mt_kahypar {


template<typename TypeTraits>
std::string EvoPartitioner<TypeTraits>::generateInitialPopulation(EvoPartitioner<TypeTraits>::Hypergraph& hg, Context& context, TargetGraph* target_graph, Population& population) {
std::string EvoPartitioner<TypeTraits>::generateInitialPopulation(const Hypergraph& hg, Context& context, TargetGraph* target_graph, Population& population) {
context.partition.verbose_output = false;
int timelimit = context.partition.time_limit;
utils::Timer& timer = utils::Utilities::instance().getTimer(context.utility_id);
Expand Down Expand Up @@ -181,7 +181,8 @@ namespace mt_kahypar {
}

template<typename TypeTraits>
const Individual & EvoPartitioner<TypeTraits>::generateIndividual(EvoPartitioner<TypeTraits>::Hypergraph& hypergraph, Context& context, TargetGraph* target_graph, Population& population) {
const Individual & EvoPartitioner<TypeTraits>::generateIndividual(const Hypergraph& input_hg, Context& context, TargetGraph* target_graph, Population& population) {
Hypergraph hypergraph = input_hg.copy(parallel_tag_t{});
DegreeZeroHypernodeRemover<TypeTraits> degree_zero_hn_remover(context);
LargeHyperedgeRemover<TypeTraits> large_he_remover(context);
preprocess(hypergraph, context, target_graph);
Expand Down Expand Up @@ -225,7 +226,7 @@ namespace mt_kahypar {


template<typename TypeTraits>
vec<PartitionID> EvoPartitioner<TypeTraits>::combinePartitions(const Context& context, Population& population, std::vector<size_t> ids) {
vec<PartitionID> EvoPartitioner<TypeTraits>::combinePartitions(const Context& context, Population& population, const std::vector<size_t>& ids) {
vec<PartitionID> combined(population.individualAt(0).partition().size());

std::unordered_map<std::string, int> tuple_to_block;
Expand All @@ -248,7 +249,7 @@ namespace mt_kahypar {
}

template<typename TypeTraits>
std::string EvoPartitioner<TypeTraits>::performCombine(EvoPartitioner<TypeTraits>::Hypergraph& hypergraph, const Context& context, TargetGraph* target_graph, Population& population) {
std::string EvoPartitioner<TypeTraits>::performCombine(const Hypergraph& input_hg, const Context& context, TargetGraph* target_graph, Population& population) {
std::vector<size_t> parents;
size_t best = population.randomIndividual();
parents.push_back(best);
Expand All @@ -261,8 +262,9 @@ namespace mt_kahypar {
}
std::unordered_map<PartitionID, int> comm_to_block;
vec<PartitionID> comms = combinePartitions(context, population, parents);
vec<PartitionID> part(hypergraph.initialNumNodes());

vec<PartitionID> part(input_hg.initialNumNodes());

Hypergraph hypergraph = input_hg.copy(parallel_tag_t{});
PartitionedHypergraph partitioned_hypergraph(context.partition.k, hypergraph);
for ( const HypernodeID& hn : hypergraph.nodes() ) {
partitioned_hypergraph.setOnlyNodePart(hn, population.individualAt(best).partition()[hn]);
Expand Down Expand Up @@ -291,7 +293,8 @@ namespace mt_kahypar {
}

template<typename TypeTraits>
std::string EvoPartitioner<TypeTraits>::performMutation(EvoPartitioner<TypeTraits>::Hypergraph& hypergraph, const Context& context, TargetGraph* target_graph, Population& population) {
std::string EvoPartitioner<TypeTraits>::performMutation(const Hypergraph& input_hg, const Context& context, TargetGraph* target_graph, Population& population) {
Hypergraph hypergraph = input_hg.copy(parallel_tag_t{});
const size_t mutation_position = population.randomIndividual();
std::vector<PartitionID> cur = population.individualAt(mutation_position).partition();
PartitionedHypergraph partitioned_hypergraph(context.partition.k, hypergraph);
Expand Down Expand Up @@ -362,7 +365,7 @@ namespace mt_kahypar {
}

template<typename TypeTraits>
std::string EvoPartitioner<TypeTraits>::performEvolution(EvoPartitioner<TypeTraits>::Hypergraph& hg, Context& context, TargetGraph* target_graph, Population& population) {
std::string EvoPartitioner<TypeTraits>::performEvolution(const Hypergraph& hg, Context& context, TargetGraph* target_graph, Population& population) {
context.partition.verbose_output = false;
int timelimit = context.partition.time_limit;
utils::Timer& timer = utils::Utilities::instance().getTimer(context.utility_id);
Expand Down Expand Up @@ -397,9 +400,9 @@ namespace mt_kahypar {
}
case EvoDecision::combine:
{
std::lock_guard<std::mutex> lock(_history_mutex);
std::string h = performCombine(hg_copy, evo_context, target_graph, population);
combinations++;
std::lock_guard<std::mutex> lock(_history_mutex);
history += h;
++context.evolutionary.iteration;
break;
Expand Down
14 changes: 7 additions & 7 deletions mt-kahypar/partition/evo_partitioner.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,28 @@ class EvoPartitioner {
using PartitionedHypergraph = typename TypeTraits::PartitionedHypergraph;

public:
static PartitionedHypergraph partition(Hypergraph& hypergraph,
static PartitionedHypergraph partition(const Hypergraph& hypergraph,
Context& context,
TargetGraph* target_graph = nullptr);
static std::string generateInitialPopulation(EvoPartitioner<TypeTraits>::Hypergraph& hg,
static std::string generateInitialPopulation(const Hypergraph& hg,
Context& context,
TargetGraph* target_graph,
Population& population);
static const Individual & generateIndividual(EvoPartitioner<TypeTraits>::Hypergraph& hg,
static const Individual & generateIndividual(const Hypergraph& hg,
Context& context,
TargetGraph* target_graph,
Population& population);
static std::string performEvolution(EvoPartitioner<TypeTraits>::Hypergraph& hg,
static std::string performEvolution(const Hypergraph& hg,
Context& context,
TargetGraph* target_graph,
Population& population);

private:
static EvoDecision decideNextMove(const Context& context);
static EvoMutateStrategy decideNextMutation(const Context& context);
static vec<PartitionID> combinePartitions(const Context& context, Population& population, std::vector<size_t> ids);
static std::string performCombine(EvoPartitioner<TypeTraits>::Hypergraph& hg, const Context& context, TargetGraph* target_graph, Population& population);
static std::string performMutation(EvoPartitioner<TypeTraits>::Hypergraph& hg, const Context& context, TargetGraph* target_graph, Population& population);
static vec<PartitionID> combinePartitions(const Context& context, Population& population, const std::vector<size_t>& ids);
static std::string performCombine(const Hypergraph& hg, const Context& context, TargetGraph* target_graph, Population& population);
static std::string performMutation(const Hypergraph& hg, const Context& context, TargetGraph* target_graph, Population& population);
};

} // namespace mt_kahypar
6 changes: 3 additions & 3 deletions mt-kahypar/partition/multilevel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ namespace {
const Context& context,
const TargetGraph* target_graph,
const bool is_vcycle,
std::unordered_map<PartitionID, int> comm_to_block) {
const std::unordered_map<PartitionID, int>& comm_to_block) {
//disableTimerAndStats(context);
using Hypergraph = typename TypeTraits::Hypergraph;
using PartitionedHypergraph = typename TypeTraits::PartitionedHypergraph;
Expand Down Expand Up @@ -246,7 +246,7 @@ namespace {
// of the input hypergraph as community IDs
const Hypergraph& hypergraph = phg.hypergraph();
phg.doParallelForAllNodes([&](const HypernodeID hn) {
const PartitionID part_id = comm_to_block[hypergraph.communityID(hn)];
const PartitionID part_id = comm_to_block.at(hypergraph.communityID(hn));
ASSERT(part_id != kInvalidPartition && part_id < context.partition.k);
ASSERT(phg.partID(hn) == kInvalidPartition);
phg.setOnlyNodePart(hn, part_id);
Expand Down Expand Up @@ -371,7 +371,7 @@ template<typename TypeTraits>
void Multilevel<TypeTraits>::evolutionPartitionVCycle(Hypergraph& hypergraph,
PartitionedHypergraph& partitioned_hg,
const Context& context,
std::unordered_map<PartitionID, int> comm_to_block,
const std::unordered_map<PartitionID, int>& comm_to_block,
const TargetGraph* target_graph) {

//disableTimerAndStats(context);
Expand Down
2 changes: 1 addition & 1 deletion mt-kahypar/partition/multilevel.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class Multilevel {
static void evolutionPartitionVCycle(Hypergraph& hypergraph,
PartitionedHypergraph& partitioned_hg,
const Context& context,
std::unordered_map<PartitionID, int> comm_to_block,
const std::unordered_map<PartitionID, int>& comm_to_block,
const TargetGraph* target_graph = nullptr);
};

Expand Down

0 comments on commit 1d7973e

Please sign in to comment.