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