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 //Use b transpose for cache performance
20 for(int i = x0; i<= x1; i++){
23 for (int j = y0; j <= y1; j++) {
25 int b[] = mmul.btranspose[j];
26 for(int k = 0; k < M; k++) {
27 innerProduct += a[k] *b[k];
29 localresults[i-x0][j-y0]=innerProduct;
34 for(int i=x0;i<=x1;i++) {
36 for(int j=y0;j<=y1;j++) {
37 c[j]=localresults[i-x0][j-y0];
42 public static void main(String[] args) {
49 matrix = new MMul(400, 400, 400);
53 mm = new MatrixMultiply[NUM_THREADS];
54 mm[0] = new MatrixMultiply(matrix,0,0,399,399);
60 // print out the matrices to be multiplied
61 System.out.print("MatrixMultiply: L=");
63 System.out.print("\t");
64 System.out.print("M=");
66 System.out.print("\t");
67 System.out.print("N=");
69 System.out.print("\n");
71 // start a thread to compute each c[l,n]
72 for (int i = 0; i < NUM_THREADS; i++) {
77 // wait for them to finish
78 for (int i = 0; i < NUM_THREADS; i++) {
81 } catch (InterruptedException e) {
82 System.out.println("Join Error");
87 // print out the result of the matrix multiply
88 System.out.println("Starting\n");
89 System.out.println("Matrix Product c =\n");
91 for (int i = 0; i < p; i++) {
93 for (int j = 0; j < r; j++) {
97 System.out.println("Finished\n");
107 public int[][] btranspose;
109 public MMul(int L, int M, int N) {
116 btranspose = new int[N][M];
119 public void setValues() {
120 for(int i = 0; i < L; i++) {
122 for(int j = 0; j < M; j++) {
127 for(int i = 0; i < M; i++) {
129 for(int j = 0; j < N; j++) {
134 for(int i = 0; i < L; i++) {
136 for(int j = 0; j < N; j++) {
140 for(int i = 0; i < N; i++) {
141 int btransposei[] = btranspose[i];
142 for(int j = 0; j < M; j++) {
148 public void transpose() {
149 for(int row = 0; row < M; row++) {
151 for(int col = 0; col < N; col++) {
152 btranspose[col][row] = brow[col];