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:
|
public static Matrix multParallel(final Matrix matA, final Matrix matB) {
Matrix[][] matASplit = split(matA);
Matrix[][] matBSplit = split(matB);
final int matASplitRows = matASplit.length;
final int matBSplitCols = matBSplit[0].length;
final int cpuCount = Runtime.getRuntime().availableProcessors();
Matrix[][] matCSplit = new Matrix[matASplitRows][matBSplitCols];
TimeUnit tUnit = TimeUnit.MINUTES;
BlockingQueue<Runnable> workQueue = new ArrayBlockingQueue<Runnable>(matASplitRows * matBSplitCols + 1);
ThreadPoolExecutor threadpool = new ThreadPoolExecutor(cpuCount, cpuCount + 2, 10, tUnit, workQueue);
for(int i = 0; i < matASplit.length; i++) {
final int iF = i;
for(int l = 0; l < matBSplit[0].length; l++) {
final int lF = l;
threadpool.execute(new Runnable() {
int j, k;
@Override
public void run() {
for(j = 0, k = 0; j < 1; j++, k++) {
matCSplit[iF][lF].data[j][k] += matASplit[iF][lF].data[j][1] * matBSplit[iF][lF].data[1][k] + matASplit[iF][lF].data[j][2] * matBSplit[iF][lF].data[2][k];
}
}
});
}
}
try {
threadpool.shutdown();
if(!threadpool.awaitTermination(500, TimeUnit.MILLISECONDS)) {
threadpool.shutdownNow();
threadpool.awaitTermination(500, TimeUnit.MILLISECONDS);
}
} catch(InterruptedException e) {
e.printStackTrace();
}
return Matrix.merge(matCSplit);
} |