Skip to content

Commit

Permalink
[Library] Remove seed parameter and add {d}KaMinPar::reseed(seed) ins…
Browse files Browse the repository at this point in the history
…tead
  • Loading branch information
DanielSeemaier committed Dec 19, 2023
1 parent 8e7850a commit 5269589
Show file tree
Hide file tree
Showing 12 changed files with 67 additions and 36 deletions.
3 changes: 2 additions & 1 deletion apps/KaMinPar.cc
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ int main(int argc, char *argv[]) {

// Compute graph partition
KaMinPar partitioner(app.num_threads, ctx);
KaMinPar::reseed(app.seed);

if (app.quiet) {
partitioner.set_output_level(OutputLevel::QUIET);
Expand All @@ -166,7 +167,7 @@ int main(int argc, char *argv[]) {
partitioner.set_max_timer_depth(app.max_timer_depth);
partitioner.take_graph(n, xadj_ptr, adjncy_ptr, vwgt_ptr, adjwgt_ptr);

partitioner.compute_partition(app.seed, app.k, partition.data());
partitioner.compute_partition(app.k, partition.data());

// Save graph partition
if (!app.partition_filename.empty()) {
Expand Down
3 changes: 2 additions & 1 deletion apps/dKaMinPar.cc
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ int main(int argc, char *argv[]) {
}

dKaMinPar partitioner(MPI_COMM_WORLD, app.num_threads, ctx);
dKaMinPar::reseed(app.seed);

if (app.quiet) {
partitioner.set_output_level(OutputLevel::QUIET);
Expand All @@ -208,7 +209,7 @@ int main(int argc, char *argv[]) {

// Compute the partition
std::vector<BlockID> partition(n);
partitioner.compute_partition(app.seed, app.k, partition.data());
partitioner.compute_partition(app.k, partition.data());

if (!app.partition_filename.empty()) {
dist::io::partition::write(app.partition_filename, partition);
Expand Down
2 changes: 1 addition & 1 deletion kaminpar-common/logger.cc
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ CompactContainerFormatter COMPACT{","sv};
Table TABLE{0};
} // namespace logger

bool Logger::_quiet = false;
std::atomic<std::uint8_t> Logger::_quiet = 0;

Logger::Logger() : Logger(std::cout) {}
Logger::Logger(std::ostream &out, std::string append)
Expand Down
3 changes: 2 additions & 1 deletion kaminpar-common/logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#pragma once

#include <algorithm>
#include <atomic>
#include <cerrno>
#include <cmath>
#include <csignal>
Expand Down Expand Up @@ -256,7 +257,7 @@ class Logger {
static bool is_quiet();

private:
static bool _quiet;
static std::atomic<std::uint8_t> _quiet;

static tbb::spin_mutex &flush_mutex();

Expand Down
2 changes: 1 addition & 1 deletion kaminpar-common/random.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Random &Random::create_instance() {
return *_instances.back();
}

void Random::seed(const int seed) {
void Random::reseed(const int seed) {
_seed = seed;
for (auto &instance : _instances) {
instance->reinit(seed);
Expand Down
3 changes: 2 additions & 1 deletion kaminpar-common/random.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ class Random {
Random();

static Random &instance();
static void seed(int seed);
static void set_seed(int seed);
static void reseed(int seed);
static int get_seed();

Random(const Random &) = delete;
Expand Down
9 changes: 6 additions & 3 deletions kaminpar-dist/dkaminpar.cc
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,10 @@ dKaMinPar::dKaMinPar(MPI_Comm comm, const int num_threads, const Context ctx)

dKaMinPar::~dKaMinPar() = default;

void dKaMinPar::reseed(const int seed) {
Random::reseed(seed);
}

void dKaMinPar::set_output_level(const OutputLevel output_level) {
_output_level = output_level;
}
Expand Down Expand Up @@ -254,7 +258,7 @@ void dKaMinPar::import_graph(
}
}

GlobalEdgeWeight dKaMinPar::compute_partition(const int seed, const BlockID k, BlockID *partition) {
GlobalEdgeWeight dKaMinPar::compute_partition(const BlockID k, BlockID *partition) {
DistributedGraph &graph = *_graph_ptr;

const PEID size = mpi::get_comm_size(_comm);
Expand All @@ -274,8 +278,7 @@ GlobalEdgeWeight dKaMinPar::compute_partition(const int seed, const BlockID k, B
_ctx.partition.k = k;
_ctx.partition.graph = std::make_unique<GraphContext>(graph, _ctx.partition);

// Initialize PRNG and console output
Random::seed(seed);
// Initialize console output
Logger::set_quiet_mode(_output_level == OutputLevel::QUIET);
if (_output_level >= OutputLevel::APPLICATION) {
print_input_summary(_ctx, graph, _output_level == OutputLevel::EXPERIMENT, root);
Expand Down
5 changes: 4 additions & 1 deletion kaminpar-dist/dkaminpar.h
Original file line number Diff line number Diff line change
Expand Up @@ -377,8 +377,11 @@ namespace kaminpar {
class dKaMinPar {
public:
dKaMinPar(MPI_Comm comm, int num_threads, dist::Context ctx);

~dKaMinPar();

static void reseed(int seed);

void set_output_level(OutputLevel output_level);

void set_max_timer_depth(int max_timer_depth);
Expand All @@ -393,7 +396,7 @@ class dKaMinPar {
dist::GlobalEdgeWeight *edge_weights
);

dist::GlobalEdgeWeight compute_partition(int seed, dist::BlockID k, dist::BlockID *partition);
dist::GlobalEdgeWeight compute_partition(dist::BlockID k, dist::BlockID *partition);

private:
MPI_Comm _comm;
Expand Down
9 changes: 6 additions & 3 deletions kaminpar-shm/kaminpar.cc
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,11 @@ void KaMinPar::copy_graph(
);
}

EdgeWeight KaMinPar::compute_partition(const int seed, const BlockID k, BlockID *partition) {
void KaMinPar::reseed(int seed) {
Random::reseed(seed);
}

EdgeWeight KaMinPar::compute_partition(const BlockID k, BlockID *partition) {
Logger::set_quiet_mode(_output_level == OutputLevel::QUIET);

cio::print_kaminpar_banner();
Expand All @@ -170,8 +174,7 @@ EdgeWeight KaMinPar::compute_partition(const int seed, const BlockID k, BlockID
// Setup graph dependent context parameters
_ctx.setup(*_graph_ptr);

// Initialize PRNG and console output
Random::seed(seed);
// Initialize console output
if (_output_level >= OutputLevel::APPLICATION) {
print(_ctx, std::cout);
}
Expand Down
6 changes: 3 additions & 3 deletions kaminpar-shm/kaminpar.h
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,8 @@ class KaMinPar {

~KaMinPar();

static void reseed(int seed);

/*!
* Sets the verbosity of the partitioner.
*/
Expand Down Expand Up @@ -421,15 +423,13 @@ class KaMinPar {
/*!
* Partitions the graph set by `take_graph()` or `copy_graph()` into `k` blocks.
*
* @param seed The seed for the random number generator. Note that when using more than one
* thread, partitioning is non-deterministic even with a fixed seed.
* @param k The number of blocks to partition the graph into.
* @param partition Array of length `n` for storing the partition. The caller is reponsible for
* allocating and freeing the memory.
*
* @return The edge-cut of the partition.
*/
shm::EdgeWeight compute_partition(int seed, shm::BlockID k, shm::BlockID *partition);
shm::EdgeWeight compute_partition(shm::BlockID k, shm::BlockID *partition);

private:
int _num_threads;
Expand Down
23 changes: 16 additions & 7 deletions tests/endtoend/dist_endtoend_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ TEST(DistEndToEndTest, partitions_empty_unweighted_graph) {
dKaMinPar dist(MPI_COMM_WORLD, 1, create_default_context());
dist.set_output_level(OutputLevel::QUIET);
dist.import_graph(vtxdist.data(), xadj.data(), adjncy.data(), nullptr, nullptr);
EXPECT_EQ(dist.compute_partition(0, 16, partition.data()), 0);
EXPECT_EQ(dist.compute_partition(16, partition.data()), 0);
}

TEST(DistEndToEndTest, partitions_empty_weighted_graph) {
Expand All @@ -82,7 +82,7 @@ TEST(DistEndToEndTest, partitions_empty_weighted_graph) {
dKaMinPar dist(MPI_COMM_WORLD, 1, create_default_context());
dist.set_output_level(OutputLevel::QUIET);
dist.import_graph(vtxdist.data(), xadj.data(), adjncy.data(), vwgt.data(), adjwgt.data());
EXPECT_EQ(dist.compute_partition(0, 16, partition.data()), 0);
EXPECT_EQ(dist.compute_partition(16, partition.data()), 0);
}

TEST(DistEndToEndTest, partitions_unweighted_walshaw_data_graph) {
Expand All @@ -100,10 +100,11 @@ TEST(DistEndToEndTest, partitions_unweighted_walshaw_data_graph) {
GlobalNodeID *adjncy_ptr = data::global_adjncy.data() + data::global_xadj[vtxdist[rank]];

std::vector<BlockID> partition(global_n);
dKaMinPar::reseed(0);
dKaMinPar dist(MPI_COMM_WORLD, 1, create_default_context()); // 1 thread: deterministic
dist.set_output_level(OutputLevel::QUIET);
dist.import_graph(vtxdist_ptr, xadj_ptr, adjncy_ptr, nullptr, nullptr);
const EdgeWeight reported_cut = dist.compute_partition(0, 16, partition.data() + vtxdist[rank]);
const EdgeWeight reported_cut = dist.compute_partition(16, partition.data() + vtxdist[rank]);

// Cut should be around 1200 -- 1300
EXPECT_LE(reported_cut, 2000);
Expand Down Expand Up @@ -143,6 +144,9 @@ TEST(DistEndToEndTest, partitions_unweighted_walshaw_data_graph) {
EXPECT_EQ(reported_cut, actual_cut / 2);
}

// Disabled: can fail since we offset the PRNG seed by the thread ID, and not all calls are made by
// the same threads across multiple runs
/*
TEST(DistEndToEndTest, partitions_unweighted_walshaw_data_graph_multiple_times_with_same_seed) {
const PEID size = mpi::get_comm_size(MPI_COMM_WORLD);
const PEID rank = mpi::get_comm_rank(MPI_COMM_WORLD);
Expand All @@ -158,20 +162,23 @@ TEST(DistEndToEndTest, partitions_unweighted_walshaw_data_graph_multiple_times_w
GlobalNodeID *adjncy_ptr = data::global_adjncy.data() + data::global_xadj[vtxdist[rank]];
std::vector<BlockID> seed0_partition(n);
dKaMinPar::reseed(0);
dKaMinPar dist(MPI_COMM_WORLD, 1, create_default_context()); // 1 thread: deterministic
dist.set_output_level(OutputLevel::QUIET);
dist.import_graph(vtxdist_ptr, xadj_ptr, adjncy_ptr, nullptr, nullptr);
const EdgeWeight reported_cut = dist.compute_partition(0, 16, seed0_partition.data());
const EdgeWeight reported_cut = dist.compute_partition(16, seed0_partition.data());
for (const int seed : {0, 0, 0}) {
std::vector<BlockID> partition(n);
dKaMinPar::reseed(seed);
dKaMinPar dist(MPI_COMM_WORLD, 1, create_default_context()); // 1 thread: deterministic
dist.set_output_level(OutputLevel::QUIET);
dist.import_graph(vtxdist_ptr, xadj_ptr, adjncy_ptr, nullptr, nullptr);
dist.compute_partition(seed, 16, partition.data());
dist.compute_partition(16, partition.data());
EXPECT_EQ(partition, seed0_partition);
}
}
*/

TEST(
DistEndToEndTest, partitions_unweighted_walshaw_data_graph_multiple_times_with_different_seeds
Expand All @@ -190,17 +197,19 @@ TEST(
GlobalNodeID *adjncy_ptr = data::global_adjncy.data() + data::global_xadj[vtxdist[rank]];

std::vector<BlockID> seed0_partition(n);
dKaMinPar::reseed(0);
dKaMinPar dist(MPI_COMM_WORLD, 1, create_default_context()); // 1 thread: deterministic
dist.set_output_level(OutputLevel::QUIET);
dist.import_graph(vtxdist_ptr, xadj_ptr, adjncy_ptr, nullptr, nullptr);
const EdgeWeight reported_cut = dist.compute_partition(0, 16, seed0_partition.data());
const EdgeWeight reported_cut = dist.compute_partition(16, seed0_partition.data());

for (const int seed : {1, 2, 3}) {
std::vector<BlockID> partition(n);
dKaMinPar::reseed(seed);
dKaMinPar dist(MPI_COMM_WORLD, 1, create_default_context()); // 1 thread: deterministic
dist.set_output_level(OutputLevel::QUIET);
dist.import_graph(vtxdist_ptr, xadj_ptr, adjncy_ptr, nullptr, nullptr);
dist.compute_partition(seed, 16, partition.data());
dist.compute_partition(16, partition.data());
EXPECT_NE(partition, seed0_partition);
}
}
Expand Down
Loading

0 comments on commit 5269589

Please sign in to comment.