OPAL (Object Oriented Parallel Accelerator Library) 2022.1
OPAL
SocialNetworkGraph.h
Go to the documentation of this file.
1//
2// Class SocialNetworkGraph
3// Modeling social graph (Cartesian neighbors plus additional random
4// link) as underlaying master network.
5//
6// @see MasterNode
7//
8// Due to its nice rumor spreading properties this master network is mimicking
9// a social network graph (augmented grid) and solution states are distributed
10// in a rumor fashion.
11//
12// Here, we use a simple mapping from processor ID's to 2D grid
13// (determining neigborhood) assuming the total number of masters:
14//
15// n_m = n_g * n_g
16//
17// This is fine, because if we have topology info the masters are numbered
18// in ascending order (done in comm splitter) and we can map them from 1D to
19// 2D by index calculations.
20//
21// Copyright (c) 2010 - 2013, Yves Ineichen, ETH Zürich
22// All rights reserved
23//
24// Implemented as part of the PhD thesis
25// "Toward massively parallel multi-objective optimization with application to
26// particle accelerators" (https://doi.org/10.3929/ethz-a-009792359)
27//
28// This file is part of OPAL.
29//
30// OPAL is free software: you can redistribute it and/or modify
31// it under the terms of the GNU General Public License as published by
32// the Free Software Foundation, either version 3 of the License, or
33// (at your option) any later version.
34//
35// You should have received a copy of the GNU General Public License
36// along with OPAL. If not, see <https://www.gnu.org/licenses/>.
37//
38#ifndef __SOCIAL_NETWORK_GRAPH__
39#define __SOCIAL_NETWORK_GRAPH__
40
41#include <set>
42#include <cstdint>
43
44#include <boost/random/mersenne_twister.hpp>
45#include <boost/random/discrete_distribution.hpp>
46
47template < class TopoDiscoveryStrategy_t >
48class SocialNetworkGraph : public TopoDiscoveryStrategy_t {
49
50public:
51
52 std::set<size_t> execute(size_t numMasters, size_t dimensions, size_t id,
53 int /*island_id*/) {
54
55 numMasters_ = numMasters;
56 dim_ = dimensions;
57 myID_ = id;
58
62 }
63
64
65private:
66
67 boost::random::mt19937 gen_;
68 double alpha_;
69
71 size_t dim_;
72 size_t myID_;
73
75 std::set<size_t> realNetworkNeighborPIDs_;
76
78 size_t m = static_cast<size_t>(sqrt(numMasters_));
79 size_t north = myID_ + m % numMasters_;
80 int64_t south = myID_ - m;
81 if(south < 0) south += numMasters_;
82
83 size_t east = myID_ + 1;
84 if((myID_ + 1) % m == 0)
85 east = myID_ - m + 1;
86 size_t west = myID_ - 1;
87 if(myID_ % m == 0)
88 west = myID_ + m - 1;
89
90 realNetworkNeighborPIDs_.insert(north);
91 realNetworkNeighborPIDs_.insert(south);
92 realNetworkNeighborPIDs_.insert(east);
93 realNetworkNeighborPIDs_.insert(west);
94 }
95
96
97 //XXX: at the moment assumes square number of masters
98 //void setNetworkNeighbors() {
99
100 //size_t m = static_cast<size_t>(sqrt(numMasters_));
101
102 //size_t north = myID_ + m;
103 //if(north < numMasters_)
104 //realNetworkNeighborPIDs_.insert(north);
105 //size_t south = myID_ - m;
106 //if(south >= 0)
107 //realNetworkNeighborPIDs_.insert(south);
108
109 //size_t east = myID_ + 1;
110 //if((myID_ + 1) % m != 0)
111 //realNetworkNeighborPIDs_.insert(east);
112 //size_t west = myID_ - 1;
113 //if(myID_ % m == 0)
114 //realNetworkNeighborPIDs_.insert(west);
115 //}
116
117
118 double manhattenDistance(size_t from, size_t to) {
119
120 size_t m = static_cast<size_t>(sqrt(numMasters_));
121 int x_from = from / m;
122 int y_from = from % m;
123 int x_to = to / m;
124 int y_to = to % m;
125
126 return abs(x_from - x_to) + abs(y_from - y_to);
127 }
128
132
133 std::vector<double> probabilities(numMasters_, 0.0);
134
135 double sum = 0.0;
136 for(size_t i = 0; i < numMasters_; i++) {
137 if(i == myID_) continue;
139 }
140
141 for(size_t i = 0; i < numMasters_; i++) {
142
143 if(i == myID_) continue;
144
145 probabilities[i] =
147 }
148
149 boost::random::discrete_distribution<>
150 dist(probabilities.begin(), probabilities.end());
151 randomNeighbor_ = dist(gen_);
152 }
153
154};
155
156#endif
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
T::PETE_Expr_t::PETE_Return_t sum(const PETE_Expr< T > &expr)
Definition: PETE.h:1111
PETE_TUTree< FnAbs, typename T::PETE_Expr_t > abs(const PETE_Expr< T > &l)
double manhattenDistance(size_t from, size_t to)
std::set< size_t > realNetworkNeighborPIDs_
boost::random::mt19937 gen_
std::set< size_t > execute(size_t numMasters, size_t dimensions, size_t id, int)