Commit bdfa296b authored by Tiago Peixoto's avatar Tiago Peixoto
Browse files

blockmodel.py: Improve hash performance via edge lookup cache

parent 9a702f44
......@@ -170,19 +170,59 @@ double do_get_ent_parallel(GraphInterface& gi, boost::any oweight)
}
boost::any do_create_emat(GraphInterface& gi)
boost::any do_create_emat(GraphInterface& gi, boost::any ob,
GraphInterface& bgi)
{
boost::any emat;
run_action<>()(gi, std::bind<void>(create_emat(), placeholders::_1,
std::ref(emat)))();
typedef property_map_type::apply<int32_t,
GraphInterface::vertex_index_map_t>::type
vmap_t;
vmap_t b = any_cast<vmap_t>(ob);
if (gi.GetDirected())
{
run_action<>()(gi, std::bind<void>(create_emat(), placeholders::_1,
std::ref(b),
std::ref(bgi.GetGraph()),
std::ref(emat)))();
}
else
{
UndirectedAdaptor<GraphInterface::multigraph_t> ug(bgi.GetGraph());
run_action<>()(gi, std::bind<void>(create_emat(), placeholders::_1,
std::ref(b),
std::ref(ug),
std::ref(emat)))();
}
return emat;
}
boost::any do_create_ehash(GraphInterface& gi)
boost::any do_create_ehash(GraphInterface& gi, boost::any ob,
GraphInterface& bgi, rng_t& rng)
{
boost::any emat;
run_action<>()(gi, std::bind<void>(create_ehash(), placeholders::_1,
std::ref(emat)))();
typedef property_map_type::apply<int32_t,
GraphInterface::vertex_index_map_t>::type
vmap_t;
vmap_t b = any_cast<vmap_t>(ob);
if (gi.GetDirected())
{
run_action<>()(gi, std::bind<void>(create_ehash(), placeholders::_1,
std::ref(b),
std::ref(bgi.GetGraph()),
std::ref(emat),
std::ref(rng)))();
}
else
{
UndirectedAdaptor<GraphInterface::multigraph_t> ug(bgi.GetGraph());
run_action<>()(gi, std::bind<void>(create_ehash(), placeholders::_1,
std::ref(b),
std::ref(ug),
std::ref(emat),
std::ref(rng)))();
}
return emat;
}
......
This diff is collapsed.
......@@ -421,14 +421,14 @@ struct overlap_partition_stats_t
bv_t bv(rs.begin(), rs.end());
_bvs[v] = bv;
_bvs[v].reserve(bv.size() * 2);
_nbvs[v] = bv;
_bvs[v] = bv;
_nbvs[v].reserve(bv.size() * 2);
_degs[v] = cdeg;
_nbvs[v] = bv;
_degs[v].reserve(cdeg.size() * 2);
_ndegs[v] = cdeg;
_degs[v] = cdeg;
_ndegs[v].reserve(cdeg.size() * 2);
_ndegs[v] = cdeg;
auto & cdh = _deg_hist[bv];
cdh[cdeg]++;
......@@ -808,8 +808,7 @@ struct overlap_partition_stats_t
}
size_t bv_count = get_bv_count(bv);
size_t n_bv_count = get_bv_count(n_bv);
size_t bv_count, n_bv_count;
auto get_S_b = [&] (bool is_bv, int delta) -> double
{
......@@ -820,6 +819,8 @@ struct overlap_partition_stats_t
if (!is_same_bv)
{
bv_count = get_bv_count(bv);
n_bv_count = get_bv_count(n_bv);
S_b += get_S_b(true, 0) + get_S_b(false, 0);
S_a += get_S_b(true, -1) + get_S_b(false, 1);
}
......@@ -1248,7 +1249,8 @@ public:
void set_move(size_t, size_t) {}
void insert_delta(size_t r, size_t s, int delta, bool source)
void insert_delta(size_t r, size_t s, int delta, bool source,
size_t mrs = numeric_limits<size_t>::max())
{
if (source)
_entries[_pos] = make_pair(s, r);
......@@ -1258,6 +1260,7 @@ public:
_entries[_pos].second < _entries[_pos].first)
std::swap(_entries[_pos].first, _entries[_pos].second);
_delta[_pos] = delta;
_mrs[_pos] = mrs;
++_pos;
}
......@@ -1270,13 +1273,15 @@ public:
void clear() { _pos = 0; }
std::array<pair<size_t, size_t>,2>& get_entries() { return _entries; }
std::array<int, 2>& get_delta() { return _delta; }
const std::array<pair<size_t, size_t>,2>& get_entries() { return _entries; }
const std::array<int, 2>& get_delta() { return _delta; }
std::array<size_t, 2>& get_mrs() { return _mrs; }
private:
size_t _pos;
std::array<pair<size_t, size_t>, 2> _entries;
std::array<int, 2> _delta;
std::array<size_t, 2> _mrs;
};
template <class RNG>
......
......@@ -75,6 +75,8 @@ public:
_probs[_small[i]] = 1;
_large.clear();
_small.clear();
_sample = uniform_int_distribution<size_t>(0, _probs.size() - 1);
}
Sampler() {}
......@@ -82,9 +84,7 @@ public:
template <class RNG>
const Value& sample(RNG& rng)
{
uniform_int_distribution<size_t> sample(0, _probs.size() - 1);
size_t i = sample(rng);
size_t i = _sample(rng);
bernoulli_distribution coin(_probs[i]);
if (coin(rng))
return _items[i];
......@@ -104,6 +104,8 @@ private:
vector<size_t> _alias;
vector<size_t> _small;
vector<size_t> _large;
uniform_int_distribution<size_t> _sample;
};
// uniform sampling from containers
......
......@@ -188,7 +188,6 @@ class BlockState(object):
else:
self.clabel = self.g.new_vertex_property("int")
self.emat = None
if max_BE is None:
max_BE = 1000
self.max_BE = max_BE
......@@ -334,9 +333,14 @@ class BlockState(object):
def __regen_emat(self):
if self.B <= self.max_BE:
self.emat = libcommunity.create_emat(self.bg._Graph__graph)
self.emat = libcommunity.create_emat(self.g._Graph__graph,
_prop("v", self.g, self.b),
self.bg._Graph__graph)
else:
self.emat = libcommunity.create_ehash(self.bg._Graph__graph)
self.emat = libcommunity.create_ehash(self.g._Graph__graph,
_prop("v", self.g, self.b),
self.bg._Graph__graph,
_get_rng())
def __build_egroups(self, empty=False):
self.esrcpos = self.g.new_edge_property("int")
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment