Commit 4d212c1b authored by Tiago Peixoto's avatar Tiago Peixoto
Browse files

Switch from boost::adjacency_list<> to custom made and lightweight adj_list<>

Dumping boost::adjacency_list<> improves memory usage by a factor of
two, and also slightly improves performance in some cases.
parent 5b67db19
......@@ -255,17 +255,17 @@ G2_loop_k: ++G2_verts.first;
{
vertex1_t vk = dfs_vertices[dfs_num_k];
num_edges_on_k -=
count_if(adjacent_vertices(f[vk], G2), make_indirect_pmap(in_S));
count_if(adjacent_vertices(vertex(f[vk], G2), G2), make_indirect_pmap(in_S));
for (int jj = 0; jj < dfs_num_k; ++jj) {
vertex1_t j = dfs_vertices[jj];
num_edges_on_k -= count(adjacent_vertices(f[j], G2), f[vk]);
num_edges_on_k -= count(adjacent_vertices(vertex(f[j], G2), G2), f[vk]);
}
}
if (num_edges_on_k != 0)
goto return_point_false;
fi_adj = adjacent_vertices(f[i], G2);
fi_adj = adjacent_vertices(vertex(f[i], G2), G2);
while (fi_adj.first != fi_adj.second) {
{
vertex2_t v = *fi_adj.first;
......@@ -290,7 +290,7 @@ fi_adj_loop_k:++fi_adj.first;
}
}
else {
if (container_contains(adjacent_vertices(f[i], G2), f[j])) {
if (container_contains(adjacent_vertices(vertex(f[i], G2), G2), f[j])) {
++num_edges_on_k;
match_continuation new_k;
new_k.position = match_continuation::pos_dfs_num;
......
......@@ -134,10 +134,7 @@ void get_motifs(GraphInterface& g, size_t k, python::list subgraph_list,
(sub, boost::bind<void>(retrieve_from_list(), _1,
boost::ref(list), boost::ref(done)))();
if (!done)
{
sub.ReIndexEdges();
subgraph_list.append(sub);
}
}
subgraph_list.reverse();
}
......
......@@ -252,8 +252,8 @@ bool graph_cmp(Graph& g1, Graph& g2)
}
// short hand for both types of subgraphs
typedef adjacency_list<vecS,vecS,bidirectionalS> d_graph_t;
typedef adjacency_list<vecS,vecS,undirectedS> u_graph_t;
typedef adj_list<size_t> d_graph_t;
typedef adj_list<size_t> u_graph_t;
// we need this wrap to use the UndirectedAdaptor only on directed graphs
struct wrap_undirected
......
......@@ -38,6 +38,7 @@
#include "graph_blockmodel.hh"
using namespace boost;
using namespace graph_tool;
// ====================
// Entropy calculation
......@@ -131,8 +132,7 @@ python::object do_min_dist(GraphInterface& gi, int n, boost::any omrs,
void do_b_join(GraphInterface& gi, size_t r, size_t s, boost::any omrs,
boost::any omrp, boost::any omrm, boost::any owr, bool deg_corr,
vector<int>& vlist)
boost::any omrp, boost::any omrm, boost::any owr)
{
typedef property_map_type::apply<int32_t,
GraphInterface::vertex_index_map_t>::type
......@@ -149,8 +149,7 @@ void do_b_join(GraphInterface& gi, size_t r, size_t s, boost::any omrs,
vmap_t wr = any_cast<vmap_t>(owr);
run_action<graph_tool::detail::all_graph_views, mpl::true_>()
(gi, boost::bind<void>(b_join(r, s), mrs, mrp, mrm, wr, deg_corr,
ref(vlist), _1))();
(gi, boost::bind<void>(b_join(r, s), mrs, mrp, mrm, wr, _1))();
}
// ===============================
......@@ -377,9 +376,8 @@ struct move_sweep_dispatch
: eweight(eweight), vweight(vweight), oegroups(egroups), esrcpos(esrcpos),
etgtpos(etgtpos), label(label), L(L), vlist(vlist),
deg_corr(deg_corr), beta(beta), verbose(verbose),
max_edge_index(max_edge_index),
rng(rng), S(S), nmoves(nmoves), bgi(bgi)
deg_corr(deg_corr), beta(beta), sequential(sequential), verbose(verbose),
max_edge_index(max_edge_index), rng(rng), S(S), nmoves(nmoves), bgi(bgi)
{}
Eprop eweight;
......@@ -421,7 +419,6 @@ struct move_sweep_dispatch
void dispatch(Eprop mrs, Vprop mrp, Vprop mrm, Vprop wr, Vprop b, Graph& g,
boost::any& aemat, BGraph& bg) const
{
const GraphWrap<BGraph> wbg(bg, bgi);
typedef typename get_emat_t::apply<BGraph>::type emat_t;
emat_t& emat = any_cast<emat_t&>(aemat);
......@@ -444,7 +441,7 @@ struct move_sweep_dispatch
egroups.get_unchecked(num_vertices(bg)),
esrcpos.get_unchecked(max_edge_index + 1),
etgtpos.get_unchecked(max_edge_index + 1),
g, wbg, emat, sequential, verbose, rng, S, nmoves);
g, bg, emat, sequential, verbose, rng, S, nmoves);
}
};
......@@ -525,8 +522,7 @@ struct collect_edge_marginals_dispatch
void operator()(Graph& g, size_t B, Vprop cb, MEprop p,
tr1::tuple<boost::any, GraphInterface&> abg) const
{
typedef typename Graph::orig_graph_t graph_t;
const GraphWrap<graph_t> bg(*any_cast<graph_t*>(get<0>(abg)), get<1>(abg));
Graph& bg = *any_cast<Graph*>(get<0>(abg));
collect_edge_marginals(B, cb.get_unchecked(num_vertices(bg)), p, g, bg);
}
};
......
......@@ -26,6 +26,7 @@ using __gnu_cxx::power;
#include "tr1_include.hh"
#include TR1_HEADER(unordered_set)
#include TR1_HEADER(unordered_map)
#include TR1_HEADER(tuple)
#include <dense_hash_set>
......@@ -95,7 +96,7 @@ inline double eterm(const Edge& e, const Eprop& mrs, const Graph& g)
// "vertex" term of the entropy
template <class Graph, class Vertex, class Vprop>
inline double vterm(Vertex v, Vprop& mrp, Vprop& mrm, Vprop& wr, bool deg_corr,
Graph& g)
Graph&)
{
double one = 0.5;
......@@ -132,9 +133,9 @@ struct entropy
// obtain the "super block" which is a merger of two blocks
template <class Graph, class Vertex, class Eprop, class Vprop>
void super_block(Vertex u, Vertex v, Eprop mrs, Vprop mrp, Vprop mrm, Vprop wr,
bool deg_corr, Graph& g, unordered_set<Vertex>& nsp,
unordered_set<Vertex>& nsm, unordered_map<Vertex, size_t>& msp,
unordered_map<Vertex, size_t>& msm, size_t& ml, size_t& m_rp,
Graph& g, tr1::unordered_set<Vertex>& nsp,
tr1::unordered_set<Vertex>& nsm, tr1::unordered_map<Vertex, size_t>& msp,
tr1::unordered_map<Vertex, size_t>& msm, size_t& ml, size_t& m_rp,
size_t& m_rm, size_t& w_r)
{
ml = 0;
......@@ -216,12 +217,12 @@ struct dist
d += eterm(*e, mrs, g);
}
unordered_set<Vertex> nsp, nsm;
unordered_map<Vertex, size_t> msp, msm;
tr1::unordered_set<Vertex> nsp, nsm;
tr1::unordered_map<Vertex, size_t> msp, msm;
size_t ml, m_rp, m_rm, w_r;
super_block(r, s, mrs, mrp, mrm, wr, deg_corr, g, nsp, nsm, msp, msm,
ml, m_rp, m_rm, w_r);
super_block(r, s, mrs, mrp, mrm, wr, g, nsp, nsm, msp, msm, ml, m_rp,
m_rm, w_r);
if (!is_directed::apply<Graph>::type::value)
{
......@@ -349,19 +350,18 @@ struct b_join
size_t rr, ss;
template <class Graph, class Eprop, class Vprop>
void operator()(Eprop mrs, Vprop mrp, Vprop mrm, Vprop wr, bool deg_corr,
vector<int>& vlist, Graph& g) const
void operator()(Eprop mrs, Vprop mrp, Vprop mrm, Vprop wr, Graph& g) const
{
typedef typename graph_traits<Graph>::vertex_descriptor vertex_t;
unordered_set<vertex_t> nsp, nsm;
unordered_map<vertex_t, size_t> msp, msm;
tr1::unordered_set<vertex_t> nsp, nsm;
tr1::unordered_map<vertex_t, size_t> msp, msm;
size_t ml, m_rp, m_rm, w_r;
vertex_t r = vertex(rr, g);
vertex_t s = vertex(ss, g);
super_block(r, s, mrs, mrp, mrm, wr, deg_corr, g, nsp, nsm, msp, msm,
ml, m_rp, m_rm, w_r);
super_block(r, s, mrs, mrp, mrm, wr, g, nsp, nsm, msp, msm, ml, m_rp,
m_rm, w_r);
mrp[r] = m_rp;
mrm[r] = m_rm;
......@@ -612,7 +612,7 @@ inline void insert_m_entry(Vertex r, Vertex s, MEntry& m_entries,
// after the move
template <class Graph, class BGraph, class Vertex, class Vprop, class MEntry,
class MEntrySet>
void move_entries(Vertex v, Vertex nr, Vprop& b, Graph& g, BGraph& bg,
void move_entries(Vertex v, Vertex nr, Vprop& b, Graph& g, BGraph&,
MEntry& m_entries, MEntrySet& m_entries_set)
{
typedef typename graph_traits<Graph>::vertex_descriptor vertex_t;
......@@ -982,7 +982,7 @@ struct build_egroups
// Sampling marginal probabilities on the edges
template <class Graph, class Vprop, class MEprop>
void collect_edge_marginals(size_t B, Vprop b, MEprop p, Graph& g, Graph& bg)
void collect_edge_marginals(size_t B, Vprop b, MEprop p, Graph& g, Graph&)
{
typedef typename graph_traits<Graph>::vertex_descriptor vertex_t;
......
......@@ -131,7 +131,4 @@ void community_network(GraphInterface& gi, GraphInterface& cgi,
_3, edge_count),
writable_vertex_properties(), eweight_properties())
(community_property, eweight);
cgi.ReIndexEdges();
}
......@@ -100,7 +100,7 @@ struct get_community_network_edges
template <class Graph, class CommunityGraph, class CommunityMap,
class CCommunityMap, class EdgeWeightMap,
class EdgeIndex, class EdgeProperty>
void operator()(const Graph& g, CommunityGraph& cg, EdgeIndex cedge_index,
void operator()(const Graph& g, CommunityGraph& cg, EdgeIndex,
CommunityMap s_map, CCommunityMap cs_map,
EdgeWeightMap eweight, EdgeProperty edge_count,
bool self_loops) const
......@@ -140,7 +140,6 @@ struct get_community_network_edges
{
ce = add_edge(cs, ct, cg).first;
comm_edges[make_pair(cs, ct)] = ce;
put(cedge_index, ce, comm_edges.size() - 1);
}
put(edge_count, ce, get(edge_count, ce) + get(eweight, *e));
}
......
......@@ -1043,7 +1043,7 @@ private:
};
template <class Graph, class VertexIterator, class PosMap>
void draw_vertices(Graph& g, pair<VertexIterator,VertexIterator> v_range,
void draw_vertices(Graph&, pair<VertexIterator,VertexIterator> v_range,
PosMap pos_map, attrs_t& attrs, attrs_t& defaults,
Cairo::Context& cr)
{
......@@ -1150,8 +1150,8 @@ struct do_cairo_draw_vertices
{
template <class Graph, class PosMap, class VertexOrder>
void operator()(Graph& g, PosMap pos, VertexOrder vertex_order,
attrs_t& vattrs, attrs_t& eattrs, attrs_t& vdefaults,
attrs_t& edefaults, Cairo::Context& cr) const
attrs_t& vattrs, attrs_t&, attrs_t& vdefaults,
attrs_t&, Cairo::Context& cr) const
{
ordered_range<typename graph_traits<Graph>::vertex_iterator>
vertex_range(vertices(g));
......@@ -1233,7 +1233,7 @@ void populate_defaults(python::dict odefaults, attrs_t& defaults)
struct populate_edge_attrs
{
template <class Graph>
void operator()(Graph& g, python::dict oeattrs, attrs_t& eattrs,
void operator()(Graph&, python::dict oeattrs, attrs_t& eattrs,
python::dict oedefaults, attrs_t& edefaults) const
{
typedef typename graph_traits<Graph>::edge_descriptor edge_t;
......
......@@ -210,6 +210,36 @@ make_unchecked_vector_property_map(IndexMap index)
return unchecked_vector_property_map<T, IndexMap>(index);
}
template <class Type, class Index>
unchecked_vector_property_map<Type, Index>
get_unchecked(checked_vector_property_map<Type, Index> prop)
{
return prop.get_unchecked();
}
template <class Prop>
Prop
get_unchecked(Prop prop)
{
return prop;
}
template <class Type, class Index>
checked_vector_property_map<Type, Index>
get_checked(unchecked_vector_property_map<Type, Index> prop)
{
return prop.get_checked();
}
template <class Prop>
Prop
get_checked(Prop prop)
{
return prop;
}
}
#endif
......@@ -97,21 +97,6 @@ void deaugment_graph(Graph& g, AugmentedMap augmented)
}
}
template <class Type, class Index>
unchecked_vector_property_map<Type, Index>
get_unchecked(checked_vector_property_map<Type, Index> prop)
{
return prop.get_unchecked();
}
template <class Prop>
Prop
get_unchecked(Prop prop)
{
return prop;
}
} // graph_tool namespace
#endif // GRAPH_AUGMENT_HH
......@@ -47,15 +47,20 @@ struct get_edmonds_karp_max_flow
unchecked_vector_property_map<edge_t,VertexIndex>
pred(vertex_index, num_vertices(g));
augment_graph(g, augmented.get_checked(), cm, reverse_map.get_checked(),
res);
typedef typename remove_const<Graph>::type GT;
GT& u = const_cast<GT&>(g);
augment_graph(u,
augmented.get_checked(),
get_checked(cm),
reverse_map.get_checked(),
get_checked(res));
boost::edmonds_karp_max_flow(g._g, vertex(src, g), vertex(sink, g),
boost::edmonds_karp_max_flow(g, vertex(src, g), vertex(sink, g),
get_unchecked(cm),
res.get_unchecked(),
get_unchecked(res),
reverse_map, color, pred);
deaugment_graph(g, augmented.get_checked());
deaugment_graph(u, augmented.get_checked());
}
};
......@@ -63,11 +68,11 @@ struct get_edmonds_karp_max_flow
void edmonds_karp_max_flow(GraphInterface& gi, size_t src, size_t sink,
boost::any capacity, boost::any res)
{
run_action<graph_tool::detail::always_directed, mpl::true_>()
run_action<graph_tool::detail::always_directed>()
(gi, bind<void>(get_edmonds_karp_max_flow(),
_1, gi.GetVertexIndex(), gi.GetEdgeIndex(),
gi.GetMaxEdgeIndex(),
src, sink, _2, _3),
edge_scalar_properties(), writable_edge_scalar_properties())
writable_edge_scalar_properties(), writable_edge_scalar_properties())
(capacity,res);
}
......@@ -61,7 +61,7 @@ struct get_kolmogorov_max_flow
{
template <class Graph, class EdgeIndex, class VertexIndex,
class CapacityMap, class ResidualMap>
void operator()(Graph& g, EdgeIndex edge_index, size_t max_e,
void operator()(Graph& g, EdgeIndex edge_index, size_t,
VertexIndex vertex_index, size_t src, size_t sink,
CapacityMap cm, ResidualMap res) const
{
......@@ -77,13 +77,16 @@ struct get_kolmogorov_max_flow
unchecked_vector_property_map<size_t,VertexIndex>
dist_map(vertex_index, num_vertices(g));
augment_graph(g, augmented, cm, reverse_map, res, true);
typedef typename remove_const<Graph>::type GT;
GT& u = const_cast<GT&>(g);
KOLMOGOROV_MAX_FLOW(g._g, cm, res, reverse_map, pred_map, color_map,
augment_graph(u, augmented, cm, reverse_map, res, true);
KOLMOGOROV_MAX_FLOW(g, cm, res, reverse_map, pred_map, color_map,
dist_map, vertex_index, vertex(src, g),
vertex(sink, g));
deaugment_graph(g, augmented);
deaugment_graph(u, augmented);
}
};
......@@ -96,6 +99,6 @@ void kolmogorov_max_flow(GraphInterface& gi, size_t src, size_t sink,
(gi, bind<void>(get_kolmogorov_max_flow(),
_1, gi.GetEdgeIndex(), gi.GetMaxEdgeIndex(),
gi.GetVertexIndex(), src, sink, _2, _3),
edge_scalar_properties(), writable_edge_scalar_properties())
writable_edge_scalar_properties(), writable_edge_scalar_properties())
(capacity,res);
}
......@@ -67,14 +67,17 @@ struct get_push_relabel_max_flow
unchecked_vector_property_map<edge_t,EdgeIndex>
reverse_map(edge_index, max_e);
augment_graph(g, augmented, cm,
typedef typename remove_const<Graph>::type GT;
GT& u = const_cast<GT&>(g);
augment_graph(u, augmented, cm,
reverse_map.get_checked(), res);
boost::push_relabel_max_flow(g._g, vertex(src, g), vertex(sink, g),
boost::push_relabel_max_flow(g, vertex(src, g), vertex(sink, g),
get_unchecked(cm),
res.get_unchecked(),
reverse_map, vertex_index);
deaugment_graph(g, augmented);
deaugment_graph(u, augmented);
}
};
......@@ -87,6 +90,6 @@ void push_relabel_max_flow(GraphInterface& gi, size_t src, size_t sink,
_1, gi.GetVertexIndex(), gi.GetEdgeIndex(),
gi.GetMaxEdgeIndex(),
src, sink, _2, _3),
edge_scalar_properties(), writable_edge_scalar_properties())
writable_edge_scalar_properties(), writable_edge_scalar_properties())
(capacity,res);
}
......@@ -74,7 +74,6 @@ void generate_graph(GraphInterface& gi, size_t N, python::object deg_sample,
no_parallel, no_self_loops,
ref(rng), verbose, verify))();
}
gi.ReIndexEdges();
}
size_t random_rewire(GraphInterface& gi, string strat, size_t niter,
......
......@@ -422,7 +422,7 @@ bool is_target(const pair<size_t, size_t>& deg)
template <class Vset, class Targets, class Sources, class Graph>
bool update_deg(size_t t_i, const pair<size_t, size_t>& deg, Vset& vset,
Targets& targets, Sources& sources, Graph& g)
Targets& targets, Sources& sources, Graph&)
{
if (is_source<Graph>(deg))
sources.insert(deg);
......
......@@ -63,5 +63,4 @@ void geometric(GraphInterface& gi, python::object opoints, double r,
ref(points), ref(range), r,
periodic),
prop_types())(pos);
gi.ReIndexEdges();
}
......@@ -32,5 +32,4 @@ void lattice(GraphInterface& gi, python::object oshape, bool periodic)
for(size_t i = 0; i < shape.size(); ++i)
shape[i] = python::extract<size_t>(oshape[i]);
get_lattice()(gi.GetGraph(), shape, periodic);
gi.ReIndexEdges();
}
......@@ -70,8 +70,8 @@ struct get_lattice
add_vertex(g);
vector<int> pos(shape.size());
#pragma omp parallel for default(shared) private(i) \
firstprivate(pos) schedule(dynamic)
//#pragma omp parallel for default(shared) private(i)
// firstprivate(pos) schedule(dynamic)
for (int i = 0; i < N; ++i)
{
get_pos(i, shape, pos);
......
......@@ -29,7 +29,7 @@ struct get_line_graph
{
template <class Graph, class VertexIndex, class LineGraph,
class EdgeIndexMap, class LGVertexIndex>
void operator()(const Graph& g, VertexIndex vertex_index,
void operator()(const Graph& g, VertexIndex,
LineGraph& line_graph, EdgeIndexMap edge_index,
LGVertexIndex vmap) const
{
......@@ -56,7 +56,6 @@ struct get_line_graph
line_edge_index_map_t;
line_edge_index_map_t line_edge_index(get(edge_index_t(), line_graph));
size_t e_index = 0;
if (boost::is_directed(g))
{
typename graph_traits<Graph>::vertex_iterator v, v_end;
......@@ -74,7 +73,6 @@ struct get_line_graph
new_edge = add_edge(edge_to_vertex_map[*e1],
edge_to_vertex_map[*e2],
line_graph).first;
line_edge_index[new_edge] = e_index++;
}
}
}
......@@ -94,7 +92,6 @@ struct get_line_graph
new_edge = add_edge(edge_to_vertex_map[*e1],
edge_to_vertex_map[*e2],
line_graph).first;
line_edge_index[new_edge] = e_index++;
}
}
}
......@@ -114,5 +111,4 @@ void line_graph(GraphInterface& gi, GraphInterface& lgi,
ref(lgi.GetGraph()), lgi.GetEdgeIndex(),
_2),
vertex_properties())(edge_index);
lgi.ReIndexEdges();
}
......@@ -30,5 +30,4 @@ void predecessor_graph(GraphInterface& gi, GraphInterface& gpi,
gi.GetVertexIndex(), ref(gpi.GetGraph()),
_2),
vertex_scalar_properties())(pred_map);
gpi.ReIndexEdges();
}
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