| 1 | # include <stdlib.h>
|
|---|
| 2 | # include <stdio.h>
|
|---|
| 3 | # include <math.h>
|
|---|
| 4 | # include <omp.h>
|
|---|
| 5 | # include <time.h>
|
|---|
| 6 |
|
|---|
| 7 | int main ( void );
|
|---|
| 8 | void monte_carlo ( int n, int *seed );
|
|---|
| 9 | double random_value ( int *seed );
|
|---|
| 10 | void timestamp ( void );
|
|---|
| 11 |
|
|---|
| 12 | /******************************************************************************/
|
|---|
| 13 |
|
|---|
| 14 | int 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 |
|
|---|
| 81 | void 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[n];
|
|---|
| 112 |
|
|---|
| 113 | # pragma omp master
|
|---|
| 114 | {
|
|---|
| 115 | printf ( "\n" );
|
|---|
| 116 | printf ( " Thread Seed I X(I)\n" );
|
|---|
| 117 | printf ( "\n" );
|
|---|
| 118 | }
|
|---|
| 119 |
|
|---|
| 120 | # pragma omp parallel private ( i, my_id, my_seed ) shared ( n, x )
|
|---|
| 121 | {
|
|---|
| 122 | my_id = omp_get_thread_num ( );
|
|---|
| 123 | my_seed = *seed + my_id;
|
|---|
| 124 | printf ( " %6d %12d\n", my_id, my_seed );
|
|---|
| 125 |
|
|---|
| 126 | # pragma omp for
|
|---|
| 127 | for ( i = 0; i < n; i++ )
|
|---|
| 128 | {
|
|---|
| 129 | x[i] = random_value ( &my_seed );
|
|---|
| 130 | printf ( " %6d %12d %6d %14.6g\n", my_id, my_seed, i, x[i] );
|
|---|
| 131 | }
|
|---|
| 132 |
|
|---|
| 133 | }
|
|---|
| 134 |
|
|---|
| 135 | //free ( x );
|
|---|
| 136 |
|
|---|
| 137 | return;
|
|---|
| 138 | }
|
|---|
| 139 | /******************************************************************************/
|
|---|
| 140 |
|
|---|
| 141 | double random_value ( int *seed )
|
|---|
| 142 |
|
|---|
| 143 | /******************************************************************************/
|
|---|
| 144 | /*
|
|---|
| 145 | Purpose:
|
|---|
| 146 |
|
|---|
| 147 | RANDOM_VALUE generates a random value R.
|
|---|
| 148 |
|
|---|
| 149 | Discussion:
|
|---|
| 150 |
|
|---|
| 151 | This is not a good random number generator. It is a SIMPLE one.
|
|---|
| 152 | It illustrates a model which works by accepting an integer seed value
|
|---|
| 153 | as input, performing some simple operation on the seed, and then
|
|---|
| 154 | producing a "random" real value using some simple transformation.
|
|---|
| 155 |
|
|---|
| 156 | Licensing:
|
|---|
| 157 |
|
|---|
| 158 | This code is distributed under the GNU LGPL license.
|
|---|
| 159 |
|
|---|
| 160 | Modified:
|
|---|
| 161 |
|
|---|
| 162 | 03 September 2012
|
|---|
| 163 |
|
|---|
| 164 | Author:
|
|---|
| 165 |
|
|---|
| 166 | John Burkardt
|
|---|
| 167 |
|
|---|
| 168 | Parameters:
|
|---|
| 169 |
|
|---|
| 170 | Input/output, int *SEED, a seed for the random
|
|---|
| 171 | number generator.
|
|---|
| 172 |
|
|---|
| 173 | Output, double RANDOM_VALUE, the random value.
|
|---|
| 174 | */
|
|---|
| 175 | {
|
|---|
| 176 | double r;
|
|---|
| 177 |
|
|---|
| 178 | *seed = ( *seed % 65536 );
|
|---|
| 179 | *seed = ( ( 3125 * *seed ) % 65536 );
|
|---|
| 180 | r = ( double ) ( *seed ) / 65536.0;
|
|---|
| 181 |
|
|---|
| 182 | return r;
|
|---|
| 183 | }
|
|---|
| 184 | /******************************************************************************/
|
|---|
| 185 |
|
|---|
| 186 | void timestamp ( void )
|
|---|
| 187 |
|
|---|
| 188 | /******************************************************************************/
|
|---|
| 189 | /*
|
|---|
| 190 | Purpose:
|
|---|
| 191 |
|
|---|
| 192 | TIMESTAMP prints the current YMDHMS date as a time stamp.
|
|---|
| 193 |
|
|---|
| 194 | Example:
|
|---|
| 195 |
|
|---|
| 196 | 31 May 2001 09:45:54 AM
|
|---|
| 197 |
|
|---|
| 198 | Modified:
|
|---|
| 199 |
|
|---|
| 200 | 24 September 2003
|
|---|
| 201 |
|
|---|
| 202 | Author:
|
|---|
| 203 |
|
|---|
| 204 | John Burkardt
|
|---|
| 205 |
|
|---|
| 206 | Parameters:
|
|---|
| 207 |
|
|---|
| 208 | None
|
|---|
| 209 | */
|
|---|
| 210 | {
|
|---|
| 211 | # define TIME_SIZE 40
|
|---|
| 212 |
|
|---|
| 213 | static char time_buffer[TIME_SIZE];
|
|---|
| 214 | const struct tm *tm;
|
|---|
| 215 | size_t len;
|
|---|
| 216 | time_t now;
|
|---|
| 217 |
|
|---|
| 218 | now = time ( NULL );
|
|---|
| 219 | tm = localtime ( &now );
|
|---|
| 220 |
|
|---|
| 221 | len = strftime ( time_buffer, TIME_SIZE, "%d %B %Y %I:%M:%S %p", tm );
|
|---|
| 222 |
|
|---|
| 223 | printf ( "%s\n", time_buffer );
|
|---|
| 224 |
|
|---|
| 225 | return;
|
|---|
| 226 | # undef TIME_SIZE
|
|---|
| 227 | }
|
|---|