| [71264c4] | 1 | # include <stdlib.h>
|
|---|
| 2 | # include <stdio.h>
|
|---|
| 3 | # include <math.h>
|
|---|
| 4 | # include <time.h>
|
|---|
| 5 | # include <omp.h>
|
|---|
| 6 |
|
|---|
| [20ac35f] | 7 | #ifdef _CIVL
|
|---|
| 8 | $input int N=100; // originally 10000000
|
|---|
| 9 | #else
|
|---|
| 10 | #define N 10000000
|
|---|
| 11 | #endif
|
|---|
| 12 |
|
|---|
| [71264c4] | 13 | int main ( int argc, char *argv[] );
|
|---|
| 14 | double f ( double x );
|
|---|
| 15 | double cpu_time ( void );
|
|---|
| 16 | void timestamp ( void );
|
|---|
| 17 |
|
|---|
| 18 | /******************************************************************************/
|
|---|
| 19 |
|
|---|
| 20 | int main ( int argc, char *argv[] )
|
|---|
| 21 |
|
|---|
| 22 | /******************************************************************************/
|
|---|
| 23 | /*
|
|---|
| 24 | Purpose:
|
|---|
| 25 |
|
|---|
| 26 | MAIN is the main program for QUAD_OPENMP.
|
|---|
| 27 |
|
|---|
| 28 | Licensing:
|
|---|
| 29 |
|
|---|
| 30 | This code is distributed under the GNU LGPL license.
|
|---|
| 31 |
|
|---|
| 32 | Modified:
|
|---|
| 33 |
|
|---|
| 34 | 14 December 2011
|
|---|
| 35 |
|
|---|
| 36 | Author:
|
|---|
| 37 |
|
|---|
| 38 | John Burkardt
|
|---|
| 39 | */
|
|---|
| 40 | {
|
|---|
| 41 | double a = 0.0;
|
|---|
| 42 | double b = 10.0;
|
|---|
| 43 | double error;
|
|---|
| 44 | double exact = 0.49936338107645674464;
|
|---|
| 45 | int i;
|
|---|
| [20ac35f] | 46 | int n = N;
|
|---|
| [71264c4] | 47 | double total;
|
|---|
| 48 | double wtime;
|
|---|
| 49 | double x;
|
|---|
| 50 |
|
|---|
| 51 | timestamp ( );
|
|---|
| 52 | printf ( "\n" );
|
|---|
| 53 | printf ( "QUAD_OPENMP:\n" );
|
|---|
| 54 | printf ( " C version\n" );
|
|---|
| 55 | printf ( " Use OpenMP for parallel execution.\n" );
|
|---|
| 56 | printf ( " Estimate the integral of f(x) from A to B.\n" );
|
|---|
| 57 | printf ( " f(x) = 50 / ( pi * ( 2500 * x * x + 1 ) ).\n" );
|
|---|
| 58 | printf ( "\n" );
|
|---|
| 59 | printf ( " A = %f\n", a );
|
|---|
| 60 | printf ( " B = %f\n", b );
|
|---|
| 61 | printf ( " N = %d\n", n );
|
|---|
| 62 | printf ( " Exact = %24.16f\n", exact );
|
|---|
| 63 |
|
|---|
| 64 | wtime = omp_get_wtime ( );
|
|---|
| 65 |
|
|---|
| 66 | total = 0.0;
|
|---|
| 67 |
|
|---|
| 68 | # pragma omp parallel shared ( a, b, n ) private ( i, x )
|
|---|
| 69 |
|
|---|
| 70 | # pragma omp for reduction ( + : total )
|
|---|
| 71 |
|
|---|
| 72 | for ( i = 0; i < n; i++ )
|
|---|
| 73 | {
|
|---|
| 74 | x = ( ( double ) ( n - i - 1 ) * a + ( double ) ( i ) * b ) / ( double ) ( n - 1 );
|
|---|
| 75 | total = total + f ( x );
|
|---|
| 76 | }
|
|---|
| 77 |
|
|---|
| 78 | wtime = omp_get_wtime ( ) - wtime;
|
|---|
| 79 |
|
|---|
| 80 | total = ( b - a ) * total / ( double ) n;
|
|---|
| 81 | error = fabs ( total - exact );
|
|---|
| 82 |
|
|---|
| 83 | printf ( "\n" );
|
|---|
| 84 | printf ( " Estimate = %24.16f\n", total );
|
|---|
| 85 | printf ( " Error = %e\n", error );
|
|---|
| 86 | printf ( " Time = %f\n", wtime );
|
|---|
| 87 | /*
|
|---|
| 88 | Terminate.
|
|---|
| 89 | */
|
|---|
| 90 | printf ( "\n" );
|
|---|
| 91 | printf ( "QUAD_OPENMP:\n" );
|
|---|
| 92 | printf ( " Normal end of execution.\n" );
|
|---|
| 93 | printf ( "\n" );
|
|---|
| 94 | timestamp ( );
|
|---|
| 95 |
|
|---|
| 96 | return 0;
|
|---|
| 97 | }
|
|---|
| 98 | /*******************************************************************************/
|
|---|
| 99 |
|
|---|
| 100 | double f ( double x )
|
|---|
| 101 |
|
|---|
| 102 | /*******************************************************************************/
|
|---|
| 103 | /*
|
|---|
| 104 | Purpose:
|
|---|
| 105 |
|
|---|
| 106 | F evaluates the function.
|
|---|
| 107 |
|
|---|
| 108 | Licensing:
|
|---|
| 109 |
|
|---|
| 110 | This code is distributed under the GNU LGPL license.
|
|---|
| 111 |
|
|---|
| 112 | Modified:
|
|---|
| 113 |
|
|---|
| 114 | 18 July 2010
|
|---|
| 115 |
|
|---|
| 116 | Author:
|
|---|
| 117 |
|
|---|
| 118 | John Burkardt
|
|---|
| 119 |
|
|---|
| 120 | Parameters:
|
|---|
| 121 |
|
|---|
| 122 | Input, double X, the argument.
|
|---|
| 123 |
|
|---|
| 124 | Output, double F, the value of the function.
|
|---|
| 125 | */
|
|---|
| 126 | {
|
|---|
| 127 | double pi = 3.141592653589793;
|
|---|
| 128 | double value;
|
|---|
| 129 |
|
|---|
| 130 | value = 50.0 / ( pi * ( 2500.0 * x * x + 1.0 ) );
|
|---|
| 131 |
|
|---|
| 132 | return value;
|
|---|
| 133 | }
|
|---|
| 134 | /*******************************************************************************/
|
|---|
| 135 |
|
|---|
| 136 | double cpu_time ( void )
|
|---|
| 137 |
|
|---|
| 138 | /*******************************************************************************/
|
|---|
| 139 | /*
|
|---|
| 140 | Purpose:
|
|---|
| 141 |
|
|---|
| 142 | CPU_TIME reports the total CPU time for a program.
|
|---|
| 143 |
|
|---|
| 144 | Licensing:
|
|---|
| 145 |
|
|---|
| 146 | This code is distributed under the GNU LGPL license.
|
|---|
| 147 |
|
|---|
| 148 | Modified:
|
|---|
| 149 |
|
|---|
| 150 | 27 September 2005
|
|---|
| 151 |
|
|---|
| 152 | Author:
|
|---|
| 153 |
|
|---|
| 154 | John Burkardt
|
|---|
| 155 |
|
|---|
| 156 | Parameters:
|
|---|
| 157 |
|
|---|
| 158 | Output, double CPU_TIME, the current total elapsed CPU time in second.
|
|---|
| 159 | */
|
|---|
| 160 | {
|
|---|
| 161 | double value;
|
|---|
| 162 |
|
|---|
| 163 | value = ( double ) clock ( ) / ( double ) CLOCKS_PER_SEC;
|
|---|
| 164 |
|
|---|
| 165 | return value;
|
|---|
| 166 | }
|
|---|
| 167 | /******************************************************************************/
|
|---|
| 168 |
|
|---|
| 169 | void timestamp ( void )
|
|---|
| 170 |
|
|---|
| 171 | /******************************************************************************/
|
|---|
| 172 | /*
|
|---|
| 173 | Purpose:
|
|---|
| 174 |
|
|---|
| 175 | TIMESTAMP prints the current YMDHMS date as a time stamp.
|
|---|
| 176 |
|
|---|
| 177 | Example:
|
|---|
| 178 |
|
|---|
| 179 | 31 May 2001 09:45:54 AM
|
|---|
| 180 |
|
|---|
| 181 | Licensing:
|
|---|
| 182 |
|
|---|
| 183 | This code is distributed under the GNU LGPL license.
|
|---|
| 184 |
|
|---|
| 185 | Modified:
|
|---|
| 186 |
|
|---|
| 187 | 24 September 2003
|
|---|
| 188 |
|
|---|
| 189 | Author:
|
|---|
| 190 |
|
|---|
| 191 | John Burkardt
|
|---|
| 192 |
|
|---|
| 193 | Parameters:
|
|---|
| 194 |
|
|---|
| 195 | None
|
|---|
| 196 | */
|
|---|
| 197 | {
|
|---|
| 198 | # define TIME_SIZE 40
|
|---|
| 199 |
|
|---|
| 200 | static char time_buffer[TIME_SIZE];
|
|---|
| 201 | const struct tm *tm;
|
|---|
| 202 | time_t now;
|
|---|
| 203 |
|
|---|
| 204 | now = time ( NULL );
|
|---|
| 205 | tm = localtime ( &now );
|
|---|
| 206 |
|
|---|
| 207 | strftime ( time_buffer, TIME_SIZE, "%d %B %Y %I:%M:%S %p", tm );
|
|---|
| 208 |
|
|---|
| 209 | printf ( "%s\n", time_buffer );
|
|---|
| 210 |
|
|---|
| 211 | return;
|
|---|
| 212 | # undef TIME_SIZE
|
|---|
| 213 | }
|
|---|