| 1 | # include <stdlib.h>
|
|---|
| 2 | # include <stdio.h>
|
|---|
| 3 | # include <time.h>
|
|---|
| 4 | # include <omp.h>
|
|---|
| 5 |
|
|---|
| 6 | int main ( int argc, char *argv[] );
|
|---|
| 7 | int circuit_value ( int n, int bvec[] );
|
|---|
| 8 | void i4_to_bvec ( int i4, int n, int bvec[] );
|
|---|
| 9 | void timestamp ( void );
|
|---|
| 10 |
|
|---|
| 11 | /******************************************************************************/
|
|---|
| 12 |
|
|---|
| 13 | int main ( int argc, char *argv[] )
|
|---|
| 14 |
|
|---|
| 15 | /******************************************************************************/
|
|---|
| 16 | /*
|
|---|
| 17 | Purpose:
|
|---|
| 18 |
|
|---|
| 19 | MAIN is the main program for SATISFY_OPENMP.
|
|---|
| 20 |
|
|---|
| 21 | Licensing:
|
|---|
| 22 |
|
|---|
| 23 | This code is distributed under the GNU LGPL license.
|
|---|
| 24 |
|
|---|
| 25 | Modified:
|
|---|
| 26 |
|
|---|
| 27 | 24 March 2009
|
|---|
| 28 |
|
|---|
| 29 | Author:
|
|---|
| 30 |
|
|---|
| 31 | John Burkardt
|
|---|
| 32 |
|
|---|
| 33 | Reference:
|
|---|
| 34 |
|
|---|
| 35 | Michael Quinn,
|
|---|
| 36 | Parallel Programming in C with MPI and OpenMP,
|
|---|
| 37 | McGraw-Hill, 2004,
|
|---|
| 38 | ISBN13: 978-0071232654,
|
|---|
| 39 | LC: QA76.73.C15.Q55.
|
|---|
| 40 | */
|
|---|
| 41 | {
|
|---|
| 42 | # define N 5
|
|---|
| 43 |
|
|---|
| 44 | int bvec[N];
|
|---|
| 45 | int i;
|
|---|
| 46 | int id;
|
|---|
| 47 | int ihi;
|
|---|
| 48 | int ihi2;
|
|---|
| 49 | int ilo;
|
|---|
| 50 | int ilo2;
|
|---|
| 51 | int j;
|
|---|
| 52 | int n = N;
|
|---|
| 53 | int proc_num;
|
|---|
| 54 | int solution_num;
|
|---|
| 55 | int solution_num_local1;
|
|---|
| 56 | int thread_num;
|
|---|
| 57 | int value;
|
|---|
| 58 | double wtime;
|
|---|
| 59 |
|
|---|
| 60 | printf ( "\n" );
|
|---|
| 61 | timestamp ( );
|
|---|
| 62 | printf ( "\n" );
|
|---|
| 63 | printf ( "SATISFY_OPENMP\n" );
|
|---|
| 64 | printf ( " C + OpenMP version\n" );
|
|---|
| 65 | printf ( " We have a logical function of N logical arguments.\n" );
|
|---|
| 66 | printf ( " We do an exhaustive search of all 2^N possibilities,\n" );
|
|---|
| 67 | printf ( " seeking those inputs that make the function TRUE.\n" );
|
|---|
| 68 |
|
|---|
| 69 | printf ( "\n" );
|
|---|
| 70 | printf ( " Number of processors available = %d\n", omp_get_num_procs ( ) );
|
|---|
| 71 | printf ( " Number of threads = %d\n", omp_get_max_threads ( ) );
|
|---|
| 72 | /*
|
|---|
| 73 | Compute the number of binary vectors to check.
|
|---|
| 74 | */
|
|---|
| 75 | ilo = 0;
|
|---|
| 76 | ihi = 1;
|
|---|
| 77 | for ( i = 1; i <= n; i++ )
|
|---|
| 78 | {
|
|---|
| 79 | ihi = ihi * 2;
|
|---|
| 80 | }
|
|---|
| 81 | printf ( "\n" );
|
|---|
| 82 | printf ( " The number of logical variables is N = %d\n", n );
|
|---|
| 83 | printf ( " The number of input vectors to check is %d\n", ihi );
|
|---|
| 84 | printf ( "\n" );
|
|---|
| 85 | printf ( " # Processor Index ---------Input Values------------------------\n" );
|
|---|
| 86 | printf ( "\n" );
|
|---|
| 87 | /*
|
|---|
| 88 | Processor ID takes the interval ILO2 <= I < IHI2.
|
|---|
| 89 | Using the formulas below yields a set of nonintersecting intervals
|
|---|
| 90 | which cover the original interval [ILO,IHI).
|
|---|
| 91 | */
|
|---|
| 92 | thread_num = omp_get_max_threads ( );
|
|---|
| 93 |
|
|---|
| 94 | solution_num = 0;
|
|---|
| 95 |
|
|---|
| 96 | wtime = omp_get_wtime ( );
|
|---|
| 97 |
|
|---|
| 98 | # pragma omp parallel \
|
|---|
| 99 | shared ( ihi, ilo, n, thread_num ) \
|
|---|
| 100 | private ( bvec, i, id, ihi2, ilo2, j, solution_num_local1, value ) \
|
|---|
| 101 | reduction ( + : solution_num )
|
|---|
| 102 | {
|
|---|
| 103 | id = omp_get_thread_num ( );
|
|---|
| 104 |
|
|---|
| 105 | ilo2 = ( ( thread_num - id ) * ilo
|
|---|
| 106 | + ( id ) * ihi )
|
|---|
| 107 | / ( thread_num );
|
|---|
| 108 |
|
|---|
| 109 | ihi2 = ( ( thread_num - id - 1 ) * ilo
|
|---|
| 110 | + ( id + 1 ) * ihi )
|
|---|
| 111 | / ( thread_num );
|
|---|
| 112 |
|
|---|
| 113 | printf ( "\n" );
|
|---|
| 114 | printf ( " Processor %8d iterates from %8d <= I < %8d.\n", id, ilo2, ihi2 );
|
|---|
| 115 | printf ( "\n" );
|
|---|
| 116 | /*
|
|---|
| 117 | Check every possible input vector.
|
|---|
| 118 | */
|
|---|
| 119 | solution_num_local1 = 0;
|
|---|
| 120 |
|
|---|
| 121 | for ( i = ilo2; i < ihi2; i++ )
|
|---|
| 122 | {
|
|---|
| 123 | i4_to_bvec ( i, n, bvec );
|
|---|
| 124 |
|
|---|
| 125 | value = circuit_value ( n, bvec );
|
|---|
| 126 |
|
|---|
| 127 | if ( value == 1 )
|
|---|
| 128 | {
|
|---|
| 129 | solution_num_local1 = solution_num_local1 + 1;
|
|---|
| 130 |
|
|---|
| 131 | printf ( " %2d %8d %10d: ", solution_num_local1, id, i );
|
|---|
| 132 | for ( j = 0; j < n; j++ )
|
|---|
| 133 | {
|
|---|
| 134 | printf ( " %d", bvec[j] );
|
|---|
| 135 | }
|
|---|
| 136 | printf ( "\n" );
|
|---|
| 137 | }
|
|---|
| 138 | }
|
|---|
| 139 | solution_num = solution_num + solution_num_local1;
|
|---|
| 140 | }
|
|---|
| 141 | wtime = omp_get_wtime ( ) - wtime;
|
|---|
| 142 | printf ( "\n" );
|
|---|
| 143 | printf ( " Number of solutions found was %d\n", solution_num );
|
|---|
| 144 | printf ( " Elapsed wall clock time (seconds) %f\n", wtime );
|
|---|
| 145 | /*
|
|---|
| 146 | Terminate.
|
|---|
| 147 | */
|
|---|
| 148 | printf ( "\n" );
|
|---|
| 149 | printf ( "SATISFY_OPENMP\n" );
|
|---|
| 150 | printf ( " Normal end of execution.\n" );
|
|---|
| 151 | printf ( "\n" );
|
|---|
| 152 | timestamp ( );
|
|---|
| 153 |
|
|---|
| 154 | return 0;
|
|---|
| 155 | # undef N
|
|---|
| 156 | }
|
|---|
| 157 | /******************************************************************************/
|
|---|
| 158 |
|
|---|
| 159 | int circuit_value ( int n, int bvec[] )
|
|---|
| 160 |
|
|---|
| 161 | /******************************************************************************/
|
|---|
| 162 | /*
|
|---|
| 163 | Purpose:
|
|---|
| 164 |
|
|---|
| 165 | CIRCUIT_VALUE returns the value of a circuit for a given input set.
|
|---|
| 166 |
|
|---|
| 167 | Licensing:
|
|---|
| 168 |
|
|---|
| 169 | This code is distributed under the GNU LGPL license.
|
|---|
| 170 |
|
|---|
| 171 | Modified:
|
|---|
| 172 |
|
|---|
| 173 | 20 March 2009
|
|---|
| 174 |
|
|---|
| 175 | Author:
|
|---|
| 176 |
|
|---|
| 177 | John Burkardt
|
|---|
| 178 |
|
|---|
| 179 | Reference:
|
|---|
| 180 |
|
|---|
| 181 | Michael Quinn,
|
|---|
| 182 | Parallel Programming in C with MPI and OpenMP,
|
|---|
| 183 | McGraw-Hill, 2004,
|
|---|
| 184 | ISBN13: 978-0071232654,
|
|---|
| 185 | LC: QA76.73.C15.Q55.
|
|---|
| 186 |
|
|---|
| 187 | Parameters:
|
|---|
| 188 |
|
|---|
| 189 | Input, int N, the length of the input vector.
|
|---|
| 190 |
|
|---|
| 191 | Input, int BVEC[N], the binary inputs.
|
|---|
| 192 |
|
|---|
| 193 | Output, int CIRCUIT_VALUE, the output of the circuit.
|
|---|
| 194 | */
|
|---|
| 195 | {
|
|---|
| 196 | int value;
|
|---|
| 197 |
|
|---|
| 198 | value =
|
|---|
| 199 | ( bvec[0] || bvec[1] )
|
|---|
| 200 | && ( !bvec[1] || !bvec[3] )
|
|---|
| 201 | && ( bvec[2] || bvec[3] )
|
|---|
| 202 | && ( !bvec[3] || !bvec[4] );
|
|---|
| 203 |
|
|---|
| 204 | return value;
|
|---|
| 205 | }
|
|---|
| 206 | /******************************************************************************/
|
|---|
| 207 |
|
|---|
| 208 | void i4_to_bvec ( int i4, int n, int bvec[] )
|
|---|
| 209 |
|
|---|
| 210 | /******************************************************************************/
|
|---|
| 211 | /*
|
|---|
| 212 | Purpose:
|
|---|
| 213 |
|
|---|
| 214 | I4_TO_BVEC converts an integer into a binary vector.
|
|---|
| 215 |
|
|---|
| 216 | Licensing:
|
|---|
| 217 |
|
|---|
| 218 | This code is distributed under the GNU LGPL license.
|
|---|
| 219 |
|
|---|
| 220 | Modified:
|
|---|
| 221 |
|
|---|
| 222 | 20 March 2009
|
|---|
| 223 |
|
|---|
| 224 | Author:
|
|---|
| 225 |
|
|---|
| 226 | John Burkardt
|
|---|
| 227 |
|
|---|
| 228 | Parameters:
|
|---|
| 229 |
|
|---|
| 230 | Input, int I4, the integer.
|
|---|
| 231 |
|
|---|
| 232 | Input, int N, the dimension of the vector.
|
|---|
| 233 |
|
|---|
| 234 | Output, int BVEC[N], the vector of binary remainders.
|
|---|
| 235 | */
|
|---|
| 236 | {
|
|---|
| 237 | int i;
|
|---|
| 238 |
|
|---|
| 239 | for ( i = n - 1; 0 <= i; i-- )
|
|---|
| 240 | {
|
|---|
| 241 | bvec[i] = i4 % 2;
|
|---|
| 242 | i4 = i4 / 2;
|
|---|
| 243 | }
|
|---|
| 244 |
|
|---|
| 245 | return;
|
|---|
| 246 | }
|
|---|
| 247 | /******************************************************************************/
|
|---|
| 248 |
|
|---|
| 249 | void timestamp ( void )
|
|---|
| 250 |
|
|---|
| 251 | /******************************************************************************/
|
|---|
| 252 | /*
|
|---|
| 253 | Purpose:
|
|---|
| 254 |
|
|---|
| 255 | TIMESTAMP prints the current YMDHMS date as a time stamp.
|
|---|
| 256 |
|
|---|
| 257 | Example:
|
|---|
| 258 |
|
|---|
| 259 | 31 May 2001 09:45:54 AM
|
|---|
| 260 |
|
|---|
| 261 | Licensing:
|
|---|
| 262 |
|
|---|
| 263 | This code is distributed under the GNU LGPL license.
|
|---|
| 264 |
|
|---|
| 265 | Modified:
|
|---|
| 266 |
|
|---|
| 267 | 24 September 2003
|
|---|
| 268 |
|
|---|
| 269 | Author:
|
|---|
| 270 |
|
|---|
| 271 | John Burkardt
|
|---|
| 272 |
|
|---|
| 273 | Parameters:
|
|---|
| 274 |
|
|---|
| 275 | None
|
|---|
| 276 | */
|
|---|
| 277 | {
|
|---|
| 278 | # define TIME_SIZE 40
|
|---|
| 279 |
|
|---|
| 280 | static char time_buffer[TIME_SIZE];
|
|---|
| 281 | const struct tm *tm;
|
|---|
| 282 | size_t len;
|
|---|
| 283 | time_t now;
|
|---|
| 284 |
|
|---|
| 285 | now = time ( NULL );
|
|---|
| 286 | tm = localtime ( &now );
|
|---|
| 287 |
|
|---|
| 288 | len = strftime ( time_buffer, TIME_SIZE, "%d %B %Y %I:%M:%S %p", tm );
|
|---|
| 289 |
|
|---|
| 290 | printf ( "%s\n", time_buffer );
|
|---|
| 291 |
|
|---|
| 292 | return;
|
|---|
| 293 | # undef TIME_SIZE
|
|---|
| 294 | }
|
|---|