26 #include <spot/bricks/brick-hashset>
27 #include <spot/kripke/kripke.hh>
28 #include <spot/misc/common.hh>
29 #include <spot/misc/fixpool.hh>
30 #include <spot/misc/timer.hh>
31 #include <spot/twacube/twacube.hh>
32 #include <spot/mc/mc.hh>
36 template<
typename State,
typename SuccIterator,
37 typename StateHash,
typename StateEqual>
49 std::atomic<bool> blue;
50 std::atomic<bool> red;
64 state_hasher(
const product_state&)
67 state_hasher() =
default;
69 brick::hash::hash128_t
70 hash(
const product_state& lhs)
const
74 unsigned u = hash(lhs.st_kripke) % (1<<30);
80 bool equal(
const product_state& lhs,
81 const product_state& rhs)
const
84 return (lhs.st_prop == rhs.st_prop)
85 && equal(lhs.st_kripke, rhs.st_kripke);
92 SuccIterator* it_kripke;
93 std::shared_ptr<trans_index> it_prop;
100 using shared_map = brick::hashset::FastConcurrent <product_state,
104 static shared_struct* make_shared_structure(
shared_map m,
unsigned i)
110 shared_map& map, shared_struct* ,
111 unsigned tid, std::atomic<bool>& stop):
112 sys_(sys), twa_(
twa), tid_(tid), map_(map),
113 nb_th_(std::thread::hardware_concurrency()),
114 p_colors_(sizeof(cndfs_colors) +
115 sizeof(local_colors)*(std::thread::hardware_concurrency() - 1)),
119 State, SuccIterator>::value,
120 "error: does not match the kripkecube requirements");
121 SPOT_ASSERT(nb_th_ > tid);
124 virtual ~swarmed_cndfs()
126 while (!todo_blue_.empty())
128 sys_.recycle(todo_blue_.back().it_kripke, tid_);
129 todo_blue_.pop_back();
131 while (!todo_red_.empty())
133 sys_.recycle(todo_red_.back().it_kripke, tid_);
134 todo_red_.pop_back();
147 tm_.start(
"DFS thread " + std::to_string(tid_));
150 std::pair<bool, product_state>
151 push_blue(product_state s,
bool from_accepting)
153 cndfs_colors* c = (cndfs_colors*) p_colors_.allocate();
156 for (
unsigned i = 0; i < nb_th_; ++i)
158 c->l[i].cyan =
false;
159 c->l[i].is_in_Rp =
false;
165 auto it = map_.insert(s);
172 p_colors_.deallocate(c);
173 bool blue = ((*it)).colors->blue.load();
174 bool cyan = ((*it)).colors->l[tid_].cyan;
180 ((*it)).colors->l[tid_].cyan =
true;
182 todo_blue_.push_back({*it,
183 sys_.succ(((*it)).st_kripke, tid_),
184 twa_->succ(((*it)).st_prop),
189 std::pair<bool, product_state>
190 push_red(product_state s,
bool ignore_cyan)
193 auto it = map_.insert(s);
197 bool red = ((*it)).colors->red.load();
198 bool cyan = ((*it)).colors->l[tid_].cyan;
199 bool in_Rp = ((*it)).colors->l[tid_].is_in_Rp;
200 if (red || (cyan && !ignore_cyan) || in_Rp)
204 ((*it)).colors->l[tid_].is_in_Rp =
true;
207 todo_red_.push_back({*it,
208 sys_.succ(((*it)).st_kripke, tid_),
209 twa_->succ(((*it)).st_prop),
217 dfs_ = todo_blue_.size() > dfs_ ? todo_blue_.size() : dfs_;
219 todo_blue_.back().st.colors->l[tid_].cyan =
false;
220 sys_.recycle(todo_blue_.back().it_kripke, tid_);
221 todo_blue_.pop_back();
228 dfs_ = todo_blue_.size() + todo_red_.size() > dfs_ ?
229 todo_blue_.size() + todo_red_.size() : dfs_;
232 sys_.recycle(todo_red_.back().it_kripke, tid_);
233 todo_red_.pop_back();
239 bool tst_val =
false;
241 bool exchanged = stop_.compare_exchange_strong(tst_val, new_val);
244 tm_.stop(
"DFS thread " + std::to_string(tid_));
257 unsigned transitions()
264 return tm_.timer(
"DFS thread " + std::to_string(tid_)).walltime();
284 SPOT_ASSERT(!is_empty_);
286 auto state_equal = [equal](product_state a, product_state b)
288 return a.st_prop == b.st_prop
289 && equal(a.st_kripke, b.st_kripke);
292 std::string res =
"Prefix:\n";
294 auto it = todo_blue_.begin();
295 while (it != todo_blue_.end())
297 if (state_equal(((*it)).st, cycle_start_))
299 res +=
" " + std::to_string(((*it)).st.st_prop)
300 +
"*" + sys_.to_string(((*it)).st.st_kripke) +
"\n";
305 while (it != todo_blue_.end())
307 res +=
" " + std::to_string(((*it)).st.st_prop)
308 +
"*" + sys_.to_string(((*it)).st.st_kripke) +
"\n";
312 if (!todo_red_.empty())
314 it = todo_red_.begin() + 1;
315 while (it != todo_red_.end())
317 res +=
" " + std::to_string(((*it)).st.st_prop)
318 +
"*" + sys_.to_string(((*it)).st.st_kripke) +
"\n";
322 res +=
" " + std::to_string(cycle_start_.st_prop)
323 +
"*" + sys_.to_string(cycle_start_.st_kripke) +
"\n";
331 product_state initial = {sys_.initial(tid_),
334 if (!push_blue(initial,
false).first)
338 if (todo_blue_.back().it_prop->done())
341 forward_iterators(sys_, twa_, todo_blue_.back().it_kripke,
342 todo_blue_.back().it_prop,
true, tid_);
344 while (!todo_blue_.empty() && !stop_.load(std::memory_order_relaxed))
346 auto current = todo_blue_.back();
348 if (!current.it_kripke->done())
352 current.it_kripke->state(),
353 twa_->trans_storage(current.it_prop, tid_).dst,
357 bool acc = (bool) twa_->trans_storage(current.it_prop, tid_).acc_;
358 forward_iterators(sys_, twa_, todo_blue_.back().it_kripke,
359 todo_blue_.back().it_prop,
false, tid_);
361 auto tmp = push_blue(s, acc);
363 forward_iterators(sys_, twa_, todo_blue_.back().it_kripke,
364 todo_blue_.back().it_prop,
true, tid_);
370 if (tmp.second.colors->l[tid_].cyan)
378 SPOT_ASSERT(tmp.second.colors->blue);
388 current.st.colors->blue.store(
true);
391 if (current.from_accepting)
393 red_dfs(todo_blue_.back().st);
406 for (product_state& s: Rp_acc_)
408 while (s.colors->red.load() && !stop_.load())
413 for (product_state& s: Rp_)
415 s.colors->red.store(
true);
416 s.colors->l[tid_].is_in_Rp =
false;
423 void red_dfs(product_state initial)
425 auto init_push = push_red(initial,
true);
426 SPOT_ASSERT(init_push.second.colors->blue);
428 if (!init_push.first)
431 forward_iterators(sys_, twa_, todo_red_.back().it_kripke,
432 todo_red_.back().it_prop,
true, tid_);
434 while (!todo_red_.empty() && !stop_.load(std::memory_order_relaxed))
436 auto current = todo_red_.back();
438 if (!current.it_kripke->done())
442 current.it_kripke->state(),
443 twa_->trans_storage(current.it_prop, tid_).dst,
446 bool acc = (bool) twa_->trans_storage(current.it_prop, tid_).acc_;
447 forward_iterators(sys_, twa_, todo_red_.back().it_kripke,
448 todo_red_.back().it_prop,
false, tid_);
450 auto res = push_red(s,
false);
453 forward_iterators(sys_, twa_, todo_red_.back().it_kripke,
454 todo_red_.back().it_prop,
true, tid_);
456 SPOT_ASSERT(res.second.colors->blue);
464 for (
auto& st: Rp_acc_)
466 if (st.colors == res.second.colors)
473 Rp_acc_.push_back(Rp_.back());
478 if (res.second.colors->l[tid_].cyan)
483 if (init_push.second.colors == res.second.colors && !acc)
490 else if (acc && res.second.colors->l[tid_].is_in_Rp)
492 auto it = map_.insert(s);
493 Rp_acc_.push_back(*it);
504 kripkecube<State, SuccIterator>& sys_;
506 std::vector<todo_element> todo_blue_;
507 std::vector<todo_element> todo_red_;
508 unsigned transitions_ = 0;
512 unsigned states_ = 0;
515 fixed_size_pool<pool_type::Unsafe> p_colors_;
516 bool is_empty_ =
true;
517 std::atomic<bool>& stop_;
518 std::vector<product_state> Rp_;
519 std::vector<product_state> Rp_acc_;
520 product_state cycle_start_;
521 bool finisher_ =
false;
This class allows to ensure (at compile time) if a given parameter is of type kripkecube....
Definition: kripke.hh:72
This class is a template representation of a Kripke structure. It is composed of two template paramet...
Definition: kripke.hh:41
brick::hashset::FastConcurrent< product_state, state_hasher > shared_map
<
Definition: cndfs.hh:101
A map of timer, where each timer has a name.
Definition: timer.hh:229
A Transition-based ω-Automaton.
Definition: twa.hh:623
size_t wang32_hash(size_t key)
Thomas Wang's 32 bit hash function.
Definition: hashfunc.hh:41
Definition: automata.hh:27
mc_rvalue
Definition: mc.hh:44
@ NOT_EMPTY
The product is not empty.
@ EMPTY
The product is empty.