1 public class MatrixMultiply extends Thread{
5 public MatrixMultiply(MMul mmul, int x0, int y0, int x1, int y1) {
18 for(i = x0; i<= x1; i++){
19 for (j = y0; j <= y1; j++) {
21 innerProduct = mmul.multiply(i,j);
24 mmul.c[i][j] = innerProduct;
30 public static void main(String[] args) {
31 int mid = (128<<24)|(195<<16)|(175<<8)|70;
33 int i, j, p, q, r, val;
39 matrix = global new MMul(4, 4, 4);
44 mm = global new MatrixMultiply[NUM_THREADS];
45 mm.mmul = global new MMul(0,0,0);
48 // Currently it is a 4 X 4 matrix divided into 4 blocks
50 mm[0] = global new MatrixMultiply(matrix,0,0,1,1);
51 mm[1] = global new MatrixMultiply(matrix,0,2,1,3);
52 mm[2] = global new MatrixMultiply(matrix,2,0,3,1);
53 mm[3] = global new MatrixMultiply(matrix,2,2,3,3);
61 // print out the matrices to be multiplied
62 System.printString("MatrixMultiply: L=");
64 System.printString("\t");
65 System.printString("M=");
67 System.printString("\t");
68 System.printString("N=");
70 System.printString("\n");
72 // start a thread to compute each c[l,n]
73 for (i = 0; i < NUM_THREADS; i++) {
80 // wait for them to finish
81 for (i = 0; i < NUM_THREADS; i++) {
88 // print out the result of the matrix multiply
89 System.printString("Starting\n");
90 System.printString("Matrix Product c =");
91 for (i = 0; i < p; i++) {
92 for (j = 0; j < r; j++) {
97 System.printString("\t");
100 System.printString("Finished\n");
111 public MMul(int L, int M, int N) {
120 public void setValues() {
123 for(i = 0; i < L; i++) {
124 for(j = 0; j < M; j++) {
129 for(i = 0; i < M; i++) {
130 for(j = 0; j < N; j++) {
135 for(i = 0; i < L; i++) {
136 for(j = 0; j < N; j++) {
142 public int multiply(int x, int y) {
145 for(i = 0; i < M; i++) {
146 prod+= a[x][i] * b[i][y];
151 //Print Matrices to be multiplied
152 System.printString("\n");
153 System.printString("a =");
154 for (int l = 0; l < k; l++) {
155 for (int m = 0; m < q; m++) {
159 System.printString(" " + tt);
160 System.printString("\t");
163 System.printString("\n");
165 //System.printString("\n");
166 System.printString("b =");
167 for (int m = 0; m < q; m++) {
168 for (int n = 0; n < r; n++) {
172 System.printString(" " + tt);
173 System.printString("\t");
176 System.printString("\n");