start of new file
[IRC.git] / Robust / src / Benchmarks / Scheduling / JGFSeries / c / JGFSeriesBench.c
1 /** Single thread C 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 *                                                                         *
18 *      This version copyright (c) The University of Edinburgh, 2001.      *
19 *                         All rights reserved.                            *
20 *                                                                         *
21 **************************************************************************/
22
23 #ifdef RAW
24 #include <raw.h>
25 #else
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <unistd.h>
30 #endif
31 #include <math.h>
32
33 void begin(void);
34 void run(int id);
35 float TrapezoidIntegrate(float x0, float x1, int nsteps, float omegan, int select);
36 float thefunction(float x, float omegan, int select);
37
38 #ifndef RAW
39 int main(int argc, char **argv) {
40   begin();
41   return 0;
42 }
43 #endif
44
45 void begin(void) {
46         int datasize = 16;
47         int i = 0;
48         /* Main loop: */
49         for(i = 0; i < datasize; ++i) {
50                 run(i);
51         }
52 #ifdef RAW
53     raw_test_pass(raw_get_cycle());
54         raw_test_done(1);
55 #endif
56 }
57
58 void run(int id) {
59         float pair[2];
60         // Calculate the fourier series. Begin by calculating A[0].
61         if (id==0) {
62             pair[0] = TrapezoidIntegrate((float)0.0, //Lower bound.
63                                                  (float)2.0, // Upper bound.
64                                                  1000,        // # of steps.
65                                              (float)0.0, // No omega*n needed.
66                                                  0) / (float)2.0; // 0 = term A[0].
67             pair[1] = 0;
68         } else {
69             // Calculate the fundamental frequency.
70             // ( 2 * pi ) / period...and since the period
71             // is 2, omega is simply pi.
72             float omega = (float) 3.1415926535897932; // Fundamental frequency.
73
74             // Calculate A[i] terms. Note, once again, that we
75             // can ignore the 2/period term outside the integral
76             // since the period is 2 and the term cancels itself
77             // out.
78             pair[0] = TrapezoidIntegrate((float)0.0,
79                                              (float)2.0,
80                                                  1000,
81                                              omega * (float)id, 
82                                                  1);                       // 1 = cosine term.
83             //System.printI(0xa2);
84             // Calculate the B[i] terms.
85             pair[1] = TrapezoidIntegrate((float)0.0,
86                                                 (float)2.0,
87                                                 1000,
88                                                 omega * (float)id,
89                                                 2);                       // 2 = sine term.
90         }
91
92 #ifdef RAW
93         //raw_test_pass_reg(id);
94         //raw_test_pass((int)(pair[0]*10000));
95         //raw_test_pass((int)(pair[1]*10000));
96 #else
97         printf("coefficient NO. %d: %f; %f \n", id, pair[0], pair[1]);
98 #endif
99
100         // validate
101         if(id < 4) {
102                 int j = 0;
103             float ref[4][2];
104             ref[0][0] = 2.8729524964837996;
105             ref[0][1] = 0.0;
106             ref[1][0] = 1.1161046676147888;
107             ref[1][1] = -1.8819691893398025;
108             ref[2][0] = 0.34429060398168704;
109             ref[2][1] = -1.1645642623320958;
110             ref[3][0] = 0.15238898702519288;
111             ref[3][1] = -0.8143461113044298;
112             for (j = 0; j < 2; j++){
113                 float error = abs(pair[j] - ref[id][j]);
114                 if (error > 1.0e-12 ){
115 #ifdef RAW
116                         //raw_test_pass(0xeeee);
117 #else
118                     printf("Validation failed for coefficient %d:%d \n", j, id);
119                     printf("Computed value = %f \n", pair[j]);
120                     printf("Reference value = %f \n", ref[id][j]);
121 #endif
122                 }
123             }
124         }
125 }
126
127 /*
128  * TrapezoidIntegrate
129  *
130  * Perform a simple trapezoid integration on the function (x+1)**x.
131  * x0,x1 set the lower and upper bounds of the integration.
132  * nsteps indicates # of trapezoidal sections.
133  * omegan is the fundamental frequency times the series member #.
134  * select = 0 for the A[0] term, 1 for cosine terms, and 2 for
135  * sine terms. Returns the value.
136  */
137
138 float TrapezoidIntegrate (float x0,     // Lower bound.
139                            float x1,     // Upper bound.
140                            int nsteps,    // # of steps.
141                            float omegan, // omega * n.
142                            int select)  { // Term type.
143         float x;               // Independent variable.
144         float dx;              // Step size.
145         float rvalue;          // Return value.
146
147         // Initialize independent variable.
148         x = x0;
149
150         // Calculate stepsize.
151         dx = (x1 - x0) / (float)nsteps;
152
153         // Initialize the return value.
154         rvalue = thefunction(x0, omegan, select) / (float)2.0;
155
156         // Compute the other terms of the integral.
157         if (nsteps != 1) {
158             --nsteps;               // Already done 1 step.
159             while (--nsteps > 0) {
160                 x += dx;
161                 rvalue += thefunction(x, omegan, select);
162             }
163         }
164
165         // Finish computation.
166         rvalue=(rvalue + thefunction(x1,omegan,select) / (float)2.0) * dx;
167         return(rvalue);
168 }
169
170 /*
171  * thefunction
172  *
173  * This routine selects the function to be used in the Trapezoid
174  * integration. x is the independent variable, omegan is omega * n,
175  * and select chooses which of the sine/cosine functions
176  * are used. Note the special case for select=0.
177  */
178
179 float thefunction(float x,      // Independent variable.
180                                float omegan, // Omega * term.
181                                int select) {  // Choose type.
182
183         // Use select to pick which function we call.
184         float result = 0.0;
185         if(0 == select) {
186             result = powf(x+(float)1.0,x);
187         } else if (1 == select) {
188             return(powf(x+(float)1.0,x) * cosf(omegan*x));
189         } else if (2 == select) {
190             return(powf(x+(float)1.0,x) * sinf(omegan*x));
191         }
192
193         // We should never reach this point, but the following
194         // keeps compilers from issuing a warning message.
195         return result;
196 }