1 // Copyright (C) 2007 Douglas Gregor
2 
3 // Use, modification and distribution is subject to the Boost Software
4 // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
6 
7 // This file contains code for the distributed adjacency list's
8 // message handlers. It should not be included directly by users.
9 
10 #ifndef BOOST_GRAPH_DISTRIBUTED_ADJLIST_HANDLERS_HPP
11 #define BOOST_GRAPH_DISTRIBUTED_ADJLIST_HANDLERS_HPP
12 
13 #ifndef BOOST_GRAPH_USE_MPI
14 #error "Parallel BGL files should not be included unless <boost/graph/use_mpi.hpp> has been included"
15 #endif
16 
17 #include <boost/graph/parallel/simple_trigger.hpp>
18 #include <boost/graph/parallel/detail/untracked_pair.hpp>
19 
20 namespace boost {
21 
22 template<PBGL_DISTRIB_ADJLIST_TEMPLATE_PARMS>
23 void
24 PBGL_DISTRIB_ADJLIST_TYPE::
setup_triggers()25 setup_triggers()
26 {
27   using boost::graph::parallel::simple_trigger;
28 
29   simple_trigger(process_group_, msg_add_vertex_with_property, this,
30                  &adjacency_list::handle_add_vertex_with_property);
31   simple_trigger(process_group_, msg_add_vertex_with_property_and_reply, this,
32                  &adjacency_list::handle_add_vertex_with_property_and_reply);
33   simple_trigger(process_group_, msg_add_edge, this,
34                  &adjacency_list::handle_add_edge);
35   simple_trigger(process_group_, msg_add_edge_with_reply, this,
36                  &adjacency_list::handle_add_edge_with_reply);
37   simple_trigger(process_group_, msg_add_edge_with_property, this,
38                  &adjacency_list::handle_add_edge_with_property);
39   simple_trigger(process_group_,  msg_add_edge_with_property_and_reply, this,
40                  &adjacency_list::handle_add_edge_with_property_and_reply);
41   simple_trigger(process_group_, msg_nonlocal_edge, this,
42                  &adjacency_list::handle_nonlocal_edge);
43   simple_trigger(process_group_, msg_remove_edge, this,
44                  &adjacency_list::handle_remove_edge);
45 }
46 
47 template<PBGL_DISTRIB_ADJLIST_TEMPLATE_PARMS>
48 void
49 PBGL_DISTRIB_ADJLIST_TYPE::
handle_add_vertex_with_property(int source,int tag,const vertex_property_type & data,trigger_receive_context)50 handle_add_vertex_with_property(int source, int tag,
51                                 const vertex_property_type& data,
52                                 trigger_receive_context)
53 {
54   vertex_descriptor v(this->processor(),
55                       add_vertex(this->build_vertex_property(data),
56                                  this->base()));
57   if (on_add_vertex)
58     on_add_vertex(v, *this);
59 }
60 
61 template<PBGL_DISTRIB_ADJLIST_TEMPLATE_PARMS>
62 typename PBGL_DISTRIB_ADJLIST_TYPE::local_vertex_descriptor
63 PBGL_DISTRIB_ADJLIST_TYPE::
handle_add_vertex_with_property_and_reply(int source,int tag,const vertex_property_type & data,trigger_receive_context)64 handle_add_vertex_with_property_and_reply(int source, int tag,
65                                           const vertex_property_type& data,
66                                           trigger_receive_context)
67 {
68   // Try to find a vertex with this name
69   local_vertex_descriptor local_v
70     = add_vertex(this->build_vertex_property(data), this->base());
71 
72   vertex_descriptor v(processor(), local_v);
73   if (on_add_vertex)
74     on_add_vertex(v, *this);
75 
76   return local_v;
77 }
78 
79 template<PBGL_DISTRIB_ADJLIST_TEMPLATE_PARMS>
80 void
81 PBGL_DISTRIB_ADJLIST_TYPE::
handle_add_edge(int source,int tag,const msg_add_edge_data & data,trigger_receive_context)82 handle_add_edge(int source, int tag, const msg_add_edge_data& data,
83                 trigger_receive_context)
84 {
85   add_edge(vertex_descriptor(processor(), data.source),
86            data.target, *this);
87 }
88 
89 template<PBGL_DISTRIB_ADJLIST_TEMPLATE_PARMS>
90 boost::parallel::detail::untracked_pair<typename PBGL_DISTRIB_ADJLIST_TYPE::edge_descriptor, bool>
91 PBGL_DISTRIB_ADJLIST_TYPE::
handle_add_edge_with_reply(int source,int tag,const msg_add_edge_data & data,trigger_receive_context)92 handle_add_edge_with_reply(int source, int tag, const msg_add_edge_data& data,
93                            trigger_receive_context)
94 {
95   std::pair<typename PBGL_DISTRIB_ADJLIST_TYPE::edge_descriptor, bool> p =
96     add_edge(vertex_descriptor(processor(), data.source),data.target, *this);
97   return p;
98 }
99 
100 template<PBGL_DISTRIB_ADJLIST_TEMPLATE_PARMS>
101 void
102 PBGL_DISTRIB_ADJLIST_TYPE::
handle_add_edge_with_property(int source,int tag,const msg_add_edge_with_property_data & data,trigger_receive_context)103 handle_add_edge_with_property(int source, int tag,
104                               const msg_add_edge_with_property_data& data,
105                               trigger_receive_context)
106 {
107   add_edge(vertex_descriptor(processor(), data.source),
108            data.target, data.get_property(), *this);
109 }
110 
111 template<PBGL_DISTRIB_ADJLIST_TEMPLATE_PARMS>
112 boost::parallel::detail::untracked_pair<typename PBGL_DISTRIB_ADJLIST_TYPE::edge_descriptor, bool>
113 PBGL_DISTRIB_ADJLIST_TYPE::
handle_add_edge_with_property_and_reply(int source,int tag,const msg_add_edge_with_property_data & data,trigger_receive_context)114 handle_add_edge_with_property_and_reply
115   (int source, int tag,
116    const msg_add_edge_with_property_data& data,
117    trigger_receive_context)
118 {
119   std::pair<typename PBGL_DISTRIB_ADJLIST_TYPE::edge_descriptor, bool> p =
120     add_edge(vertex_descriptor(processor(), data.source),
121                   data.target, data.get_property(), *this);
122   return p;
123 }
124 
125 template<PBGL_DISTRIB_ADJLIST_TEMPLATE_PARMS>
126 void
127 PBGL_DISTRIB_ADJLIST_TYPE::
handle_nonlocal_edge(int source,int tag,const msg_nonlocal_edge_data & data,trigger_receive_context)128 handle_nonlocal_edge(int source, int tag,
129                      const msg_nonlocal_edge_data& data,
130                      trigger_receive_context)
131 {
132   add_remote_edge(data, source, directed_selector());
133 }
134 
135 template<PBGL_DISTRIB_ADJLIST_TEMPLATE_PARMS>
136 void
137 PBGL_DISTRIB_ADJLIST_TYPE::
handle_remove_edge(int source,int tag,const msg_remove_edge_data & data,trigger_receive_context)138 handle_remove_edge(int source, int tag,
139                    const msg_remove_edge_data& data,
140                    trigger_receive_context)
141 {
142   remove_local_edge(data, source, directed_selector());
143 }
144 
145 }
146 
147 #endif // BOOST_GRAPH_DISTRIBUTED_ADJLIST_HANDLERS_HPP
148 
149