-
Notifications
You must be signed in to change notification settings - Fork 0
/
gossip.ex
374 lines (313 loc) · 10 KB
/
gossip.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
defmodule Project2 do
use GenServer
def checkEtsTable(numNodes, startTime,table, parent) do
[{_, currentCount}] = :ets.lookup(table, "count")
if currentCount == (0.9*numNodes) do
currentTime = System.system_time(:millisecond)
endTime = currentTime - startTime
IO.puts "Convergence Achieved in = "<> Integer.to_string(endTime)
Process.exit(parent, :kill)
end
checkEtsTable(numNodes,startTime, table, parent)
end
def buildTopology(topology,allNodes) do
case topology do
"full" -> buildFull(allNodes)
"line" -> buildLine(allNodes)
"impLine" -> impBuildLine(allNodes)
"random2D" -> random2DInitiator(allNodes)
"3D" -> build3D(allNodes)
"torus" -> buildTorus(allNodes)
end
end
def buildFull(allNodes) do
Enum.each(allNodes, fn(k) ->
adjList=List.delete(allNodes,k)
updateAdjacentListState(k,adjList)
end)
end
def buildLine(allNodes) do
numNodes=Enum.count allNodes
Enum.each(allNodes, fn(k) ->
index = Enum.find_index(allNodes, fn(x) -> x==k end)
adjList = []
adjList = adjList ++
if index > 0 do
neighbhour1 = Enum.fetch!(allNodes, index - 1)
[neighbhour1]
else
[]
end
adjList = adjList ++
if index < numNodes - 1 do
neighbhour2 = Enum.fetch!(allNodes, index + 1)
[neighbhour2]
else
[]
end
updateAdjacentListState(k,adjList)
end)
end
def impBuildLine(allNodes) do
numNodes=Enum.count allNodes
Enum.each(allNodes, fn(k) ->
index = Enum.find_index(allNodes, fn(x) -> x==k end)
adjList = []
adjList = adjList ++
if index > 0 do
neighbour1 = Enum.fetch!(allNodes, index - 1)
[neighbour1]
else
[]
end
adjList = adjList ++
if index < numNodes - 1 do
neighbour2 = Enum.fetch!(allNodes, index + 1)
[neighbour2]
else
[]
end
tempNodes = List.delete(allNodes, k)
tempNodes = tempNodes -- adjList
neighbour3 = Enum.random(tempNodes)
adjList = adjList ++ [neighbour3]
updateAdjacentListState(k,adjList)
end)
end
def random2DInitiator(allNodes) do
tempCoList = []
allCordinatesList = Enum.map(allNodes, fn k -> {:rand.uniform(),:rand.uniform()} end)
Enum.each(allNodes, fn(k) ->
index=Enum.find_index(allNodes, fn(x) -> x==k end)
cordinates = Enum.fetch!(allCordinatesList, index)
tempCoList = List.delete_at(allCordinatesList, index)
neighbourIndexes = findNeighbours(cordinates, tempCoList, allCordinatesList)
adjList = Enum.map(neighbourIndexes, fn x -> Enum.fetch!(allNodes, x) end)
updateAdjacentListState(k,adjList)
end)
end
def findNeighbours(cordinates, tempCoList, allCordinatesList) do
neighbourCordinates = Enum.filter(tempCoList, fn x -> :math.sqrt(:math.pow((elem(cordinates,0) - elem(x,0)),2) + :math.pow((elem(cordinates,1) - elem(x,1)),2)) < 0.1 end)
neighbourIndexes = Enum.map(neighbourCordinates, fn x -> Enum.find_index(allCordinatesList, fn(y) -> y==x end) end)
neighbourIndexes
end
def build3D(allNodes) do
numNodes=Enum.count allNodes
cr=Float.floor(nth_root(3,numNodes))
li=Enum.map(allNodes,fn(x)->getCoordinates(Enum.find_index(allNodes,fn k -> k == x end),cr) end)
Enum.each(0..numNodes-1,fn(nn) ->
adjList=getAdjacentNodes(nn,li)
adjList=Enum.map(adjList,fn g -> Enum.at(allNodes,g) end)
updateAdjacentListState(Enum.at(allNodes,nn),adjList)
end)
end
def nth_root(n, x, precision \\ 1.0e-5) do
f = fn(prev) -> ((n - 1) * prev + x / :math.pow(prev, (n-1))) / n end
fixed_point(f, x, precision, f.(x))
end
def getCoordinates(x,cr) do
node=x
zC=div(node,trunc(cr*cr))
point=rem(node,trunc(cr*cr))
yC=div(point,trunc(cr))
xC=rem(point,trunc(cr))
{x,xC,yC,zC}
end
def getAdjacentNodes(x,li) do
del=Enum.filter(li,fn c -> elem(c,0) == x end)
List.delete(li,del)
adj1=Enum.filter(li,fn(a)-> abs(elem(Enum.at(del,0),1)-elem(a,1))+abs(elem(Enum.at(del,0),2)-elem(a,2))+abs(elem(Enum.at(del,0),3)-elem(a,3)) == 1 end)
adj=Enum.map(adj1,fn(y)->elem(y,0) end)
adj
end
defp fixed_point(_, guess, tolerance, next) when abs(guess - next) < tolerance, do: next
defp fixed_point(f, _, tolerance, next), do: fixed_point(f, next, tolerance, f.(next))
def getNeighboursInTorus(magicNumber,actualN,x) do
x=x+1
neighbours=[]
neighbours= neighbours ++
if x-magicNumber > 0 do
[x-magicNumber]
else
if rem(x,magicNumber) != 0 do
[actualN - magicNumber + rem(x, magicNumber)]
else
[actualN]
end
end
neighbours= neighbours ++
if x + magicNumber <= actualN do
[x+magicNumber]
else
if rem(x,magicNumber) != 0 do
[rem(x, magicNumber)]
else
[magicNumber]
end
end
neighbours= neighbours ++
if rem(x,magicNumber) == 0 do
[x-1,x+1-magicNumber]
else
if rem(x,magicNumber) == 1 do
[x+1,x-1+magicNumber]
else
[x-1,x+1]
end
end
neighbours
end
def buildTorus(allNodes) do
numNodes=Enum.count allNodes
magicNumber=:math.sqrt(numNodes)
magicNumber=trunc(magicNumber)
Enum.each(allNodes,fn(k)->
adjList=getNeighboursInTorus(magicNumber,numNodes,Enum.find_index(allNodes,fn g -> g==k end))
adjList=Enum.map(adjList,fn r -> Enum.at(allNodes,r-1) end)
updateAdjacentListState(k,adjList)
end)
end
def startAlgorithm(algorithm,allNodes, startTime) do
case algorithm do
"gossip" -> startGossip(allNodes, startTime)
"push-sum" ->startPushSum(allNodes, startTime)
end
end
def startGossip(allNodes, startTime) do
chosenFirstNode = Enum.random(allNodes)
updateCountState(chosenFirstNode, startTime, length(allNodes))
recurseGossip(chosenFirstNode, startTime, length(allNodes))
end
def recurseGossip(chosenRandomNode, startTime, total) do
myCount = getCountState(chosenRandomNode)
cond do
myCount < 11 ->
adjacentList = getAdjacentList(chosenRandomNode)
chosenRandomAdjacent=Enum.random(adjacentList)
Task.start(Project2,:receiveMessage,[chosenRandomAdjacent, startTime, total])
recurseGossip(chosenRandomNode, startTime, total)
true ->
Process.exit(chosenRandomNode, :normal)
end
recurseGossip(chosenRandomNode, startTime, total)
end
def startPushSum(allNodes, startTime) do
chosenFirstNode = Enum.random(allNodes)
GenServer.cast(chosenFirstNode, {:ReceivePushSum,0,0,startTime, length(allNodes)})
end
def handle_cast({:ReceivePushSum,incomingS,incomingW,startTime, total_nodes},state) do
{s,pscount,adjList,w} = state
myS = s + incomingS
myW = w + incomingW
difference = abs((myS/myW) - (s/w))
if(difference < :math.pow(10,-10) && pscount==2) do
count = :ets.update_counter(:table, "count", {2,1})
if count == total_nodes do
endTime = System.monotonic_time(:millisecond) - startTime
IO.puts "Convergence achieved in = " <> Integer.to_string(endTime) <>" Milliseconds"
System.halt(1)
end
end
pscount = pscount +
if(difference < :math.pow(10,-10) && pscount<2) do
1
else
0
end
pscount =
if(difference > :math.pow(10,-10)) do
0
else
pscount
end
state = {myS/2,pscount,adjList,myW/2}
randomNode = Enum.random(adjList)
sendPushSum(randomNode, myS/2, myW/2,startTime, total_nodes)
{:noreply,state}
end
def sendPushSum(randomNode, myS, myW,startTime, total_nodes) do
GenServer.cast(randomNode, {:ReceivePushSum,myS,myW,startTime, total_nodes})
end
def updatePIDState(pid,nodeID) do
GenServer.call(pid, {:UpdatePIDState,nodeID})
end
def handle_call({:UpdatePIDState,nodeID}, _from ,state) do
{a,b,c,d} = state
state={nodeID,b,c,d}
{:reply,a, state}
end
def updateAdjacentListState(pid,map) do
GenServer.call(pid, {:UpdateAdjacentState,map})
end
def handle_call({:UpdateAdjacentState,map}, _from, state) do
{a,b,c,d}=state
state={a,b,map,d}
{:reply,a, state}
end
def updateCountState(pid, startTime, total) do
GenServer.call(pid, {:UpdateCountState,startTime, total})
end
def handle_call({:UpdateCountState,startTime, total}, _from,state) do
{a,b,c,d}=state
if(b==0) do
count = :ets.update_counter(:table, "count", {2,1})
if(count == total) do
endTime = System.monotonic_time(:millisecond) - startTime
IO.puts "Convergence achieved in = #{endTime} Milliseconds"
System.halt(1)
end
end
state={a,b+1,c,d}
{:reply, b+1, state}
end
def getCountState(pid) do
GenServer.call(pid,{:GetCountState})
end
def handle_call({:GetCountState}, _from ,state) do
{a,b,c,d}=state
{:reply,b, state}
end
def receiveMessage(pid, startTime, total) do
updateCountState(pid, startTime, total)
recurseGossip(pid, startTime, total)
end
def getAdjacentList(pid) do
GenServer.call(pid,{:GetAdjacentList})
end
def handle_call({:GetAdjacentList}, _from ,state) do
{a,b,c,d}=state
{:reply,c, state}
end
def init(:ok) do
{:ok, {0,0,[],1}}
end
def start_node() do
{:ok,pid}=GenServer.start_link(__MODULE__, :ok,[])
pid
end
def infiniteLoop() do
infiniteLoop()
end
def main() do
arguments = System.argv()
if (Enum.count(arguments) != 3) do
IO.puts "Wrong Arguments Given"
System.halt(1)
end
n=Enum.at(arguments, 0) |> String.to_integer()
topology=Enum.at(arguments, 1)
algorithm=Enum.at(arguments, 2)
table = :ets.new(:table, [:named_table,:public])
:ets.insert(table, {"count",0})
allNodes = Enum.map((1..n), fn(x) ->
pid=start_node()
updatePIDState(pid, x)
pid
end)
buildTopology(topology,allNodes)
startTime = System.monotonic_time(:millisecond)
startAlgorithm(algorithm, allNodes, startTime)
infiniteLoop()
end
end
Project2.main()