-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathParallelProcessingLogBook.txt
593 lines (491 loc) · 17.5 KB
/
ParallelProcessingLogBook.txt
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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
Exercise 2.1:
def processList = [ new Producer ( outChannel: connect1.out() ),
new Multiplier ( inChannel: connect1.in(), outChannel: connect2.out(), factor: 4),
new Consumer ( inChannel: connect2.in() )
]
while (i > 0) {
// write i * factor to outChannel
// read in the next value of i
outChannel.write(i * factor)
i = inChannel.read()
}
while ( i > 0 ) {
//insert a modified println statement
println "The multiplied value is $i"
i = inChannel.read()
}
Outputs:
next: 4
next: The multiplied value is 16
2
next: The multiplied value is 8
0
Finished
Process finished with exit code 0
Exercise 2.2:
def threeList = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
[10, 11, 12],
[13, 14, 15],
[16, 17, 18],
[19, 20, 21],
[22, 23, 24],
[-1, -1,-1]]
for ( i in 0 ..< threeList.size)outChannel.write(threeList[i])
//write the terminating List as per exercise definition
OR
def threeList = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
[10, 11, 12],
[13, 14, 15],
[16, 17, 18],
[19, 20, 21],
[22, 23, 24]]
for ( i in 0 ..< threeList.size)outChannel.write(threeList[i])
outChannel.write([-1,-1,-1])
def inList = inChannel.read()
while (inList[0] != -1) {
// hint: output list elements as single integers
for ( i in inList){
outChannel.write(i)
}
inList = inChannel.read()
}
outChannel.write(-1)
def outList = []
def v = inChannel.read()
while (v != -1){
for ( i in 0 .. 7 ) {
// put v into outList and read next input
outList.add(v)
v = inChannel.read()
}
println " Eight Object is ${outList}"
outList = []
}
println "Finished"
Outputs:
Eight Object is [1, 2, 3, 4, 5, 6, 7, 8]
Eight Object is [9, 10, 11, 12, 13, 14, 15, 16]
Eight Object is [17, 18, 19, 20, 21, 22, 23, 24]
Finished
Process finished with exit code 0
Exercise 3.1:
Minus:
while (true) {
parRead2.run()
// output one value subtracted from the other
// be certain you know which way round you are doing the subtraction!!
outChannel.write(read0.value - read1.value)
}
Differentiate:
def differentiateList = [ new GPrefix ( prefixValue: 0,
inChannel: b.in(),
outChannel: c.out() ),
new GPCopy ( inChannel: inChannel,
outChannel0: a.out(),
outChannel1: b.out() ),
// insert a constructor for Minus
new Minus( inChannel0: a.in(), inChannel1: c.in(),
outChannel: outChannel)
]
Outputs:
Differentiated Numbers
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
...
Process finished with exit code -1
DifferentiateNeg:
def differentiateList = [ new GPrefix ( prefixValue: 0,
inChannel: b.in(),
outChannel: c.out() ),
new GPCopy ( inChannel: inChannel,
outChannel0: a.out(),
outChannel1: b.out() ),
//insert a constructor for Negator
new Negator( inChannel: c.in(),
outChannel: d.out()),
new GPlus ( inChannel0: a.in(),
inChannel1: d.in(),
outChannel: outChannel )
]
Negator:
while (true) {
//output the negative of the input value
def num = inChannel.read()
outChannel.write( num * -1 )
}
}
Outputs:
Differentiated Numbers
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
...
Process finished with exit code -1
Exercise 3.2:
TestGSCopy.groovy:
GSPairsA:
def testList = [ new GNumbers ( outChannel: N2I.out() ),
new GIntegrate ( inChannel: N2I.in(),
outChannel: I2P.out() ),
// you will need to modify thi twice
//first modification is to insert a constructor for GSPairsA
// then run the network using TestGSCopy
//second modification replace the constructor for GSPairsA with GSPairsB
// then run the network again using TestGSCopy
// you will then be able to compare the behaviour and also to
// explain why this happens!
new GSPairsA(inChannel: I2P.in(), outChannel: outChannel)
]
Output:
Squares DEADLOCK
GSPairsB:
def testList = [ new GNumbers ( outChannel: N2I.out() ),
new GIntegrate ( inChannel: N2I.in(),
outChannel: I2P.out() ),
// you will need to modify thi twice
//first modification is to insert a constructor for GSPairsA
// then run the network using TestGSCopy
//second modification replace the constructor for GSPairsA with GSPairsB
// then run the network again using TestGSCopy
// you will then be able to compare the behaviour and also to
// explain why this happens!
new GSPairsB(inChannel: I2P.in(), outChannel: outChannel)
]
Ouput:
Squares
1
4
9
16
25
36
49
64
81
100
121
DOES NOT DEADLOCK
Conclusion:
- The difference between GSPairsA and GSPairsB is the fact that the output channels have been changed around.
- GPCopy makes two copies of the first value passed by GPrefix, which will be 0.
- The first output will go to tail which will then drop that first value, being 0.
- GPCopy will then send the second 0 to GPlus which will wait for another value to so it can add the two values together.
- By this time the second value (which will be 1) is being passed into GCopy by GPrefix and be instantly passed to tail which is now ready to write the
next value it receives to GPlus.
- By this time the queue will be cleared between GPCopy and GPlus so it can write the next value thus not causing a deadlock.
- However, if the write order is changed between GPCopy to GPlus and Tail there will already be a value sitting in GPlus waiting for the value coming from tail
which will not be written until GPCopy has written to GPlus again and therefore causing a deadlock as the channel will already be full with a value.
Exercise 3.3:
Conclusion:
GPrint only has one input channel meaning it can only be written to one at a time or by creating multiple instances of the same process, being GPrint. However, if you use
GParPrint it contains an array/list of channels which you can define and then write to depending on what you are doing. This means instead of having multiple processes
you can just have one process with multiple input channels therefore it being less intensive on the system.
Exercise 4.1:
- Now when you input a new value in the reset value generator it starts a new counter from 0 but maintains the original counter which has started from 1000
- However if you enter in a second reset value it causes a deadlock. This means there is no value coming from GSuccessor to ResetPrefix. This is caused because we have
removed the output channel between GSuccessor and ResetPrefix. Once all the Processes are full with numbers the processes cannot output and thus the deadlock is caused.
Exercise 4.2:
ResetSuccessor:
while (true) {
// deal with inputs from resetChannel and inChannel
def index = alt.priSelect()
if (index == 0 ) { // resetChannel input
def resetValue = resetChannel.read()
resetChannel.read()
outChannel.write(resetValue)
}else {
outChannel.write(inChannel.read() + 1)
}
}
ResetNumbers:
new ResetSuccessor(inChannel: b.in(), outChannel: c.out(), resetChannel: resetChannel)
Conclusion:
- We still have still not fixed the issue caused in 4.2 because there is still no channel between GPrefix and ResetSuccessor and thus the same deadlock as in 4.1 is caused because
eventually when the we enter the second reset number all the processes are filled with numbers and cannot output to each other.
Exercise 5.1:
Output With Delay on QProducer at 1000 ms and QConsumer with a delay of 0 ms:
QProducer has started
QConsumer has started
QConsumer has read 1
QConsumer has read 2
QConsumer has read 3
...
QConsumer has read 50
Q finished
QConsumer has read null
Process finished with exit code 0
Output With Delay on QConsumer at 1000ms and QProducer with a delay of 0 ms:
QProducer has started
QConsumer has started
QConsumer has read 1
QConsumer has read 2
QConsumer has read 3
...
QConsumer has read 50
Q finished
QConsumer has read null
Process finished with exit code 0
Conclusion:
It does not matter what process you add the delay to because they both run in parallel and work in sync if one is delayed the other will also have a delay to it.
Exercise 5.2:
Output:
Original Scaled
0 0
1 2
2 4
3 6
4 8
Normal Timer: new scaling timer is 4
5 20
Suspended
6 6
New scaling: 5
7 35
8 40
9 45
10 50
11 55
Normal Timer: new scaling timer is 10
12 120
Suspended
13 13
New scaling: 11
14 154
15 165
16 176
17 187
18 198
Normal Timer: new scaling timer is 22
19 418
Suspended
20 20
New scaling: 23
21 483
22 506
23 529
24 552
25 575
Scale Code:
while (true) {
switch ( scaleAlt.priSelect(preCon) ) {
case SUSPEND :
// deal with suspend input
preCon[SUSPEND] = false
suspended = true
println "Suspended"
suspend.read()
factor.write(scaling)
preCon[INJECT] = true
break
case INJECT:
// deal with inject input
preCon[INJECT] = false
preCon[SUSPEND] = true
suspended = false
scaling = injector.read()
println "New scaling: $scaling"
timeout = timer.read() + DOUBLE_INTERVAL
timer.setAlarm( timeout )
break
case TIMER:
// deal with Timer input
timeout = timer.read() + DOUBLE_INTERVAL
timer.setAlarm( timeout )
scaling = scaling * 2
println "Normal Timer: new scaling timer is $scaling"
break
case INPUT:
// deal with Input channel
def inValue = inChannel.read()
def result = new ScaledData()
if( suspended == true ) {
result.original = inValue
result.scaled = inValue
}
else {
result.original = inValue
result.scaled = inValue * scaling
}
outChannel.write( result )
break
} //end-switch
} //end-while
} //end-run
Conclusion:
Using pre-conditions is a more elegant solution because we can more easily control the flow of the scaler. There is also some duplicate code that could be removed in the nested alt version. This is the SUSPENDED_IN and NORMAL_IN.
Using pre-cons we can also reduce the amount of alts needed to one and thus making it less confusing to control which alt is currently selected using the pre conditions. Using pre conditions also allows
us to prioritize processes in a more efficient way.
Exercise 6.1:
GenerateSetsOfEight:
class CreateSetsOfEight implements CSProcess{
def ChannelInput inChannel
def ChannelOutput outChannel
def outList = []
def allLists = []
void run(){
def v = inChannel.read()
while (v != -1){
outList = []
for ( i in 0 .. 7 ) {
// put v into outList and read next input
outList.add(v)
v = inChannel.read()
}
println " Eight Object is ${outList}"
allLists.add(outList)
}
println "Finished"
}
SetsOfEightTest is a new JCSP Class file:
class SetsOfEightTest extends GroovyTestCase {
void testMessage() {
One2OneChannel connect1 = Channel.createOne2One()
One2OneChannel connect2 = Channel.createOne2One()
One2OneChannel toTest = Channel.createOne2One()
def generateSetsOfThree = new GenerateSetsOfThree( outChannel: connect1.out() )
def listToStream = new ListToStream( inChannel: connect1.in(), outChannel: connect2.out() )
def setsOfEight = new CreateSetsOfEight( inChannel: connect2.in(), outChannel: toTest.out())
def processList = [generateSetsOfThree, listToStream, setsOfEight]
new PAR(processList).run()
def expected = [[1, 2 , 3, 4, 5, 6, 7, 8], [9, 10, 11, 12, 13, 14, 15, 16], [17, 18, 19, 20, 21, 22, 23, 24]]
def actual = setsOfEight.allLists
assertTrue(actual == expected)
}
}
Output:
Eight Object is [1, 2, 3, 4, 5, 6, 7, 8]
Eight Object is [9, 10, 11, 12, 13, 14, 15, 16]
Eight Object is [17, 18, 19, 20, 21, 22, 23, 24]
Finished
Process finished with exit code 0
Exercise 7.1:
Output:
THIS_RECEIVE Server 1 Recieveing A Value Datamap: [1:10, 2:20, 3:30, 4:40, 5:50, 6:60, 7:70, 8:80, 9:90, 10:100]
OTHER_REQUEST Data map contains 15 writing 150 to client Server: 2 has datamap [11:110, 12:120, 13:130, 14:140, 15:150, 16:160, 17:170, 18:180, 19:190, 20:200]
Client 0 is requesting 16
Client 1 is requesting 6
CLIENT REQUEST Sever: 1 clients request 16 Datamap: [1:10, 2:20, 3:30, 4:40, 5:50, 6:60, 7:70, 8:80, 9:90, 10:100]
CLIENT REQUEST Sever: 2 clients request 6 Datamap: [11:110, 12:120, 13:130, 14:140, 15:150, 16:160, 17:170, 18:180, 19:190, 20:200]
Conclusion:
By adding the print statements in both the client and server classes we can see which client is requesting the data and what they are requesting.
We can also tell which server has received the request and what values they currently have in their datamap.
From this we can see that if both servers do not have the value needed by the client and they need to transfer the data to each other,
therefore both servers will be acting as a client requesting at the same time causing the deadlock.
Exercise 8-1:
Client:
for ( i in 0 ..< iterations) {
def key = selectList[i]
requestChannel.write(key)
println "Client $clientNumber is requesting $key"
def v = receiveChannel.read()
println "Client $clientNumber is recieving $v"
if( key == v / 10 )
println "Server returned correct value. " + key + ":" + v
else
println "Server returned incorrect value. " + key + ":" + v
}
Output for correct value received:
Client 1 is recieving 140
CLIENT REQUEST Sever: 2 Data map contains 14 writing 140 to client Server 2 has datamap [11:110, 12:120, 13:130, 14:140, 15:150, 16:160, 17:170, 18:180, 19:190, 20:200]
Server returned correct value. 14:140
Output for incorrect value received:
Client 1 is requesting 13
Client 0 is recieving 50
CLIENT REQUEST Sever: 1 Data map contains 3 writing 50 to client Server 1 has datamap [1:10, 2:20, 3:50, 4:40, 5:50, 6:60, 7:70, 8:80, 9:90, 10:100]
CLIENT REQUEST Sever: 2 clients request 13 Datamap: [11:110, 12:120, 13:130, 14:140, 15:150, 16:160, 17:170, 18:180, 19:190, 20:200]
Server returned incorrect value. 3:50
Exercise 9-1:
RunSingleStream.groovy:
def eventTestList = [
new EventGenerator ( source: 1,
initialValue: 100,
iterations: 100,
outChannel: eg2h.out(),
minTime: 100,
maxTime:200 ),
new EventHandler ( inChannel: eg2h.in(),
outChannel: h2udd.out() ),
new checkMissed( inChannel: h2udd.in(),
checkedOut: checkedChannel.out()),
new UniformlyDistributedDelay ( inChannel:checkedChannel.in(),
outChannel: udd2prn.out(),
minTime: 1000,
maxTime: 2000 ),
new GPrint ( inChannel: udd2prn.in(),
heading : "Event Output",
delay: 0)
]
checkMissed:
class checkMissed implements CSProcess{
ChannelInput inChannel
ChannelOutput checkedOut
void run(){
def eventObj = inChannel.read()
def previousData = eventObj.data
checkedOut.write(eventObj.copy())
def expected = previousData + eventObj.missed + 1
while (true){
eventObj = inChannel.read().copy()
expected = previousData + eventObj.missed + 1
if(expected != eventObj.data){
println "Expected does n ot equal previous"
}
println "[ Previous value: " + previousData + "] Missed: [" + eventObj.missed + "] Next expected ["+
expected + "]"
previousData = expected
checkedOut.write(eventObj)
}
}
}
Output:
Event Output
Event Generator for source 1 has started
[ Previous value: 100] Missed: [0] Next expected [101]
[ Previous value: 101] Missed: [0] Next expected [102]
EventData -> [source: 1, data: 100, missed: 0]
EventData -> [source: 1, data: 101, missed: 0]
[ Previous value: 102] Missed: [8] Next expected [111]
EventData -> [source: 1, data: 102, missed: 0]
[ Previous value: 111] Missed: [8] Next expected [120]
EventData -> [source: 1, data: 111, missed: 8]
[ Previous value: 120] Missed: [9] Next expected [130]
EventData -> [source: 1, data: 120, missed: 8]
[ Previous value: 130] Missed: [12] Next expected [143]
Exercise 9-2:
Conclusion:
The UniformlyDistributedDelay will take the difference between minimum time for the delay and maxTime for the delay and adds a random integer to minTime make the time between each event random.
Changing the min and max amount of time for the UniformlyDistributedDelay causes a greater
Exercise 11-1: