adding a test case
[IRC.git] / Robust / src / Benchmarks / Scheduling / JGFSeries / SeriesRunner.java
1 /** Bristlecone Version  **/
2
3 /**************************************************************************
4  *                                                                         *
5  *         Java Grande Forum Benchmark Suite - Thread Version 1.0          *
6  *                                                                         *
7  *                            produced by                                  *
8  *                                                                         *
9  *                  Java Grande Benchmarking Project                       *
10  *                                                                         *
11  *                                at                                       *
12  *                                                                         *
13  *                Edinburgh Parallel Computing Centre                      *
14  *                                                                         * 
15  *                email: epcc-javagrande@epcc.ed.ac.uk                     *
16  *                                                                         *
17  *                  Original version of this code by                       *
18  *                 Gabriel Zachmann (zach@igd.fhg.de)                      *
19  *                                                                         *
20  *      This version copyright (c) The University of Edinburgh, 2001.      *
21  *                         All rights reserved.                            *
22  *                                                                         *
23  **************************************************************************/
24
25 /**
26  * Class SeriesRunner
27  *
28  * Performs the transcendental/trigonometric portion of the
29  * benchmark. This test calculates the nth fourier
30  * coefficients of the function (x+1)^x defined on the interval
31  * 0,2 (where n is an arbitrary number set in the constructor).
32  *
33  * The first four pairs of coefficients calculated shoud be:
34  * (2.83777, 0), (1.04578, -1.8791), (0.2741, -1.15884), and
35  * (0.0824148, -0.805759).
36  */
37 public class SeriesRunner {
38     flag finish;
39
40     int id;
41         int range;
42
43     public SeriesRunner(int id, int range){
44         this.id=id;
45         this.range = range;
46     }
47
48     public void run() {
49         float pair[][] = new float[2][range];
50         // Calculate the fourier series. Begin by calculating A[0].
51         if (id==0) {
52             pair[0][0] = TrapezoidIntegrate((float)0.0, //Lower bound.
53                                          (float)2.0, // Upper bound.
54                                          1000,        // # of steps.
55                                          (float)0.0, // No omega*n needed.
56                                          0) / (float)2.0; // 0 = term A[0].
57             pair[1][0] = 0;
58         } 
59         // Calculate the fundamental frequency.
60         // ( 2 * pi ) / period...and since the period
61         // is 2, omega is simply pi.
62         float omega = (float) 3.1415926535897932; // Fundamental frequency.
63
64         int ilow = id*range;
65         if(id==0) ilow += 1;
66         int iupper = (id+1)*range;
67         for(int i = ilow; i < iupper; i++) {
68                 int j = i-id*range;
69             // Calculate A[i] terms. Note, once again, that we
70             // can ignore the 2/period term outside the integral
71             // since the period is 2 and the term cancels itself
72             // out.
73             pair[0][j] = TrapezoidIntegrate((float)0.0,
74                                          (float)2.0,
75                                          1000,
76                                          omega * (float)i, 
77                                          1);                       // 1 = cosine term.
78             // Calculate the B[i] terms.
79             pair[1][j] = TrapezoidIntegrate((float)0.0,
80                                          (float)2.0,
81                                          1000,
82                                          omega * (float)i,
83                                          2);                       // 2 = sine term.
84         }
85
86         // validate
87         if(id == 0) {
88             float ref[][] = new float[4][2];
89             ref[0][0] = (float)2.87290112;
90             ref[0][1] = (float)0.0;
91             ref[1][0] = (float)1.11594856;
92             ref[1][1] = (float)-1.88199680;
93             ref[2][0] = (float)0.34412988;
94             ref[2][1] = (float)-1.16458096;
95             ref[3][0] = (float)0.15222694;
96             ref[3][1] = (float)-0.81435320;
97                 for(int i = 0; i < 4; i++) {
98                         for (int j = 0; j < 2; j++){
99                                 float error = Math.abs(pair[j][i] - ref[i][j]);
100                                 if (error > 1.0e-7 ){
101                                         //System.printI(0xa7);
102                                         //System.printString("Validation failed for coefficient " + j + "," + id + "\n");
103                                         //System.printString("Computed value = " + (int)(pair[j]*100000000) + "\n");
104                                         //System.printString("Reference value = " + (int)(ref[id][j]*100000000) + "\n");
105                                         //System.printI((int)(pair[j]*10000));
106                                         //System.printI((int)(ref[id][j]*10000));
107                                 }
108                         }
109                 }
110     }
111         }
112
113     /*
114      * TrapezoidIntegrate
115      *
116      * Perform a simple trapezoid integration on the function (x+1)**x.
117      * x0,x1 set the lower and upper bounds of the integration.
118      * nsteps indicates # of trapezoidal sections.
119      * omegan is the fundamental frequency times the series member #.
120      * select = 0 for the A[0] term, 1 for cosine terms, and 2 for
121      * sine terms. Returns the value.
122      */
123
124     private float TrapezoidIntegrate (float x0,     // Lower bound.
125                                       float x1,     // Upper bound.
126                                       int nsteps,    // # of steps.
127                                       float omegan, // omega * n.
128                                       int select)    // Term type.
129     {
130         float x;               // Independent variable.
131         float dx;              // Step size.
132         float rvalue;          // Return value.
133         float tmpvalue = (float)0.0;
134
135         // Initialize independent variable.
136         x = x0;
137
138         // Calculate stepsize.
139         dx = (x1 - x0) / (float)nsteps;
140
141         // Initialize the return value.
142         rvalue = thefunction(x0, omegan, select) / (float)2.0;
143
144         // Compute the other terms of the integral.
145         if (nsteps != 1)
146         {
147             --nsteps;               // Already done 1 step.
148             while (nsteps > 1)
149             {
150                 x += dx;
151                 tmpvalue = thefunction(x, omegan, select);
152                 nsteps--;
153                 rvalue += tmpvalue;
154             }
155         }
156
157         // Finish computation.
158         tmpvalue = thefunction(x1,omegan,select) / (float)2.0;
159         rvalue=(float)(rvalue + tmpvalue) * dx;
160         return(rvalue);
161     }
162
163     /*
164      * thefunction
165      *
166      * This routine selects the function to be used in the Trapezoid
167      * integration. x is the independent variable, omegan is omega * n,
168      * and select chooses which of the sine/cosine functions
169      * are used. Note the special case for select=0.
170      */
171
172     private float thefunction(float x,      // Independent variable.
173                               float omegan, // Omega * term.
174                               int select)    // Choose type.
175     {
176
177         // Use select to pick which function we call.
178         float tmp = x+(float)1.0;
179         float result = (float)0.0;
180         float v1 = (float)0.0;
181         float v2 = (float)1.0;
182         v1 = Math.powf(tmp,x);
183         if(0 == select) {
184             return v1;
185         } else if (1 == select) {
186             v2 = Math.cosf(omegan*x);
187         } else if (2 == select) {
188             v2 = Math.sinf(omegan*x);
189         }
190         result = v1 * v2;
191
192         // We should never reach this point, but the following
193         // keeps compilers from issuing a warning message.
194         return result;
195     }    
196 }
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215