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) {
14 double localresults[][];
18 localresults=new double[1+x1-x0][1+y1-y0];
20 //Use b transpose for cache performance
21 for(int i = x0; i<= x1; i++){
24 for (int j = y0; j <= y1; j++) {
25 double innerProduct=0;
26 double b[] = mmul.btranspose[j];
27 for(int k = 0; k < M; k++) {
28 innerProduct += a[k] *b[k];
30 localresults[i-x0][j-y0]=innerProduct;
37 for(int i=x0;i<=x1;i++) {
39 for(int j=y0;j<=y1;j++) {
40 c[j]=localresults[i-x0][j-y0];
46 public static void main(String[] args) {
48 int[] mid = new int[NUM_THREADS];
49 mid[0] = (128<<24)|(195<<16)|(175<<8)|80;
50 mid[1] = (128<<24)|(195<<16)|(175<<8)|73;
51 mid[2] = (128<<24)|(195<<16)|(175<<8)|78;
52 mid[3] = (128<<24)|(195<<16)|(175<<8)|79;
53 //int mid1 = (128<<24)|(195<<16)|(175<<8)|69;
54 //int mid2 = (128<<24)|(195<<16)|(175<<8)|73;
61 matrix = global new MMul(600, 600, 600);
67 mm = global new MatrixMultiply[NUM_THREADS];
71 mm[0] = global new MatrixMultiply(matrix,0,0,300,300);
72 mm[1] = global new MatrixMultiply(matrix,0,301,300,599);
73 mm[2] = global new MatrixMultiply(matrix,301,0,599,300);
74 mm[3] = global new MatrixMultiply(matrix,301,301,599,599);
83 // print out the matrices to be multiplied
84 System.printString("\n");
85 System.printString("MatrixMultiply: L=");
87 System.printString("\t");
88 System.printString("M=");
90 System.printString("\t");
91 System.printString("N=");
93 System.printString("\n");
95 // start a thread to compute each c[l,n]
96 for (int i = 0; i < NUM_THREADS; i++) {
103 // wait for them to finish
104 for (int i = 0; i < NUM_THREADS; i++) {
111 // print out the result of the matrix multiply
112 System.printString("Starting\n");
113 System.printString("Matrix Product c =\n");
116 for (int i = 0; i < p; i++) {
117 double c[]=matrix.c[i];
118 for (int j = 0; j < r; j++) {
123 System.printString("Finished\n");
133 public double[][] btranspose;
135 public MMul(int L, int M, int N) {
139 a = global new double[L][M];
140 b = global new double[M][N];
141 c = global new double[L][N];
142 btranspose = global new double[N][M];
145 public void setValues() {
146 for(int i = 0; i < L; i++) {
148 for(int j = 0; j < M; j++) {
153 for(int i = 0; i < M; i++) {
155 for(int j = 0; j < N; j++) {
160 for(int i = 0; i < L; i++) {
162 for(int j = 0; j < N; j++) {
166 for(int i = 0; i < N; i++) {
167 double btransposei[] = btranspose[i];
168 for(int j = 0; j < M; j++) {
174 public void transpose() {
175 for(int row = 0; row < M; row++) {
176 double brow[] = b[row];
177 for(int col = 0; col < N; col++) {
178 btranspose[col][row] = brow[col];