Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 5d6c19c

Browse files
Implement suggestions from merge request discussions
1 parent a3a877f commit 5d6c19c

File tree

1 file changed

+32
-39
lines changed

1 file changed

+32
-39
lines changed

‎framework/colouring/graph_colouring.cpp

Lines changed: 32 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,6 @@ namespace graph_colouring {
3535
/**< The actual colouring */
3636
};
3737

38-
/**
39-
* Used in the parallel colouring algorithm to track certain events
40-
*/
41-
struct ColouringStrategyContext {
42-
/**> Counts the number of finished pedigrees (=populationSize/2) */
43-
std::atomic<size_t> wpCount;
44-
};
45-
4638
ColorCount colorCount(const Colouring &s) {
4739
std::vector<bool> usedColor(s.size());
4840
ColorCount color_count = 0;
@@ -108,9 +100,9 @@ namespace graph_colouring {
108100
return nextTry;
109101
}
110102

111-
inline bool hasFinished(const std::vector<ColouringStrategyContext> &context) {
112-
for (auto &c : context) {
113-
if (c.wpCount > 0) {
103+
inline bool hasFinished(const std::vector<std::atomic<size_t>> &context) {
104+
for (auto &wpCount : context) {
105+
if (wpCount > 0) {
114106
return false;
115107
}
116108
}
@@ -122,20 +114,15 @@ namespace graph_colouring {
122114
const size_t populationSize,
123115
const size_t maxItr,
124116
const size_t threadId,
125-
std::vector<ColouringStrategyContext> &context,
117+
std::vector<std::atomic<size_t>> &context,
126118
boost::lockfree::queue<WorkingPackage> &workQueue,
127119
boost::lockfree::queue<MasterPackage> &masterQueue,
128120
std::vector<Colouring> &population,
129121
std::vector<Colouring> &localBestColourings,
130122
std::vector<std::atomic<bool>> &lock,
131123
std::atomic<ColorCount> &target_k,
132124
std::atomic<bool> &terminated) {
133-
typedef std::mt19937::result_type seed_type;
134-
typename std::chrono::system_clock seed_clock;
135-
auto init_seed = static_cast<seed_type>(seed_clock.now().time_since_epoch().count());
136-
init_seed += static_cast<seed_type>(threadId);
137-
138-
std::mt19937 generator(init_seed);
125+
std::mt19937 generator(threadId);
139126

140127
//Only used to avoid rapid reporting of already known colourings
141128
ColorCount last_reported_k = target_k + 1;
@@ -146,12 +133,10 @@ namespace graph_colouring {
146133
const ColouringStrategy &strategy = *strategies[wp.strategyId];
147134

148135
if (target_k < wp.target_k && strategy.isFixedKStrategy()) {
149-
context[wp.strategyId].wpCount.fetch_sub(1);
136+
context[wp.strategyId].fetch_sub(1);
150137
continue;
151138
}
152139

153-
Colouring result;
154-
155140
if (wp.itr > 0) {
156141
auto p1 = chooseParent(wp.strategyId, populationSize, lock, generator);
157142
auto p2 = chooseParent(wp.strategyId, populationSize, lock, generator);
@@ -170,16 +155,22 @@ namespace graph_colouring {
170155
auto lsOp = strategies[wp.strategyId]->lsOperators[
171156
lsOprDist(generator)];
172157

173-
result = lsOp(crossoverOp(*parents[0], *parents[1], G), G);
174-
*parents[weakerParent] = result;
158+
*parents[weakerParent] = lsOp(crossoverOp(*parents[0], *parents[1], G), G);
159+
160+
if (strategy.isSolution(G, target_k, *parents[weakerParent]) && last_reported_k > target_k) {
161+
last_reported_k = colorCount(*parents[weakerParent]);
162+
masterQueue.push({last_reported_k, wp.strategyId});
163+
size_t threadCount = localBestColourings.size() / strategies.size();
164+
localBestColourings[wp.strategyId * threadCount + threadId] = *parents[weakerParent];
165+
}
175166

176167
lock[p1] = false;
177168
lock[p2] = false;
178169

179170
if (wp.itr < maxItr) {
180171
workQueue.push({wp.itr + 1, wp.strategyId, wp.target_k, wp.colouring});
181172
} else {
182-
context[wp.strategyId].wpCount.fetch_sub(1);
173+
context[wp.strategyId].fetch_sub(1);
183174
}
184175
} else {
185176
std::uniform_int_distribution<size_t> initOprDist(0, strategy.initOperators.size() - 1);
@@ -188,24 +179,26 @@ namespace graph_colouring {
188179
auto initOpr = strategy.initOperators[initOprDist(generator)];
189180
auto lsOpr = strategy.lsOperators[lsOprDist(generator)];
190181

191-
result = lsOpr(initOpr(G, wp.target_k), G);
192-
population[wp.strategyId * populationSize + wp.colouring] = result;
182+
population[wp.strategyId * populationSize + wp.colouring] = lsOpr(initOpr(G, wp.target_k), G);
183+
184+
if (strategy.isSolution(G, target_k, population[wp.strategyId * populationSize + wp.colouring])
185+
&& last_reported_k > target_k) {
186+
last_reported_k = colorCount(population[wp.strategyId * populationSize + wp.colouring]);
187+
masterQueue.push({last_reported_k, wp.strategyId});
188+
size_t threadCount = localBestColourings.size() / strategies.size();
189+
localBestColourings[wp.strategyId * threadCount + threadId] = population[
190+
wp.strategyId * populationSize + wp.colouring];
191+
}
193192

194193
lock[wp.strategyId * populationSize + wp.colouring] = false;
195194

196195
auto matingPopulationSize = populationSize / 2;
197196
if (wp.colouring < matingPopulationSize) {
198197
workQueue.push({wp.itr + 1, wp.strategyId, wp.target_k, wp.colouring});
199198
} else {
200-
context[wp.strategyId].wpCount.fetch_sub(1);
199+
context[wp.strategyId].fetch_sub(1);
201200
}
202201
}
203-
if (strategy.isSolution(G, target_k, result) && last_reported_k > target_k) {
204-
last_reported_k = colorCount(result);
205-
masterQueue.push({last_reported_k, wp.strategyId});
206-
size_t threadCount = localBestColourings.size() / strategies.size();
207-
localBestColourings[wp.strategyId * threadCount + threadId] = result;
208-
}
209202
}
210203
std::this_thread::yield();
211204
}
@@ -233,7 +226,7 @@ namespace graph_colouring {
233226
std::vector<Colouring> localBestColourings(strategies.size() * threadCount);
234227
//lock[i] = true -> i-th individual is free for mating
235228
std::vector<std::atomic<bool>> lock(strategies.size() * populationSize);
236-
std::vector<ColouringStrategyContext> context(strategies.size());
229+
std::vector<std::atomic<size_t>> context(strategies.size());
237230

238231
//Represents the smallest number of colors used in a recently found colouring
239232
std::atomic<ColorCount> target_k(k);
@@ -268,7 +261,7 @@ namespace graph_colouring {
268261
for (size_t colouringId = 0; colouringId < populationSize; colouringId++) {
269262
for (size_t strategyId = 0; strategyId < strategies.size(); strategyId++) {
270263
lock[strategyId * populationSize + colouringId] = true;
271-
context[strategyId].wpCount.fetch_add(1);
264+
context[strategyId].fetch_add(1);
272265
workQueue.push({0, strategyId, target_k, colouringId});
273266
}
274267
}
@@ -285,16 +278,16 @@ namespace graph_colouring {
285278
target_k = mp.next_k - 1;
286279
for (size_t strategyId = 0; strategyId < strategies.size(); strategyId++) {
287280
if (strategies[strategyId]->isFixedKStrategy()) {
288-
//Wait until every worker stopped working on the effected population
289-
while (context[strategyId].wpCount > 0) {
281+
//Wait until every worker stopped working on the affected population
282+
while (context[strategyId] > 0) {
290283
std::this_thread::yield();
291284
}
292-
context[strategyId].wpCount = 0;
285+
context[strategyId] = 0;
293286
for (size_t colouringId = 0; colouringId < populationSize; colouringId++) {
294287
lock[strategyId * populationSize + colouringId] = true;
295288
}
296289
for (size_t colouringId = 0; colouringId < populationSize; colouringId++) {
297-
context[strategyId].wpCount.fetch_add(1);
290+
context[strategyId].fetch_add(1);
298291
workQueue.push({0, strategyId, target_k, colouringId});
299292
}
300293
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /