371b2904ee99191ac549620f39b99f1d0a28403c
[IRC.git] / Robust / src / Benchmarks / Prefetch / Moldyn / dsm / JGFMolDynBench.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 public class JGFMolDynBench {
21   public int ITERS;
22   public double LENGTH;
23   public double m;
24   public double mu;
25   public double kb;
26   public double TSIM;
27   public double deltat;
28
29   public int PARTSIZE;
30
31   public DoubleWrapper[] epot;
32   public DoubleWrapper[] vir;
33   public DoubleWrapper[] ek;
34
35   int size,mm;
36   int[] datasizes;
37
38   public int interactions;
39   public IntWrapper[] interacts;
40
41   public int nthreads;
42   public JGFInstrumentor instr;
43
44   public JGFMolDynBench(int nthreads) {
45     this.nthreads=nthreads;
46   }
47
48   public void JGFsetsize(int size){
49     this.size = size;
50   }
51
52   public void JGFinitialise(){
53     interactions = 0;
54     datasizes = global new int[2];
55     datasizes[0] = 8;
56     datasizes[1] = 13;
57
58     mm = datasizes[size];
59     PARTSIZE = mm*mm*mm*4;
60     ITERS = 100;
61     LENGTH = 50e-10;
62     m = 4.0026;
63     mu = 1.66056e-27;
64     kb = 1.38066e-23;
65     TSIM = 50;
66     deltat = 5e-16;
67   }
68
69   public static void JGFapplication(JGFMolDynBench mold) { 
70     // Create new arrays 
71     BarrierServer mybarr;
72     int[] mid = new int[4];
73     mid[0] = (128<<24)|(195<<16)|(175<<8)|79;
74     mid[1] = (128<<24)|(195<<16)|(175<<8)|80;
75     mid[2] = (128<<24)|(195<<16)|(175<<8)|78;
76     mid[3] = (128<<24)|(195<<16)|(175<<8)|73; 
77
78     double sh_force [][];
79     double sh_force2 [][][];
80     int partsize, numthreads;
81     atomic {
82       partsize = mold.PARTSIZE;
83       numthreads = mold.nthreads;
84       mybarr = global new BarrierServer(numthreads);
85     }
86     mybarr.start(mid[0]);
87     
88     atomic {
89       sh_force = global new double[3][partsize];
90       sh_force2 = global new double[3][numthreads][partsize];
91       mold.epot = global new DoubleWrapper[numthreads];
92       mold.vir  = global new DoubleWrapper[numthreads];
93       mold.ek   = global new DoubleWrapper[numthreads];
94       mold.interacts = global new IntWrapper[numthreads];
95       for(int i=0;i<numthreads;i++) {
96         mold.epot[i]=global new DoubleWrapper();
97         mold.vir[i]=global new DoubleWrapper();
98         mold.ek[i]=global new DoubleWrapper();
99         mold.interacts[i]=global new IntWrapper();
100       }
101     }
102     
103     // spawn threads 
104     MDWrap[] thobjects = new MDWrap[numthreads];
105     
106     atomic {
107       for(int i=0;i<numthreads;i++) {
108         thobjects[i] = new MDWrap(global new mdRunner(i,mold.mm,sh_force,sh_force2,mold.nthreads,mold));
109       }
110     }
111     
112     boolean waitfordone=true;
113     while(waitfordone) {
114       atomic{
115         if (mybarr.done)
116           waitfordone=false;
117       }
118     }
119     
120     for(int i=0;i<numthreads;i++) {
121       thobjects[i].md.start(mid[i]);
122     }
123     
124     for(int i=0;i<numthreads;i++) {
125       thobjects[i].md.join();
126     }
127   }
128
129   public void JGFvalidate(){
130     double[] refval = new double[2];
131     refval[0] = 1731.4306625334357;
132     refval[1] = 7397.392307839352;
133     double dev = Math.fabs(ek[0].d - refval[size]);
134     if (dev > 1.0e-10 ){
135       //System.printString("Validation failed\n");
136       //System.printString("Kinetic Energy = " + (long)ek[0] + "  " + (long)dev + "  " + size + "\n");
137     }
138   }
139 }
140
141 class mdRunner extends Thread {
142
143   double count;
144   int id,i,j,k,lg,mm;
145   double l,rcoff,rcoffs,side,sideh,hsq,hsq2,vel,velt;
146   double a,r,sum,tscale,sc,ekin,ts,sp;
147   double den;
148   double tref;
149   double h;
150   double vaver,vaverh,rand;
151   double etot,temp,pres,rp;
152   double u1, u2, s, xx, yy, zz;
153   double xvelocity, yvelocity, zvelocity;
154
155   double [][] sh_force;
156   double [][][] sh_force2;
157
158   int ijk,npartm,iseed,tint;
159   int irep;
160   int istop;
161   int iprint;
162
163   JGFMolDynBench mymd;
164   int nthreads;
165
166   public mdRunner(int id, int mm, double [][] sh_force, double [][][] sh_force2, 
167                   int nthreads, JGFMolDynBench mymd) {
168     this.id=id;
169     this.mm=mm;
170     this.sh_force=sh_force;
171     this.sh_force2=sh_force2;
172     this.nthreads = nthreads;
173     this.mymd = mymd;
174     count = 0.0;
175     den = 0.83134;
176     tref = 0.722;
177     h = 0.064;
178     irep = 10;
179     istop = 19;
180     iprint = 10;
181   } 
182
183     public void init(particle[] one, int mdsize) {
184       for (lg=0; lg<=1; lg++) {
185         for (i=0; i<mm; i++) {
186           for (j=0; j<mm; j++) {
187             for (k=0; k<mm; k++) {
188               one[ijk] = new particle((i*a+lg*a*0.5),(j*a+lg*a*0.5),(k*a),
189                   xvelocity,yvelocity,zvelocity,sh_force,sh_force2,id,one);
190               ijk = ijk + 1;
191             }
192           }
193         }
194       }
195
196       for (lg=1; lg<=2; lg++) {
197         for (i=0; i<mm; i++) {
198           for (j=0; j<mm; j++) {
199             for (k=0; k<mm; k++) {
200               one[ijk] = new particle((i*a+(2-lg)*a*0.5),(j*a+(lg-1)*a*0.5),
201                                       (k*a+a*0.5),xvelocity,yvelocity,zvelocity,sh_force,sh_force2,id,one);
202               ijk = ijk + 1;
203             }
204           }
205         }
206       }
207
208       /* Initialise velocities */
209
210       iseed = 0;
211       double v1 = 0.0;
212       double v2 = 0.0;
213       random randnum = new random(iseed,v1,v2);
214       
215       
216       for (i=0; i<mdsize; i+=2) {
217         r  = randnum.seed();
218         one[i].xvelocity = r*randnum.v1;
219         one[i+1].xvelocity  = r*randnum.v2;
220       }
221
222       for (i=0; i<mdsize; i+=2) {
223         r  = randnum.seed();
224         one[i].yvelocity = r*randnum.v1;
225         one[i+1].yvelocity  = r*randnum.v2;
226       }
227
228       for (i=0; i<mdsize; i+=2) {
229         r  = randnum.seed();
230         one[i].zvelocity = r*randnum.v1;
231         one[i+1].zvelocity  = r*randnum.v2;
232       }
233
234
235       /* velocity scaling */
236
237       ekin = 0.0;
238       sp = 0.0;
239
240       for(i=0;i<mdsize;i++) {
241         sp = sp + one[i].xvelocity;
242       }
243       sp = sp / mdsize;
244
245       for(i=0;i<mdsize;i++) {
246         one[i].xvelocity = one[i].xvelocity - sp;
247         ekin = ekin + one[i].xvelocity*one[i].xvelocity;
248       }
249
250       sp = 0.0;
251       for(i=0;i<mdsize;i++) {
252         sp = sp + one[i].yvelocity;
253       }
254       sp = sp / mdsize;
255
256       for(i=0;i<mdsize;i++) {
257         one[i].yvelocity = one[i].yvelocity - sp;
258         ekin = ekin + one[i].yvelocity*one[i].yvelocity;
259       }
260
261
262       sp = 0.0;
263       for(i=0;i<mdsize;i++) {
264         sp = sp + one[i].zvelocity;
265       }
266       sp = sp / mdsize;
267
268       for(i=0;i<mdsize;i++) {
269         one[i].zvelocity = one[i].zvelocity - sp;
270         ekin = ekin + one[i].zvelocity*one[i].zvelocity;
271       }
272
273       ts = tscale * ekin;
274       sc = h * Math.sqrt(tref/ts);
275
276
277       for(i=0;i<mdsize;i++) {
278
279         one[i].xvelocity = one[i].xvelocity * sc;     
280         one[i].yvelocity = one[i].yvelocity * sc;     
281         one[i].zvelocity = one[i].zvelocity * sc;     
282
283       }
284
285     }
286
287   public void run() {
288     /* Parameter determination */
289     
290     int mdsize;
291     double tmpden;
292     int movemx=50;
293     Barrier barr=new Barrier("128.195.175.79");
294     particle[] one;
295     atomic {
296       mdsize = mymd.PARTSIZE;
297       one=new particle[mdsize];
298       l = mymd.LENGTH;
299       tmpden = den;
300       side = Math.pow((mdsize/tmpden),0.3333333);
301       rcoff = mm/4.0;
302
303       a = side/mm;
304       sideh = side*0.5;
305       hsq = h*h;
306       hsq2 = hsq*0.5;
307       npartm = mdsize - 1;
308       rcoffs = rcoff * rcoff;
309       tscale = 16.0 / (1.0 * mdsize - 1.0);
310       vaver = 1.13 * Math.sqrt(tref / 24.0);
311       vaverh = vaver * h;
312
313       /* Particle Generation */
314
315       xvelocity = 0.0;
316       yvelocity = 0.0;
317       zvelocity = 0.0;
318       ijk = 0;
319       init(one, mdsize);
320
321     }
322
323     /* Synchronise threads and start timer before MD simulation */
324
325     Barrier.enterBarrier(barr);
326
327     /* MD simulation */
328
329     for (int move=0;move<movemx;move++) {
330       atomic {
331         /* move the particles and update velocities */
332         for (i=0;i<mdsize;i++) {
333           one[i].domove(side,i);       
334         }
335       }
336
337       /* Barrier */
338       Barrier.enterBarrier(barr);
339
340       atomic {
341         if(id==0) {
342           for(j=0;j<3;j++) {
343             for (i=0;i<mdsize;i++) {
344               sh_force[j][i] = 0.0;
345             }
346           }
347         }
348         
349         mymd.epot[id].d = 0.0;
350         mymd.vir[id].d = 0.0;
351         mymd.interacts[id].i = 0;
352       }
353
354
355       /* Barrier */
356       Barrier.enterBarrier(barr);
357       
358       atomic {
359         /* compute forces */
360         
361         for (i=0+id;i<mdsize;i+=nthreads) {
362           one[i].force(side,rcoff,mdsize,i,xx,yy,zz,mymd); 
363         }
364       }
365
366       /* Barrier */
367       Barrier.enterBarrier(barr);
368
369       /* update force arrays */
370       atomic {
371         if(id == 0) {
372           for(int k=0;k<3;k++) {
373             for(i=0;i<mdsize;i++) {
374               for(j=0;j<nthreads;j++) {
375                 sh_force[k][i] += sh_force2[k][j][i];
376               }
377             }
378           }
379           
380           for(int k=0;k<3;k++) {
381             for(i=0;i<mdsize;i++) {
382               for(j=0;j<nthreads;j++) {
383                 sh_force2[k][j][i] = 0.0;
384               }
385             }
386           }
387
388           for(j=1;j<nthreads;j++) {
389             mymd.epot[0].d += mymd.epot[j].d;
390             mymd.vir[0].d += mymd.vir[j].d;
391           }
392           for(j=1;j<nthreads;j++) {       
393             mymd.epot[j].d = mymd.epot[0].d;
394             mymd.vir[j].d = mymd.vir[0].d;
395           }
396           for(j=0;j<nthreads;j++) {
397             mymd.interactions += mymd.interacts[j].i; 
398           }
399
400           for (j=0;j<3;j++) {
401             for (i=0;i<mdsize;i++) {
402               sh_force[j][i] = sh_force[j][i] * hsq2;
403             }
404           }
405         }
406       }
407
408       /* Barrier */
409       Barrier.enterBarrier(barr);
410
411       atomic {
412         /*scale forces, update velocities */
413         sum = 0.0;
414         for (i=0;i<mdsize;i++) {
415           sum = sum + one[i].mkekin(hsq2,i);  
416         }
417
418         ekin = sum/hsq;
419
420         vel = 0.0;
421         count = 0.0;
422
423         /* average velocity */
424
425         for (i=0;i<mdsize;i++) {
426           velt = one[i].velavg(vaverh,h);
427           if(velt > vaverh) { count = count + 1.0; }
428           vel = vel + velt;                    
429         }
430
431         vel = vel / h;
432
433         /* temperature scale if required */
434
435         if((move < istop) && (((move+1) % irep) == 0)) {
436           sc = Math.sqrt(tref / (tscale*ekin));
437           for (i=0;i<mdsize;i++) {
438             one[i].dscal(sc,1);
439           }
440           ekin = tref / tscale;
441         }
442
443         /* sum to get full potential energy and virial */
444
445         if(((move+1) % iprint) == 0) {
446           mymd.ek[id].d = 24.0*ekin;
447           mymd.epot[id].d = 4.0*mymd.epot[id].d;
448           etot = mymd.ek[id].d + mymd.epot[id].d;
449           temp = tscale * ekin;
450           pres = tmpden * 16.0 * (ekin - mymd.vir[id].d) / mdsize;
451           vel = vel / mdsize; 
452           rp = (count / mdsize) * 100.0;
453         }
454       }
455       Barrier.enterBarrier(barr);
456     }
457
458     Barrier.enterBarrier(barr);
459     //if (id == 0) JGFInstrumentor.stopTimer("Section3:MolDyn:Run", instr.timers);
460   }
461
462 }
463
464 class particle {
465
466   public double xcoord, ycoord, zcoord;
467   public double xvelocity,yvelocity,zvelocity;
468   int part_id;
469   int id;
470   global double [][] sh_force;
471   global double [][][] sh_force2;
472   particle[] one;
473   
474   public particle(double xcoord, double ycoord, double zcoord, double xvelocity,
475                   double yvelocity,double zvelocity, double [][] sh_force, 
476                   double [][][] sh_force2,int id, particle[] one) {
477
478     this.xcoord = xcoord; 
479     this.ycoord = ycoord; 
480     this.zcoord = zcoord;
481     this.xvelocity = xvelocity;
482     this.yvelocity = yvelocity;
483     this.zvelocity = zvelocity;
484     this.sh_force = sh_force;
485     this.sh_force2 = sh_force2;
486     this.id=id;
487     this.one=one;
488   }
489
490   public void domove(double side,int part_id) {
491
492     xcoord = xcoord + xvelocity + sh_force[0][part_id];
493     ycoord = ycoord + yvelocity + sh_force[1][part_id];
494     zcoord = zcoord + zvelocity + sh_force[2][part_id];
495
496     if(xcoord < 0) { xcoord = xcoord + side; } 
497     if(xcoord > side) { xcoord = xcoord - side; }
498     if(ycoord < 0) { ycoord = ycoord + side; }
499     if(ycoord > side) { ycoord = ycoord - side; }
500     if(zcoord < 0) { zcoord = zcoord + side; }
501     if(zcoord > side) { zcoord = zcoord - side; }
502
503     xvelocity = xvelocity + sh_force[0][part_id];
504     yvelocity = yvelocity + sh_force[1][part_id];
505     zvelocity = zvelocity + sh_force[2][part_id];
506
507   }
508
509   public void force(double side, double rcoff,int mdsize,int x, double xx, double yy, double zz, JGFMolDynBench mymd) {
510
511     double sideh;
512     double rcoffs;
513
514     double fxi,fyi,fzi;
515     double rd,rrd,rrd2,rrd3,rrd4,rrd6,rrd7,r148;
516     double forcex,forcey,forcez;
517
518     sideh = 0.5*side; 
519     rcoffs = rcoff*rcoff;
520
521     fxi = 0.0;
522     fyi = 0.0;
523     fzi = 0.0;
524
525     for (int i=x+1;i<mdsize;i++) {
526       xx = this.xcoord - one[i].xcoord;
527       yy = this.ycoord - one[i].ycoord;
528       zz = this.zcoord - one[i].zcoord;
529
530       if(xx < (-sideh)) { xx = xx + side; }
531       if(xx > (sideh))  { xx = xx - side; }
532       if(yy < (-sideh)) { yy = yy + side; }
533       if(yy > (sideh))  { yy = yy - side; }
534       if(zz < (-sideh)) { zz = zz + side; }
535       if(zz > (sideh))  { zz = zz - side; }
536
537
538       rd = xx*xx + yy*yy + zz*zz;
539
540       if(rd <= rcoffs) {
541         rrd = 1.0/rd;
542         rrd2 = rrd*rrd;
543         rrd3 = rrd2*rrd;
544         rrd4 = rrd2*rrd2;
545         rrd6 = rrd2*rrd4;
546         rrd7 = rrd6*rrd;
547         mymd.epot[id].d += (rrd6 - rrd3);
548         r148 = rrd7 - 0.5*rrd4;
549         mymd.vir[id].d += - rd*r148;
550         forcex = xx * r148;
551         fxi = fxi + forcex;
552
553         sh_force2[0][id][i] = sh_force2[0][id][i] - forcex;
554
555         forcey = yy * r148;
556         fyi = fyi + forcey;
557
558         sh_force2[1][id][i] = sh_force2[1][id][i] - forcey;
559
560         forcez = zz * r148;
561         fzi = fzi + forcez;
562
563         sh_force2[2][id][i] = sh_force2[2][id][i] - forcez;
564
565         mymd.interacts[id].i++;
566       }
567
568     }
569
570     sh_force2[0][id][x] = sh_force2[0][id][x] + fxi;
571     sh_force2[1][id][x] = sh_force2[1][id][x] + fyi;
572     sh_force2[2][id][x] = sh_force2[2][id][x] + fzi;
573
574   }
575
576   public double mkekin(double hsq2,int part_id) {
577
578     double sumt = 0.0; 
579
580     xvelocity = xvelocity + sh_force[0][part_id]; 
581     yvelocity = yvelocity + sh_force[1][part_id]; 
582     zvelocity = zvelocity + sh_force[2][part_id]; 
583
584     sumt = (xvelocity*xvelocity)+(yvelocity*yvelocity)+(zvelocity*zvelocity);
585     return sumt;
586   }
587
588   public double velavg(double vaverh,double h) {
589
590     double velt;
591     double sq;
592
593     sq = Math.sqrt(xvelocity*xvelocity + yvelocity*yvelocity +
594         zvelocity*zvelocity);
595
596     velt = sq;
597     return velt;
598   }
599
600   public void dscal(double sc,int incx) {
601     xvelocity = xvelocity * sc;
602     yvelocity = yvelocity * sc;   
603     zvelocity = zvelocity * sc;   
604   }
605 }
606
607 class random {
608
609   public int iseed;
610   public double v1,v2;
611
612   public random(int iseed,double v1,double v2) {
613     this.iseed = iseed;
614     this.v1 = v1;
615     this.v2 = v2;
616   }
617
618   public double update() {
619
620     double rand;
621     double scale= 4.656612875e-10;
622
623     int is1,is2,iss2;
624     int imult=16807;
625     int imod = 2147483647;
626
627     if (iseed<=0) { iseed = 1; }
628
629     is2 = iseed % 32768;
630     is1 = (iseed-is2)/32768;
631     iss2 = is2 * imult;
632     is2 = iss2 % 32768;
633     is1 = (is1*imult+(iss2-is2)/32768) % (65536);
634
635     iseed = (is1*32768+is2) % imod;
636
637     rand = scale * iseed;
638
639     return rand;
640
641   }
642
643   public double seed() {
644
645     double s,u1,u2,r;
646     s = 1.0;
647     do {
648       u1 = update();
649       u2 = update();
650
651       v1 = 2.0 * u1 - 1.0;
652       v2 = 2.0 * u2 - 1.0;
653       s = v1*v1 + v2*v2;
654
655     } while (s >= 1.0);
656
657     r = Math.sqrt(-2.0*Math.log(s)/s);
658
659     return r;
660
661   }
662 }
663
664