source: CIVL/examples/omp/random_openmp.c@ abb7e049

1.23 2.0 main test-branch
Last change on this file since abb7e049 was 5f600f6, checked in by Michael Rogers <mrogers08@…>, 11 years ago

Added some new examples from the burkhardt source where other OpenMP examples were taken from but these weren't included.

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

  • Property mode set to 100644
File size: 4.7 KB
RevLine 
[5f600f6]1# include <stdlib.h>
2# include <stdio.h>
3# include <math.h>
4# include <omp.h>
5# include <time.h>
6
7int main ( void );
8void monte_carlo ( int n, int *seed );
9double random_value ( int *seed );
10void timestamp ( void );
11
12/******************************************************************************/
13
14int main ( void )
15
16/******************************************************************************/
17/*
18 Purpose:
19
20 MAIN is the main program for RANDOM_OPENMP.
21
22 Discussion:
23
24 This program simply explores one issue in the generation of random
25 numbers in a parallel program. If the random number generator uses
26 an integer seed to determine the next entry, then it is not easy for
27 a parallel program to reproduce the same exact sequence.
28
29 But what is worse is that it might not be clear how the separate
30 OpenMP threads should handle the SEED value - as a shared or private
31 variable? It seems clear that each thread should have a private
32 seed that is initialized to a distinct value at the beginning of
33 the computation.
34
35 Licensing:
36
37 This code is distributed under the GNU LGPL license.
38
39 Modified:
40
41 03 September 2012
42
43 Author:
44
45 John Burkardt
46*/
47{
48 int n;
49 int seed;
50
51 timestamp ( );
52
53 printf ( "\n" );
54 printf ( "RANDOM_OPENMP\n" );
55 printf ( " C version\n" );
56 printf ( " An OpenMP program using random numbers.\n" );
57 printf ( " The random numbers depend on a seed.\n" );
58 printf ( " We need to insure that each OpenMP thread\n" );
59 printf ( " starts with a different seed.\n" );
60 printf ( "\n" );
61 printf ( " Number of processors available = %d\n", omp_get_num_procs ( ) );
62 printf ( " Number of threads = %d\n", omp_get_max_threads ( ) );
63
64 n = 100;
65 seed = 123456789;
66 monte_carlo ( n, &seed );
67/*
68 Terminate.
69*/
70 printf ( "\n" );
71 printf ( "RANDOM_OPENMP\n" );
72 printf ( " Normal end of execution.\n" );
73
74 printf ( "\n" );
75 timestamp ( );
76
77 return 0;
78}
79/******************************************************************************/
80
81void monte_carlo ( int n, int *seed )
82
83/******************************************************************************/
84/*
85 Purpose:
86
87 MONTE_CARLO carries out a Monte Carlo calculation with random values.
88
89 Licensing:
90
91 This code is distributed under the GNU LGPL license.
92
93 Modified:
94
95 03 September 2012
96
97 Author:
98
99 John Burkardt
100
101 Parameter:
102
103 Input, int N, the number of values to generate.
104
105 Input, int *SEED, a seed for the random number generator.
106*/
107{
108 int i;
109 int my_id;
110 int my_seed;
111 double *x;
112
113 x = ( double * ) malloc ( n * sizeof ( double ) );
114
115# pragma omp master
116{
117 printf ( "\n" );
118 printf ( " Thread Seed I X(I)\n" );
119 printf ( "\n" );
120}
121
122# pragma omp parallel private ( i, my_id, my_seed ) shared ( n, x )
123{
124 my_id = omp_get_thread_num ( );
125 my_seed = *seed + my_id;
126 printf ( " %6d %12d\n", my_id, my_seed );
127
128# pragma omp for
129 for ( i = 0; i < n; i++ )
130 {
131 x[i] = random_value ( &my_seed );
132 printf ( " %6d %12d %6d %14.6g\n", my_id, my_seed, i, x[i] );
133 }
134
135}
136
137 free ( x );
138
139 return;
140}
141/******************************************************************************/
142
143double random_value ( int *seed )
144
145/******************************************************************************/
146/*
147 Purpose:
148
149 RANDOM_VALUE generates a random value R.
150
151 Discussion:
152
153 This is not a good random number generator. It is a SIMPLE one.
154 It illustrates a model which works by accepting an integer seed value
155 as input, performing some simple operation on the seed, and then
156 producing a "random" real value using some simple transformation.
157
158 Licensing:
159
160 This code is distributed under the GNU LGPL license.
161
162 Modified:
163
164 03 September 2012
165
166 Author:
167
168 John Burkardt
169
170 Parameters:
171
172 Input/output, int *SEED, a seed for the random
173 number generator.
174
175 Output, double RANDOM_VALUE, the random value.
176*/
177{
178 double r;
179
180 *seed = ( *seed % 65536 );
181 *seed = ( ( 3125 * *seed ) % 65536 );
182 r = ( double ) ( *seed ) / 65536.0;
183
184 return r;
185}
186/******************************************************************************/
187
188void timestamp ( void )
189
190/******************************************************************************/
191/*
192 Purpose:
193
194 TIMESTAMP prints the current YMDHMS date as a time stamp.
195
196 Example:
197
198 31 May 2001 09:45:54 AM
199
200 Modified:
201
202 24 September 2003
203
204 Author:
205
206 John Burkardt
207
208 Parameters:
209
210 None
211*/
212{
213# define TIME_SIZE 40
214
215 static char time_buffer[TIME_SIZE];
216 const struct tm *tm;
217 size_t len;
218 time_t now;
219
220 now = time ( NULL );
221 tm = localtime ( &now );
222
223 len = strftime ( time_buffer, TIME_SIZE, "%d %B %Y %I:%M:%S %p", tm );
224
225 printf ( "%s\n", time_buffer );
226
227 return;
228# undef TIME_SIZE
229}
Note: See TracBrowser for help on using the repository browser.