-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
175 lines (147 loc) · 4.7 KB
/
main.cpp
File metadata and controls
175 lines (147 loc) · 4.7 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include <cassert>
#include <ios>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <random>
#include "bfs.hpp"
#include "time.hpp"
unsigned max_len;
void set_max_len(unsigned new_max_len) {
max_len = new_max_len;
std::cout << "max_len: " << max_len << std::endl;
}
/**
* Dummy object to test potential
* speedup from parallelism.
*/
struct S {
std::vector<int> a;
friend bool operator==(const S &l, const S &r) {
return l.a == r.a;
}
};
std::vector<S> cheap_sparse(const S &s) {
std::vector<S> transitions;
if (s.a.size() < max_len) {
transitions.push_back(s);
transitions.back().a.push_back(0);
transitions.push_back(s);
transitions.back().a.push_back(1);
}
if (s.a.size() > 0) {
transitions.push_back(s);
transitions.back().a.pop_back();
if (s.a.size() < max_len) {
for (int bit0 : {0, 1})
for (int bit1 : {0, 1}) {
transitions.push_back(s);
transitions.back().a.pop_back();
transitions.back().a.push_back(bit0);
transitions.back().a.push_back(bit1);
}
}
}
return transitions;
}
std::vector<S> cheap_dense(const S &s) {
std::vector<S> transitions;
if (s.a.size() < max_len) {
transitions.push_back(s);
transitions.back().a.push_back(0);
}
unsigned as_bits = 0;
for (int i : s.a)
as_bits = as_bits << 1U | i;
unsigned mask = ~as_bits & ((1U << s.a.size()) - 1);
for (unsigned activate = mask; activate != 0; activate = (activate-1) & mask) {
transitions.push_back(s);
auto &a = transitions.back().a;
for (int i = 0; i < (int)a.size(); ++i)
a[i] ^= activate >> (a.size() - i - 1) & 1;
}
return transitions;
}
auto expensive_sparse(const S &s) {
auto transitions = cheap_sparse(s);
std::mt19937 rng(123);
for (int rep = 0; rep < 100; ++rep)
std::shuffle(transitions.begin(), transitions.end(), rng);
return transitions;
}
namespace std {
/**
* Extend std to make S hashable.
*/
template <>
struct hash<S> {
size_t operator()(const S &s) const {
size_t res = 0;
for (int bit : s.a) res = res << 1 | bit;
return res;
}
};
} // namespace std
int main() {
std::cout
<< "format: bfs_type(transition_type, source, threads [, log2(set_size)])\n"
<< std::endl;
//*
set_max_len(20);
TIME(sequential_bfs(cheap_sparse, S{}));
//TIME(bfs_phmap(cheap_sparse, S{}, 64));
TIME(bfs_phmap(cheap_sparse, S{}, 32));
TIME(bfs_phmap(cheap_sparse, S{}, 16));
TIME(bfs_phmap(cheap_sparse, S{}, 8));
TIME(bfs_phmap(cheap_sparse, S{}, 4));
TIME(bfs_phmap(cheap_sparse, S{}, 2));
TIME(bfs_phmap(cheap_sparse, S{}, 1));
//TIME(bfs_fixed_size_set(cheap_sparse, S{}, 64, max_len));
TIME(bfs_fixed_size_set(cheap_sparse, S{}, 32, max_len));
TIME(bfs_fixed_size_set(cheap_sparse, S{}, 16, max_len));
TIME(bfs_fixed_size_set(cheap_sparse, S{}, 8, max_len));
TIME(bfs_fixed_size_set(cheap_sparse, S{}, 4, max_len));
TIME(bfs_fixed_size_set(cheap_sparse, S{}, 2, max_len));
TIME(bfs_fixed_size_set(cheap_sparse, S{}, 1, max_len));
std::cout << std::endl;
// */
//*
set_max_len(15);
TIME(sequential_bfs(cheap_dense, S{}));
//TIME(bfs_phmap(cheap_dense, S{}, 64));
TIME(bfs_phmap(cheap_dense, S{}, 32));
TIME(bfs_phmap(cheap_dense, S{}, 16));
TIME(bfs_phmap(cheap_dense, S{}, 8));
TIME(bfs_phmap(cheap_dense, S{}, 4));
TIME(bfs_phmap(cheap_dense, S{}, 2));
TIME(bfs_phmap(cheap_dense, S{}, 1));
//TIME(bfs_fixed_size_set(cheap_dense, S{}, 64, max_len));
TIME(bfs_fixed_size_set(cheap_dense, S{}, 32, max_len));
TIME(bfs_fixed_size_set(cheap_dense, S{}, 16, max_len));
TIME(bfs_fixed_size_set(cheap_dense, S{}, 8, max_len));
TIME(bfs_fixed_size_set(cheap_dense, S{}, 4, max_len));
TIME(bfs_fixed_size_set(cheap_dense, S{}, 2, max_len));
TIME(bfs_fixed_size_set(cheap_dense, S{}, 1, max_len));
std::cout << std::endl;
// */
//*
set_max_len(20);
TIME(sequential_bfs(expensive_sparse, S{}));
//TIME(bfs_phmap(expensive_sparse, S{}, 64));
TIME(bfs_phmap(expensive_sparse, S{}, 32));
TIME(bfs_phmap(expensive_sparse, S{}, 16));
TIME(bfs_phmap(expensive_sparse, S{}, 8));
TIME(bfs_phmap(expensive_sparse, S{}, 4));
TIME(bfs_phmap(expensive_sparse, S{}, 2));
TIME(bfs_phmap(expensive_sparse, S{}, 1));
//TIME(bfs_fixed_size_set(expensive_sparse, S{}, 64, max_len));
TIME(bfs_fixed_size_set(expensive_sparse, S{}, 32, max_len));
TIME(bfs_fixed_size_set(expensive_sparse, S{}, 16, max_len));
TIME(bfs_fixed_size_set(expensive_sparse, S{}, 8, max_len));
TIME(bfs_fixed_size_set(expensive_sparse, S{}, 4, max_len));
TIME(bfs_fixed_size_set(expensive_sparse, S{}, 2, max_len));
TIME(bfs_fixed_size_set(expensive_sparse, S{}, 1, max_len));
std::cout << std::endl;
// */
}