Commit dfee9795 authored by Etienne Renault's avatar Etienne Renault
Browse files

mc: refactor parallel algorithms

* spot/mc/Makefile.am,
spot/mc/bloemen.hh,
spot/mc/bloemen_ec.hh,
spot/mc/cndfs.hh,
spot/mc/deadlock.hh,
spot/mc/ec.hh,
spot/mc/intersect.hh,
spot/mc/mc.hh,
spot/mc/mc_instanciator.hh,
spot/mc/utils.hh,
tests/ltsmin/modelcheck.cc: Here.
parent 86f2f646
......@@ -22,7 +22,7 @@ AM_CXXFLAGS = $(WARNING_CXXFLAGS)
mcdir = $(pkgincludedir)/mc
mc_HEADERS = reachability.hh intersect.hh ec.hh unionfind.hh utils.hh\
mc.hh deadlock.hh bloemen.hh bloemen_ec.hh cndfs.hh
mc.hh mc_instanciator.hh deadlock.hh bloemen.hh bloemen_ec.hh cndfs.hh
noinst_LTLIBRARIES = libmc.la
......
// -*- coding: utf-8 -*-
// Copyright (C) 2015, 2016, 2017, 2018, 2019 Laboratoire de Recherche et
// Copyright (C) 2015, 2016, 2017, 2018, 2019, 2020 Laboratoire de Recherche et
// Developpement de l'Epita
//
// This file is part of Spot, a model checking library.
......@@ -21,15 +21,16 @@
#include <atomic>
#include <chrono>
#include <spot/bricks/brick-hashset>
#include <stdlib.h>
#include <thread>
#include <vector>
#include <utility>
#include <spot/misc/common.hh>
#include <spot/bricks/brick-hashset>
#include <spot/kripke/kripke.hh>
#include <spot/misc/common.hh>
#include <spot/misc/fixpool.hh>
#include <spot/misc/timer.hh>
#include <spot/mc/mc.hh>
namespace spot
{
......@@ -405,16 +406,6 @@ namespace spot
fixed_size_pool<pool_type::Unsafe> p_; ///< \brief The allocator
};
/// \brief This object is returned by the algorithm below
struct SPOT_API bloemen_stats
{
unsigned inserted; ///< \brief Number of states inserted
unsigned states; ///< \brief Number of states visited
unsigned transitions; ///< \brief Number of transitions visited
unsigned sccs; ///< \brief Number of SCCs visited
unsigned walltime; ///< \brief Walltime for this thread in ms
};
/// \brief This class implements the SCC decomposition algorithm of bloemen
/// as described in PPOPP'16. It uses a shared union-find augmented to manage
/// work stealing between threads.
......@@ -426,10 +417,25 @@ namespace spot
swarmed_bloemen() = delete;
public:
swarmed_bloemen(kripkecube<State, SuccIterator>& sys,
iterable_uf<State, StateHash, StateEqual>& uf,
unsigned tid):
sys_(sys), uf_(uf), tid_(tid),
using uf = iterable_uf<State, StateHash, StateEqual>;
using uf_element = typename uf::uf_element;
using shared_struct = uf;
using shared_map = typename uf::shared_map;
static shared_struct* make_shared_st(shared_map m, unsigned i)
{
return new uf(m, i);
}
swarmed_bloemen(kripkecube<State, SuccIterator>& sys,
twacube_ptr, /* useless here */
shared_map& map, /* useless here */
iterable_uf<State, StateHash, StateEqual>* uf,
unsigned tid,
std::atomic<bool>& /*useless here*/):
sys_(sys), uf_(*uf), tid_(tid),
nb_th_(std::thread::hardware_concurrency())
{
static_assert(spot::is_a_kripkecube_ptr<decltype(&sys),
......@@ -437,13 +443,9 @@ namespace spot
"error: does not match the kripkecube requirements");
}
using uf = iterable_uf<State, StateHash, StateEqual>;
using uf_element = typename uf::uf_element;
void run()
{
tm_.start("DFS thread " + std::to_string(tid_));
setup();
State init = sys_.initial(tid_);
auto pair = uf_.make_claim(init);
todo_.push_back(pair.second);
......@@ -496,17 +498,53 @@ namespace spot
Rp_.pop_back();
todo_.pop_back();
}
finalize();
}
void setup()
{
tm_.start("DFS thread " + std::to_string(tid_));
}
void finalize()
{
tm_.stop("DFS thread " + std::to_string(tid_));
}
unsigned states()
{
return states_;
}
unsigned transitions()
{
return transitions_;
}
unsigned walltime()
{
return tm_.timer("DFS thread " + std::to_string(tid_)).walltime();
}
bloemen_stats stats()
std::string name()
{
return "bloemen_scc";
}
int sccs()
{
return sccs_;
}
mc_rvalue result()
{
return mc_rvalue::SUCCESS;
}
std::string trace()
{
return {uf_.inserted(), states_, transitions_, sccs_, walltime()};
// Returning a trace has no sense in this algorithm
return "";
}
private:
......
// -*- coding: utf-8 -*-
// Copyright (C) 2015, 2016, 2017, 2018, 2019 Laboratoire de Recherche et
// Copyright (C) 2015, 2016, 2017, 2018, 2019, 2020 Laboratoire de Recherche et
// Developpement de l'Epita
//
// This file is part of Spot, a model checking library.
......@@ -31,6 +31,7 @@
#include <spot/misc/fixpool.hh>
#include <spot/misc/timer.hh>
#include <spot/twacube/twacube.hh>
#include <spot/mc/intersect.hh>
namespace spot
{
......@@ -100,7 +101,6 @@ namespace spot
using shared_map = brick::hashset::FastConcurrent <uf_element*,
uf_element_hasher>;
iterable_uf_ec(shared_map& map, unsigned tid):
map_(map), tid_(tid), size_(std::thread::hardware_concurrency()),
nb_th_(std::thread::hardware_concurrency()), inserted_(0),
......@@ -446,17 +446,6 @@ namespace spot
fixed_size_pool<pool_type::Unsafe> p_; ///< \brief The allocator
};
/// \brief This object is returned by the algorithm below
struct SPOT_API bloemen_ec_stats
{
unsigned inserted; ///< \brief Number of states inserted
unsigned states; ///< \brief Number of states visited
unsigned transitions; ///< \brief Number of transitions visited
unsigned sccs; ///< \brief Number of SCCs visited
bool is_empty; ///< \brief Is the model empty
unsigned walltime; ///< \brief Walltime for this thread in ms
};
/// \brief This class implements the SCC decomposition algorithm of bloemen
/// as described in PPOPP'16. It uses a shared union-find augmented to manage
/// work stealing between threads.
......@@ -466,12 +455,24 @@ namespace spot
{
public:
using uf = iterable_uf_ec<State, StateHash, StateEqual>;
using uf_element = typename uf::uf_element;
using shared_struct = uf;
using shared_map = typename uf::shared_map;
static shared_struct* make_shared_st(shared_map m, unsigned i)
{
return new uf(m, i);
}
swarmed_bloemen_ec(kripkecube<State, SuccIterator>& sys,
twacube_ptr twa,
iterable_uf_ec<State, StateHash, StateEqual>& uf,
shared_map& map, /* useless here */
iterable_uf_ec<State, StateHash, StateEqual>* uf,
unsigned tid,
std::atomic<bool>& stop):
sys_(sys), twa_(twa), uf_(uf), tid_(tid),
sys_(sys), twa_(twa), uf_(*uf), tid_(tid),
nb_th_(std::thread::hardware_concurrency()),
stop_(stop)
{
......@@ -480,12 +481,9 @@ namespace spot
"error: does not match the kripkecube requirements");
}
using uf = iterable_uf_ec<State, StateHash, StateEqual>;
using uf_element = typename uf::uf_element;
void run()
{
tm_.start("DFS thread " + std::to_string(tid_));
setup();
State init_kripke = sys_.initial(tid_);
unsigned init_twa = twa_->get_initial();
auto pair = uf_.make_claim(init_kripke, init_twa);
......@@ -509,7 +507,7 @@ namespace spot
auto it_kripke = sys_.succ(v_prime->st_kripke, tid_);
auto it_prop = twa_->succ(v_prime->st_prop);
forward_iterators(it_kripke, it_prop, true);
forward_iterators(sys_, twa_, it_kripke, it_prop, true, tid_);
while (!it_kripke->done())
{
auto w = uf_.make_claim(it_kripke->state(),
......@@ -558,7 +556,8 @@ namespace spot
return;
}
}
forward_iterators(it_kripke, it_prop, false);
forward_iterators(sys_, twa_, it_kripke, it_prop,
false, tid_);
}
uf_.remove_from_list(v_prime);
sys_.recycle(it_kripke, tid_);
......@@ -568,53 +567,27 @@ namespace spot
Rp_.pop_back();
todo_.pop_back();
}
finalize();
}
void setup()
{
tm_.start("DFS thread " + std::to_string(tid_));
}
void finalize()
{
tm_.stop("DFS thread " + std::to_string(tid_));
}
/// \brief Find the first couple of iterator (from the top of the
/// todo stack) that intersect. The \a parameter indicates wheter
/// the state has just been pushed since the underlying job is
/// slightly different.
void forward_iterators(SuccIterator* it_kripke,
std::shared_ptr<trans_index> it_prop,
bool just_pushed)
unsigned states()
{
SPOT_ASSERT(!(it_prop->done() &&
it_kripke->done()));
// Sometimes kripke state may have no successors.
if (it_kripke->done())
return;
// The state has just been push and the 2 iterators intersect.
// There is no need to move iterators forward.
SPOT_ASSERT(!(it_prop->done()));
if (just_pushed && twa_->get_cubeset()
.intersect(twa_->trans_data(it_prop, tid_).cube_,
it_kripke->condition()))
return;
// Otherwise we have to compute the next valid successor (if it exits).
// This requires two loops. The most inner one is for the twacube since
// its costless
if (it_prop->done())
it_prop->reset();
else
it_prop->next();
return states_;
}
while (!it_kripke->done())
{
while (!it_prop->done())
{
if (SPOT_UNLIKELY(twa_->get_cubeset()
.intersect(twa_->trans_data(it_prop, tid_).cube_,
it_kripke->condition())))
return;
it_prop->next();
}
it_prop->reset();
it_kripke->next();
}
unsigned transitions()
{
return transitions_;
}
unsigned walltime()
......@@ -622,15 +595,19 @@ namespace spot
return tm_.timer("DFS thread " + std::to_string(tid_)).walltime();
}
bool is_empty()
std::string name()
{
return "bloemen_ec";
}
int sccs()
{
return is_empty_;
return sccs_;
}
bloemen_ec_stats stats()
mc_rvalue result()
{
return {uf_.inserted(), states_, transitions_, sccs_, is_empty_,
walltime()};
return is_empty_ ? mc_rvalue::EMPTY : mc_rvalue::NOT_EMPTY;
}
std::string trace()
......
// -*- coding: utf-8 -*-
// Copyright (C) 2015, 2016, 2017, 2018, 2019 Laboratoire de Recherche et
// Copyright (C) 2015, 2016, 2017, 2018, 2019, 2020 Laboratoire de Recherche et
// Developpement de l'Epita
//
// This file is part of Spot, a model checking library.
......@@ -29,22 +29,13 @@
#include <spot/misc/fixpool.hh>
#include <spot/misc/timer.hh>
#include <spot/twacube/twacube.hh>
#include <spot/mc/mc.hh>
namespace spot
{
/// \brief This object is returned by the algorithm below
struct SPOT_API cndfs_stats
{
unsigned states; ///< \brief Number of states visited
unsigned transitions; ///< \brief Number of transitions visited
unsigned instack_dfs; ///< \brief Maximum DFS stack
bool is_empty; ///< \brief Is the model empty
unsigned walltime; ///< \brief Walltime for this thread in ms
};
template<typename State, typename SuccIterator,
typename StateHash, typename StateEqual>
class swarmed_cndfs
class SPOT_API swarmed_cndfs
{
struct local_colors
{
......@@ -95,7 +86,7 @@ namespace spot
}
};
struct todo__element
struct todo_element
{
product_state st;
SuccIterator* it_kripke;
......@@ -108,9 +99,16 @@ namespace spot
///< \brief Shortcut to ease shared map manipulation
using shared_map = brick::hashset::FastConcurrent <product_state,
state_hasher>;
using shared_struct = shared_map;
static shared_struct* make_shared_st(shared_map m, unsigned i)
{
return nullptr; // Useless here.
}
swarmed_cndfs(kripkecube<State, SuccIterator>& sys, twacube_ptr twa,
shared_map& map, unsigned tid, std::atomic<bool>& stop):
shared_map& map, shared_struct* /* useless here*/,
unsigned tid, std::atomic<bool>& stop):
sys_(sys), twa_(twa), tid_(tid), map_(map),
nb_th_(std::thread::hardware_concurrency()),
p_colors_(sizeof(cndfs_colors) +
......@@ -120,6 +118,7 @@ namespace spot
static_assert(spot::is_a_kripkecube_ptr<decltype(&sys),
State, SuccIterator>::value,
"error: does not match the kripkecube requirements");
SPOT_ASSERT(nb_th_ > tid);
}
virtual ~swarmed_cndfs()
......@@ -136,6 +135,13 @@ namespace spot
}
}
void run()
{
setup();
blue_dfs();
finalize();
}
void setup()
{
tm_.start("DFS thread " + std::to_string(tid_));
......@@ -180,17 +186,6 @@ namespace spot
return {true, *it};
}
bool pop_blue()
{
// Track maximum dfs size
dfs_ = todo_blue_.size() > dfs_ ? todo_blue_.size() : dfs_;
todo_blue_.back().st.colors->l[tid_].cyan = false;
sys_.recycle(todo_blue_.back().it_kripke, tid_);
todo_blue_.pop_back();
return true;
}
std::pair<bool, product_state>
push_red(product_state s, bool ignore_cyan)
{
......@@ -216,6 +211,17 @@ namespace spot
return {true, *it};
}
bool pop_blue()
{
// Track maximum dfs size
dfs_ = todo_blue_.size() > dfs_ ? todo_blue_.size() : dfs_;
todo_blue_.back().st.colors->l[tid_].cyan = false;
sys_.recycle(todo_blue_.back().it_kripke, tid_);
todo_blue_.pop_back();
return true;
}
bool pop_red()
{
// Track maximum dfs size
......@@ -243,13 +249,73 @@ namespace spot
return transitions_;
}
void run()
unsigned walltime()
{
setup();
blue_dfs();
finalize();
return tm_.timer("DFS thread " + std::to_string(tid_)).walltime();
}
std::string name()
{
return "cndfs";
}
int sccs()
{
return -1;
}
mc_rvalue result()
{
return is_empty_ ? mc_rvalue::EMPTY : mc_rvalue::NOT_EMPTY;
}
std::string trace()
{
SPOT_ASSERT(!is_empty_);
StateEqual equal;
auto state_equal = [equal](product_state a, product_state b)
{
return a.st_prop == b.st_prop
&& equal(a.st_kripke, b.st_kripke);
};
std::string res = "Prefix:\n";
auto it = todo_blue_.begin();
while (it != todo_blue_.end())
{
if (state_equal(((*it)).st, cycle_start_))
break;
res += " " + std::to_string(((*it)).st.st_prop)
+ "*" + sys_.to_string(((*it)).st.st_kripke) + "\n";
++it;
}
res += "Cycle:\n";
while (it != todo_blue_.end())
{
res += " " + std::to_string(((*it)).st.st_prop)
+ "*" + sys_.to_string(((*it)).st.st_kripke) + "\n";
++it;
}
if (!todo_red_.empty())
{
it = todo_red_.begin() + 1; // skip first element, also in blue
while (it != todo_red_.end())
{
res += " " + std::to_string(((*it)).st.st_prop)
+ "*" + sys_.to_string(((*it)).st.st_kripke) + "\n";
++it;
}
}
res += " " + std::to_string(cycle_start_.st_prop)
+ "*" + sys_.to_string(cycle_start_.st_kripke) + "\n";
return res;
}
private:
void blue_dfs()
{
product_state initial = {sys_.initial(tid_),
......@@ -262,7 +328,8 @@ namespace spot
if (todo_blue_.back().it_prop->done())
return;
forward_iterators(todo_blue_, true);
forward_iterators(sys_, twa_, todo_blue_.back().it_kripke,
todo_blue_.back().it_prop, true, tid_);
while (!todo_blue_.empty() && !stop_.load(std::memory_order_relaxed))
{
......@@ -278,11 +345,13 @@ namespace spot
};
bool acc = (bool) twa_->trans_storage(current.it_prop, tid_).acc_;
forward_iterators(todo_blue_, false);
forward_iterators(sys_, twa_, todo_blue_.back().it_kripke,
todo_blue_.back().it_prop, false, tid_);
auto tmp = push_blue(s, acc);
if (tmp.first)
forward_iterators(todo_blue_, true);
forward_iterators(sys_, twa_, todo_blue_.back().it_kripke,
todo_blue_.back().it_prop, true, tid_);
else if (acc)
{
// The state cyan and we can reach it throught an
......@@ -324,14 +393,14 @@ namespace spot
void post_red_dfs()
{
for (product_state& s : Rp_acc_)
for (product_state& s: Rp_acc_)
{
while (s.colors->red.load() && !stop_.load())
{
// await
}
}
for (product_state& s : Rp_)
for (product_state& s: Rp_)
{
s.colors->red.store(true);
s.colors->l[tid_].is_in_Rp = false; // empty Rp
......@@ -349,7 +418,8 @@ namespace spot
if (!init_push.first)
return;
forward_iterators(todo_red_, true);
forward_iterators(sys_, twa_, todo_red_.back().it_kripke,
todo_red_.back().it_prop, true, tid_);
while (!todo_red_.empty() && !stop_.load(std::memory_order_relaxed))
{
......@@ -364,12 +434,14 @@ namespace spot
nullptr
};
bool acc = (bool) twa_->trans_storage(current.it_prop, tid_).acc_;
forward_iterators(todo_red_, false);
forward_iterators(sys_, twa_, todo_red_.back().it_kripke,
todo_red_.back().it_prop, false, tid_);
auto res = push_red(s, false);
if (res.first) // could push properly
{
forward_iterators(todo_red_, true);
forward_iterators(sys_, twa_, todo_red_.back().it_kripke,
todo_red_.back().it_prop, true, tid_);
SPOT_ASSERT(res.second.colors->blue);
......@@ -419,130 +491,22 @@ namespace spot
}
}
std::string trace()
{
SPOT_ASSERT(!is_empty());
StateEqual equal;
auto state_equal = [equal](product_state a, product_state b)
{
return a.st_prop == b.st_prop
&& equal(a.st_kripke, b.st_kripke);
};
std::string res = "Prefix:\n";
auto it = todo_blue_.begin();
while (it != todo_blue_.end())
{
if (state_equal(((*it)).st, cycle_start_))
break;
res += " " + std::to_string(((*it)).st.st_prop)
+ "*" + sys_.to_string(((*it)).st.st_kripke) + "\n";
++it;
}
res += "Cycle:\n";