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[4];
55 mid[0] = (128<<24)|(195<<16)|(175<<8)|79;
56 mid[1] = (128<<24)|(195<<16)|(175<<8)|73;
57 mid[2] = (128<<24)|(195<<16)|(175<<8)|78;
58 mid[3] = (128<<24)|(195<<16)|(175<<8)|69;
65 matrix = global new MMul(NUM_MATRIX, SIZE, SIZE, SIZE);
68 mm = global new MatrixMultiply[NUM_THREADS];
69 int increment=SIZE/NUM_THREADS;
71 for(int i=0;i<NUM_THREADS;i++) {
72 if ((i+1)==NUM_THREADS)
73 mm[i]=global new MatrixMultiply(matrix,base, SIZE, 0, SIZE);
75 mm[i]=global new MatrixMultiply(matrix,base, base+increment, 0, SIZE);
83 // start a thread to compute each c[l,n]
84 for (int i = 0; i < NUM_THREADS; i++) {
91 // wait for them to finish
92 for (int i = 0; i < NUM_THREADS; i++) {
99 // print out the result of the matrix multiply
100 System.printString("Finished\n");
106 public int L, M, N, P;
107 public double[][][] a;
108 public double[][][] b;
109 public double[][][] c;
110 public double[][][] btranspose;
112 public MMul(int P, int L, int M, int N) {
117 a = global new double[P][L][M];
118 b = global new double[P][M][N];
119 c = global new double[P][L][N];
120 btranspose = global new double[P][N][M];
123 public void setValues() {
124 for(int q = 0; q < P; q++) {
125 for(int i = 0; i < L; i++) {
126 double ai[] = a[q][i];
127 for(int j = 0; j < M; j++) {
132 for(int i = 0; i < M; i++) {
133 double bi[] = b[q][i];
134 for(int j = 0; j < N; j++) {
141 public void transpose() {
142 for(int q=0;q<P;q++) {
144 double bt[][]=btranspose[q];
145 for(int row = 0; row < M; row++) {
146 double brow[] = br[row];
147 for(int col = 0; col < N; col++) {
148 bt[col][row] = brow[col];