-
Notifications
You must be signed in to change notification settings - Fork 0
/
Producer.java
176 lines (157 loc) · 3.99 KB
/
Producer.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
172
173
174
175
176
import java.util.LinkedList;
import java.util.concurrent.ThreadLocalRandom;
public class Producer implements Runnable
{
String ProducerName;
int[][] A;
int[][] B;
int[][] C;
SharedBuffer SB;
int ProducedItems;
int TotalSleep;
public Producer(String Name, SharedBuffer SB, int[][] A, int[][] B)
{
ProducerName = Name;
this.SB = SB;
this.A = A;
this.B = B;
ProducedItems = 0;
}
public int[][] getSubColumns(int lowB, int highB, int highA)
{
int[][] FlippedColumns = MatrixManager.getColumns(B);
int[][] subB = new int[highB-lowB][FlippedColumns[0].length];
// GETTING SUB COLUMNS
for(int Row = 0; Row <= subB.length-1; Row++)
{
for(int Column = 0; Column <= FlippedColumns[0].length-1; Column++)
{
subB[Row][Column] = FlippedColumns[lowB][Column];
}
lowB++;
}
return subB;
}
public int[][] getSubRows(int lowA, int highA)
{
int[][] subA = new int[highA-lowA][A[0].length];
for(int Row = 0; Row <= subA.length-1; Row++)
{
for(int Column = 0; Column <= subA[0].length-1; Column++)
{
subA[Row][Column] = A[lowA][Column];
}
lowA++;
}
return subA;
}
public void run()
{
System.out.println(ProducerName + " has started.");
int ARowCount = A.length;
int BColCount = B[0].length;
int SplitRows = ARowCount / Config.SplitSize;
int SplitCols = BColCount / Config.SplitSize;
// Increase split size to 1 if split size is greater than row count or column count which would return a decimal
if(SplitRows == 0)
{
SplitRows = 1;
}
if(SplitCols == 0)
{
SplitCols = 1;
}
int lowA = 0;
int lowB = 0;
int highA = SplitRows;
int highB = SplitCols;
LinkedList<WorkItem> WorkItems = new LinkedList<WorkItem>();
while(true)
{
for(int Row = 0; Row <= (BColCount/SplitCols)-1; Row++)
{
// if we're on the last loop and still rows/columns left, add them to last round.
if(Row == ((BColCount/SplitCols)-1))
{
if(B[0].length % Config.SplitSize == 1)
{
highB = B[0].length;
System.out.println("adding column");
}
}
if(highA == A.length-1)
{
if(A.length % Config.SplitSize == 1)
{
highA = A.length;
}
}
System.out.println("Producer puts row " + lowA + "-" + highA + " of matrix A and column " + lowB + "-" + highB + " of B to buffer");
int[][] subB = getSubColumns(lowB, highB, highA);
int[][] subA = getSubRows(lowA, highA);
WorkItem NewWorkItem = new WorkItem(subA, subB, lowA, highA, lowB, highB);
SB.put(NewWorkItem, ProducerName);
ProducedItems++;
WorkItems.add(NewWorkItem);
highB += SplitCols;
lowB += SplitCols;
int SleepTime = ThreadLocalRandom.current().nextInt(0, Config.MaxProducerSleepTime + 1);
TotalSleep += SleepTime;
try
{
Thread.sleep(ThreadLocalRandom.current().nextInt(0, Config.MaxProducerSleepTime + 1));
}
catch(InterruptedException ex)
{
Thread.currentThread().interrupt();
}
}
highB = SplitCols;
lowB = 0;
lowA += SplitRows;
highA += SplitRows;
if(highA > A.length)
{
break;
}
}
// Wait for all WorkItems to finish calculating
while(true)
{
boolean AllDone = true;
for(WorkItem W : WorkItems)
{
if(W.done == false)
{
AllDone = false;
}
}
if(AllDone == false)
{
continue;
}
else
{
break;
}
}
// Create final Matrix C
C = new int [A.length][B[0].length];
// Calculate the final matrix
for(WorkItem W : WorkItems)
{
for(int Row = W.lowA; Row <= W.highA-1; Row++)
{
for(int Column = W.lowB; Column <= W.highB-1; Column++)
{
C[Row][Column] = W.subC[Row-W.lowA][Column-W.lowB];
}
}
}
System.out.println("Producer successfully assembly all the results from consumer threads");
}
synchronized public int[][] getFinalMatrix()
{
return C;
}
}