-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.fs
197 lines (150 loc) · 5.85 KB
/
Main.fs
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
// Learn more about F# at http://fsharp.org
module Main
open Gossip
open PushSum
open System
open Akka.Actor
open Akka.FSharp
open System.Collections.Generic
let system = ActorSystem.Create("FSharp")
let neighbors = new Dictionary<int, int []>( )
let getNextPerfectSq(numNodes) =
let sqroot:int = int(Math.Ceiling(sqrt(float(numNodes)))**2.0)
sqroot
let full_network numNodes =
for i in 0..numNodes-1 do
let mutable adjList : int list = []
for j in 0..numNodes-1 do
if i <> j then
adjList <- j :: adjList
let adjArray = adjList |> List.toArray
neighbors.Add(i, adjArray)
let twoD_network numNodes sqroot =
for i in 0..numNodes-1 do
let mutable adjList : int list = []
// For the right neighbors
if (i+1) % sqroot <> 0 then
adjList <- i+1 :: adjList
// For the lower neighbors
if i > sqroot-1 then
adjList <- i-sqroot :: adjList
// For the upper neighbors
if numNodes-i > sqroot then
adjList <- i+sqroot :: adjList
// For the left neighbors
if i % sqroot <> 0 then
adjList <- i-1 :: adjList
let adjArray = adjList |> List.toArray
neighbors.Add(i, adjArray)
let line_network numNodes =
for i in 0..numNodes-1 do
let mutable adjList : int list = []
if i = 0 then
adjList <- i+1 :: adjList
elif i = numNodes-1 then
adjList <- i-1 :: adjList
else
adjList <- i+1 :: adjList
adjList <- i-1 :: adjList
let adjArray = adjList |> List.toArray
neighbors.Add(i, adjArray)
let impTwoD_network numNodes sqroot =
for i in 0..numNodes-1 do
let mutable adjList : int list = []
let rand = new Random()
let mutable index = i
while index = i do
index <- rand.Next(0, numNodes-1)
// For the random neighbor
adjList <- index :: adjList
// For the right neighbors
if (i+1) % sqroot <> 0 then
adjList <- i+1 :: adjList
// For the lower neighbors
if i > sqroot-1 then
adjList <- i-sqroot :: adjList
// For the upper neighbors
if numNodes-i > sqroot then
adjList <- i+sqroot :: adjList
// For the left neighbors
if i % sqroot <> 0 then
adjList <- i-1 :: adjList
let adjArray = adjList |> List.toArray
neighbors.Add(i, adjArray)
let createRefArr (algorithm: string) (numNodes: int) (mailbox : Actor<'a>)=
if algorithm = "gossip" then
[|
for i in 0 .. numNodes-1 ->
(spawn mailbox ("worker"+i.ToString()) (gossipActor (neighbors.Item(i))))
|]
else
[|
for i in 0 .. numNodes-1 ->
(spawn mailbox ("worker"+i.ToString()) (pushSumActor (float i) (neighbors.Item(i))))
|]
let startProtocol (algorithm: string) (refArr: IActorRef []) (numNodes: int) =
if algorithm = "gossip" then
Console.WriteLine("Using Gossip Algorithm...")
startGossip refArr "rumor"
elif algorithm = "push-sum" then
Console.WriteLine("Using Push Sum Algorithm...")
startPushSum refArr
else
Console.WriteLine("Enter either gossip or push-sum")
let supervisorActor (algorithm: string) (numNodes: int) (mailbox : Actor<'a>)=
let refArr = createRefArr algorithm numNodes mailbox
startProtocol algorithm refArr numNodes
let mutable numHeard = 0
let mutable returnAddress = mailbox.Context.Parent
let rec loop () =
actor {
let! msg = mailbox.Receive()
let sender = mailbox.Sender()
if returnAddress = mailbox.Context.Parent then
returnAddress <- sender
return! loop()
else
numHeard <- numHeard + 1
if numHeard < numNodes then
// if algorithm = "gossip" then
// Console.WriteLine("{0} heard the rumor, '{1}'!. ({2})", sender, msg, numHeard)
// elif algorithm = "push-sum" then
// Console.WriteLine("{0} converged to the sum, {1}!. ({2})", sender, msg, numHeard)
return! loop()
else
if algorithm = "gossip" then
Console.WriteLine("All {0} nodes heard the rumor!", numHeard)
elif algorithm = "push-sum" then
Console.WriteLine("All {0} nodes converged to the sum! (~{1})", numHeard, msg)
returnAddress <! "done!"
}
loop()
let buildTopology topology numNodes =
if topology = "full" then
full_network(numNodes)
elif topology = "2D" then
let sqroot:int = int(sqrt(float(numNodes)))
twoD_network numNodes sqroot
elif topology = "line" then
line_network(numNodes)
else
let sqroot:int = int(sqrt(float(numNodes)))
impTwoD_network numNodes sqroot
[<EntryPoint>]
let main argv =
if argv.Length <> 3 then
Console.WriteLine("Invalid Input Provided")
Console.WriteLine("Ex.: project2 100 2D gossip")
else
let mutable numNodes:int = int argv.[0]
let topology = argv.[1]
let algorithm = argv.[2]
if topology = "2D" || topology = "imp2D" then
numNodes <- getNextPerfectSq(numNodes)
buildTopology topology numNodes
let stopWatch = System.Diagnostics.Stopwatch.StartNew()
let supervisor = spawn system "supervisorActor" (supervisorActor algorithm numNodes)
let res = supervisor <? "done?" |> Async.RunSynchronously
stopWatch.Stop()
Console.WriteLine("Time to complete: {0} ms", stopWatch.Elapsed.TotalMilliseconds)
0 // return an integer exit code