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) {
15 //Use btranspose for cache performance
16 for(int i = x0; i<= x1; i++){
20 for (int j = y0; j <= y1; j++) {
21 double innerProduct=0;
22 double b[] = mmul.btranspose[j];
23 for(int k = 0; k < M; k++) {
24 innerProduct += a[k] *b[k];
32 public static void main(String[] args) {
36 NUM_THREADS=Integer.parseInt(args[0]);
38 SIZE=Integer.parseInt(args[1]);
41 int[] mid = new int[4];
42 mid[0] = (128<<24)|(195<<16)|(175<<8)|80;
43 mid[1] = (128<<24)|(195<<16)|(175<<8)|73;
44 mid[2] = (128<<24)|(195<<16)|(175<<8)|78;
45 mid[3] = (128<<24)|(195<<16)|(175<<8)|79;
52 matrix = global new MMul(SIZE, SIZE, SIZE);
55 mm = global new MatrixMultiply[NUM_THREADS];
56 int increment=SIZE/NUM_THREADS;
58 for(int i=0;i<NUM_THREADS;i++) {
59 if ((i+1)==NUM_THREADS)
60 mm[i]=global new MatrixMultiply(matrix,base, SIZE-1, 0, SIZE-1);
62 mm[i]=global new MatrixMultiply(matrix,base, base+increment, 0, SIZE-1);
70 // print out the matrices to be multiplied
71 System.printString("\n");
72 System.printString("MatrixMultiply: L=");
74 System.printString("\t");
75 System.printString("M=");
77 System.printString("\t");
78 System.printString("N=");
80 System.printString("\n");
82 // start a thread to compute each c[l,n]
83 for (int i = 0; i < NUM_THREADS; i++) {
90 // wait for them to finish
91 for (int i = 0; i < NUM_THREADS; i++) {
98 // print out the result of the matrix multiply
100 System.printString("Finished\n");
110 public double[][] btranspose;
112 public MMul(int L, int M, int N) {
116 a = global new double[L][M];
117 b = global new double[M][N];
118 c = global new double[L][N];
119 btranspose = global new double[N][M];
122 public void setValues() {
123 for(int i = 0; i < L; i++) {
125 for(int j = 0; j < M; j++) {
130 for(int i = 0; i < M; i++) {
132 for(int j = 0; j < N; j++) {
137 for(int i = 0; i < L; i++) {
139 for(int j = 0; j < N; j++) {
143 for(int i = 0; i < N; i++) {
144 double btransposei[] = btranspose[i];
145 for(int j = 0; j < M; j++) {
151 public void transpose() {
152 for(int row = 0; row < M; row++) {
153 double brow[] = b[row];
154 for(int col = 0; col < N; col++) {
155 btranspose[col][row] = brow[col];