Skip to content

Commit 54be989

Browse files
committed
improve time profile implementation
1 parent c02cefb commit 54be989

5 files changed

Lines changed: 122 additions & 15 deletions

File tree

algorithms/partitioner/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ target_sources(
1414
partition_graph.cpp
1515
partition_metrics_api.cpp
1616
partition_strategy.cpp
17+
time_profiler.cpp
1718
weighted_sb_graph.cpp
1819
)
1920

algorithms/partitioner/kernighan_lin_partitioner.cpp

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@
2424
#include "build_sb_graph.hpp"
2525
#include "communication_cost.hpp"
2626
#include "kernighan_lin_partitioner.hpp"
27+
#include "time_profiler.hpp"
2728
#include "sbg_partitioner_log.hpp"
2829

2930

3031
#define PARTITION_IMBALANCE_DEBUG 0
31-
#define PARTITION_IMBALANCE_PROFILE 0
3232

3333

3434
// This code is based on https://github.com/CIFASIS/sbg-partitioner/discussions/17
@@ -72,6 +72,7 @@ CostMatrixImbalance generate_gain_matrix(
7272
unsigned LMin,
7373
unsigned LMax)
7474
{
75+
internal::TimeProfiler profiler("generate_gain_matrix");
7576
CostMatrixImbalance local_cost_matrix;
7677

7778
for (size_t i = 0; i < partition_a.size(); i++) {
@@ -178,6 +179,7 @@ void update_diff(
178179
unsigned LMin,
179180
unsigned LMax)
180181
{
182+
internal::TimeProfiler profiler("update_diff");
181183
logging::sbg_log << affected_node_a.first << ", " << affected_node_a.second << endl;
182184
logging::sbg_log << affected_node_b.first << ", " << affected_node_b.second << endl;
183185

@@ -328,22 +330,14 @@ int kl_sbg_imbalance(
328330
Set b_v = graph.fact().createSet();
329331
const auto node_weights = graph.get_node_weights();
330332

331-
auto start_generate_gain_matrix = chrono::high_resolution_clock::now();
332333
CostMatrixImbalance gm = generate_gain_matrix(graph, cost_matrix, partition_a_id, partition_a, partition_b_id, partition_b, LMin, LMax);
333-
auto end_generate_gain_matrix = chrono::high_resolution_clock::now();
334-
auto time_to_generate_gain_matrix = chrono::duration<double, std::milli>(end_generate_gain_matrix - start_generate_gain_matrix).count();
335-
#if PARTITION_IMBALANCE_PROFILE
336-
cout << "time_to_generate_gain_matrix: " << time_to_generate_gain_matrix << endl;
337-
#endif
338-
339334

340335
#if PARTITION_IMBALANCE_DEBUG
341336
logging::sbg_log << LMin << ", "
342337
<< LMax
343338
<< gm << endl;
344339
#endif
345340

346-
double time_to_update_diff = 0.;
347341
while ((not a_c.empty()) and (not b_c.empty())) {
348342
logging::sbg_log << "inside the while " << a_c << ", " << b_c << " ";
349343
logging::sbg_log << get_partition_size(a_c, node_weights, graph.fact()) << ", " << get_partition_size(b_c, node_weights, graph.fact()) << endl;
@@ -353,15 +347,9 @@ int kl_sbg_imbalance(
353347
logging::sbg_log << g << endl;
354348
pair<Set, Set> a_ = {graph.fact().createSet(), graph.fact().createSet()}, b_ = {graph.fact().createSet(), graph.fact().createSet()};
355349
tie(a_, b_) = update_sets(a_c, b_c, a_v, b_v, g, graph);
356-
auto start_update_diff = chrono::high_resolution_clock::now();
357350
update_diff(gm, a_c, a_v, a_, b_c, b_v, b_, graph, node_weights, g, LMin, LMax);
358-
auto end_update_diff = chrono::high_resolution_clock::now();
359-
time_to_update_diff += chrono::duration<double, std::milli>(end_update_diff - start_update_diff).count();
360351
update_sum(par_sum, g.gain, max_par_sum, max_par_sum_set, a_v, b_v);
361352
}
362-
#if PARTITION_IMBALANCE_PROFILE
363-
cout << "time_to_update_diff: " << time_to_update_diff << endl;
364-
#endif
365353

366354
if (max_par_sum > 0) {
367355

algorithms/partitioner/main.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "kernighan_lin_partitioner.hpp"
3030
#include "partition_graph.hpp"
3131
#include "partition_metrics_api.hpp"
32+
#include "time_profiler.hpp"
3233

3334

3435
using namespace std;
@@ -242,6 +243,9 @@ int main(int argc, char** argv)
242243
}
243244
}
244245

246+
// print profiler results if they are enabled
247+
sbg_partitioner::time_profiler_results();
248+
245249
cout << "time_to_build_graph = " << time_to_build_graph << " ms" << endl;
246250
cout << "time_to_partitionate = " << time_to_partitionate << " ms" << endl;
247251

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/**
2+
This file is part of Set--Based Graph Library.
3+
4+
SBG Library is free software: you can redistribute it and/or modify
5+
it under the terms of the GNU General Public License as published by
6+
the Free Software Foundation, either version 3 of the License, or
7+
(at your option) any later version.
8+
9+
SBG Library is distributed in the hope that it will be useful,
10+
but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
GNU General Public License for more details.
13+
14+
You should have received a copy of the GNU General Public License
15+
along with SBG Library. If not, see <http://www.gnu.org/licenses/>.
16+
17+
******************************************************************************/
18+
19+
#include "time_profiler.hpp"
20+
21+
namespace sbg_partitioner {
22+
23+
void time_profiler_results()
24+
{
25+
internal::TimeProfiler::print_execution_time();
26+
}
27+
28+
29+
namespace internal{
30+
31+
std::unordered_map<std::string, double> TimeProfiler::_execution_time = {};
32+
33+
TimeProfiler::TimeProfiler(std::string&& function_name)
34+
: _function_name(move(function_name))
35+
{
36+
if (time_profiler_enabled) {
37+
_start = std::chrono::high_resolution_clock::now();
38+
}
39+
}
40+
41+
42+
TimeProfiler::~TimeProfiler()
43+
{
44+
if (time_profiler_enabled) {
45+
auto end = std::chrono::high_resolution_clock::now();
46+
auto exec_time = std::chrono::duration<double, std::milli>(end - _start).count();
47+
if (_execution_time.find(_function_name) == _execution_time.end()) {
48+
_execution_time.insert({_function_name, 0.0});
49+
}
50+
_execution_time[_function_name] += exec_time;
51+
}
52+
}
53+
54+
55+
void TimeProfiler::print_execution_time() {
56+
if (time_profiler_enabled) {
57+
for (auto&& pair : TimeProfiler::_execution_time) {
58+
std::cout << pair.first << ": " << pair.second << " ms" << std::endl;
59+
}
60+
}
61+
}
62+
63+
}
64+
65+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
This file is part of Set--Based Graph Library.
3+
4+
SBG Library is free software: you can redistribute it and/or modify
5+
it under the terms of the GNU General Public License as published by
6+
the Free Software Foundation, either version 3 of the License, or
7+
(at your option) any later version.
8+
9+
SBG Library is distributed in the hope that it will be useful,
10+
but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
GNU General Public License for more details.
13+
14+
You should have received a copy of the GNU General Public License
15+
along with SBG Library. If not, see <http://www.gnu.org/licenses/>.
16+
17+
******************************************************************************/
18+
19+
#include <chrono>
20+
#include <iostream>
21+
#include <string>
22+
#include <unordered_map>
23+
24+
#pragma once
25+
26+
namespace sbg_partitioner {
27+
28+
constexpr bool time_profiler_enabled = false;
29+
30+
void time_profiler_results();
31+
32+
namespace internal {
33+
34+
struct TimeProfiler {
35+
TimeProfiler(std::string&& function_name);
36+
37+
~TimeProfiler();
38+
39+
static void print_execution_time();
40+
41+
private:
42+
std::string _function_name;
43+
std::chrono::_V2::system_clock::time_point _start;
44+
static std::unordered_map<std::string, double> _execution_time;
45+
};
46+
47+
}
48+
49+
}

0 commit comments

Comments
 (0)