1 public class MatrixMultiply extends Thread{
3 public int x0, y0, x1, y1;
5 public MatrixMultiply(MMul mmul, int x0, int y0, int x1, int y1) {
25 for(i = xx0; i<= xx1; i++){
26 for (j = yy0; j <= yy1; j++) {
28 innerProduct = mmul.multiply(i,j);
31 mmul.c[i][j] = innerProduct;
37 public static void main(String[] args) {
38 int mid = (128<<24)|(195<<16)|(175<<8)|70;
40 int i, j, p, q, r, val;
46 matrix = global new MMul(4, 4, 4);
51 mm = global new MatrixMultiply[NUM_THREADS];
54 for(i = 0; i < NUM_THREADS; i++) {
56 mm[i].mmul = global new MMul(0, 0, 0);
60 // Currently it is a 4 X 4 matrix divided into 4 blocks
62 mm[0] = global new MatrixMultiply(matrix,0,0,1,1);
63 mm[1] = global new MatrixMultiply(matrix,0,2,1,3);
64 mm[2] = global new MatrixMultiply(matrix,2,0,3,1);
65 mm[3] = global new MatrixMultiply(matrix,2,2,3,3);
73 // print out the matrices to be multiplied
74 System.printString("MatrixMultiply: L=");
76 System.printString("\t");
77 System.printString("M=");
79 System.printString("\t");
80 System.printString("N=");
82 System.printString("\n");
84 //Print Matrices to be multiplied
85 System.printString("\n");
86 System.printString("a =");
87 for (i = 0; i < p; i++) {
88 for (j = 0; j < q; j++) {
92 System.printString(" " + val);
93 System.printString("\t");
96 System.printString("\n");
98 System.printString("b =");
99 for (i = 0; i < q; i++) {
100 for (j = 0; j < r; j++) {
102 val = matrix.b[i][j];
104 System.printString(" " + val);
105 System.printString("\t");
108 System.printString("\n");
110 // start a thread to compute each c[l,n]
111 for (i = 0; i < NUM_THREADS; i++) {
118 // wait for them to finish
119 for (i = 0; i < NUM_THREADS; i++) {
126 // print out the result of the matrix multiply
127 System.printString("Starting\n");
128 System.printString("Matrix Product c =");
129 for (i = 0; i < p; i++) {
130 for (j = 0; j < r; j++) {
132 val = matrix.c[i][j];
134 System.printInt(val);
135 System.printString("\t");
138 System.printString("Finished\n");
149 public MMul(int L, int M, int N) {
153 a = global new int[L][M];
154 b = global new int[M][N];
155 c = global new int[L][N];
158 public void setValues() {
161 for(i = 0; i < L; i++) {
162 for(j = 0; j < M; j++) {
167 for(i = 0; i < M; i++) {
168 for(j = 0; j < N; j++) {
173 for(i = 0; i < L; i++) {
174 for(j = 0; j < N; j++) {
180 public int multiply(int x, int y) {
183 for(i = 0; i < M; i++) {
184 prod+= a[x][i] * b[i][y];