Commit 775878d7 authored by Tiago Peixoto's avatar Tiago Peixoto
Browse files

blockmodel: avoid copying of entropy_args_t

parent 2fe5bab4
......@@ -1356,7 +1356,7 @@ public:
template <class MEntries>
double virtual_move(size_t v, size_t r, size_t nr, entropy_args_t ea,
double virtual_move(size_t v, size_t r, size_t nr, const entropy_args_t& ea,
MEntries& m_entries)
{
assert(size_t(_b[v]) == r || r == null_group);
......@@ -1483,7 +1483,7 @@ public:
double propagate_entries_dS(size_t u, size_t v, int du, int dv,
std::vector<std::tuple<size_t, size_t, GraphInterface::edge_t, int,
std::vector<double>>>& entries,
entropy_args_t& ea, std::vector<double>& dBdx,
const entropy_args_t& ea, std::vector<double>& dBdx,
int dL)
{
openmp_scoped_lock lock(_lock);
......@@ -1651,13 +1651,13 @@ public:
return dS;
}
double virtual_move(size_t v, size_t r, size_t nr, entropy_args_t ea)
double virtual_move(size_t v, size_t r, size_t nr, const entropy_args_t& ea)
{
return virtual_move(v, r, nr, ea, _m_entries);
}
double get_delta_partition_dl(size_t v, size_t r, size_t nr,
entropy_args_t& ea)
const entropy_args_t& ea)
{
if (r == nr)
return 0;
......@@ -2029,7 +2029,7 @@ public:
return S;
}
double entropy(entropy_args_t ea, bool propagate=false)
double entropy(const entropy_args_t& ea, bool propagate=false)
{
double S = 0, S_dl = 0;
......@@ -2162,7 +2162,7 @@ public:
}
template <bool Add>
double edge_entropy_term(size_t u, size_t v, entropy_args_t ea)
double edge_entropy_term(size_t u, size_t v, const entropy_args_t& ea)
{
double S = 0, S_dl = 0;
size_t r = _b[u];
......@@ -2348,14 +2348,14 @@ public:
return S + S_dl * ea.beta_dl;
}
double edge_entropy_term(size_t u, size_t v, entropy_args_t ea)
double edge_entropy_term(size_t u, size_t v, const entropy_args_t& ea)
{
return edge_entropy_term<true>(u, v, ea);
}
template <bool Add>
double modify_edge_dS(size_t u, size_t v, GraphInterface::edge_t& e,
const std::vector<double>& recs, entropy_args_t ea)
const std::vector<double>& recs, const entropy_args_t& ea)
{
double dS = 0;
dS -= edge_entropy_term<Add>(u, v, ea);
......@@ -2441,7 +2441,7 @@ public:
disable_partition_stats();
}
void couple_state(BlockStateVirtualBase& s, entropy_args_t ea)
void couple_state(BlockStateVirtualBase& s, const entropy_args_t& ea)
{
_coupled_state = &s;
_coupled_entropy_args = ea;
......
......@@ -157,6 +157,7 @@ public:
const pair<size_t, size_t>& get_move() { return _rnr; }
template <bool First, bool Source>
inline __attribute__((always_inline)) __attribute__((flatten))
size_t& get_field_rnr(size_t s, size_t t)
{
auto& out_field = First ? _r_out_field : _nr_out_field;
......@@ -171,6 +172,7 @@ public:
}
}
inline __attribute__((always_inline)) __attribute__((flatten))
size_t& get_field(size_t s, size_t t)
{
if (s == _rnr.first)
......@@ -185,6 +187,7 @@ public:
}
template <bool Add, class... DVals>
inline __attribute__((always_inline)) __attribute__((flatten))
void insert_delta_dispatch(size_t s, size_t t, size_t& f, int d, DVals&&... delta)
{
if (f == _null)
......@@ -196,7 +199,7 @@ public:
_edelta.emplace_back();
}
if (Add)
if constexpr (Add)
{
_delta[f] += d;
tuple_op(_edelta[f], [&](auto&& r, auto&& v){ r += v; },
......@@ -211,7 +214,7 @@ public:
}
template <bool First, bool Source, bool Add, class... DVals>
__attribute__((flatten))
inline __attribute__((always_inline)) __attribute__((flatten))
void insert_delta_rnr(size_t s, size_t t, int d, DVals&&... delta)
{
auto& f = get_field_rnr<First, Source>(s, t);
......@@ -226,6 +229,7 @@ public:
insert_delta_dispatch<Add>(s, t, f, d, std::forward<DVals>(delta)...);
}
inline __attribute__((always_inline)) __attribute__((flatten))
int get_delta(size_t r, size_t s)
{
size_t f = get_field(r, s);
......
......@@ -49,7 +49,7 @@ void export_sbm_state()
void (state_t::*move_vertices)(python::object, python::object) =
&state_t::move_vertices;
double (state_t::*virtual_move)(size_t, size_t, size_t,
entropy_args_t) =
const entropy_args_t&) =
&state_t::virtual_move;
size_t (state_t::*sample_block)(size_t, double, double, rng_t&) =
&state_t::sample_block;
......
......@@ -33,13 +33,13 @@ namespace graph_tool
class BlockStateVirtualBase {
public:
virtual double entropy(entropy_args_t eargs, bool propagate) = 0;
virtual double entropy(const entropy_args_t& eargs, bool propagate) = 0;
virtual void add_partition_node(size_t v, size_t r) = 0;
virtual void remove_partition_node(size_t v, size_t r) = 0;
virtual void set_vertex_weight(size_t v, int w) = 0;
virtual void coupled_resize_vertex(size_t v) = 0;
virtual double virtual_move(size_t v, size_t r, size_t nr,
entropy_args_t eargs) = 0;
const entropy_args_t& eargs) = 0;
virtual void sample_branch(size_t v, size_t u, rng_t& rng) = 0;
virtual size_t sample_block(size_t v, double c, double d, rng_t& rng) = 0;
virtual double get_move_prob(size_t v, size_t r, size_t s, double c, double d,
......@@ -55,7 +55,7 @@ public:
const std::vector<double>& rec) = 0;
virtual void remove_edge(size_t u, size_t v, GraphInterface::edge_t& e,
const std::vector<double>& rec) = 0;
virtual double edge_entropy_term(size_t u, size_t v, entropy_args_t ea) = 0;
virtual double edge_entropy_term(size_t u, size_t v, const entropy_args_t& ea) = 0;
virtual void propagate_delta(size_t u, size_t v,
std::vector<std::tuple<size_t, size_t,
GraphInterface::edge_t, int,
......@@ -64,10 +64,10 @@ public:
std::vector<std::tuple<size_t, size_t,
GraphInterface::edge_t, int,
std::vector<double>>>& entries,
entropy_args_t& ea,
const entropy_args_t& ea,
std::vector<double>& dBdx, int dL) = 0;
virtual double get_delta_partition_dl(size_t v, size_t r, size_t nr,
entropy_args_t& ea) = 0;
const entropy_args_t& ea) = 0;
virtual vprop_map_t<int32_t>::type::unchecked_t& get_b() = 0;
virtual bool check_edge_counts(bool emat=true) = 0;
virtual bool allow_move(size_t r, size_t nr) = 0;
......
......@@ -116,7 +116,7 @@ double poisson_w_log_P(DT N, double x, double alpha, double beta)
template <class State>
std::tuple<double,double> rec_entropy(State& state, entropy_args_t& ea)
std::tuple<double,double> rec_entropy(State& state, const entropy_args_t& ea)
{
double S = 0, S_dl = 0;
for (size_t i = 0; i < state._rec_types.size(); ++i)
......@@ -200,7 +200,7 @@ std::tuple<double,double> rec_entropy(State& state, entropy_args_t& ea)
template <class State, class MEntries>
std::tuple<double, double> rec_entries_dS(State& state, MEntries& m_entries,
entropy_args_t& ea,
const entropy_args_t& ea,
std::vector<double>& dBdx, int& dL)
{
int ddL = 0;
......
......@@ -496,8 +496,8 @@ struct Layers
}
template <class MEntries>
double virtual_move(size_t v, size_t r, size_t s, entropy_args_t ea,
MEntries& m_entries)
double virtual_move(size_t v, size_t r, size_t s,
const entropy_args_t& ea, MEntries& m_entries)
{
if (s == r)
{
......@@ -582,7 +582,8 @@ struct Layers
return dS;
}
double virtual_move(size_t v, size_t r, size_t s, entropy_args_t ea)
double virtual_move(size_t v, size_t r, size_t s,
const entropy_args_t& ea)
{
return virtual_move(v, r, s, ea, _m_entries);
}
......@@ -679,7 +680,7 @@ struct Layers
// assert(check_edge_counts());
}
double entropy(entropy_args_t ea, bool propagate=false)
double entropy(const entropy_args_t& ea, bool propagate=false)
{
double S = 0, S_dl = 0;
if (_master)
......@@ -799,7 +800,7 @@ struct Layers
}
}
double edge_entropy_term(size_t, size_t, entropy_args_t) { return 0; }
double edge_entropy_term(size_t, size_t, const entropy_args_t&) { return 0; }
void enable_partition_stats()
{
......@@ -849,7 +850,7 @@ struct Layers
}
void couple_state(LayeredBlockStateVirtualBase& s,
entropy_args_t ea)
const entropy_args_t& ea)
{
_lcoupled_state = &s;
......@@ -880,7 +881,7 @@ struct Layers
}
void couple_state(BlockStateVirtualBase& s,
entropy_args_t ea)
const entropy_args_t& ea)
{
BaseState::couple_state(s, ea);
}
......@@ -1022,7 +1023,7 @@ struct Layers
double propagate_entries_dS(size_t u, size_t v, int du, int dv,
std::vector<std::tuple<size_t, size_t, GraphInterface::edge_t, int,
std::vector<double>>>& entries,
entropy_args_t& ea, std::vector<double>& dBdx,
const entropy_args_t& ea, std::vector<double>& dBdx,
int dL)
{
double dS = BaseState::propagate_entries_dS(u, v, du, dv, entries, ea, dBdx, dL);
......@@ -1044,7 +1045,7 @@ struct Layers
}
double get_delta_partition_dl(size_t v, size_t r, size_t nr,
entropy_args_t& ea)
const entropy_args_t& ea)
{
return BaseState::get_delta_partition_dl(v, r, nr, ea);
}
......
......@@ -56,7 +56,7 @@ void export_lsbm()
{
typedef typename std::remove_reference<decltype(*s)>::type state_t;
double (state_t::*virtual_move)(size_t, size_t, size_t, entropy_args_t) =
double (state_t::*virtual_move)(size_t, size_t, size_t, const entropy_args_t&) =
&state_t::virtual_move;
size_t (state_t::*sample_block)(size_t, double, double, rng_t&)
= &state_t::sample_block;
......@@ -74,7 +74,7 @@ void export_lsbm()
void (state_t::*add_vertices)(python::object, python::object) =
&state_t::add_vertices;
void (state_t::*couple_state)(LayeredBlockStateVirtualBase&,
entropy_args_t) =
const entropy_args_t&) =
&state_t::couple_state;
class_<state_t, bases<LayeredBlockStateVirtualBase>>
......
......@@ -76,7 +76,7 @@ void export_layered_overlap_blockmodel_state()
state_t;
double (state_t::*virtual_move)(size_t, size_t, size_t,
entropy_args_t) =
const entropy_args_t&) =
&state_t::virtual_move;
size_t (state_t::*sample_block)(size_t, double, double,
rng_t&)
......@@ -88,7 +88,7 @@ void export_layered_overlap_blockmodel_state()
python::object) =
&state_t::move_vertices;
void (state_t::*couple_state)(LayeredBlockStateVirtualBase&,
entropy_args_t) =
const entropy_args_t&) =
&state_t::couple_state;
class_<state_t> c(name_demangle(typeid(state_t).name()).c_str(),
......
......@@ -156,7 +156,7 @@ void export_overlap_blockmodel_state()
typedef typename std::remove_reference<decltype(*s)>::type state_t;
double (state_t::*virtual_move)(size_t, size_t, size_t,
entropy_args_t) =
const entropy_args_t&) =
&state_t::virtual_move;
size_t (state_t::*sample_block)(size_t, double, double, rng_t&)
= &state_t::sample_block;
......
......@@ -412,7 +412,7 @@ public:
}
template <class MEntries>
double virtual_move(size_t v, size_t r, size_t nr, entropy_args_t ea,
double virtual_move(size_t v, size_t r, size_t nr, const entropy_args_t& ea,
MEntries& m_entries)
{
if (r == nr)
......@@ -503,12 +503,13 @@ public:
return dS + ea.beta_dl * dS_dl;
}
double virtual_move(size_t v, size_t r, size_t nr, entropy_args_t ea)
double virtual_move(size_t v, size_t r, size_t nr, const entropy_args_t& ea)
{
return virtual_move(v, r, nr, ea, _m_entries);
}
double get_delta_partition_dl(size_t v, size_t r, size_t nr, entropy_args_t& ea)
double get_delta_partition_dl(size_t v, size_t r, size_t nr,
const entropy_args_t& ea)
{
if (r == nr)
return 0;
......@@ -833,7 +834,7 @@ public:
throw GraphException("Dense entropy for overlapping model not implemented!");
}
double entropy(entropy_args_t ea, bool propagate=false)
double entropy(const entropy_args_t& ea, bool propagate=false)
{
double S = 0, S_dl = 0;
if (ea.adjacency)
......@@ -926,7 +927,7 @@ public:
return S;
}
double edge_entropy_term(size_t, size_t, entropy_args_t)
double edge_entropy_term(size_t, size_t, const entropy_args_t&)
{
return 0;
}
......@@ -934,7 +935,7 @@ public:
std::vector<std::tuple<size_t, size_t,
GraphInterface::edge_t, int,
std::vector<double>>>&,
entropy_args_t&, std::vector<double>&, int)
const entropy_args_t&, std::vector<double>&, int)
{
return 0;
}
......@@ -992,7 +993,7 @@ public:
return _partition_stats[_pclabel[v]];
}
void couple_state(BlockStateVirtualBase& s, entropy_args_t ea)
void couple_state(BlockStateVirtualBase& s, const entropy_args_t& ea)
{
_coupled_state = &s;
_coupled_entropy_args = ea;
......
......@@ -87,7 +87,7 @@ inline double lgamma_fast(T x)
{
if (size_t(x) >= __lgamma_cache.size())
{
if (Init)
if constexpr (Init)
init_lgamma(x);
else
return lgamma(x);
......
......@@ -36,7 +36,7 @@ double log_q(T n, T k)
return 0;
if (k > n)
k = n;
if (n < T(__q_cache.shape()[0]))
if (size_t(n) < __q_cache.shape()[0])
return __q_cache[n][k];
return log_q_approx(n, k);
}
......
Supports Markdown
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