25#include <spot/bricks/brick-hashset>
26#include <spot/kripke/kripke.hh>
27#include <spot/misc/common.hh>
28#include <spot/misc/fixpool.hh>
29#include <spot/misc/timer.hh>
30#include <spot/twacube/twacube.hh>
31#include <spot/mc/mc.hh>
35 template<
typename State,
typename SuccIterator,
36 typename StateHash,
typename StateEqual>
48 std::atomic<bool> blue;
49 std::atomic<bool> red;
63 state_hasher(
const product_state&)
66 state_hasher() =
default;
68 brick::hash::hash128_t
69 hash(
const product_state& lhs)
const
73 unsigned u = hash(lhs.st_kripke) % (1<<30);
79 bool equal(
const product_state& lhs,
80 const product_state& rhs)
const
83 return (lhs.st_prop == rhs.st_prop)
84 && equal(lhs.st_kripke, rhs.st_kripke);
91 SuccIterator* it_kripke;
92 std::shared_ptr<trans_index> it_prop;
99 using shared_map = brick::hashset::FastConcurrent <product_state,
103 static shared_struct* make_shared_structure(
shared_map m,
unsigned i)
109 shared_map& map, shared_struct* ,
110 unsigned tid, std::atomic<bool>& stop):
111 sys_(sys), twa_(
twa), tid_(tid), map_(map),
112 nb_th_(std::thread::hardware_concurrency()),
113 p_colors_(sizeof(cndfs_colors) +
114 sizeof(local_colors)*(std::thread::hardware_concurrency() - 1)),
118 State, SuccIterator>::value,
119 "error: does not match the kripkecube requirements");
120 SPOT_ASSERT(nb_th_ > tid);
123 virtual ~swarmed_cndfs()
125 while (!todo_blue_.empty())
127 sys_.recycle(todo_blue_.back().it_kripke, tid_);
128 todo_blue_.pop_back();
130 while (!todo_red_.empty())
132 sys_.recycle(todo_red_.back().it_kripke, tid_);
133 todo_red_.pop_back();
146 tm_.start(
"DFS thread " + std::to_string(tid_));
149 std::pair<bool, product_state>
150 push_blue(product_state s,
bool from_accepting)
152 cndfs_colors* c = (cndfs_colors*) p_colors_.allocate();
155 for (
unsigned i = 0; i < nb_th_; ++i)
157 c->l[i].cyan =
false;
158 c->l[i].is_in_Rp =
false;
164 auto it = map_.insert(s);
171 p_colors_.deallocate(c);
172 bool blue = ((*it)).colors->blue.load();
173 bool cyan = ((*it)).colors->l[tid_].cyan;
179 ((*it)).colors->l[tid_].cyan =
true;
181 todo_blue_.push_back({*it,
182 sys_.succ(((*it)).st_kripke, tid_),
183 twa_->succ(((*it)).st_prop),
188 std::pair<bool, product_state>
189 push_red(product_state s,
bool ignore_cyan)
192 auto it = map_.insert(s);
193 SPOT_ASSERT(!it.isnew());
194 bool red = ((*it)).colors->red.load();
195 bool cyan = ((*it)).colors->l[tid_].cyan;
196 bool in_Rp = ((*it)).colors->l[tid_].is_in_Rp;
197 if (red || (cyan && !ignore_cyan) || in_Rp)
201 ((*it)).colors->l[tid_].is_in_Rp =
true;
204 todo_red_.push_back({*it,
205 sys_.succ(((*it)).st_kripke, tid_),
206 twa_->succ(((*it)).st_prop),
214 dfs_ = todo_blue_.size() > dfs_ ? todo_blue_.size() : dfs_;
216 todo_blue_.back().st.colors->l[tid_].cyan =
false;
217 sys_.recycle(todo_blue_.back().it_kripke, tid_);
218 todo_blue_.pop_back();
225 dfs_ = todo_blue_.size() + todo_red_.size() > dfs_ ?
226 todo_blue_.size() + todo_red_.size() : dfs_;
229 sys_.recycle(todo_red_.back().it_kripke, tid_);
230 todo_red_.pop_back();
236 bool tst_val =
false;
238 bool exchanged = stop_.compare_exchange_strong(tst_val, new_val);
241 tm_.stop(
"DFS thread " + std::to_string(tid_));
254 unsigned transitions()
261 return tm_.timer(
"DFS thread " + std::to_string(tid_)).walltime();
281 SPOT_ASSERT(!is_empty_);
283 auto state_equal = [equal](product_state a, product_state b)
285 return a.st_prop == b.st_prop
286 && equal(a.st_kripke, b.st_kripke);
289 std::string res =
"Prefix:\n";
291 auto it = todo_blue_.begin();
292 while (it != todo_blue_.end())
294 if (state_equal(((*it)).st, cycle_start_))
296 res +=
" " + std::to_string(((*it)).st.st_prop)
297 +
"*" + sys_.to_string(((*it)).st.st_kripke) +
"\n";
302 while (it != todo_blue_.end())
304 res +=
" " + std::to_string(((*it)).st.st_prop)
305 +
"*" + sys_.to_string(((*it)).st.st_kripke) +
"\n";
309 if (!todo_red_.empty())
311 it = todo_red_.begin() + 1;
312 while (it != todo_red_.end())
314 res +=
" " + std::to_string(((*it)).st.st_prop)
315 +
"*" + sys_.to_string(((*it)).st.st_kripke) +
"\n";
319 res +=
" " + std::to_string(cycle_start_.st_prop)
320 +
"*" + sys_.to_string(cycle_start_.st_kripke) +
"\n";
328 product_state initial = {sys_.initial(tid_),
331 if (!push_blue(initial,
false).first)
335 if (todo_blue_.back().it_prop->done())
338 forward_iterators(sys_, twa_, todo_blue_.back().it_kripke,
339 todo_blue_.back().it_prop,
true, tid_);
341 while (!todo_blue_.empty() && !stop_.load(std::memory_order_relaxed))
343 auto current = todo_blue_.back();
345 if (!current.it_kripke->done())
349 current.it_kripke->state(),
350 twa_->trans_storage(current.it_prop, tid_).dst,
354 bool acc = (bool) twa_->trans_storage(current.it_prop, tid_).acc_;
355 forward_iterators(sys_, twa_, todo_blue_.back().it_kripke,
356 todo_blue_.back().it_prop,
false, tid_);
358 auto tmp = push_blue(s, acc);
360 forward_iterators(sys_, twa_, todo_blue_.back().it_kripke,
361 todo_blue_.back().it_prop,
true, tid_);
367 if (tmp.second.colors->l[tid_].cyan)
375 SPOT_ASSERT(tmp.second.colors->blue);
385 current.st.colors->blue.store(
true);
388 if (current.from_accepting)
390 red_dfs(todo_blue_.back().st);
403 for (product_state& s: Rp_acc_)
405 while (s.colors->red.load() && !stop_.load())
410 for (product_state& s: Rp_)
412 s.colors->red.store(
true);
413 s.colors->l[tid_].is_in_Rp =
false;
420 void red_dfs(product_state initial)
422 auto init_push = push_red(initial,
true);
423 SPOT_ASSERT(init_push.second.colors->blue);
425 if (!init_push.first)
428 forward_iterators(sys_, twa_, todo_red_.back().it_kripke,
429 todo_red_.back().it_prop,
true, tid_);
431 while (!todo_red_.empty() && !stop_.load(std::memory_order_relaxed))
433 auto current = todo_red_.back();
435 if (!current.it_kripke->done())
439 current.it_kripke->state(),
440 twa_->trans_storage(current.it_prop, tid_).dst,
443 bool acc = (bool) twa_->trans_storage(current.it_prop, tid_).acc_;
444 forward_iterators(sys_, twa_, todo_red_.back().it_kripke,
445 todo_red_.back().it_prop,
false, tid_);
447 auto res = push_red(s,
false);
450 forward_iterators(sys_, twa_, todo_red_.back().it_kripke,
451 todo_red_.back().it_prop,
true, tid_);
453 SPOT_ASSERT(res.second.colors->blue);
461 for (
auto& st: Rp_acc_)
463 if (st.colors == res.second.colors)
470 Rp_acc_.push_back(Rp_.back());
475 if (res.second.colors->l[tid_].cyan)
480 if (init_push.second.colors == res.second.colors && !acc)
487 else if (acc && res.second.colors->l[tid_].is_in_Rp)
489 auto it = map_.insert(s);
490 Rp_acc_.push_back(*it);
501 kripkecube<State, SuccIterator>& sys_;
503 std::vector<todo_element> todo_blue_;
504 std::vector<todo_element> todo_red_;
505 unsigned transitions_ = 0;
509 unsigned states_ = 0;
512 fixed_size_pool<pool_type::Unsafe> p_colors_;
513 bool is_empty_ =
true;
514 std::atomic<bool>& stop_;
515 std::vector<product_state> Rp_;
516 std::vector<product_state> Rp_acc_;
517 product_state cycle_start_;
518 bool finisher_ =
false;
This class allows to ensure (at compile time) if a given parameter is of type kripkecube....
Definition: kripke.hh:71
This class is a template representation of a Kripke structure. It is composed of two template paramet...
Definition: kripke.hh:40
brick::hashset::FastConcurrent< product_state, state_hasher > shared_map
<
Definition: cndfs.hh:100
A map of timer, where each timer has a name.
Definition: timer.hh:225
A Transition-based ω-Automaton.
Definition: twa.hh:619
size_t wang32_hash(size_t key)
Thomas Wang's 32 bit hash function.
Definition: hashfunc.hh:37
Definition: automata.hh:26
mc_rvalue
Definition: mc.hh:43
@ NOT_EMPTY
The product is not empty.
@ EMPTY
The product is empty.