1 public class MatrixMultiply extends Thread{
3 public int x0, y0, x1, y1;
4 public MatrixMultiply(MMul mmul, int x0, int x1, int y0, int y1) {
14 short[] offsets = new short[4];
15 // Prefetch mmul.btranspose[][] matrix
16 //Get all of B first...we need them first
17 offsets[0] = getoffset{MMul, btranspose};
18 offsets[1] = (short) 0;
19 offsets[2] = (short) y0;
20 offsets[3] = (short) (y1 - y0 -1);
21 System.rangePrefetch(mmul, offsets);
24 offsets[0] = getoffset{MMul, a};
25 offsets[1] = (short) 0;
26 offsets[2] = (short) x0;
27 offsets[3] = (short) 15;
28 System.rangePrefetch(mmul, offsets);
31 offsets[0] = getoffset{MMul, c};
32 offsets[1] = (short) 0;
33 System.rangePrefetch(mmul, offsets);
34 short[] offsets2=new short[2];
37 double lb[][]=mmul.btranspose;
40 //Use btranspose for cache performance
41 for(int i = x0; i< x1; i++,l++){
45 offsets2[0] = (short) (x0+l);
49 offsets2[1]=(short) x;
50 System.rangePrefetch(la, offsets2);
51 System.rangePrefetch(lc, offsets2);
54 offsets2[1] = (short) 15;
55 System.rangePrefetch(la, offsets2);
56 System.rangePrefetch(lc, offsets2);
59 for (int j = y0; j < y1; j++) {
60 double innerProduct=0;
62 for(int k = 0; k < M; k++) {
63 innerProduct += a[k] *b[k];
71 public static void main(String[] args) {
75 NUM_THREADS=Integer.parseInt(args[0]);
77 SIZE=Integer.parseInt(args[1]);
80 int[] mid = new int[8];
81 mid[0] = (128<<24)|(195<<16)|(136<<8)|162; //dc-1.calit2
82 mid[1] = (128<<24)|(195<<16)|(136<<8)|163; //dc-2.calit2
83 mid[2] = (128<<24)|(195<<16)|(136<<8)|164; //dc-3.calit2
84 mid[3] = (128<<24)|(195<<16)|(136<<8)|165; //dc-4.calit2
85 mid[4] = (128<<24)|(195<<16)|(136<<8)|166; //dc-5.calit2
86 mid[5] = (128<<24)|(195<<16)|(136<<8)|167; //dc-6.calit2
87 mid[6] = (128<<24)|(195<<16)|(136<<8)|168; //dc-7.calit2
88 mid[7] = (128<<24)|(195<<16)|(136<<8)|169; //dc-8.calit2
96 matrix = global new MMul(SIZE, SIZE, SIZE);
99 mm = global new MatrixMultiply[NUM_THREADS];
100 int increment=SIZE/NUM_THREADS;
102 for(int i=0;i<NUM_THREADS;i++) {
103 if ((i+1)==NUM_THREADS)
104 mm[i]=global new MatrixMultiply(matrix,base, SIZE, 0, SIZE);
106 mm[i]=global new MatrixMultiply(matrix,base, base+increment, 0, SIZE);
114 // print out the matrices to be multiplied
115 System.printString("\n");
116 System.printString("MatrixMultiply: L=");
118 System.printString("\t");
119 System.printString("M=");
121 System.printString("\t");
122 System.printString("N=");
124 System.printString("\n");
126 // start a thread to compute each c[l,n]
127 for (int i = 0; i < NUM_THREADS; i++) {
135 // wait for them to finish
136 for (int i = 0; i < NUM_THREADS; i++) {
143 // print out the result of the matrix multiply
145 System.printString("Finished\n");
155 public double[][] btranspose;
157 public MMul(int L, int M, int N) {
161 a = global new double[L][M];
162 b = global new double[M][N];
163 c = global new double[L][N];
164 btranspose = global new double[N][M];
167 public void setValues() {
168 for(int i = 0; i < L; i++) {
170 for(int j = 0; j < M; j++) {
175 for(int i = 0; i < M; i++) {
177 for(int j = 0; j < N; j++) {
182 for(int i = 0; i < L; i++) {
184 for(int j = 0; j < N; j++) {
188 for(int i = 0; i < N; i++) {
189 double btransposei[] = btranspose[i];
190 for(int j = 0; j < M; j++) {
196 public void transpose() {
197 for(int row = 0; row < M; row++) {
198 double brow[] = b[row];
199 for(int col = 0; col < N; col++) {
200 btranspose[col][row] = brow[col];