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 // Currently it is a 4 X 4 matrix divided into 4 blocks
56 mm[0] = global new MatrixMultiply(matrix,0,0,1,1);
57 mm[1] = global new MatrixMultiply(matrix,0,2,1,3);
58 mm[2] = global new MatrixMultiply(matrix,2,0,3,1);
59 mm[3] = global new MatrixMultiply(matrix,2,2,3,3);
67 // print out the matrices to be multiplied
68 System.printString("\n");
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("a =\n");
81 for (i = 0; i < p; i++) {
82 for (j = 0; j < q; j++) {
86 System.printString(" " + val);
88 System.printString("\n");
90 System.printString("\n");
92 System.printString("b =\n");
93 for (i = 0; i < q; i++) {
94 for (j = 0; j < r; j++) {
98 System.printString(" " + val);
100 System.printString("\n");
102 System.printString("\n");
104 // start a thread to compute each c[l,n]
105 for (i = 0; i < NUM_THREADS; i++) {
112 // wait for them to finish
113 for (i = 0; i < NUM_THREADS; i++) {
120 // print out the result of the matrix multiply
121 System.printString("Starting\n");
122 System.printString("Matrix Product c =\n");
123 for (i = 0; i < p; i++) {
124 for (j = 0; j < r; j++) {
126 val = matrix.c[i][j];
128 System.printString(" " + val);
130 System.printString("\n");
132 System.printString("Finished\n");
143 public MMul(int L, int M, int N) {
147 a = global new int[L][M];
148 b = global new int[M][N];
149 c = global new int[L][N];
152 public void setValues() {
155 for(i = 0; i < L; i++) {
156 for(j = 0; j < M; j++) {
161 for(i = 0; i < M; i++) {
162 for(j = 0; j < N; j++) {
167 for(i = 0; i < L; i++) {
168 for(j = 0; j < N; j++) {
174 public int multiply(int x, int y) {
177 for(i = 0; i < M; i++) {
178 prod+= a[x][i] * b[i][y];