OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
OPAL
SocialNetworkGraph.h
Go to the documentation of this file.
1 #ifndef __SOCIAL_NETWORK_GRAPH__
2 #define __SOCIAL_NETWORK_GRAPH__
3 
4 #include <set>
5 #include <cstdint>
6 
7 #include <boost/random/mersenne_twister.hpp>
8 #include <boost/random/discrete_distribution.hpp>
9 
10 // Here, we use a simple mapping from processor ID's to 2D grid
11 // (determining neigborhood) assuming the total number of masters:
12 //
13 // n_m = n_g * n_g
14 //
15 // This is fine, because if we have topology info the masters are numbered
16 // in ascending order (done in comm splitter) and we can map them from 1D to
17 // 2D by index calculations.
18 
29 template < class TopoDiscoveryStrategy_t >
30 class SocialNetworkGraph : public TopoDiscoveryStrategy_t {
31 
32 public:
33 
34  std::set<size_t> execute(size_t numMasters, size_t dimensions, size_t id,
35  int island_id) {
36 
37  numMasters_ = numMasters;
38  dim_ = dimensions;
39  myID_ = id;
40 
44  }
45 
46 
47 private:
48 
49  boost::random::mt19937 gen_;
50  double alpha_;
51 
52  size_t numMasters_;
53  size_t dim_;
54  size_t myID_;
55 
57  std::set<size_t> realNetworkNeighborPIDs_;
58 
60  size_t m = static_cast<size_t>(sqrt(numMasters_));
61  size_t north = myID_ + m % numMasters_;
62  int64_t south = myID_ - m;
63  if(south < 0) south += numMasters_;
64 
65  size_t east = myID_ + 1;
66  if((myID_ + 1) % m == 0)
67  east = myID_ - m + 1;
68  size_t west = myID_ - 1;
69  if(myID_ % m == 0)
70  west = myID_ + m - 1;
71 
72  realNetworkNeighborPIDs_.insert(north);
73  realNetworkNeighborPIDs_.insert(south);
74  realNetworkNeighborPIDs_.insert(east);
75  realNetworkNeighborPIDs_.insert(west);
76  }
77 
78 
79  //XXX: at the moment assumes square number of masters
80  //void setNetworkNeighbors() {
81 
82  //size_t m = static_cast<size_t>(sqrt(numMasters_));
83 
84  //size_t north = myID_ + m;
85  //if(north < numMasters_)
86  //realNetworkNeighborPIDs_.insert(north);
87  //size_t south = myID_ - m;
88  //if(south >= 0)
89  //realNetworkNeighborPIDs_.insert(south);
90 
91  //size_t east = myID_ + 1;
92  //if((myID_ + 1) % m != 0)
93  //realNetworkNeighborPIDs_.insert(east);
94  //size_t west = myID_ - 1;
95  //if(myID_ % m == 0)
96  //realNetworkNeighborPIDs_.insert(west);
97  //}
98 
99 
100  double manhattenDistance(size_t from, size_t to) {
101 
102  size_t m = static_cast<size_t>(sqrt(numMasters_));
103  int x_from = from / m;
104  int y_from = from % m;
105  int x_to = to / m;
106  int y_to = to % m;
107 
108  return abs(x_from - x_to) + abs(y_from - y_to);
109  }
110 
114 
115  std::vector<double> probabilities(numMasters_, 0.0);
116 
117  double sum = 0.0;
118  for(size_t i = 0; i < numMasters_; i++) {
119  if(i == myID_) continue;
120  sum += std::pow(manhattenDistance(myID_, i), -alpha_);
121  }
122 
123  for(size_t i = 0; i < numMasters_; i++) {
124 
125  if(i == myID_) continue;
126 
127  probabilities[i] =
129  }
130 
131  boost::random::discrete_distribution<>
132  dist(probabilities.begin(), probabilities.end());
133  randomNeighbor_ = dist(gen_);
134  }
135 
136 };
137 
138 #endif
boost::random::mt19937 gen_
PETE_TUTree< FnAbs, typename T::PETE_Expr_t > abs(const PETE_Expr< T > &l)
double manhattenDistance(size_t from, size_t to)
T::PETE_Expr_t::PETE_Return_t sum(const PETE_Expr< T > &expr)
Definition: PETE.h:1213
Tps< T > pow(const Tps< T > &x, int y)
Integer power.
Definition: TpsMath.h:76
Tps< T > sqrt(const Tps< T > &x)
Square root.
Definition: TpsMath.h:91
std::set< size_t > realNetworkNeighborPIDs_
Modeling social graph (Cartesian neighbors plus additional random link) as underlaying master network...
std::set< size_t > execute(size_t numMasters, size_t dimensions, size_t id, int island_id)