FlashGraph-ng
A new frontier in large-scale graph analysis and data mining
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Pages
load_balancer.h
1 #ifndef __LOAD_BALANCER_H__
2 #define __LOAD_BALANCER_H__
3 
4 /*
5  * Copyright 2014 Open Connectome Project (http://openconnecto.me)
6  * Written by Da Zheng (zhengda1936@gmail.com)
7  *
8  * This file is part of FlashGraph.
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  */
22 
23 #include <unordered_map>
24 
25 #include "container.h"
26 #include "vertex.h"
27 
28 namespace fg
29 {
30 
31 class worker_thread;
32 class graph_engine;
33 class compute_vertex;
34 class compute_vertex_pointer;
35 
36 /*
37  * This class is to help balance the load.
38  * If the owner thread has finished the work originally assigned to it,
39  * it can steal work from other threads through this class.
40  */
41 class load_balancer
42 {
43  typedef std::unordered_map<const compute_vertex *, int> vertex_map_t;
44 
45  worker_thread &owner;
46  graph_engine &graph;
47 
48  // This map records the owner threads of vertices stolen from another
49  // partition.
50  vertex_map_t stolen_vertex_map;
51 
52  // This is a local buffer that contains the completed stolen vertices.
53  // All vertices here need to be returned to their owner threads.
54  fifo_queue<vertex_id_t> *completed_stolen_vertices;
55  int num_completed_stolen_vertices;
56  // The thread where we should steal activated vertices from.
57  int steal_thread_id;
58 public:
59  load_balancer(graph_engine &_graph, worker_thread &_owner);
60 
61  ~load_balancer();
62 
63  int get_stolen_vertex_part(const compute_vertex &v) const;
64 
65  int steal_activated_vertices(compute_vertex_pointer vertices[], int num);
70  void return_vertices(const compute_vertex_pointer vs[], int num);
71 
72  // This method is to return all completed stolen vertices to their owner
73  // threads.
74  void process_completed_stolen_vertices();
75 
76  void reset();
77 };
78 
79 }
80 
81 #endif