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) {
17 localresults=new int[1+x1-x0][1+y1-y0];
19 for(int i = x0; i<= x1; i++){
20 for (int j = y0; j <= y1; j++) {
22 for(int k = 0; k < mmul.M; k++) {
23 innerProduct += mmul.a[i][k] * mmul.b[k][j];
25 localresults[i-x0][j-y0]=innerProduct;
31 for(int i=x0;i<=x1;i++) {
32 for(int j=y0;j<=y1;j++) {
33 mmul.c[i][j]=localresults[i-x0][j-y0];
39 public static void main(String[] args) {
40 int mid = (128<<24)|(195<<16)|(175<<8)|70;
42 int i, j, p, q, r, val;
48 matrix = global new MMul(4, 4, 4);
53 mm = global new MatrixMultiply[NUM_THREADS];
56 // Currently it is a 4 X 4 matrix divided into 4 blocks
58 mm[0] = global new MatrixMultiply(matrix,0,0,1,1);
59 mm[1] = global new MatrixMultiply(matrix,0,2,1,3);
60 mm[2] = global new MatrixMultiply(matrix,2,0,3,1);
61 mm[3] = global new MatrixMultiply(matrix,2,2,3,3);
69 // print out the matrices to be multiplied
70 System.printString("\n");
71 System.printString("MatrixMultiply: L=");
73 System.printString("\t");
74 System.printString("M=");
76 System.printString("\t");
77 System.printString("N=");
79 System.printString("\n");
81 //Print Matrices to be multiplied
82 System.printString("a =\n");
83 for (i = 0; i < p; i++) {
84 for (j = 0; j < q; j++) {
88 System.printString(" " + val);
90 System.printString("\n");
92 System.printString("\n");
94 System.printString("b =\n");
95 for (i = 0; i < q; i++) {
96 for (j = 0; j < r; j++) {
100 System.printString(" " + val);
102 System.printString("\n");
104 System.printString("\n");
106 // start a thread to compute each c[l,n]
107 for (i = 0; i < NUM_THREADS; i++) {
114 // wait for them to finish
115 for (i = 0; i < NUM_THREADS; i++) {
122 // print out the result of the matrix multiply
123 System.printString("Starting\n");
124 System.printString("Matrix Product c =\n");
125 for (i = 0; i < p; i++) {
126 for (j = 0; j < r; j++) {
128 val = matrix.c[i][j];
130 System.printString(" " + val);
132 System.printString("\n");
134 System.printString("Finished\n");
145 public MMul(int L, int M, int N) {
149 a = global new int[L][M];
150 b = global new int[M][N];
151 c = global new int[L][N];
154 public void setValues() {
157 for(i = 0; i < L; i++) {
158 for(j = 0; j < M; j++) {
163 for(i = 0; i < M; i++) {
164 for(j = 0; j < N; j++) {
169 for(i = 0; i < L; i++) {
170 for(j = 0; j < N; j++) {