changes for the new build
[IRC.git] / Robust / src / Benchmarks / Prefetch / LUFact / dsm / LinpackRunner.java
1 /**************************************************************************
2  *                                                                         *
3  *         Java Grande Forum Benchmark Suite - Thread Version 1.0          *
4  *                                                                         *
5  *                            produced by                                  *
6  *                                                                         *
7  *                  Java Grande Benchmarking Project                       *
8  *                                                                         *
9  *                                at                                       *
10  *                                                                         *
11  *                Edinburgh Parallel Computing Centre                      *
12  *                                                                         *
13  *                email: epcc-javagrande@epcc.ed.ac.uk                     *
14  *                                                                         *
15  *                                                                         *
16  *      This version copyright (c) The University of Edinburgh, 2001.      *
17  *                         All rights reserved.                            *
18  *                                                                         *
19  **************************************************************************/
20
21 class LinpackRunner extends Thread {
22   int id,lda,n,info,ipvt[];
23   double a[][];
24   int nthreads;
25
26   public LinpackRunner(int id, double a[][], int lda, int n, int ipvt[], int nthreads) {
27     this.id = id;
28     this.a=a;
29     this.lda=lda;
30     this.n=n;
31     this.ipvt=ipvt;
32     this.nthreads = nthreads;
33   }
34
35   double abs (double d) {
36     if (d >= 0) return d;
37     else return -d;
38   }
39
40   public void run() {
41     Barrier barr;
42     barr = new Barrier("128.195.136.162");
43     double[] col_k, col_j;
44     double t;
45     int j,k,kp1,l,nm1;
46     int info;
47     int slice,ilow,iupper;
48     // gaussian elimination with partial pivoting
49     info = 0;
50     int nlocal;
51     int lid;
52     atomic {
53      //System.printString("Atomic #1\t");
54       nlocal=n;
55       lid=id;
56     }
57
58
59     nm1 = nlocal - 1;
60     if (nm1 >=  0) {
61       //System.printString("nm1 = " +nm1+ "\n");
62       for (k = 0; k < nm1; k++) {
63         atomic {
64           //System.printString("Atomic #2\t");
65           col_k = a[k];
66           kp1 = k + 1;
67           // find l = pivot index
68           l = idamax(nlocal-k,col_k,k,1) + k;
69           if(lid==0) {
70             ipvt[k] = l;
71           }
72         }
73         // synchronise threads
74         Barrier.enterBarrier(barr);
75
76         // zero pivot implies this column already triangularized
77         boolean b;
78         atomic {
79           //System.printString("Atomic #3\t");
80           b=col_k[l]!=0;
81         }
82         if (b) {
83           Barrier.enterBarrier(barr);
84           // interchange if necessary
85           if(lid == 0 ) {
86             if (l != k) {
87               atomic {
88                 t = col_k[l];
89                 col_k[l] = col_k[k];
90                 col_k[k] = t;
91               }
92             }
93           }
94           // synchronise threads
95           Barrier.enterBarrier(barr);
96           // compute multipliers
97           // t = -1.0/col_k[k];
98           if(lid == 0) {
99             atomic {
100               t = -1.0/col_k[k];
101               dscal(nlocal-(kp1),t,col_k,kp1,1);
102             }
103           }
104
105           // synchronise threads
106           Barrier.enterBarrier(barr);
107
108           // row elimination with column indexing
109           atomic {
110             //System.printString("Atomic #4\t");
111             slice = ((nlocal-kp1) + nthreads-1)/nthreads;
112             ilow = (lid*slice)+kp1;
113             iupper = ((lid+1)*slice)+kp1;
114             if (iupper > nlocal ) iupper=nlocal;
115             if (ilow > nlocal ) ilow=nlocal;
116             //System.printString("ilow= " + ilow + " iupper= " + iupper + "\n");
117             for (j = ilow; j < iupper; j++) {
118               col_j = a[j];
119               t = col_j[l];
120               if (l != k) {
121                 col_j[l] = col_j[k];
122                 col_j[k] = t;
123               }
124               daxpy(nlocal-(kp1),t,col_k,kp1,1,
125                   col_j,kp1,1);
126             }
127           }
128
129           // synchronise threads
130           Barrier.enterBarrier(barr);
131         } else {
132           info = k;
133         }
134         Barrier.enterBarrier(barr);
135       }
136     }
137
138     //atomic {
139       //System.printString("Atomic #5\t");
140       if(lid==0) {
141         atomic {
142         ipvt[nlocal-1] = nlocal-1;
143         }
144       }
145       atomic {
146       if (a[(nlocal-1)][(nlocal-1)] == 0) info = nlocal-1;
147       }
148     //}
149   }
150
151   /*
152      finds the index of element having max. absolute value.
153      jack dongarra, linpack, 3/11/78.
154      */
155   int idamax( int n, double dx[], int dx_off, int incx)
156   {
157     double dmax, dtemp;
158     int i, ix, itemp=0;
159
160     if (n < 1) {
161       itemp = -1;
162     } else if (n ==1) {
163       itemp = 0;
164     } else if (incx != 1) {
165       // code for increment not equal to 1
166       dmax = abs(dx[0 +dx_off]);
167       ix = 1 + incx;
168       for (i = 1; i < n; i++) {
169         dtemp = abs(dx[ix + dx_off]);
170         if (dtemp > dmax)  {
171           itemp = i;
172           dmax = dtemp;
173         }
174         ix += incx;
175       }
176     } else {
177       // code for increment equal to 1
178       itemp = 0;
179       dmax = abs(dx[0 +dx_off]);
180       for (i = 1; i < n; i++) {
181         dtemp = abs(dx[i + dx_off]);
182         if (dtemp > dmax) {
183           itemp = i;
184           dmax = dtemp;
185         }
186       }
187     }
188     return (itemp);
189   }
190
191   /*
192      scales a vector by a constant.
193      jack dongarra, linpack, 3/11/78.
194      */
195   void dscal( int n, double da, double dx[], int dx_off, int incx)
196   {
197     int i,nincx;
198     if (n > 0) {
199       if (incx != 1) {
200         // code for increment not equal to 1
201         nincx = n*incx;
202         for (i = 0; i < nincx; i += incx)
203           dx[i +dx_off] *= da;
204       } else {
205         // code for increment equal to 1
206         for (i = 0; i < n; i++)
207           dx[i +dx_off] *= da;
208       }
209     }
210   }
211
212   /*
213      constant times a vector plus a vector.
214      jack dongarra, linpack, 3/11/78.
215      */
216   void daxpy( int n, double da, double dx[], int dx_off, int incx,
217       double dy[], int dy_off, int incy)
218   {
219     int i,ix,iy;
220     if ((n > 0) && (da != 0)) {
221       if (incx != 1 || incy != 1) {
222         // code for unequal increments or equal increments not equal to 1
223         ix = 0;
224         iy = 0;
225         if (incx < 0) ix = (-n+1)*incx;
226         if (incy < 0) iy = (-n+1)*incy;
227         for (i = 0;i < n; i++) {
228           dy[iy +dy_off] += da*dx[ix +dx_off];
229           ix += incx;
230           iy += incy;
231         }
232         return;
233       } else {
234         // code for both increments equal to 1
235         for (i=0; i < n; i++)
236           dy[i +dy_off] += da*dx[i +dx_off];
237       }
238     }
239   }
240 }