GraphLab: Distributed Graph-Parallel API  2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
distributed_constrained_random_ingress.hpp
1 /**
2  * Copyright (c) 2009 Carnegie Mellon University.
3  * All rights reserved.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing,
12  * software distributed under the License is distributed on an "AS
13  * IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
14  * express or implied. See the License for the specific language
15  * governing permissions and limitations under the License.
16  *
17  * For more about this software visit:
18  *
19  * http://www.graphlab.ml.cmu.edu
20  *
21  */
22 
23 #ifndef GRAPHLAB_DISTRIBUTED_CONSTRAINED_RANDOM_INGRESS_HPP
24 #define GRAPHLAB_DISTRIBUTED_CONSTRAINED_RANDOM_INGRESS_HPP
25 
26 #include <boost/functional/hash.hpp>
27 
28 #include <graphlab/rpc/buffered_exchange.hpp>
29 #include <graphlab/graph/graph_basic_types.hpp>
30 #include <graphlab/graph/ingress/idistributed_ingress.hpp>
31 #include <graphlab/graph/ingress/distributed_ingress_base.hpp>
32 #include <graphlab/graph/distributed_graph.hpp>
33 #include <graphlab/graph/ingress/sharding_constraint.hpp>
34 #include <graphlab/graph/ingress/ingress_edge_decision.hpp>
35 
36 
37 #include <graphlab/macros_def.hpp>
38 namespace graphlab {
39  template<typename VertexData, typename EdgeData>
40  class distributed_graph;
41 
42  /**
43  * \brief Ingress object assigning edges using randoming hash function.
44  */
45  template<typename VertexData, typename EdgeData>
47  public distributed_ingress_base<VertexData, EdgeData> {
48  public:
50  /// The type of the vertex data stored in the graph
51  typedef VertexData vertex_data_type;
52  /// The type of the edge data stored in the graph
53  typedef EdgeData edge_data_type;
54 
55 
56  typedef distributed_ingress_base<VertexData, EdgeData> base_type;
57 
58  sharding_constraint* constraint;
59  boost::hash<vertex_id_type> hashvid;
60 
61  public:
63  const std::string& method) :
64  base_type(dc, graph) {
65  constraint = new sharding_constraint(dc.numprocs(), method);
66  } // end of constructor
67 
69  delete constraint;
70  }
71 
72  /** Add an edge to the ingress object using random assignment. */
73  void add_edge(vertex_id_type source, vertex_id_type target,
74  const EdgeData& edata) {
76 
77  std::vector<procid_t> candidates;
78  constraint->get_joint_neighbors(get_master(source), get_master(target), candidates);
79 
80  const procid_t owning_proc =
81  base_type::edge_decision.edge_to_proc_random(source, target, candidates);
82 
83 
84  const edge_buffer_record record(source, target, edata);
85  base_type::edge_exchange.send(owning_proc, record);
86  } // end of add edge
87 
88  private:
89  procid_t get_master (vertex_id_type vid) {
90  return ingress_edge_decision<VertexData, EdgeData>::mix(vid) % base_type::rpc.numprocs();
91  }
92  }; // end of distributed_constrained_random_ingress
93 }; // end of namespace graphlab
94 #include <graphlab/macros_undef.hpp>
95 
96 
97 #endif