source: CIVL/examples/omp/satisfy_openmp.c@ ea8d8ce

1.23 2.0 main test-branch
Last change on this file since ea8d8ce 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: 6.8 KB
Line 
1# include <stdlib.h>
2# include <stdio.h>
3# include <time.h>
4# include <omp.h>
5
6int main ( int argc, char *argv[] );
7int circuit_value ( int n, int bvec[] );
8void i4_to_bvec ( int i4, int n, int bvec[] );
9void timestamp ( void );
10
11/******************************************************************************/
12
13int 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 23
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_local;
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_local, 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_local = 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_local = solution_num_local + 1;
130
131 printf ( " %2d %8d %10d: ", solution_num_local, 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_local;
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
159int 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 && ( bvec[4] || !bvec[5] )
204 && ( bvec[5] || !bvec[6] )
205 && ( bvec[5] || bvec[6] )
206 && ( bvec[6] || !bvec[15] )
207 && ( bvec[7] || !bvec[8] )
208 && ( !bvec[7] || !bvec[13] )
209 && ( bvec[8] || bvec[9] )
210 && ( bvec[8] || !bvec[9] )
211 && ( !bvec[9] || !bvec[10] )
212 && ( bvec[9] || bvec[11] )
213 && ( bvec[10] || bvec[11] )
214 && ( bvec[12] || bvec[13] )
215 && ( bvec[13] || !bvec[14] )
216 && ( bvec[14] || bvec[15] )
217 && ( bvec[14] || bvec[16] )
218 && ( bvec[17] || bvec[1] )
219 && ( bvec[18] || !bvec[0] )
220 && ( bvec[19] || bvec[1] )
221 && ( bvec[19] || !bvec[18] )
222 && ( !bvec[19] || !bvec[9] )
223 && ( bvec[0] || bvec[17] )
224 && ( !bvec[1] || bvec[20] )
225 && ( !bvec[21] || bvec[20] )
226 && ( !bvec[22] || bvec[20] )
227 && ( !bvec[21] || !bvec[20] )
228 && ( bvec[22] || !bvec[20] );
229
230 return value;
231}
232/******************************************************************************/
233
234void i4_to_bvec ( int i4, int n, int bvec[] )
235
236/******************************************************************************/
237/*
238 Purpose:
239
240 I4_TO_BVEC converts an integer into a binary vector.
241
242 Licensing:
243
244 This code is distributed under the GNU LGPL license.
245
246 Modified:
247
248 20 March 2009
249
250 Author:
251
252 John Burkardt
253
254 Parameters:
255
256 Input, int I4, the integer.
257
258 Input, int N, the dimension of the vector.
259
260 Output, int BVEC[N], the vector of binary remainders.
261*/
262{
263 int i;
264
265 for ( i = n - 1; 0 <= i; i-- )
266 {
267 bvec[i] = i4 % 2;
268 i4 = i4 / 2;
269 }
270
271 return;
272}
273/******************************************************************************/
274
275void timestamp ( void )
276
277/******************************************************************************/
278/*
279 Purpose:
280
281 TIMESTAMP prints the current YMDHMS date as a time stamp.
282
283 Example:
284
285 31 May 2001 09:45:54 AM
286
287 Licensing:
288
289 This code is distributed under the GNU LGPL license.
290
291 Modified:
292
293 24 September 2003
294
295 Author:
296
297 John Burkardt
298
299 Parameters:
300
301 None
302*/
303{
304# define TIME_SIZE 40
305
306 static char time_buffer[TIME_SIZE];
307 const struct tm *tm;
308 size_t len;
309 time_t now;
310
311 now = time ( NULL );
312 tm = localtime ( &now );
313
314 len = strftime ( time_buffer, TIME_SIZE, "%d %B %Y %I:%M:%S %p", tm );
315
316 printf ( "%s\n", time_buffer );
317
318 return;
319# undef TIME_SIZE
320}
Note: See TracBrowser for help on using the repository browser.