1 // Copyright Daniel Wallin 2007. Use, modification and distribution is
2 // subject to the Boost Software License, Version 1.0. (See accompanying
3 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5 #ifndef BOOST_GRAPH_DISTRIBUTED_ADJLIST_SERIALIZATION_070925_HPP
6 #define BOOST_GRAPH_DISTRIBUTED_ADJLIST_SERIALIZATION_070925_HPP
8 #ifndef BOOST_GRAPH_USE_MPI
9 #error "Parallel BGL files should not be included unless <boost/graph/use_mpi.hpp> has been included"
12 # include <boost/assert.hpp>
13 # include <boost/lexical_cast.hpp>
14 # include <boost/foreach.hpp>
15 # include <boost/filesystem/path.hpp>
16 # include <boost/filesystem/operations.hpp>
22 namespace detail { namespace parallel
25 // Wraps a local descriptor, making it serializable.
26 template <class Local>
27 struct serializable_local_descriptor
29 serializable_local_descriptor()
32 serializable_local_descriptor(Local local)
36 operator Local const&() const
41 bool operator==(serializable_local_descriptor const& other) const
43 return local == other.local;
46 bool operator<(serializable_local_descriptor const& other) const
48 return local < other.local;
51 template <class Archive>
52 void serialize(Archive& ar, const unsigned int /*version*/)
54 ar & unsafe_serialize(local);
60 template <class Vertex, class Properties>
64 Vertex source, Vertex target
65 , Properties properties, void* property_ptr
69 , properties(properties)
70 , property_ptr(property_ptr)
75 Properties properties;
79 inline bool is_digit(char c)
81 return std::isdigit(c) != 0;
84 inline std::vector<int>
85 available_process_files(std::string const& filename)
87 if (!filesystem::exists(filename))
88 return std::vector<int>();
90 std::vector<int> result;
92 for (filesystem::directory_iterator i(filename), end; i != end; ++i)
94 if (!filesystem::is_regular(*i))
95 boost::throw_exception(std::runtime_error("directory contains non-regular entries"));
97 std::string process_name = i->path().filename().string();
98 for (std::string::size_type i = 0; i < process_name.size(); ++i)
99 if (!is_digit(process_name[i]))
100 boost::throw_exception(std::runtime_error("directory contains files with invalid names"));
102 result.push_back(boost::lexical_cast<int>(process_name));
108 template <class Archive, class Tag, class T, class Base>
109 void maybe_load_properties(
110 Archive& ar, char const* name, property<Tag, T, Base>& properties)
112 ar >> serialization::make_nvp(name, get_property_value(properties, Tag()));
113 maybe_load_properties(ar, name, static_cast<Base&>(properties));
116 template <class Archive>
117 void maybe_load_properties(
118 Archive&, char const*, no_property&)
121 template <class Archive, typename Bundle>
122 void maybe_load_properties(
123 Archive& ar, char const* name, Bundle& bundle)
125 ar >> serialization::make_nvp(name, bundle);
127 maybe_load_properties(ar, name, prop);
135 template <class Graph, class Archive, class VertexListS>
138 typedef typename Graph::vertex_descriptor vertex_descriptor;
139 typedef typename Graph::local_vertex_descriptor local_vertex_descriptor;
140 typedef typename Graph::vertex_property_type vertex_property_type;
141 typedef typename Graph::edge_descriptor edge_descriptor;
142 typedef typename Graph::local_edge_descriptor local_edge_descriptor;
143 typedef typename Graph::edge_property_type edge_property_type;
144 typedef typename Graph::process_group_type process_group_type;
145 typedef typename process_group_type::process_id_type process_id_type;
146 typedef typename Graph::directed_selector directed_selector;
147 typedef typename mpl::if_<
148 is_same<VertexListS, defaultS>, vecS, VertexListS
149 >::type vertex_list_selector;
150 typedef pending_edge<vertex_descriptor, edge_property_type>
152 typedef serializable_local_descriptor<local_vertex_descriptor>
153 serializable_vertex_descriptor;
155 graph_loader(Graph& g, Archive& ar)
158 , m_pg(g.process_group())
159 , m_requested_vertices(num_processes(m_pg))
160 , m_remote_vertices(num_processes(m_pg))
161 , m_property_ptrs(num_processes(m_pg))
167 ar >> make_nvp("distribution", m_g.distribution());
171 struct pending_in_edge
174 vertex_descriptor u, vertex_descriptor v, void* property_ptr
178 , property_ptr(property_ptr)
188 return process_id(m_pg) == 0;
192 serialization::nvp<T> const make_nvp(char const* name, T& value) const
194 return serialization::nvp<T>(name, value);
198 void load_vertices();
200 template <class Anything>
201 void maybe_load_and_store_local_vertex(Anything);
202 void maybe_load_and_store_local_vertex(vecS);
205 void load_in_edges(bidirectionalS);
206 void load_in_edges(directedS);
207 void add_pending_in_edge(
208 vertex_descriptor u, vertex_descriptor v, void* property_ptr, vecS);
209 template <class Anything>
210 void add_pending_in_edge(
211 vertex_descriptor u, vertex_descriptor v, void* property_ptr, Anything);
212 template <class Anything>
214 vertex_descriptor u, vertex_descriptor v
215 , edge_property_type const& property, void* property_ptr, Anything);
217 vertex_descriptor u, vertex_descriptor v
218 , edge_property_type const& property, void* property_ptr, vecS);
219 void add_remote_vertex_request(
220 vertex_descriptor u, vertex_descriptor v, directedS);
221 void add_remote_vertex_request(
222 vertex_descriptor u, vertex_descriptor v, bidirectionalS);
224 edge_descriptor const&, void*, directedS);
226 edge_descriptor const& edge, void* old_property_ptr, bidirectionalS);
228 void resolve_remote_vertices(directedS);
229 void resolve_remote_vertices(bidirectionalS);
230 vertex_descriptor resolve_remote_vertex(vertex_descriptor u) const;
231 vertex_descriptor resolve_remote_vertex(vertex_descriptor u, vecS) const;
232 template <class Anything>
233 vertex_descriptor resolve_remote_vertex(vertex_descriptor u, Anything) const;
235 void resolve_property_ptrs();
237 void commit_pending_edges(vecS);
238 template <class Anything>
239 void commit_pending_edges(Anything);
240 void commit_pending_in_edges(directedS);
241 void commit_pending_in_edges(bidirectionalS);
243 void* maybe_load_property_ptr(directedS) { return 0; }
244 void* maybe_load_property_ptr(bidirectionalS);
248 process_group_type m_pg;
250 std::vector<process_id_type> m_id_mapping;
252 // Maps local vertices as loaded from the archive to
253 // the ones actually added to the graph. Only used
255 std::map<local_vertex_descriptor, local_vertex_descriptor> m_local_vertices;
257 // This is the list of remote vertex descriptors that we
258 // are going to receive from other processes. This is
259 // kept sorted so that we can determine the position of
260 // the matching vertex descriptor in m_remote_vertices.
261 std::vector<std::vector<serializable_vertex_descriptor> > m_requested_vertices;
263 // This is the list of remote vertex descriptors that
264 // we send and receive from other processes.
265 std::vector<std::vector<serializable_vertex_descriptor> > m_remote_vertices;
268 std::vector<pending_edge_type> m_pending_edges;
270 // The pending in-edges that will be added in the commit step, after
271 // the remote vertex descriptors has been resolved. Only used
272 // when bidirectionalS and !vecS.
273 std::vector<pending_in_edge> m_pending_in_edges;
275 std::vector<std::vector<unsafe_pair<void*,void*> > > m_property_ptrs;
278 template <class Graph, class Archive, class VertexListS>
279 void graph_loader<Graph, Archive, VertexListS>::load_prefix()
281 typename process_group_type::process_size_type num_processes_;
282 m_ar >> make_nvp("num_processes", num_processes_);
284 if (num_processes_ != num_processes(m_pg))
285 boost::throw_exception(std::runtime_error("number of processes mismatch"));
287 process_id_type old_id;
288 m_ar >> make_nvp("id", old_id);
290 std::vector<typename Graph::distribution_type::size_type> mapping;
291 m_ar >> make_nvp("mapping", mapping);
293 // Fetch all the old id's from the other processes.
294 std::vector<process_id_type> old_ids;
295 all_gather(m_pg, &old_id, &old_id+1, old_ids);
297 m_id_mapping.resize(num_processes(m_pg), -1);
299 for (process_id_type i = 0; i < num_processes(m_pg); ++i)
301 # ifdef PBGL_SERIALIZE_DEBUG
303 std::cout << i << " used to be " << old_ids[i] << "\n";
305 BOOST_ASSERT(m_id_mapping[old_ids[i]] == -1);
306 m_id_mapping[old_ids[i]] = i;
309 std::vector<typename Graph::distribution_type::size_type> new_mapping(
312 for (int i = 0; i < num_processes(m_pg); ++i)
314 new_mapping[mapping[old_ids[i]]] = i;
317 m_g.distribution().assign_mapping(
318 new_mapping.begin(), new_mapping.end());
321 template <class Graph, class Archive, class VertexListS>
322 void graph_loader<Graph, Archive, VertexListS>::load_vertices()
325 m_ar >> BOOST_SERIALIZATION_NVP(V);
327 # ifdef PBGL_SERIALIZE_DEBUG
329 std::cout << "Loading vertices\n";
332 for (int i = 0; i < V; ++i)
334 maybe_load_and_store_local_vertex(vertex_list_selector());
338 template <class Graph, class Archive, class VertexListS>
339 template <class Anything>
340 void graph_loader<Graph, Archive, VertexListS>::maybe_load_and_store_local_vertex(Anything)
342 // Load the original vertex descriptor
343 local_vertex_descriptor local;
344 m_ar >> make_nvp("local", unsafe_serialize(local));
346 // Load the properties
347 vertex_property_type property;
348 detail::parallel::maybe_load_properties(m_ar, "vertex_property",
352 vertex_descriptor v(process_id(m_pg), add_vertex(property, m_g.base()));
354 if (m_g.on_add_vertex)
355 m_g.on_add_vertex(v, m_g);
357 // Create the mapping from the "old" local descriptor to the new
359 m_local_vertices[local] = v.local;
362 template <class Graph, class Archive, class VertexListS>
363 void graph_loader<Graph, Archive, VertexListS>::maybe_load_and_store_local_vertex(vecS)
365 // Load the properties
366 vertex_property_type property;
367 detail::parallel::maybe_load_properties(m_ar, "vertex_property",
371 vertex_descriptor v(process_id(m_pg),
372 add_vertex(m_g.build_vertex_property(property),
375 if (m_g.on_add_vertex)
376 m_g.on_add_vertex(v, m_g);
379 template <class Graph, class Archive, class VertexListS>
380 void graph_loader<Graph, Archive, VertexListS>::load_edges()
383 m_ar >> BOOST_SERIALIZATION_NVP(E);
385 # ifdef PBGL_SERIALIZE_DEBUG
387 std::cout << "Loading edges\n";
390 for (int i = 0; i < E; ++i)
392 local_vertex_descriptor local_src;
393 process_id_type target_owner;
394 local_vertex_descriptor local_tgt;
396 m_ar >> make_nvp("source", unsafe_serialize(local_src));
397 m_ar >> make_nvp("target_owner", target_owner);
398 m_ar >> make_nvp("target", unsafe_serialize(local_tgt));
400 process_id_type new_src_owner = process_id(m_pg);
401 process_id_type new_tgt_owner = m_id_mapping[target_owner];
403 vertex_descriptor source(new_src_owner, local_src);
404 vertex_descriptor target(new_tgt_owner, local_tgt);
406 edge_property_type properties;
407 detail::parallel::maybe_load_properties(m_ar, "edge_property", properties);
409 void* property_ptr = maybe_load_property_ptr(directed_selector());
410 add_edge(source, target, properties, property_ptr, vertex_list_selector());
413 load_in_edges(directed_selector());
414 commit_pending_edges(vertex_list_selector());
417 template <class Graph, class Archive, class VertexListS>
418 void graph_loader<Graph, Archive, VertexListS>::load_in_edges(bidirectionalS)
421 m_ar >> BOOST_SERIALIZATION_NVP(I);
423 # ifdef PBGL_SERIALIZE_DEBUG
425 std::cout << "Loading in-edges\n";
428 for (int i = 0; i < I; ++i)
430 process_id_type src_owner;
431 local_vertex_descriptor local_src;
432 local_vertex_descriptor local_target;
435 m_ar >> make_nvp("src_owner", src_owner);
436 m_ar >> make_nvp("source", unsafe_serialize(local_src));
437 m_ar >> make_nvp("target", unsafe_serialize(local_target));
438 m_ar >> make_nvp("property_ptr", unsafe_serialize(property_ptr));
440 src_owner = m_id_mapping[src_owner];
442 vertex_descriptor u(src_owner, local_src);
443 vertex_descriptor v(process_id(m_pg), local_target);
445 add_pending_in_edge(u, v, property_ptr, vertex_list_selector());
449 template <class Graph, class Archive, class VertexListS>
450 void graph_loader<Graph, Archive, VertexListS>::load_in_edges(directedS)
453 template <class Graph, class Archive, class VertexListS>
454 void graph_loader<Graph, Archive, VertexListS>::add_pending_in_edge(
455 vertex_descriptor u, vertex_descriptor v, void* property_ptr, vecS)
457 m_pending_in_edges.push_back(pending_in_edge(u,v,property_ptr));
460 template <class Graph, class Archive, class VertexListS>
461 template <class Anything>
462 void graph_loader<Graph, Archive, VertexListS>::add_pending_in_edge(
463 vertex_descriptor u, vertex_descriptor v, void* property_ptr, Anything)
465 // u and v represent the out-edge here, meaning v is local
466 // to us, and u is always remote.
467 m_pending_in_edges.push_back(pending_in_edge(u,v,property_ptr));
468 add_remote_vertex_request(v, u, bidirectionalS());
471 template <class Graph, class Archive, class VertexListS>
472 template <class Anything>
473 void graph_loader<Graph, Archive, VertexListS>::add_edge(
474 vertex_descriptor u, vertex_descriptor v
475 , edge_property_type const& property, void* property_ptr, Anything)
477 m_pending_edges.push_back(pending_edge_type(u, v, property, property_ptr));
478 add_remote_vertex_request(u, v, directed_selector());
481 template <class Graph, class Archive, class VertexListS>
482 void graph_loader<Graph, Archive, VertexListS>::add_remote_vertex_request(
483 vertex_descriptor u, vertex_descriptor v, directedS)
485 // We have to request the remote vertex.
486 m_requested_vertices[owner(v)].push_back(local(v));
489 template <class Graph, class Archive, class VertexListS>
490 void graph_loader<Graph, Archive, VertexListS>::add_remote_vertex_request(
491 vertex_descriptor u, vertex_descriptor v, bidirectionalS)
493 // If the edge spans to another process, we know
494 // that that process has a matching in-edge, so
495 // we can just send our vertex. No requests
497 if (owner(v) != m_g.processor())
499 m_remote_vertices[owner(v)].push_back(local(u));
500 m_requested_vertices[owner(v)].push_back(local(v));
504 template <class Graph, class Archive, class VertexListS>
505 void graph_loader<Graph, Archive, VertexListS>::add_edge(
506 vertex_descriptor u, vertex_descriptor v
507 , edge_property_type const& property, void* property_ptr, vecS)
509 std::pair<local_edge_descriptor, bool> inserted =
510 detail::parallel::add_local_edge(
512 , m_g.build_edge_property(property), m_g.base());
513 BOOST_ASSERT(inserted.second);
514 put(edge_target_processor_id, m_g.base(), inserted.first, owner(v));
516 edge_descriptor e(owner(u), owner(v), true, inserted.first);
518 if (inserted.second && m_g.on_add_edge)
519 m_g.on_add_edge(e, m_g);
521 add_in_edge(e, property_ptr, directed_selector());
524 template <class Graph, class Archive, class VertexListS>
525 void graph_loader<Graph, Archive, VertexListS>::add_in_edge(
526 edge_descriptor const&, void*, directedS)
529 template <class Graph, class Archive, class VertexListS>
530 void graph_loader<Graph, Archive, VertexListS>::add_in_edge(
531 edge_descriptor const& edge, void* old_property_ptr, bidirectionalS)
533 if (owner(target(edge, m_g)) == m_g.processor())
535 detail::parallel::stored_in_edge<local_edge_descriptor>
536 e(m_g.processor(), local(edge));
537 boost::graph_detail::push(get(
538 vertex_in_edges, m_g.base())[local(target(edge, m_g))], e);
542 // We send the (old,new) property pointer pair to
543 // the remote process. This could be optimized to
544 // only send the new one -- the ordering can be
545 // made implicit because the old pointer value is
546 // stored on the remote process.
548 // Doing that is a little bit more complicated, but
549 // in case it turns out it's important we can do it.
550 void* property_ptr = local(edge).get_property();
551 m_property_ptrs[owner(target(edge, m_g))].push_back(
552 unsafe_pair<void*,void*>(old_property_ptr, property_ptr));
556 template <class Graph, class Archive, class VertexListS>
557 void graph_loader<Graph, Archive, VertexListS>::resolve_property_ptrs()
559 # ifdef PBGL_SERIALIZE_DEBUG
561 std::cout << "Resolving property pointers\n";
564 for (int i = 0; i < num_processes(m_pg); ++i)
567 m_property_ptrs[i].begin(), m_property_ptrs[i].end());
570 boost::parallel::inplace_all_to_all(m_pg, m_property_ptrs);
573 template <class Graph, class Archive, class VertexListS>
574 void graph_loader<Graph, Archive, VertexListS>::resolve_remote_vertices(directedS)
576 for (int i = 0; i < num_processes(m_pg); ++i)
578 std::sort(m_requested_vertices[i].begin(), m_requested_vertices[i].end());
581 boost::parallel::inplace_all_to_all(
582 m_pg, m_requested_vertices, m_remote_vertices);
584 for (int i = 0; i < num_processes(m_pg); ++i)
586 BOOST_FOREACH(serializable_vertex_descriptor& u, m_remote_vertices[i])
588 u = m_local_vertices[u];
592 boost::parallel::inplace_all_to_all(m_pg, m_remote_vertices);
595 template <class Graph, class Archive, class VertexListS>
596 void graph_loader<Graph, Archive, VertexListS>::resolve_remote_vertices(bidirectionalS)
598 # ifdef PBGL_SERIALIZE_DEBUG
600 std::cout << "Resolving remote vertices\n";
603 for (int i = 0; i < num_processes(m_pg); ++i)
605 std::sort(m_requested_vertices[i].begin(), m_requested_vertices[i].end());
606 std::sort(m_remote_vertices[i].begin(), m_remote_vertices[i].end());
608 BOOST_FOREACH(serializable_vertex_descriptor& u, m_remote_vertices[i])
610 u = m_local_vertices[u];
614 boost::parallel::inplace_all_to_all(m_pg, m_remote_vertices);
616 for (int i = 0; i < num_processes(m_pg); ++i)
617 BOOST_ASSERT(m_remote_vertices[i].size() == m_requested_vertices[i].size());
620 template <class Graph, class Archive, class VertexListS>
621 void graph_loader<Graph, Archive, VertexListS>::commit_pending_edges(vecS)
623 commit_pending_in_edges(directed_selector());
626 template <class Graph, class Archive, class VertexListS>
627 template <class Anything>
628 void graph_loader<Graph, Archive, VertexListS>::commit_pending_edges(Anything)
630 resolve_remote_vertices(directed_selector());
632 BOOST_FOREACH(pending_edge_type const& e, m_pending_edges)
634 vertex_descriptor u = resolve_remote_vertex(e.source);
635 vertex_descriptor v = resolve_remote_vertex(e.target);
636 add_edge(u, v, e.properties, e.property_ptr, vecS());
639 commit_pending_in_edges(directed_selector());
642 template <class Graph, class Archive, class VertexListS>
643 void graph_loader<Graph, Archive, VertexListS>::commit_pending_in_edges(directedS)
646 template <class Graph, class Archive, class VertexListS>
647 void graph_loader<Graph, Archive, VertexListS>::commit_pending_in_edges(bidirectionalS)
649 resolve_property_ptrs();
651 BOOST_FOREACH(pending_in_edge const& e, m_pending_in_edges)
653 vertex_descriptor u = resolve_remote_vertex(e.u, vertex_list_selector());
654 vertex_descriptor v = resolve_remote_vertex(e.v, vertex_list_selector());
656 typedef detail::parallel::stored_in_edge<local_edge_descriptor> stored_edge;
658 std::vector<unsafe_pair<void*,void*> >::iterator i = std::lower_bound(
659 m_property_ptrs[owner(u)].begin()
660 , m_property_ptrs[owner(u)].end()
661 , unsafe_pair<void*,void*>(e.property_ptr, 0)
664 if (i == m_property_ptrs[owner(u)].end()
665 || i->first != e.property_ptr)
670 local_edge_descriptor local_edge(local(u), local(v), i->second);
671 stored_edge edge(owner(u), local_edge);
672 boost::graph_detail::push(
673 get(vertex_in_edges, m_g.base())[local(v)], edge);
677 template <class Graph, class Archive, class VertexListS>
678 typename graph_loader<Graph, Archive, VertexListS>::vertex_descriptor
679 graph_loader<Graph, Archive, VertexListS>::resolve_remote_vertex(
680 vertex_descriptor u) const
682 if (owner(u) == process_id(m_pg))
684 return vertex_descriptor(
685 process_id(m_pg), m_local_vertices.find(local(u))->second);
688 typename std::vector<serializable_vertex_descriptor>::const_iterator
689 i = std::lower_bound(
690 m_requested_vertices[owner(u)].begin()
691 , m_requested_vertices[owner(u)].end()
692 , serializable_vertex_descriptor(local(u))
695 if (i == m_requested_vertices[owner(u)].end()
701 local_vertex_descriptor local =
702 m_remote_vertices[owner(u)][m_requested_vertices[owner(u)].end() - i];
703 return vertex_descriptor(owner(u), local);
706 template <class Graph, class Archive, class VertexListS>
707 typename graph_loader<Graph, Archive, VertexListS>::vertex_descriptor
708 graph_loader<Graph, Archive, VertexListS>::resolve_remote_vertex(
709 vertex_descriptor u, vecS) const
714 template <class Graph, class Archive, class VertexListS>
715 template <class Anything>
716 typename graph_loader<Graph, Archive, VertexListS>::vertex_descriptor
717 graph_loader<Graph, Archive, VertexListS>::resolve_remote_vertex(
718 vertex_descriptor u, Anything) const
720 return resolve_remote_vertex(u);
723 template <class Graph, class Archive, class VertexListS>
725 graph_loader<Graph, Archive, VertexListS>::maybe_load_property_ptr(bidirectionalS)
728 m_ar >> make_nvp("property_ptr", unsafe_serialize(ptr));
732 template <class Archive, class D>
733 void maybe_save_local_descriptor(Archive& ar, D const&, vecS)
736 template <class Archive, class D, class NotVecS>
737 void maybe_save_local_descriptor(Archive& ar, D const& d, NotVecS)
739 ar << serialization::make_nvp(
740 "local", unsafe_serialize(const_cast<D&>(d)));
743 template <class Archive>
744 void maybe_save_properties(
745 Archive&, char const*, no_property const&)
748 template <class Archive, class Tag, class T, class Base>
749 void maybe_save_properties(
750 Archive& ar, char const* name, property<Tag, T, Base> const& properties)
752 ar & serialization::make_nvp(name, get_property_value(properties, Tag()));
753 maybe_save_properties(ar, name, static_cast<Base const&>(properties));
756 template <class Archive, class Graph>
757 void save_in_edges(Archive& ar, Graph const& g, directedS)
760 // We need to save the edges in the base edge
761 // list, and the in_edges that are stored in the
762 // vertex_in_edges vertex property.
763 template <class Archive, class Graph>
764 void save_in_edges(Archive& ar, Graph const& g, bidirectionalS)
766 typedef typename Graph::process_group_type
768 typedef typename process_group_type::process_id_type
770 typedef typename graph_traits<
771 Graph>::vertex_descriptor vertex_descriptor;
772 typedef typename vertex_descriptor::local_descriptor_type
773 local_vertex_descriptor;
774 typedef typename graph_traits<
775 Graph>::edge_descriptor edge_descriptor;
777 process_id_type id = g.processor();
779 std::vector<edge_descriptor> saved_in_edges;
781 BGL_FORALL_VERTICES_T(v, g, Graph)
783 BOOST_FOREACH(edge_descriptor const& e, in_edges(v, g))
785 // Only save the in_edges that isn't owned by this process.
789 saved_in_edges.push_back(e);
793 std::size_t I = saved_in_edges.size();
794 ar << BOOST_SERIALIZATION_NVP(I);
796 BOOST_FOREACH(edge_descriptor const& e, saved_in_edges)
798 process_id_type src_owner = owner(source(e,g));
799 local_vertex_descriptor local_src = local(source(e,g));
800 local_vertex_descriptor local_target = local(target(e,g));
801 void* property_ptr = local(e).get_property();
803 using serialization::make_nvp;
805 ar << make_nvp("src_owner", src_owner);
806 ar << make_nvp("source", unsafe_serialize(local_src));
807 ar << make_nvp("target", unsafe_serialize(local_target));
808 ar << make_nvp("property_ptr", unsafe_serialize(property_ptr));
812 template <class Archive, class Edge>
813 void maybe_save_property_ptr(Archive&, Edge const&, directedS)
816 template <class Archive, class Edge>
817 void maybe_save_property_ptr(Archive& ar, Edge const& e, bidirectionalS)
819 void* ptr = local(e).get_property();
820 ar << serialization::make_nvp("property_ptr", unsafe_serialize(ptr));
823 template <class Archive, class Graph, class DirectedS>
824 void save_edges(Archive& ar, Graph const& g, DirectedS)
826 typedef typename Graph::process_group_type
828 typedef typename process_group_type::process_id_type
830 typedef typename graph_traits<
831 Graph>::vertex_descriptor vertex_descriptor;
833 typedef typename Graph::edge_property_type edge_property_type;
835 int E = num_edges(g);
836 ar << BOOST_SERIALIZATION_NVP(E);
838 // For *directed* graphs, we can just save
839 // the edge list and be done.
841 // For *bidirectional* graphs, we need to also
842 // save the "vertex_in_edges" property map,
843 // because it might contain in-edges that
844 // are not locally owned.
845 BGL_FORALL_EDGES_T(e, g, Graph)
847 vertex_descriptor src(source(e, g));
848 vertex_descriptor tgt(target(e, g));
850 typename vertex_descriptor::local_descriptor_type
852 typename vertex_descriptor::local_descriptor_type
855 process_id_type target_owner = owner(tgt);
857 using serialization::make_nvp;
859 ar << make_nvp("source", unsafe_serialize(local_u));
860 ar << make_nvp("target_owner", target_owner);
861 ar << make_nvp("target", unsafe_serialize(local_v));
863 maybe_save_properties(
865 , static_cast<edge_property_type const&>(get(edge_all_t(), g, e))
868 maybe_save_property_ptr(ar, e, DirectedS());
871 save_in_edges(ar, g, DirectedS());
874 }} // namespace detail::parallel
876 template <PBGL_DISTRIB_ADJLIST_TEMPLATE_PARMS>
877 template <class IStreamConstructibleArchive>
878 void PBGL_DISTRIB_ADJLIST_TYPE::load(std::string const& filename)
880 process_group_type pg = process_group();
881 process_id_type id = process_id(pg);
885 std::vector<int> disk_files = detail::parallel::available_process_files(filename);
886 std::sort(disk_files.begin(), disk_files.end());
888 // Negotiate which process gets which file. Serialized.
889 std::vector<int> consumed_files;
890 int picked_file = -1;
893 receive_oob(pg, id-1, 0, consumed_files);
895 std::sort(consumed_files.begin(), consumed_files.end());
896 std::vector<int> available_files;
898 disk_files.begin(), disk_files.end()
899 , consumed_files.begin(), consumed_files.end()
900 , std::back_inserter(available_files)
903 if (available_files.empty())
904 boost::throw_exception(std::runtime_error("no file available"));
906 // back() used for debug purposes. Making sure the
907 // ranks are shuffled.
908 picked_file = available_files.back();
910 # ifdef PBGL_SERIALIZE_DEBUG
911 std::cout << id << " picked " << picked_file << "\n";
914 consumed_files.push_back(picked_file);
916 if (id < num_processes(pg) - 1)
917 send_oob(pg, id+1, 0, consumed_files);
919 std::string local_filename = filename + "/" +
920 lexical_cast<std::string>(picked_file);
922 std::ifstream in(local_filename.c_str(), std::ios_base::binary);
923 IStreamConstructibleArchive ar(in);
925 detail::parallel::graph_loader<
926 graph_type, IStreamConstructibleArchive, InVertexListS
929 # ifdef PBGL_SERIALIZE_DEBUG
930 std::cout << "Process " << id << " done loading.\n";
936 template <PBGL_DISTRIB_ADJLIST_TEMPLATE_PARMS>
937 template <class OStreamConstructibleArchive>
938 void PBGL_DISTRIB_ADJLIST_TYPE::save(std::string const& filename) const
940 typedef typename config_type::VertexListS vertex_list_selector;
942 process_group_type pg = process_group();
943 process_id_type id = process_id(pg);
945 if (filesystem::exists(filename) && !filesystem::is_directory(filename))
946 boost::throw_exception(std::runtime_error("entry exists, but is not a directory"));
948 filesystem::remove_all(filename);
949 filesystem::create_directory(filename);
953 std::string local_filename = filename + "/" +
954 lexical_cast<std::string>(id);
956 std::ofstream out(local_filename.c_str(), std::ios_base::binary);
957 OStreamConstructibleArchive ar(out);
959 using serialization::make_nvp;
961 typename process_group_type::process_size_type num_processes_ = num_processes(pg);
962 ar << make_nvp("num_processes", num_processes_);
963 ar << BOOST_SERIALIZATION_NVP(id);
964 ar << make_nvp("mapping", this->distribution().mapping());
966 int V = num_vertices(*this);
967 ar << BOOST_SERIALIZATION_NVP(V);
969 BGL_FORALL_VERTICES_T(v, *this, graph_type)
971 local_vertex_descriptor local_descriptor(local(v));
972 detail::parallel::maybe_save_local_descriptor(
973 ar, local_descriptor, vertex_list_selector());
974 detail::parallel::maybe_save_properties(
975 ar, "vertex_property"
976 , static_cast<vertex_property_type const&>(get(vertex_all_t(), *this, v))
980 detail::parallel::save_edges(ar, *this, directed_selector());
982 ar << make_nvp("distribution", this->distribution());
987 #endif // BOOST_GRAPH_DISTRIBUTED_ADJLIST_SERIALIZATION_070925_HPP