OPAL (Object Oriented Parallel Accelerator Library) 2024.2
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 <algorithm>
42#include <cmath>
43#include <cstdint>
44#include <random>
45#include <set>
46#include <vector>
47
48template < class TopoDiscoveryStrategy_t >
49class SocialNetworkGraph : public TopoDiscoveryStrategy_t {
50
51public:
52 std::set<size_t> execute(size_t numMasters, size_t dimensions,
53 size_t id, int /*island_id*/) {
54
55 numMasters_ = numMasters;
56 dim_ = dimensions;
57 myID_ = id;
58
62 }
63
64
65private:
66 std::mt19937 gen_{5489u}; // default seed for reproducibility
67 double alpha_;
68
70 size_t dim_;
71 size_t myID_;
72
74 std::set<size_t> realNetworkNeighborPIDs_;
75
77 size_t m = static_cast<size_t>(std::sqrt(numMasters_));
78 size_t north = myID_ + m % numMasters_;
79 int64_t south = myID_ - m;
80 if (south < 0) {
81 south += numMasters_;
82 }
83 size_t east = myID_ + 1;
84 if ((myID_ + 1) % m == 0) {
85 east = myID_ - m + 1;
86 }
87 size_t west = myID_ - 1;
88 if (myID_ % m == 0) {
89 west = myID_ + m - 1;
90 }
91
92 realNetworkNeighborPIDs_.insert(north);
93 realNetworkNeighborPIDs_.insert(south);
94 realNetworkNeighborPIDs_.insert(east);
95 realNetworkNeighborPIDs_.insert(west);
96 }
97
98 //XXX: at the moment assumes square number of masters
99 //void setNetworkNeighbors() {
100 //size_t m = static_cast<size_t>(std::sqrt(numMasters_));
101 //size_t north = myID_ + m;
102 //if(north < numMasters_)
103 //realNetworkNeighborPIDs_.insert(north);
104 //size_t south = myID_ - m;
105 //if(south >= 0)
106 //realNetworkNeighborPIDs_.insert(south);
107 //size_t east = myID_ + 1;
108 //if((myID_ + 1) % m != 0)
109 //realNetworkNeighborPIDs_.insert(east);
110 //size_t west = myID_ - 1;
111 //if(myID_ % m == 0)
112 //realNetworkNeighborPIDs_.insert(west);
113 //}
114
115 double manhattenDistance(size_t from, size_t to) {
116
117 size_t m = static_cast<size_t>(std::sqrt(numMasters_));
118 int x_from = from / m;
119 int y_from = from % m;
120 int x_to = to / m;
121 int y_to = to % m;
122
123 return std::abs(x_from - x_to) + std::abs(y_from - y_to);
124 }
125
129
130 std::vector<double> probabilities(numMasters_, 0.0);
131
132 double sum = 0.0;
133 for (size_t i = 0; i < numMasters_; i++) {
134 if (i == myID_) continue;
135 sum += std::pow(manhattenDistance(myID_, i), -alpha_);
136 }
137
138 for (size_t i = 0; i < numMasters_; i++) {
139 if (i == myID_) continue;
140 probabilities[i] =
141 std::pow(manhattenDistance(myID_, i), -alpha_) / sum;
142 }
143
144 std::discrete_distribution<size_t>
145 dist(probabilities.begin(), probabilities.end());
146
147 randomNeighbor_ = dist(gen_);
148 }
149
150};
151
152#endif
T::PETE_Expr_t::PETE_Return_t sum(const PETE_Expr< T > &expr)
Definition PETE.h:1111
double manhattenDistance(size_t from, size_t to)
std::set< size_t > realNetworkNeighborPIDs_
std::set< size_t > execute(size_t numMasters, size_t dimensions, size_t id, int)