Skip to content

Commit 4d1fe34

Browse files
committed
datastructure optimization in GrowLocal
replace set by prority_queque update update
1 parent e0f77c0 commit 4d1fe34

File tree

1 file changed

+42
-31
lines changed

1 file changed

+42
-31
lines changed

include/osp/bsp/scheduler/GreedySchedulers/GrowLocalAutoCores.hpp

Lines changed: 42 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@ limitations under the License.
2121
#include <chrono>
2222
#include <climits>
2323
#include <list>
24+
#include <queue>
2425
#include <map>
25-
#include <set>
26+
#include <unordered_set>
2627
#include <string>
2728
#include <vector>
2829

@@ -114,19 +115,18 @@ class GrowLocalAutoCores : public Scheduler<Graph_t> {
114115
const unsigned P = instance.numberOfProcessors();
115116
const auto &G = instance.getComputationalDag();
116117

117-
std::set<vertex_idx> ready;
118-
std::vector<std::set<std::size_t>::iterator> place_in_ready(N);
118+
std::unordered_set<vertex_idx> ready;
119119

120-
std::set<vertex_idx> allReady;
121-
std::vector<std::set<vertex_idx>> procReady(P);
120+
std::vector<vertex_idx> allReady;
121+
std::vector<std::vector<vertex_idx>> procReady(P);
122122

123123
std::vector<vertex_idx> predec(N);
124124

125125
for (const auto &node : G.vertices()) {
126126
predec[node] = G.in_degree(node);
127127

128128
if (predec[node] == 0) {
129-
place_in_ready[node] = ready.insert(node).first;
129+
ready.insert(node);
130130
}
131131
}
132132

@@ -160,7 +160,8 @@ class GrowLocalAutoCores : public Scheduler<Graph_t> {
160160
}
161161

162162
new_ready.clear();
163-
allReady = ready;
163+
allReady.assign(ready.begin(), ready.end());
164+
std::make_heap(allReady.begin(), allReady.end(), std::greater<vertex_idx>());
164165

165166
vertex_idx new_total_assigned = 0;
166167
v_workw_t<Graph_t> weight_limit = 0, total_weight_assigned = 0;
@@ -172,23 +173,27 @@ class GrowLocalAutoCores : public Scheduler<Graph_t> {
172173
vertex_idx chosen_node = std::numeric_limits<vertex_idx>::max();
173174

174175
if constexpr (use_memory_constraint) {
175-
if (!procReady[0].empty() && local_memory_constraint.can_add(*procReady[0].begin(), 0)) {
176-
chosen_node = *procReady[0].begin();
177-
procReady[0].erase(procReady[0].begin());
178-
} else if (!allReady.empty() && local_memory_constraint.can_add(*allReady.begin(), 0)) {
179-
chosen_node = *allReady.begin();
180-
allReady.erase(allReady.begin());
176+
if (!procReady[0].empty() && local_memory_constraint.can_add(procReady[0].front(), 0)) {
177+
chosen_node = procReady[0].front();
178+
std::pop_heap(procReady[0].begin(), procReady[0].end(), std::greater<vertex_idx>());
179+
procReady[0].pop_back();
180+
} else if (!allReady.empty() && local_memory_constraint.can_add(allReady.front(), 0)) {
181+
chosen_node = allReady.front();
182+
std::pop_heap(allReady.begin(), allReady.end(), std::greater<vertex_idx>());
183+
allReady.pop_back();
181184
} else {
182185
early_memory_break = true;
183186
break;
184187
}
185188
} else {
186189
if (!procReady[0].empty()) {
187-
chosen_node = *procReady[0].begin();
188-
procReady[0].erase(procReady[0].begin());
190+
chosen_node = procReady[0].front();
191+
std::pop_heap(procReady[0].begin(), procReady[0].end(), std::greater<vertex_idx>());
192+
procReady[0].pop_back();
189193
} else if (!allReady.empty()) {
190-
chosen_node = *allReady.begin();
191-
allReady.erase(allReady.begin());
194+
chosen_node = allReady.front();
195+
std::pop_heap(allReady.begin(), allReady.end(), std::greater<vertex_idx>());
196+
allReady.pop_back();
192197
} else {
193198
break;
194199
}
@@ -215,7 +220,8 @@ class GrowLocalAutoCores : public Scheduler<Graph_t> {
215220
new_ready.push_back(succ);
216221

217222
if (node_to_proc[succ] == 0) {
218-
procReady[0].insert(succ);
223+
procReady[0].push_back(succ);
224+
std::push_heap(procReady[0].begin(), procReady[0].end(), std::greater<vertex_idx>());
219225
}
220226
}
221227
}
@@ -230,23 +236,27 @@ class GrowLocalAutoCores : public Scheduler<Graph_t> {
230236
vertex_idx chosen_node = std::numeric_limits<vertex_idx>::max();
231237

232238
if constexpr (use_memory_constraint) {
233-
if (!procReady[proc].empty() && local_memory_constraint.can_add(*procReady[proc].begin(), proc)) {
234-
chosen_node = *procReady[proc].begin();
235-
procReady[proc].erase(procReady[proc].begin());
236-
} else if (!allReady.empty() && local_memory_constraint.can_add(*allReady.begin(), proc)) {
237-
chosen_node = *allReady.begin();
238-
allReady.erase(allReady.begin());
239+
if (!procReady[proc].empty() && local_memory_constraint.can_add(procReady[proc].front(), proc)) {
240+
chosen_node = procReady[proc].front();
241+
std::pop_heap(procReady[proc].begin(), procReady[proc].end(), std::greater<vertex_idx>());
242+
procReady[proc].pop_back();
243+
} else if (!allReady.empty() && local_memory_constraint.can_add(allReady.front(), proc)) {
244+
chosen_node = allReady.front();
245+
std::pop_heap(allReady.begin(), allReady.end(), std::greater<vertex_idx>());
246+
allReady.pop_back();
239247
} else {
240248
early_memory_break = true;
241249
break;
242250
}
243251
} else {
244252
if (!procReady[proc].empty()) {
245-
chosen_node = *procReady[proc].begin();
246-
procReady[proc].erase(procReady[proc].begin());
253+
chosen_node = procReady[proc].front();
254+
std::pop_heap(procReady[proc].begin(), procReady[proc].end(), std::greater<vertex_idx>());
255+
procReady[proc].pop_back();
247256
} else if (!allReady.empty()) {
248-
chosen_node = *allReady.begin();
249-
allReady.erase(allReady.begin());
257+
chosen_node = allReady.front();
258+
std::pop_heap(allReady.begin(), allReady.end(), std::greater<vertex_idx>());
259+
allReady.pop_back();
250260
} else {
251261
break;
252262
}
@@ -272,7 +282,8 @@ class GrowLocalAutoCores : public Scheduler<Graph_t> {
272282
new_ready.push_back(succ);
273283

274284
if (node_to_proc[succ] == proc) {
275-
procReady[proc].insert(succ);
285+
procReady[proc].push_back(succ);
286+
std::push_heap(procReady[proc].begin(), procReady[proc].end(), std::greater<vertex_idx>());
276287
}
277288
}
278289
}
@@ -350,14 +361,14 @@ class GrowLocalAutoCores : public Scheduler<Graph_t> {
350361

351362
// apply best iteration
352363
for (const auto &node : best_new_ready) {
353-
place_in_ready[node] = ready.insert(node).first;
364+
ready.insert(node);
354365
}
355366

356367
for (unsigned proc = 0; proc < P; ++proc) {
357368
for (const auto &node : best_new_assignments[proc]) {
358369
node_to_proc[node] = proc;
359370
node_to_supstep[node] = supstep;
360-
ready.erase(place_in_ready[node]);
371+
ready.erase(node);
361372
++total_assigned;
362373

363374
for (const auto &succ : G.children(node)) {

0 commit comments

Comments
 (0)