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];
52 mm.mmul = global new MMul(0, 0, 0);
55 // Currently it is a 4 X 4 matrix divided into 4 blocks
57 mm[0] = global new MatrixMultiply(matrix,0,0,1,1);
58 mm[1] = global new MatrixMultiply(matrix,0,2,1,3);
59 mm[2] = global new MatrixMultiply(matrix,2,0,3,1);
60 mm[3] = global new MatrixMultiply(matrix,2,2,3,3);
68 // print out the matrices to be multiplied
69 System.printString("MatrixMultiply: L=");
71 System.printString("\t");
72 System.printString("M=");
74 System.printString("\t");
75 System.printString("N=");
77 System.printString("\n");
79 //Print Matrices to be multiplied
80 System.printString("\n");
81 System.printString("a =");
82 for (i = 0; i < p; i++) {
83 for (j = 0; j < q; j++) {
87 System.printString(" " + val);
88 System.printString("\t");
91 System.printString("\n");
93 System.printString("b =");
94 for (i = 0; i < q; i++) {
95 for (j = 0; j < r; j++) {
99 System.printString(" " + val);
100 System.printString("\t");
103 System.printString("\n");
105 // start a thread to compute each c[l,n]
106 for (i = 0; i < NUM_THREADS; i++) {
113 // wait for them to finish
114 for (i = 0; i < NUM_THREADS; i++) {
121 // print out the result of the matrix multiply
122 System.printString("Starting\n");
123 System.printString("Matrix Product c =");
124 for (i = 0; i < p; i++) {
125 for (j = 0; j < r; j++) {
127 val = matrix.c[i][j];
129 System.printInt(val);
130 System.printString("\t");
133 System.printString("Finished\n");
144 public MMul(int L, int M, int N) {
148 a = global new int[L][M];
149 b = global new int[M][N];
150 c = global new int[L][N];
153 public void setValues() {
156 for(i = 0; i < L; i++) {
157 for(j = 0; j < M; j++) {
162 for(i = 0; i < M; i++) {
163 for(j = 0; j < N; j++) {
168 for(i = 0; i < L; i++) {
169 for(j = 0; j < N; j++) {
175 public int multiply(int x, int y) {
178 for(i = 0; i < M; i++) {
179 prod+= a[x][i] * b[i][y];