Turi Create  4.0
sgraph_synchronize_interface.hpp
1 /* Copyright © 2017 Apple Inc. All rights reserved.
2  *
3  * Use of this source code is governed by a BSD-3-clause license that can
4  * be found in the LICENSE.txt file or at https://opensource.org/licenses/BSD-3-Clause
5  */
6 #ifndef TURI_SGRAPH_SGRAPH_SYNCHRONIZE_INTERFACE_HPP
7 #define TURI_SGRAPH_SGRAPH_SYNCHRONIZE_INTERFACE_HPP
8 
9 #include<core/storage/sgraph_data/sgraph_types.hpp>
10 
11 namespace turi {
12 
13 
14 /**
15  * \internal
16  * \ingroup sgraph_physical
17  * \addtogroup sgraph_compute_internal SGraph Compute Internal
18  * \{
19  */
20 
21 /**
22  * Graph Computation Functions
23  */
24 namespace sgraph_compute {
25 
26 
27 
28 /**
29  * Storing a subset of vertex data of a subset of vertices from an sgraph partition.
30  *
31  * The vertex data can be a subset of fields, but all vertices
32  * in the same exchange object must contain the same set of fields.
33  *
34  * \note This really is implementation detail and is used to allow graph
35  * computation methods that are implemented in Python
36  */
38 
39  /// id of the partition that vertices belong to.
40  size_t partition_id;
41 
42  /**
43  * index and data pair of the vertices to be exchanged.
44  * vertices[i] : (vindex, vdata)
45  * where \ref vindex is the local_id of the vertex in the partition;
46  * and \ref vdata contains the subset of vertex data.
47  * The subset is defined by \ref field_ids.
48  */
49  std::vector<std::pair<size_t, sgraph_vertex_data>> vertices;
50 
51  /// A subset of field ids the vertex data contain.
52  std::vector<size_t> field_ids;
53 
54  void save(oarchive& oarc) const {
55  oarc << vertices << field_ids << partition_id;
56  }
57  void load(iarchive& iarc) {
58  iarc >> vertices >> field_ids >> partition_id;
59  }
60 };
61 
62 /**
63  * Defines the interface for serializing vertex data and edge data
64  * of an sgraph.
65  *
66  * The main application of this is for communication of graph information.
67  *
68  * \ref vertex_partition_exchange is the struct that can hold
69  * a subset data of a subset of vertices from a sgraph partition.
70  *
71  * The choice for sparse vertices packing is motivated by the
72  * "triple_apply" computation pattern:
73  * as we process each edge partition, the associated vertex partition
74  * are sparsely visited and updated.
75  *
76  * \ref sgraph_synchronize_interface is used for both ends of the communication
77  * to deal with initialization, sending and receiving the vertex and edge exchange data.
78  *
79  * Example:
80  * \code
81  *
82  * //// Worker side ////
83  * sgraph_synchronize_interface* worker_graph_sync; // a client side implementation
84  *
85  * // initialize vertex data in partition 0, using all_vertices sent from server.
86  * worker_graph_sync.load_vertex_partition(0, all_vertices_from_server);
87  *
88  * // recevie a vertex_exchange from server, let's update the local vertices
89  * worker_graph_sync->update_vertex_partition(vexchange_from_server);
90  *
91  * // do some work to update vertex data in partition 0, and obtain a set of changed vertex data..
92  * vertex_exchange updated_vertices = worker_graph_sync->get_vertex_partition_exchange(0);
93  *
94  * // now we can send updated_vertices to the server.
95  *
96  * //// Server side ////
97  * sgraph_synchronize_interface* server_graph_sync; // a server side implementation
98  *
99  * // call to worker to initialize vertex partition.
100  *
101  * // recevie a vertex_exchange server, let's update the local vertices
102  * server_graph_sync->update_vertex_partition(vexchange_from_client);
103  *
104  * // do some work to update vertex data in partition 0, and obtain a set of changed vertex data..
105  * vertex_exchange updated_vertices = server_graph_sync->get_vertex_partition_exchange(0);
106  *
107  * // now we can send updated_vertices to the worker.
108  *
109  * \endcode
110  *
111  * \note This really is implementation detail and is used to allow graph
112  * computation methods that are implemented in Python
113  */
115  public:
116  // Vertex sync
117  /// Given a vector of all vertices of partition, initialize the local vertex storage.
118  virtual void load_vertex_partition(size_t partition_id, std::vector<sgraph_vertex_data>& all_vertices) = 0;
119 
120  /// Given a vertex exchange object, update the local vertex storage.
121  virtual void update_vertex_partition(vertex_partition_exchange& vpartition_exchange) = 0;
122 
123  /// Obtain a vertex exchange object containing a subset of vertices and fields.
124  virtual vertex_partition_exchange get_vertex_partition_exchange(size_t partition_id, const std::unordered_set<size_t>& vertex_ids, const std::vector<size_t>& field_ids) = 0;
125 
126  virtual ~sgraph_synchronize_interface() { }
127 };
128 
129 }
130 
131 /// \}
132 }
133 #endif
The serialization input archive object which, provided with a reference to an istream, will read from the istream, providing deserialization capabilities.
Definition: iarchive.hpp:60
std::vector< std::pair< size_t, sgraph_vertex_data > > vertices
The serialization output archive object which, provided with a reference to an ostream, will write to the ostream, providing serialization capabilities.
Definition: oarchive.hpp:80
std::vector< size_t > field_ids
A subset of field ids the vertex data contain.
size_t partition_id
id of the partition that vertices belong to.