1 public class MatrixMultiply extends Thread{
3 public int x0, y0, x1, y1;
5 public MatrixMultiply(MMul mmul, int x0, int x1, int y0, int y1) {
17 double lb[][]=mmul.btranspose;
20 //Use btranspose for cache performance
21 for(int i = x0; i< x1; i++){
24 for (int j = y0; j < y1; j++) {
25 double innerProduct=0;
27 for(int k = 0; k < M; k++) {
28 innerProduct += a[k] *b[k];
36 public static void main(String[] args) {
40 NUM_THREADS=Integer.parseInt(args[0]);
42 SIZE=Integer.parseInt(args[1]);
45 int[] mid = new int[4];
46 mid[0] = (128<<24)|(195<<16)|(175<<8)|69;
47 mid[1] = (128<<24)|(195<<16)|(175<<8)|70;
48 mid[2] = (128<<24)|(195<<16)|(175<<8)|71;
49 mid[3] = (128<<24)|(195<<16)|(175<<8)|79;
56 matrix = global new MMul(SIZE, SIZE, SIZE);
59 mm = global new MatrixMultiply[NUM_THREADS];
60 int increment=SIZE/NUM_THREADS;
62 for(int i=0;i<NUM_THREADS;i++) {
63 if ((i+1)==NUM_THREADS)
64 mm[i]=global new MatrixMultiply(matrix,base, SIZE, 0, SIZE);
66 mm[i]=global new MatrixMultiply(matrix,base, base+increment, 0, SIZE);
74 // print out the matrices to be multiplied
75 System.printString("\n");
76 System.printString("MatrixMultiply: L=");
78 System.printString("\t");
79 System.printString("M=");
81 System.printString("\t");
82 System.printString("N=");
84 System.printString("\n");
86 // start a thread to compute each c[l,n]
87 for (int i = 0; i < NUM_THREADS; i++) {
94 // wait for them to finish
95 for (int i = 0; i < NUM_THREADS; i++) {
102 // print out the result of the matrix multiply
104 System.printString("Finished\n");
114 public double[][] btranspose;
116 public MMul(int L, int M, int N) {
120 a = global new double[L][M];
121 b = global new double[M][N];
122 c = global new double[L][N];
123 btranspose = global new double[N][M];
126 public void setValues() {
127 for(int i = 0; i < L; i++) {
129 for(int j = 0; j < M; j++) {
134 for(int i = 0; i < M; i++) {
136 for(int j = 0; j < N; j++) {
141 for(int i = 0; i < L; i++) {
143 for(int j = 0; j < N; j++) {
147 for(int i = 0; i < N; i++) {
148 double btransposei[] = btranspose[i];
149 for(int j = 0; j < M; j++) {
155 public void transpose() {
156 for(int row = 0; row < M; row++) {
157 double brow[] = b[row];
158 for(int col = 0; col < N; col++) {
159 btranspose[col][row] = brow[col];