-
Notifications
You must be signed in to change notification settings - Fork 0
/
project1-bonus.scala
259 lines (197 loc) · 7.21 KB
/
project1-bonus.scala
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
import akka.actor.{Actor, Props, ActorSystem}
import com.typesafe.config.ConfigFactory
import akka.remote._
import akka.routing.RoundRobinRouter
import scala.concurrent.duration._
import java.util._
object NewApp3 extends App
{
val sampleActorSystem = ActorSystem("sampleActorSystem", ConfigFactory.parseString("""
akka {
actor {
provider = "akka.remote.RemoteActorRefProvider"
deployment {
/masterActor {
remote = "akka://[email protected]:2551"
}
/dispatcher {
remote = "akka://[email protected]:2552"
}
/dispatcher2 {
remote = "akka://[email protected]:2553"
}
/dispatcher3 {
remote = "akka://[email protected]:2554"
}
/dispatcher4 {
remote = "akka://[email protected]:2555"
}
/dispatcher5 {
remote = "akka://[email protected]:2556"
}
/dispatcher6 {
remote = "akka://[email protected]:2557"
}
/dispatcher7 {
remote = "akka://[email protected]:2558"
}
/dispatcher8 {
remote = "akka://[email protected]:2559"
}
/dispatcher9 {
remote = "akka://[email protected]:2560"
}
/dispatcher10 {
remote = "akka://[email protected]:2561"
}
}
}
remote {
transport = "akka.remote.netty.NettyRemoteTransport"
netty {
hostname = "localhost"
//port = 2556
}
}
}
"""))
var limit:Int = _
var length:Int = _
//var parts:Int = _
var counter: Int = _
start(1,100000000,nrOfMessages=100000000,20)
sealed trait Message //to ensure message are passed in a controlled fashion
case object Calculate extends Message
case class Work(startNum:Int,endNum:Int) extends Message
case class Result(answer: Int) extends Message
//case class PrintPerfectSquares(counter: Int, time: Duration) extends Message
class SampleActor2 extends Actor
{
def receive = {
case x:String =>
println(x)
}
}
def start(nrOfWorkers: Int,limit:Int,nrOfMessages: Int,length: Int )
{
val sampleActorSystem2 = ActorSystem("sampleActorSystem2")
val actor = sampleActorSystem2.actorOf(Props(new Master(nrOfWorkers,nrOfMessages,limit,length)),"masterActor")
actor ! Calculate
}
class Worker extends Actor
{
var sum : Int = 0
def doCalculation(startNum:Int, endNum:Int):Int =
{
sum = 0
//main logic
for(i <- startNum until endNum+1)
{
sum =sum + i*i
}
var number = sum.toDouble
//println((Math.sqrt(number))%1)
if(Math.sqrt(number) % 1== 0.0)
{
println("StarNum: "+startNum+" EndNum "+endNum+" sum "+number)
return 1;
}
else
{
//println("hola its false")
return 0;
}
}
def receive =
{
case Work(startNum,endNum) =>
sender ! Result(doCalculation(startNum,endNum)) //check the sequence and send back the result
//println(doCalculation(startNum,endNum))
}
}
class Master(nrOfWorkers: Int,nrOfMessages: Int,limit:Int,length: Int) extends Actor
{
var nrOfResults: Int = _
val beginTime: Long = System.currentTimeMillis
var time: Long = 0
val dispatcher = context.actorOf(Props[Worker].withRouter(RoundRobinRouter(nrOfWorkers)),name="dispatcher")
val dispatcher2 = context.actorOf(Props[Worker].withRouter(RoundRobinRouter(nrOfWorkers)),name="dispatcher2")
val dispatcher3 = context.actorOf(Props[Worker].withRouter(RoundRobinRouter(nrOfWorkers)),name="dispatcher3")
val dispatcher4 = context.actorOf(Props[Worker].withRouter(RoundRobinRouter(nrOfWorkers)),name="dispatcher4")
val dispatcher5 = context.actorOf(Props[Worker].withRouter(RoundRobinRouter(nrOfWorkers)),name="dispatcher5")
val dispatcher6 = context.actorOf(Props[Worker].withRouter(RoundRobinRouter(nrOfWorkers)),name="dispatcher6")
val dispatcher7 = context.actorOf(Props[Worker].withRouter(RoundRobinRouter(nrOfWorkers)),name="dispatcher7")
val dispatcher8 = context.actorOf(Props[Worker].withRouter(RoundRobinRouter(nrOfWorkers)),name="dispatcher8")
val dispatcher9 = context.actorOf(Props[Worker].withRouter(RoundRobinRouter(nrOfWorkers)),name="dispatcher9")
val dispatcher10 = context.actorOf(Props[Worker].withRouter(RoundRobinRouter(nrOfWorkers)),name="dispatcher10")
def receive =
{
case Calculate =>
for (i <- 1 until (limit+1)/10)
{
//println("dispathing"+"\t"+i+"\t"+(i+length-1))
dispatcher ! Work(i,i+length-1)
}
for (i <-(limit+1)/10 until (2*(limit+1))/10)
{
//println("dispathing"+"\t"+i+"\t"+(i+length-1))
dispatcher2 ! Work(i,i+length-1)
}
for (i <-(2*(limit+1))/10 until (3*(limit+1))/10)
{
//println("dispathing"+"\t"+i+"\t"+(i+length-1))
dispatcher3 ! Work(i,i+length-1)
}
for (i <-(3*(limit+1))/10 until (4*(limit+1))/10)
{
//println("dispathing"+"\t"+i+"\t"+(i+length-1))
dispatcher4 ! Work(i,i+length-1)
}
for (i <-(4*(limit+1))/10 until (5*(limit+1))/10)
{
//println("dispathing"+"\t"+i+"\t"+(i+length-1))
dispatcher5 ! Work(i,i+length-1)
}
for (i <-(5*(limit+1))/10 until (6*(limit+1))/10)
{
//println("dispathing"+"\t"+i+"\t"+(i+length-1))
dispatcher6 ! Work(i,i+length-1)
}
for (i <-(6*(limit+1))/10 until (7*(limit+1))/10)
{
//println("dispathing"+"\t"+i+"\t"+(i+length-1))
dispatcher7 ! Work(i,i+length-1)
}
for (i <-(7*(limit+1))/10 until (8*(limit+1))/10)
{
//println("dispathing"+"\t"+i+"\t"+(i+length-1))
dispatcher8 ! Work(i,i+length-1)
}
for (i <-(8*(limit+1))/10 until (9*(limit+1))/10)
{
//println("dispathing"+"\t"+i+"\t"+(i+length-1))
dispatcher9 ! Work(i,i+length-1)
}
for (i <-(9*(limit+1))/10 until limit+1)
{
//println("dispathing"+"\t"+i+"\t"+(i+length-1))
dispatcher10 ! Work(i,i+length-1)
}
case Result(value) =>
//println(value)
if(value == 1)
{
counter = counter+1
//println(counter)
}
nrOfResults += 1
if(nrOfResults == nrOfMessages)
{
//printer ! PrintPerfectSquares(counter,time =(System.currentTimeMillis - beginTime).millis)
//context.stop(self)
println(counter)
println(((System.currentTimeMillis - beginTime).millis).toString())
}
}
}
}