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)|79;
56 matrix = global new MMul(600, 600, 600);
62 mm = global new MatrixMultiply[NUM_THREADS];
66 mm[0] = global new MatrixMultiply(matrix,0,0,599,599);
75 // print out the matrices to be multiplied
76 System.printString("\n");
77 System.printString("MatrixMultiply: L=");
79 System.printString("\t");
80 System.printString("M=");
82 System.printString("\t");
83 System.printString("N=");
85 System.printString("\n");
87 // start a thread to compute each c[l,n]
88 for (int i = 0; i < NUM_THREADS; i++) {
95 // wait for them to finish
96 for (int i = 0; i < NUM_THREADS; i++) {
103 // print out the result of the matrix multiply
104 System.printString("Starting\n");
105 System.printString("Matrix Product c =\n");
108 for (int i = 0; i < p; i++) {
109 double c[]=matrix.c[i];
110 for (int j = 0; j < r; j++) {
115 System.printString("Finished\n");
125 public double[][] btranspose;
127 public MMul(int L, int M, int N) {
131 a = global new double[L][M];
132 b = global new double[M][N];
133 c = global new double[L][N];
134 btranspose = global new double[N][M];
137 public void setValues() {
138 for(int i = 0; i < L; i++) {
140 for(int j = 0; j < M; j++) {
145 for(int i = 0; i < M; i++) {
147 for(int j = 0; j < N; j++) {
152 for(int i = 0; i < L; i++) {
154 for(int j = 0; j < N; j++) {
158 for(int i = 0; i < N; i++) {
159 double btransposei[] = btranspose[i];
160 for(int j = 0; j < M; j++) {
166 public void transpose() {
167 for(int row = 0; row < M; row++) {
168 double brow[] = b[row];
169 for(int col = 0; col < N; col++) {
170 btranspose[col][row] = brow[col];