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) {
16 int xx0, xx1, yy0, yy1;
25 for(i = xx0; i<= xx1; i++){
26 for (j = yy0; j <= yy1; j++) {
28 for(k = 0; k < mmul.M; k++) {
29 innerProduct += mmul.a[i][k] * mmul.b[k][j];
33 mmul.c[i][j] = innerProduct;
40 public static void main(String[] args) {
41 int mid = (128<<24)|(195<<16)|(175<<8)|70;
43 int i, j, p, q, r, val;
49 matrix = global new MMul(4, 4, 4);
54 mm = global new MatrixMultiply[NUM_THREADS];
57 // Currently it is a 4 X 4 matrix divided into 4 blocks
59 mm[0] = global new MatrixMultiply(matrix,0,0,1,1);
60 mm[1] = global new MatrixMultiply(matrix,0,2,1,3);
61 mm[2] = global new MatrixMultiply(matrix,2,0,3,1);
62 mm[3] = global new MatrixMultiply(matrix,2,2,3,3);
70 // print out the matrices to be multiplied
71 System.printString("\n");
72 System.printString("MatrixMultiply: L=");
74 System.printString("\t");
75 System.printString("M=");
77 System.printString("\t");
78 System.printString("N=");
80 System.printString("\n");
82 //Print Matrices to be multiplied
83 System.printString("a =\n");
84 for (i = 0; i < p; i++) {
85 for (j = 0; j < q; j++) {
89 System.printString(" " + val);
91 System.printString("\n");
93 System.printString("\n");
95 System.printString("b =\n");
96 for (i = 0; i < q; i++) {
97 for (j = 0; j < r; j++) {
101 System.printString(" " + val);
103 System.printString("\n");
105 System.printString("\n");
107 // start a thread to compute each c[l,n]
108 for (i = 0; i < NUM_THREADS; i++) {
115 // wait for them to finish
116 for (i = 0; i < NUM_THREADS; i++) {
123 // print out the result of the matrix multiply
124 System.printString("Starting\n");
125 System.printString("Matrix Product c =\n");
126 for (i = 0; i < p; i++) {
127 for (j = 0; j < r; j++) {
129 val = matrix.c[i][j];
131 System.printString(" " + val);
133 System.printString("\n");
135 System.printString("Finished\n");
147 public MMul(int L, int M, int N) {
151 a = global new int[L][M];
152 b = global new int[M][N];
153 c = global new int[L][N];
156 public void setValues() {
159 for(i = 0; i < L; i++) {
160 for(j = 0; j < M; j++) {
165 for(i = 0; i < M; i++) {
166 for(j = 0; j < N; j++) {
171 for(i = 0; i < L; i++) {
172 for(j = 0; j < N; j++) {