1 public class MatrixMultiply extends Thread{
3 public int x0, y0, x1, y1;
5 public MatrixMultiply(MMul mmul, int x0, int x1, int y0, int y1) {
15 double la[][][]=mmul.a;
16 double lc[][][]=mmul.c;
17 double lb[][][]=mmul.btranspose;
20 //Use btranspose for cache performance
21 for(int q=0;q<P;q++) {
25 for(int i = x0; i< x1; i++){
28 for (int j = y0; j < y1; j++) {
29 double innerProduct=0;
31 for(int k = 0; k < M; k++) {
32 innerProduct += a[k] *b[k];
41 public static void main(String[] args) {
46 NUM_THREADS=Integer.parseInt(args[0]);
48 SIZE=Integer.parseInt(args[1]);
50 NUM_MATRIX=Integer.parseInt(args[2]);
54 int[] mid = new int[8];
55 mid[0] = (128<<24)|(195<<16)|(175<<8)|84;//dw-10
56 mid[1] = (128<<24)|(195<<16)|(175<<8)|85;//dw-11
57 mid[2] = (128<<24)|(195<<16)|(175<<8)|86;//dw-12
58 mid[3] = (128<<24)|(195<<16)|(175<<8)|87;//dw-13
59 mid[4] = (128<<24)|(195<<16)|(175<<8)|88;//dw-14
60 mid[5] = (128<<24)|(195<<16)|(175<<8)|89;//dw-15
61 mid[6] = (128<<24)|(195<<16)|(175<<8)|90;//dw-16
62 mid[7] = (128<<24)|(195<<16)|(175<<8)|91;//dw-17
70 matrix = global new MMul(NUM_MATRIX, SIZE, SIZE, SIZE);
73 mm = global new MatrixMultiply[NUM_THREADS];
74 int increment=SIZE/NUM_THREADS;
76 for(int i=0;i<NUM_THREADS;i++) {
77 if ((i+1)==NUM_THREADS)
78 mm[i]=global new MatrixMultiply(matrix,base, SIZE, 0, SIZE);
80 mm[i]=global new MatrixMultiply(matrix,base, base+increment, 0, SIZE);
88 // start a thread to compute each c[l,n]
89 for (int i = 0; i < NUM_THREADS; i++) {
96 // wait for them to finish
97 for (int i = 0; i < NUM_THREADS; i++) {
104 // print out the result of the matrix multiply
105 System.printString("Finished\n");
111 public int L, M, N, P;
112 public double[][][] a;
113 public double[][][] b;
114 public double[][][] c;
115 public double[][][] btranspose;
117 public MMul(int P, int L, int M, int N) {
122 a = global new double[P][L][M];
123 b = global new double[P][M][N];
124 c = global new double[P][L][N];
125 btranspose = global new double[P][N][M];
128 public void setValues() {
129 for(int q = 0; q < P; q++) {
130 for(int i = 0; i < L; i++) {
131 double ai[] = a[q][i];
132 for(int j = 0; j < M; j++) {
137 for(int i = 0; i < M; i++) {
138 double bi[] = b[q][i];
139 for(int j = 0; j < N; j++) {
146 public void transpose() {
147 for(int q=0;q<P;q++) {
149 double bt[][]=btranspose[q];
150 for(int row = 0; row < M; row++) {
151 double brow[] = br[row];
152 for(int col = 0; col < N; col++) {
153 bt[col][row] = brow[col];