new versionw
[IRC.git] / Robust / src / Benchmarks / Prefetch / MatrixMultiply / dsm / MatrixMultiplyD.java
1 public class MatrixMultiply extends Thread{
2     MMul mmul;
3     public int x0, y0, x1, y1;
4     public int tid, numthreads;
5
6     public MatrixMultiply(MMul mmul, int x0, int x1, int y0, int y1, int tid, int numthreads) {
7         this.mmul = mmul;
8         this.x0 = x0;
9         this.y0 = y0;
10         this.x1 = x1;
11         this.y1 = y1;
12         this.tid=tid;
13         this.numthreads=numthreads;
14     }
15     
16     public void run() {
17         Barrier barr=new Barrier("128.195.136.162");
18         atomic {
19             mmul.setValues(tid, numthreads);
20         }
21         
22         Barrier.enterBarrier(barr);
23
24       atomic {
25         double la[][][]=mmul.a;
26         double lc[][][]=mmul.c;
27         double lb[][][]=mmul.btranspose;
28         int M=mmul.M;
29         int P=mmul.P;
30         //Use btranspose for cache performance
31         for(int q=0;q<P;q++) {
32             double ra[][]=la[q];
33             double rb[][]=lb[q];
34             double rc[][]=lc[q];
35             for(int i = x0; i< x1; i++){
36                 double a[]=ra[i];
37                 double c[]=rc[i];
38                 for (int j = y0; j < y1; j++) {
39                     double innerProduct=0;
40                     double b[] = rb[j];
41                     for(int k = 0; k < M; k++) {
42                         innerProduct += a[k] * b[k];
43                     }
44                     c[j]=innerProduct;
45                 }
46             }
47         }
48       }
49     }
50     
51     public static void main(String[] args) {
52         int NUM_THREADS = 4;
53         int SIZE=150;
54         int NUM_MATRIX = 1;
55         if (args.length>0) {
56             NUM_THREADS=Integer.parseInt(args[0]);
57             if (args.length>1) {
58                 SIZE=Integer.parseInt(args[1]);
59                 if (args.length>2)
60                     NUM_MATRIX=Integer.parseInt(args[2]);
61             }
62         }
63         
64         int[] mid = new int[8];
65         mid[0] = (128<<24)|(195<<16)|(136<<8)|162; 
66         mid[1] = (128<<24)|(195<<16)|(136<<8)|163;
67         mid[2] = (128<<24)|(195<<16)|(136<<8)|164;
68         mid[3] = (128<<24)|(195<<16)|(136<<8)|165;
69         mid[4] = (128<<24)|(195<<16)|(136<<8)|166;
70         mid[5] = (128<<24)|(195<<16)|(136<<8)|167;
71         mid[6] = (128<<24)|(195<<16)|(136<<8)|168;
72         mid[7] = (128<<24)|(195<<16)|(136<<8)|169;
73
74         int p, q, r;
75         MatrixMultiply[] mm;
76         MatrixMultiply tmp;
77         MMul matrix;
78         BarrierServer mybarr;
79
80         atomic {
81             mybarr = global new BarrierServer(NUM_THREADS);
82         }
83         mybarr.start(mid[0]);
84
85
86         atomic {
87             matrix = global new MMul(NUM_MATRIX, SIZE, SIZE, SIZE);
88             mm = global new MatrixMultiply[NUM_THREADS];
89             int increment=SIZE/NUM_THREADS;
90             int base=0;
91             for(int i=0;i<NUM_THREADS;i++) {
92                 if ((i+1)==NUM_THREADS)
93                     mm[i]=global new MatrixMultiply(matrix,base, SIZE, 0, SIZE, i, NUM_THREADS);
94                 else
95                     mm[i]=global new MatrixMultiply(matrix,base, base+increment, 0, SIZE, i, NUM_THREADS);
96                 base+=increment;
97             }
98             p = matrix.L;
99             q = matrix.M;
100             r = matrix.N;
101         }
102         boolean waitfordone=true;
103         while(waitfordone) {
104             atomic { //Master aborts come from here
105                 if (mybarr.done)
106                     waitfordone=false;
107             }
108         }
109         
110         // start a thread to compute each c[l,n]
111         for (int i = 0; i < NUM_THREADS; i++) {
112             atomic {
113                 tmp = mm[i];
114             }
115             tmp.start(mid[i]);
116         }
117
118       // wait for them to finish
119       for (int i = 0; i < NUM_THREADS; i++) {
120         atomic {
121           tmp = mm[i];
122         }
123         tmp.join();
124       }
125     
126         // print out the result of the matrix multiply
127         System.printString("Finished\n");
128     }
129 }
130
131 public class MMul{
132     public int L, M, N, P;
133     public double[][][] a;
134     public double[][][] c;
135     public double[][][] btranspose;
136     
137     public MMul(int P, int L, int M, int N) {
138         this.L = L;
139         this.M = M;
140         this.N = N;
141         this.P = P;
142         //      a = global new double[P][L][M];  
143         //      c = global new double[P][L][N]; 
144         //      btranspose = global new double[P][N][M];
145         a = global new double[P][L][];
146         c = global new double[P][L][];
147         btranspose = global new double[P][N][];
148     }
149
150     public void setValues(int tid, int numthreads) {
151         if(tid==0) {
152             for(int q = 0; q < P; q++) {
153                 for(int i = 0; i < L; i++) {
154                     double ai[] = global new double[M];
155                     for(int j = 0; j < M; j++) {
156                         ai[j] = j+1;
157                     }
158                     a[q][i]=ai;
159                 }
160             }
161             for(int q = 0; q < P; q++) {
162                 for(int i = 0; i < L; i++) {
163                     c[q][i]=global new double[N];
164                 }
165             }
166         }
167         if(tid>=0||numthreads==1) {
168             int delta=numthreads>1?numthreads-1:1;
169             int start=numthreads>1?tid-1:0;
170
171             for(int q = start; q < P; q+=delta) {
172                 for(int i = 0; i < N; i++) {
173                     double bi[] = global new double[M];
174                     for(int j = 0; j < M; j++) {
175                         bi[j] = j+1;
176                     }
177                     btranspose[q][i]=bi;
178                 }
179             }
180         }
181     }
182 }