-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathproject2_bonus.fsx
429 lines (356 loc) · 15.3 KB
/
project2_bonus.fsx
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
#r "nuget: Akka.FSharp"
#r "nuget: Akka.TestKit"
open System
open Akka.Actor
open Akka.Configuration
open Akka.FSharp
open System.Diagnostics
open System.Collections.Generic
///////////////////////////Initialization////////////////////////////////////////
type GossipMessageTypes =
| Initailize of IActorRef []
| InitializeVariables of int
| StartGossip of String
| ReportMsgRecvd of String
| StartPushSum of Double
| ComputePushSum of Double * Double * Double * bool
| Result of Double * Double
| Time of int
| TotalNodes of int
| ActivateWorker
| CallWorker
| AddNeighbors
| Shutdown of int*IActorRef []
| GetNeighborsToIgnore of int []
| AddPushSum
| ActivateWorker2
type Topology =
| Gossip of String
| PushSum of String
type Protocol =
| Line of String
| Full of String
| TwoDimension of String
let mutable nodes =
int (string (fsi.CommandLineArgs.GetValue 1))
let topology = string (fsi.CommandLineArgs.GetValue 2)
let protocol = string (fsi.CommandLineArgs.GetValue 3)
let numNodesToIgnore = string (fsi.CommandLineArgs.GetValue 4) |>int
let mutable shutdownNeighborsList : int [] = [||]
let timer = Diagnostics.Stopwatch()
let system = ActorSystem.Create("System")
let mutable actualNumOfNodes = nodes |> float
nodes <-
match topology with
| "2D" | "Imp2D" ->
((actualNumOfNodes ** 0.5)|>ceil ) ** 2.0 |> int
| _ -> nodes
let mutable nodeArray = [||]
///////////////////////////Initialization////////////////////////////////////////
///////////////////////////Supervisor Actor////////////////////////////////////////
let Supervisor(mailbox: Actor<_>) =
let mutable count = 0
let mutable start = 0
let mutable totalNodes = 0
let rec loop()= actor{
let! msg = mailbox.Receive();
match msg with
| ReportMsgRecvd _ ->
let ending = DateTime.Now.TimeOfDay.Milliseconds
count <- count + 1
if count = totalNodes then
timer.Stop()
printfn "Time for convergence: %f ms" timer.Elapsed.TotalMilliseconds
Environment.Exit(0)
| Result (sum, weight) ->
count <- count + 1
if count = totalNodes then
timer.Stop()
printfn "Sum = %f Weight= %f Average=%f" sum weight (sum / weight)
printfn "Time for convergence: %f ms" timer.Elapsed.TotalMilliseconds
Environment.Exit(0)
| Time strtTime -> start <- strtTime
| TotalNodes n -> totalNodes <- n
| _ -> ()
return! loop()
}
loop()
///////////////////////////Supervisor Actor////////////////////////////////////////
let supervisor = spawn system "Supervisor" Supervisor
let dictionary = new Dictionary<IActorRef, bool>()
let indexDictionary = new Dictionary<IActorRef, int>()
///////////////////////////Worker Actor////////////////////////////////////////
let Worker(mailbox: Actor<_>) =
let mutable rumourCount = 0
let mutable neighbours: IActorRef [] = [||]
let mutable sum = 0 |>float
let mutable weight = 1.0
let mutable termRound = 1
let mutable neighborsToIgoreArray:int [] = [||]
let mutable faultynode = false
let mutable alreadyConverged = false
let rec loop()= actor{
let! message = mailbox.Receive();
match message with
| Initailize aref ->
neighbours <- aref
| GetNeighborsToIgnore shutdownNeighborsList ->
neighborsToIgoreArray <-shutdownNeighborsList
if Array.contains indexDictionary.[mailbox.Self] neighborsToIgoreArray then
faultynode <- true
| ActivateWorker ->
if rumourCount < 11 then
let rnd = Random().Next(0, neighbours.Length)
if not dictionary.[neighbours.[rnd]] then
neighbours.[rnd] <! CallWorker
mailbox.Self <! ActivateWorker
| CallWorker ->
if rumourCount = 0 then
mailbox.Self <! ActivateWorker
if (rumourCount = 10) then
supervisor <! ReportMsgRecvd "Rumor"
dictionary.[mailbox.Self] <- true
rumourCount <- rumourCount + 1
| InitializeVariables number ->
sum <- number |> double
| StartPushSum delta ->
let index = Random().Next(0, neighbours.Length)
sum <- sum / 2.0
weight <- weight / 2.0
neighbours.[index] <! ComputePushSum(sum, weight, delta,true)
| ComputePushSum (s: float, w, delta,z) ->
if (not faultynode || z = true ) then
let newsum = sum + s
let newweight = weight + w
let cal = sum / weight - newsum / newweight |> abs
if alreadyConverged then
let index = Random().Next(0, neighbours.Length)
neighbours.[index] <! ComputePushSum(s, w, delta,false)
else
if cal > delta then
termRound <- 0
else
termRound <- termRound + 1
if termRound = 3 then
termRound <- 0
alreadyConverged <- true
supervisor <! Result(sum, weight)
sum <- newsum / 2.0
weight <- newweight / 2.0
let index = Random().Next(0, neighbours.Length)
neighbours.[index] <! ComputePushSum(sum, weight, delta,false)
| _ -> ()
return! loop()
}
loop()
let ActorConverger (mailbox: Actor<_>) =
let neighbors = new List<IActorRef>()
let rec loop() = actor {
let! message = mailbox.Receive()
match message with
| AddNeighbors _ ->
for i in [0..nodes-1] do
neighbors.Add nodeArray.[i]
mailbox.Self <! ActivateWorker
| ActivateWorker ->
if neighbors.Count > 0 then
let randomNumber = Random().Next(neighbors.Count)
let randomActor = neighbors.[randomNumber]
if (dictionary.[neighbors.[randomNumber]]) then
(neighbors.Remove randomActor) |>ignore
else
randomActor <! CallWorker
mailbox.Self <! ActivateWorker
| AddPushSum ->
for i in [0..nodes-1] do
neighbors.Add nodeArray.[i]
mailbox.Self <! ActivateWorker2
| ActivateWorker2 ->
if neighbors.Count > 0 then
let randomNumber = Random().Next(neighbors.Count)
let randomActor = neighbors.[randomNumber]
if (dictionary.[neighbors.[randomNumber]]) then
(neighbors.Remove randomActor) |>ignore
else
randomActor <! StartPushSum(10.0 ** -10.0)
mailbox.Self <! ActivateWorker2
| _ -> ()
return! loop()
}
loop()
let GossipActor = spawn system "ActorConverger" ActorConverger
///////////////////////////Worker Actor////////////////////////////////////////
///////////////////////////Program////////////////////////////////////////
match topology with
| "line" ->
nodeArray <- Array.zeroCreate (nodes + 1)
for x in [0..nodes] do
let key: string = "demo" + string(x)
let actorRef = spawn system (key) Worker
nodeArray.[x] <- actorRef
dictionary.Add(nodeArray.[x], false)
indexDictionary.Add(nodeArray.[x], x)
nodeArray.[x] <! InitializeVariables x
for i = 1 to numNodesToIgnore do
let mutable rnd_shutdown = Random().Next(0, nodes)
printfn "shutting down %i" rnd_shutdown
shutdownNeighborsList <- (Array.append shutdownNeighborsList [|rnd_shutdown|])
[0..nodes] |> List.iter (fun i -> nodeArray.[i] <! GetNeighborsToIgnore(shutdownNeighborsList))
for i in [ 0 .. nodes ] do
let mutable neighbourArray = [||]
if i = 0 then
neighbourArray <- (Array.append neighbourArray [|nodeArray.[i+1]|])
elif i = nodes then
neighbourArray <- (Array.append neighbourArray [|nodeArray.[i-1]|])
else
neighbourArray <- (Array.append neighbourArray [| nodeArray.[(i - 1)] ; nodeArray.[(i + 1 ) ] |] )
nodeArray.[i] <! Initailize(neighbourArray)
let leader = Random().Next(0, nodes)
timer.Start()
match protocol with
| "gossip" ->
supervisor <! TotalNodes(nodes)
supervisor <! Time(DateTime.Now.TimeOfDay.Milliseconds)
printfn "Starting Protocol Gossip"
nodeArray.[leader] <! ActivateWorker
GossipActor<! AddNeighbors
| "push-sum" ->
supervisor <! TotalNodes(nodes)
supervisor <! Time(DateTime.Now.TimeOfDay.Milliseconds)
printfn "Starting Push Sum Protocol for Line"
nodeArray.[leader] <! StartPushSum(10.0 ** -10.0)
GossipActor<! AddPushSum
| _ ->
printfn "Invlaid topology"
| "full" ->
nodeArray <- Array.zeroCreate (nodes + 1)
for x in [0..nodes] do
let key: string = "demo" + string(x)
let actorRef = spawn system (key) Worker
nodeArray.[x] <- actorRef
nodeArray.[x] <! InitializeVariables x
indexDictionary.Add(nodeArray.[x], x)
dictionary.Add(nodeArray.[x], false)
for i = 1 to numNodesToIgnore do
let mutable rnd_shutdown = Random().Next(0, nodes)
shutdownNeighborsList <- (Array.append shutdownNeighborsList [|rnd_shutdown|])
[0..nodes] |> List.iter (fun i -> nodeArray.[i] <! GetNeighborsToIgnore(shutdownNeighborsList))
for i in [ 0 .. nodes ] do
let mutable neighbourArray = [||]
for j in [0..nodes] do
if i <> j then
neighbourArray <- (Array.append neighbourArray [|nodeArray.[j]|])
nodeArray.[i]<! Initailize(neighbourArray)
timer.Start()
//Choose a random worker to start the gossip
let leader = Random().Next(0, nodes)
match protocol with
| "gossip" ->
supervisor <! TotalNodes(nodes)
supervisor <! Time(DateTime.Now.TimeOfDay.Milliseconds)
printfn "------------- Begin Gossip -------------"
nodeArray.[leader] <! CallWorker
| "push-sum" ->
supervisor <! TotalNodes(nodes)
supervisor <! Time(DateTime.Now.TimeOfDay.Milliseconds)
printfn "------------- Begin Push Sum -------------"
nodeArray.[leader] <! StartPushSum(10.0 ** -10.0)
GossipActor<! AddPushSum
| _ ->
printfn "Invalid topology"
| "2D" ->
let gridSize = nodes |> float |> sqrt |> ceil |> int
nodeArray <- Array.zeroCreate (nodes+1)
for x in [0..nodes] do
let key: string = "demo" + string(x)
let actorRef = spawn system (key) Worker
nodeArray.[x] <- actorRef
nodeArray.[x] <! InitializeVariables x
indexDictionary.Add(nodeArray.[x], x)
dictionary.Add(nodeArray.[x], false)
for i = 1 to numNodesToIgnore do
let mutable rnd_shutdown = Random().Next(0, nodes)
printfn "shutting down %i" rnd_shutdown
shutdownNeighborsList <- (Array.append shutdownNeighborsList [|rnd_shutdown|])
[0..nodes] |> List.iter (fun i -> nodeArray.[i] <! GetNeighborsToIgnore(shutdownNeighborsList))
for i in [ 0 .. (gridSize-1)] do
for j in [ 0 .. (gridSize-1) ] do
let mutable neighbours: IActorRef [] = [||]
if j + 1 < gridSize then
neighbours <- (Array.append neighbours [| nodeArray.[i * gridSize + j + 1] |])
if j - 1 >= 0 then
neighbours <- (Array.append neighbours [| nodeArray.[i * gridSize + j - 1] |])
if i - 1 >= 0 then
neighbours <- Array.append neighbours [| nodeArray.[(i - 1 ) * gridSize + j ] |]
if i + 1 < gridSize then
neighbours <- (Array.append neighbours [| nodeArray.[(i + 1) * gridSize + j] |])
nodeArray.[i * gridSize + j] <! Initailize(neighbours)
//Choose a random worker to start the gossip
let leader = Random().Next(0, nodes)
timer.Start()
match protocol with
| "gossip" ->
supervisor <! TotalNodes(nodes)
supervisor <! Time(DateTime.Now.TimeOfDay.Milliseconds)
printfn "------------- Begin Gossip -------------"
nodeArray.[leader] <! ActivateWorker
GossipActor<! AddNeighbors
| "push-sum" ->
supervisor <! TotalNodes(nodes)
supervisor <! Time(DateTime.Now.TimeOfDay.Milliseconds)
printfn "------------- Begin Push Sum -------------"
nodeArray.[leader] <! StartPushSum(10.0 ** -10.0)
GossipActor<! AddNeighbors
GossipActor<! AddPushSum
| _ ->
printfn "Invalid topology"
| "Imp2D" ->
let gridSize = nodes |> float |> sqrt |> ceil |> int
nodeArray <- Array.zeroCreate (nodes+1)
for x in [0..nodes] do
let key: string = "demo" + string(x)
let actorRef = spawn system (key) Worker
nodeArray.[x] <- actorRef
nodeArray.[x] <! InitializeVariables x
dictionary.Add(nodeArray.[x], false)
indexDictionary.Add(nodeArray.[x], x)
for i = 1 to numNodesToIgnore do
let mutable rnd_shutdown = Random().Next(0, nodes)
printfn "shutting down %i" rnd_shutdown
shutdownNeighborsList <- (Array.append shutdownNeighborsList [|rnd_shutdown|])
[0..nodes] |> List.iter (fun i -> nodeArray.[i] <! GetNeighborsToIgnore(shutdownNeighborsList))
for i in [ 0 .. (gridSize-1)] do
for j in [ 0 .. (gridSize-1) ] do
let mutable neighbours: IActorRef [] = [||]
if j + 1 < gridSize then
neighbours <- (Array.append neighbours [| nodeArray.[i * gridSize + j + 1] |])
if j - 1 >= 0 then
neighbours <- (Array.append neighbours [| nodeArray.[i * gridSize + j - 1] |])
if i - 1 >= 0 then
neighbours <- Array.append neighbours [| nodeArray.[(i - 1 ) * gridSize + j ] |]
if i + 1 < gridSize then
neighbours <- (Array.append neighbours [| nodeArray.[(i + 1) * gridSize + j] |])
let rnd = Random().Next(0, nodes-1)
neighbours <- (Array.append neighbours [|nodeArray.[rnd] |])
nodeArray.[i * gridSize + j] <! Initailize(neighbours)
let leader = Random().Next(0, nodes)
timer.Start()
match protocol with
| "gossip" ->
supervisor <! TotalNodes(nodes)
supervisor <! Time(DateTime.Now.TimeOfDay.Milliseconds)
printfn "------------- Begin Gossip -------------"
nodeArray.[leader] <! ActivateWorker
GossipActor<! AddNeighbors
| "push-sum" ->
supervisor <! TotalNodes(nodes)
supervisor <! Time(DateTime.Now.TimeOfDay.Milliseconds)
printfn "------------- Begin Push Sum -------------"
nodeArray.[leader] <! StartPushSum(10.0 ** -10.0)
GossipActor<! AddNeighbors
GossipActor<! AddPushSum
| _ ->
printfn "Invalid topology"
| _ -> ()
Console.ReadLine() |> ignore
///////////////////////////Program////////////////////////////////////////