source: CIVL/mods/dev.civl.abc/examples/omp/quad_openmp.c

main
Last change on this file was aad342c, checked in by Stephen Siegel <siegel@…>, 3 years ago

Performing huge refactor to incorporate ABC, GMC, and SARL into CIVL repo and use Java modules.

git-svn-id: svn://vsl.cis.udel.edu/civl/trunk@5664 fb995dde-84ed-4084-dfe6-e5aef3e2452c

  • Property mode set to 100644
File size: 3.8 KB
Line 
1# include <stdlib.h>
2# include <stdio.h>
3# include <math.h>
4# include <time.h>
5# include <omp.h>
6
7int main ( int argc, char *argv[] );
8double f ( double x );
9double cpu_time ( void );
10void timestamp ( void );
11
12/******************************************************************************/
13
14int 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# pragma omp parallel shared ( a, b, n ) private ( i, x )
63
64# pragma omp for reduction ( + : total )
65
66 for ( i = 0; i < n; i++ )
67 {
68 x = ( ( double ) ( n - i - 1 ) * a + ( double ) ( i ) * b ) / ( double ) ( n - 1 );
69 total = total + f ( x );
70 }
71
72 wtime = omp_get_wtime ( ) - wtime;
73
74 total = ( b - a ) * total / ( double ) n;
75 error = fabs ( total - exact );
76
77 printf ( "\n" );
78 printf ( " Estimate = %24.16f\n", total );
79 printf ( " Error = %e\n", error );
80 printf ( " Time = %f\n", wtime );
81/*
82 Terminate.
83*/
84 printf ( "\n" );
85 printf ( "QUAD_OPENMP:\n" );
86 printf ( " Normal end of execution.\n" );
87 printf ( "\n" );
88 timestamp ( );
89
90 return 0;
91}
92/*******************************************************************************/
93
94double f ( double x )
95
96/*******************************************************************************/
97/*
98 Purpose:
99
100 F evaluates the function.
101
102 Licensing:
103
104 This code is distributed under the GNU LGPL license.
105
106 Modified:
107
108 18 July 2010
109
110 Author:
111
112 John Burkardt
113
114 Parameters:
115
116 Input, double X, the argument.
117
118 Output, double F, the value of the function.
119*/
120{
121 double pi = 3.141592653589793;
122 double value;
123
124 value = 50.0 / ( pi * ( 2500.0 * x * x + 1.0 ) );
125
126 return value;
127}
128/*******************************************************************************/
129
130double cpu_time ( void )
131
132/*******************************************************************************/
133/*
134 Purpose:
135
136 CPU_TIME reports the total CPU time for a program.
137
138 Licensing:
139
140 This code is distributed under the GNU LGPL license.
141
142 Modified:
143
144 27 September 2005
145
146 Author:
147
148 John Burkardt
149
150 Parameters:
151
152 Output, double CPU_TIME, the current total elapsed CPU time in second.
153*/
154{
155 double value;
156
157 value = ( double ) clock ( ) / ( double ) CLOCKS_PER_SEC;
158
159 return value;
160}
161/******************************************************************************/
162
163void timestamp ( void )
164
165/******************************************************************************/
166/*
167 Purpose:
168
169 TIMESTAMP prints the current YMDHMS date as a time stamp.
170
171 Example:
172
173 31 May 2001 09:45:54 AM
174
175 Licensing:
176
177 This code is distributed under the GNU LGPL license.
178
179 Modified:
180
181 24 September 2003
182
183 Author:
184
185 John Burkardt
186
187 Parameters:
188
189 None
190*/
191{
192# define TIME_SIZE 40
193
194 static char time_buffer[TIME_SIZE];
195 const struct tm *tm;
196 time_t now;
197
198 now = time ( NULL );
199 tm = localtime ( &now );
200
201 strftime ( time_buffer, TIME_SIZE, "%d %B %Y %I:%M:%S %p", tm );
202
203 printf ( "%s\n", time_buffer );
204
205 return;
206# undef TIME_SIZE
207}
Note: See TracBrowser for help on using the repository browser.