GraphLab: Distributed Graph-Parallel API  2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
context.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 
24 #ifndef GRAPHLAB_CONTEXT_HPP
25 #define GRAPHLAB_CONTEXT_HPP
26 
27 #include <set>
28 #include <vector>
29 #include <cassert>
30 
31 #include <graphlab/macros_def.hpp>
32 namespace graphlab {
33 
34 
35 
36  /**
37  * \brief The context object mediates the interaction between the
38  * vertex program and the graphlab execution environment and
39  * implements the \ref icontext interface.
40  *
41  * \tparam Engine the engine that is using this context.
42  */
43  template<typename Engine>
44  class context :
45  public icontext<typename Engine::graph_type,
46  typename Engine::gather_type,
47  typename Engine::message_type> {
48  public:
49  // Type members ===========================================================
50  /** The engine that created this context object */
51  typedef Engine engine_type;
52 
53  /** The parent type */
54  typedef icontext<typename Engine::graph_type,
55  typename Engine::gather_type,
56  typename Engine::message_type> icontext_type;
62 
63 
64 
65  private:
66  /** A reference to the engine that created this context */
67  engine_type& engine;
68  /** A reference to the graph that is being operated on by the engine */
69  graph_type& graph;
70 
71  public:
72  /**
73  * \brief Construct a context for a particular engine and graph pair.
74  */
75  context(engine_type& engine, graph_type& graph) :
76  engine(engine), graph(graph) { }
77 
78  size_t num_vertices() const { return graph.num_vertices(); }
79 
80  /**
81  * Get the number of edges in the graph
82  */
83  size_t num_edges() const { return graph.num_edges(); }
84 
85  // /**
86  // * Get an estimate of the number of update functions executed up
87  // * to this point.
88  // */
89  // size_t num_updates() const { return engine.num_updates(); }
90 
91  size_t procid() const { return graph.procid(); }
92 
93  size_t num_procs() const { return graph.numprocs(); }
94 
95  std::ostream& cout() const {
96  return graph.dc().cout();
97  }
98 
99  std::ostream& cerr() const {
100  return graph.dc().cerr();
101  }
102 
103  /**
104  * Get the elapsed time in seconds
105  */
106  float elapsed_seconds() const { return engine.elapsed_seconds(); }
107 
108  /**
109  * Return the current interation number (if supported).
110  */
111  int iteration() const { return engine.iteration(); }
112 
113  /**
114  * Force the engine to stop executing additional update functions.
115  */
116  void stop() { engine.internal_stop(); }
117 
118  /**
119  * Send a message to a vertex.
120  */
121  void signal(const vertex_type& vertex,
122  const message_type& message = message_type()) {
123  engine.internal_signal(vertex, message);
124  }
125 
126  /**
127  * Send a message to a vertex ID.
128  * \warning This function will be slow since the current machine do
129  * not know the location of the vertex ID.
130  * \warning This may be unreliable. signals issued near to engine
131  * termination may be lost.
132  */
134  const message_type& message = message_type()) {
135  engine.internal_signal_broadcast(vid, message);
136  }
137 
138 
139  /**
140  * Post a change to the cached sum for the vertex
141  */
142  void post_delta(const vertex_type& vertex,
143  const gather_type& delta) {
144  engine.internal_post_delta(vertex, delta);
145  }
146 
147  /**
148  * Invalidate the cached gather on the vertex.
149  */
150  virtual void clear_gather_cache(const vertex_type& vertex) {
151  engine.internal_clear_gather_cache(vertex);
152  }
153 
154 
155 
156 
157  }; // end of context
158 
159 } // end of namespace
160 #include <graphlab/macros_undef.hpp>
161 
162 #endif
163