Skip to content

Commit cb66c7b

Browse files
committed
prepare benchmarking
1 parent 542f8ae commit cb66c7b

3 files changed

Lines changed: 79 additions & 16 deletions

File tree

test/partitioner/external_tools/graph_partitioner.cpp

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
#include <array>
2121
#include <algorithm>
2222
#include <cassert>
23-
#include <chrono>
2423
#include <cstdlib>
2524
#include <fstream>
2625
#include <iostream>
@@ -39,6 +38,7 @@
3938
#include <algorithms/partitioner/build_sb_graph.hpp>
4039
#include <algorithms/partitioner/kernighan_lin_partitioner.hpp>
4140
#include <algorithms/partitioner/partition_graph.hpp>
41+
#include <algorithms/partitioner/sbg_partitioner_types.hpp>
4242
#include <algorithms/partitioner/weighted_sb_graph.hpp>
4343

4444
constexpr const char *VALID_PARTITION_METHODS = "{ Scotch, Metis, HMetis, Kahip, SBG }";
@@ -68,14 +68,15 @@ std::string GraphPartitioner::validPartitionMethodsStr()
6868
return valid_methods.str();
6969
}
7070

71-
Partition GraphPartitioner::createPartition(const std::string &partition_method_name, unsigned int partitions)
71+
std::tuple<Partition, std::chrono::duration<double>> GraphPartitioner::createPartition(const std::string &partition_method_name,
72+
unsigned int partitions, bool save_to_file)
7273
{
7374
Partition partition;
7475
PartitionMethod partition_method = partitionMethod(partition_method_name);
7576

7677
if (partition_method == PartitionMethod::Unknown) {
7778
std::cerr << "Unknown partition method, valid values are: " << validPartitionMethodsStr() << std::endl;
78-
return partition;
79+
return {partition, std::chrono::duration<double>()};
7980
}
8081

8182
partition.resize(_nbr_vtxs);
@@ -84,6 +85,10 @@ Partition GraphPartitioner::createPartition(const std::string &partition_method_
8485

8586
partition.resize(_nbr_vtxs);
8687

88+
// just in case we are using sbg
89+
sbg_partitioner::PartitionMap sbg_partitions = {};
90+
sbg_partitions.resize(_nbr_parts);
91+
8792
auto start = std::chrono::high_resolution_clock::now();
8893

8994
if (_nbr_vtxs > _nbr_parts) {
@@ -101,7 +106,7 @@ Partition GraphPartitioner::createPartition(const std::string &partition_method_
101106
partitionUsingKaHip(partition);
102107
break;
103108
case PartitionMethod::SBG:
104-
partitionUsingSBG();
109+
partitionUsingSBG(sbg_partitions);
105110
break;
106111
default:
107112
break;
@@ -113,8 +118,15 @@ Partition GraphPartitioner::createPartition(const std::string &partition_method_
113118

114119
std::cout << "Partition Time: " << duration.count() << " seconds." << std::endl;
115120

116-
savePartitionToFile(partition, partition_method_name);
117-
return partition;
121+
if (save_to_file) {
122+
if (partition_method == PartitionMethod::SBG) {
123+
// do something
124+
} else {
125+
savePartitionToFile(partition, partition_method_name);
126+
}
127+
}
128+
129+
return {partition, duration};
118130
}
119131

120132
bool GraphPartitioner::endsWithJson() { return _name.size() >= 5 && _name.compare(_name.size() - 5, 5, ".json") == 0; }
@@ -323,11 +335,9 @@ void GraphPartitioner::partitionUsingKaHip(Partition &partition)
323335
partition.values.data());
324336
}
325337

326-
void GraphPartitioner::partitionUsingSBG()
338+
void GraphPartitioner::partitionUsingSBG(sbg_partitioner::PartitionMap &partitions)
327339
{
328-
std::cout << "partitionUsingSBG" << std::endl;
329-
330-
auto partitions = sbg_partitioner::best_initial_partition(*sbg_graph, _nbr_parts, sbg_partitioner::InitialPartitionStrategy::ALL, false);
340+
partitions = sbg_partitioner::best_initial_partition(*sbg_graph, _nbr_parts, sbg_partitioner::InitialPartitionStrategy::ALL, false);
331341
// std::cout << "chosen partition " << partitions << std::endl;
332342

333343
sbg_partitioner::kl_sbg_imbalance_partitioner(*sbg_graph, partitions, _imbalance, false);

test/partitioner/external_tools/graph_partitioner.hpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,21 @@
1919

2020
#pragma once
2121

22+
#include <chrono>
2223
#include <vector>
2324
#include <string>
2425
#include <memory>
2526
#include <metis.h>
2627
#include <scotch/scotch.h>
2728

29+
#include <algorithms/partitioner/sbg_partitioner_types.hpp>
30+
2831
namespace SBG {
2932
namespace LIB {
3033
class WeightedSBGraph;
3134
}
3235
}
3336

34-
3537
using grp_t = int;
3638

3739
enum class PartitionMethod {
@@ -54,7 +56,7 @@ class GraphPartitioner {
5456
public:
5557
explicit GraphPartitioner(const std::string &name);
5658

57-
Partition createPartition(const std::string& partition_method, unsigned int partitions);
59+
std::tuple<Partition, std::chrono::duration<double>> createPartition(const std::string& partition_method, unsigned int partitions, bool save_to_file);
5860

5961
static std::string validPartitionMethodsStr();
6062

@@ -73,7 +75,7 @@ class GraphPartitioner {
7375

7476
void partitionUsingMetis(Partition &partition);
7577
void partitionUsingHMetis(Partition &partition);
76-
void partitionUsingSBG();
78+
void partitionUsingSBG(sbg_partitioner::PartitionMap& partitions);
7779
void createHMetisGraphFile(const std::string &file_name);
7880
void executeKhmetis(const std::string &h_graph_name) const;
7981

test/partitioner/external_tools/main.cpp

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,17 @@
1818
*****************************************************************************/
1919

2020
#include <algorithm>
21-
// #include <filesystem>
21+
#include <filesystem>
2222
#include <fstream>
2323
#include <getopt.h>
2424
#include <iostream>
2525
#include <vector>
2626

2727
#include "graph_partitioner.hpp"
2828

29+
static int number_of_executions = 15;
30+
static int number_of_cold_executions = 5;
31+
2932
void usage()
3033
{
3134
std::cout << "Usage: grap_partitioner -f <file_name> -n <num_partitions> -m <partition_method> -i <imbalance>" << std::endl;
@@ -67,6 +70,40 @@ void parseArgs(int argc, char* argv[], std::string& sbg_json_input, int& partiti
6770
}
6871
}
6972

73+
void addRecord(const std::string& filename, const std::string& method, int partitions,
74+
const std::vector<std::chrono::duration<double>>& durations)
75+
{
76+
// Check if the file already exists before we open it
77+
bool exists = std::filesystem::exists(filename);
78+
79+
// Open in append mode
80+
std::ofstream myFile(filename, std::ios::app);
81+
82+
if (!myFile.is_open()) {
83+
std::cerr << "Error opening file!" << std::endl;
84+
return;
85+
}
86+
87+
// If the file is new, write the header first
88+
if (!exists) {
89+
myFile << "Method,Partitions";
90+
for (size_t i = 0; i < number_of_executions - number_of_cold_executions; i++) {
91+
myFile << ", exec " << i;
92+
}
93+
myFile << "\n";
94+
}
95+
96+
// Add the new row
97+
myFile << method << "," << partitions;
98+
for (const auto& d : durations) {
99+
myFile << "," << d.count();
100+
}
101+
myFile << "\n";
102+
103+
myFile.close();
104+
std::cout << "Results in " << filename << std::endl;
105+
}
106+
70107
int main(int argc, char* argv[])
71108
{
72109
std::string json_file_name;
@@ -76,8 +113,22 @@ int main(int argc, char* argv[])
76113

77114
parseArgs(argc, argv, json_file_name, partitions, partition_method, imbalance);
78115

79-
GraphPartitioner partitioner(json_file_name);
80-
Partition partition = partitioner.createPartition(partition_method, partitions);
116+
std::vector<std::chrono::duration<double>> durations;
117+
118+
for (size_t i = 0; i < number_of_executions; i++) {
119+
GraphPartitioner partitioner(json_file_name);
120+
auto [partition, duration] = partitioner.createPartition(partition_method, partitions, i == number_of_executions - 1);
121+
122+
if (i < number_of_cold_executions) {
123+
continue;
124+
}
125+
126+
durations.push_back(duration);
127+
}
128+
129+
std::filesystem::path json_file_name_path(json_file_name);
130+
131+
addRecord(json_file_name_path.filename().replace_extension("csv"), partition_method, partitions, durations);
81132

82133
// write results to a file
83134
// std::filesystem::path json_filesystem_path(json_file_name);

0 commit comments

Comments
 (0)