-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathBoostBellmanFordBenchmarking.cpp
More file actions
112 lines (85 loc) · 3.45 KB
/
BoostBellmanFordBenchmarking.cpp
File metadata and controls
112 lines (85 loc) · 3.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
//===- BoostBellmanforBenchmarking.cpp -----------------------------------===//
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
//===----------------------------------------------------------------------===//
//
// This file implements the benchmark for Boost Bellmanford example benchmark.
//
//===----------------------------------------------------------------------===//
#include <iostream>
#include <benchmark/benchmark.h>
#include <boost/config.hpp>
#include <boost/graph/undirected_graph.hpp>
#include <boost/graph/exterior_property.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/bellman_ford_shortest_paths.hpp>
#include <vector>
#include <Utility/Utils.h>
using namespace std;
#define SIZE 100
namespace {
typedef int t_weight;
// define the graph type
typedef boost::property<boost::edge_weight_t, t_weight> EdgeWeightProperty;
typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS,
boost::no_property, EdgeWeightProperty>
Graph;
typedef boost::property_map<Graph, boost::edge_weight_t>::type WeightMap;
Graph g;
std::vector<int> distnce(SIZE, (std::numeric_limits<int>::max)());
int source_node_index = 0;
std::vector<std::size_t> parent(SIZE);
} // namespace
void initializeBoostBellmanFord() {
const int vertices = SIZE;
int num_edges = vertices * (vertices -1) / 2;
// define edges
// int edges[] = {1, 2, 2, 3, 3, 4, 4, 1, 1, 3, 2, 4};
// t_weight weight[] = {4, 3, 3, 6, 2, 2};
std::vector<int> edges;
std::vector<int> weight;
graph::generateRandomGraph(edges, weight, vertices);
for (std::size_t k = 0; k < num_edges; ++k)
boost::add_edge(edges[k * 2] - 1, edges[k * 2 + 1] - 1, weight[k], g);
WeightMap weight_pmap = boost::get(boost::edge_weight, g);
for (int i = 0; i < num_vertices(g); ++i)
parent[i] = i;
distnce[source_node_index] = 0;
}
// Benchmarking function.
static void Boost_BellmanFord(benchmark::State &state) {
for (auto _ : state) {
WeightMap weight_pmap = boost::get(boost::edge_weight, g);
for (int i = 0; i < state.range(0); ++i) {
bool r = bellman_ford_shortest_paths(g, num_vertices(g), weight_pmap, &parent[0],
&distnce[0], boost::closed_plus< int >(), std::less< int >(),
boost::default_bellman_visitor());
}
}
}
// Register benchmarking function.
BENCHMARK(Boost_BellmanFord)->Arg(1);
void generateResultBoostBellmanFord() {
initializeBoostBellmanFord();
WeightMap weight_pmap = boost::get(boost::edge_weight, g);
for (int i = 0; i < num_vertices(g); ++i)
parent[i] = i;
distnce[source_node_index] = 0;
cout << "-------------------------------------------------------\n";
cout << "[ BOOST FloydWarshall Result Information ]\n";
bool r = bellman_ford_shortest_paths(g, num_vertices(g), weight_pmap, &parent[0],
&distnce[0], boost::closed_plus< int >(), std::less< int >(),
boost::default_bellman_visitor());
cout << "Boost FloydWarshall operation finished!\n";
}