-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathExample5.java
171 lines (125 loc) · 5.17 KB
/
Example5.java
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
package applications.threading;
import datastructs.maths.Vector;
import utils.Pair;
import utils.PairBuilder;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
/**
* Category: Threading
* ID: Example5
* Description: Illustrates CyclicBarrier class
* Taken From:
*
* Details:
* A barrier is a synchronization mechanism which can be used to ensure that multiple threads are waiting on each other at a certain point.
* Once all threads reach the synchronization point, the barrier is released and all threads can continue their work. Such a mechanism is
* useful often in iterative algorithms where a tolerance has to be computed that depends on the result or operations of all the participating threads.
*
* The barrier is called cyclic because it can be re-used after the waiting threads are released.
*/
public class Example5 {
public static final int NUM_THREADS = 4;
/**
* The CyclicBarrier used to synchronize the threads
*/
CyclicBarrier barrier;
List<LocalDotProduct> tasks;
class AggregatorThread implements Runnable {
public AggregatorThread(List<LocalDotProduct> tasks){
this.tasks = tasks;
}
public double getSum(){
return this.sum;
}
public boolean isDone(){
return this.isDone;
}
@Override
public void run() {
String thisThreadName = Thread.currentThread().getName();
System.out.println(thisThreadName + ": Computing sum from " + Example5.NUM_THREADS + " worker threads");
for (LocalDotProduct threadResult : this.tasks) {
this.sum += threadResult.getLocalSum();
}
this.isDone = true;
System.out.println();
System.out.println(thisThreadName + ": Final result = " + this.sum);
}
List<LocalDotProduct> tasks;
double sum = 0.0;
boolean isDone = false;
}
class LocalDotProduct implements Runnable{
/**
* Constructor
*/
public LocalDotProduct(Pair<Integer, Integer> range, Vector v1, Vector v2, CyclicBarrier barrier){
this.range = range;
this.v1 = v1;
this.v2 = v2;
this.localSum = 0.0;
this.barrier = barrier;
}
@Override
public void run() {
for(int i= this.range.first; i< this.range.second; ++i){
this.localSum += this.v1.get(i)*this.v2.get(i);
}
// now wait until all threads are summed
try {
System.out.println(Thread.currentThread().getName() + " waiting for others to reach barrier.");
this.barrier.await();
}
catch(InterruptedException e){
System.out.println("InterruptedException: "+ e.getMessage());
}
catch(BrokenBarrierException e){
System.out.println("BorkenBarrierException: "+ e.getMessage());
}
}
public double getLocalSum(){
return localSum;
}
private Pair<Integer, Integer> range;
private Vector v1;
private Vector v2;
private double localSum;
CyclicBarrier barrier;
}
public double compute(Vector v1, Vector v2){
int localWorkSize = v1.size()/Example5.NUM_THREADS;
System.out.println("Local work size: " + localWorkSize);
// calculate the intervals
Pair<Integer, Integer> range1 = PairBuilder.makePair(0, localWorkSize);
Pair<Integer, Integer> range2 = PairBuilder.makePair(localWorkSize, 2*localWorkSize);
Pair<Integer, Integer> range3 = PairBuilder.makePair(2*localWorkSize, 3*localWorkSize);
Pair<Integer, Integer> range4 = PairBuilder.makePair(3*localWorkSize, 4*localWorkSize);
this.tasks = new ArrayList<>(Example5.NUM_THREADS);
// create the barrier and the threads
AggregatorThread aggregatorThread = new AggregatorThread(this.tasks);
this.barrier = new CyclicBarrier(Example5.NUM_THREADS, aggregatorThread);
this.tasks.add(new LocalDotProduct(range1, v1, v2, this.barrier));
this.tasks.add(new LocalDotProduct(range2, v1, v2, this.barrier));
this.tasks.add(new LocalDotProduct(range3, v1, v2, this.barrier));
this.tasks.add(new LocalDotProduct(range4, v1, v2, this.barrier));
System.out.println("Spawning " + Example5.NUM_THREADS + " worker threads to compute ");
for(int t=0; t<Example5.NUM_THREADS; ++t) {
Thread thread = new Thread(this.tasks.get(t));
thread.setName("Thread "+t);
thread.start();
}
while(!aggregatorThread.isDone()){
System.out.println("Not finished yet...");
}
return aggregatorThread.getSum();
}
public static void main(String[] args){
Vector v1 = new Vector(200, 1.0);
Vector v2 = new Vector(200, 1.0);
Example5 example5 = new Example5();
double dotProduct = example5.compute(v1, v2);
System.out.println("Dot product computed: "+dotProduct);
}
}