PR target/77349
[official-gcc.git] / libgomp / testsuite / libgomp.c / nqueens-1.c
blobdb5517f1233cee70019b38147dcd0c493c1f71f6
1 /* { dg-do run } */
2 /* { dg-require-effective-target tls_runtime } */
4 #include <omp.h>
5 #include <stdio.h>
6 #include <string.h>
7 #include <stdlib.h>
9 int cnt;
10 #pragma omp threadprivate (cnt)
12 void
13 nqueens (char *a, int n, int pos)
15 /* b[i] = j means the queen in i-th row is in column j. */
16 char b[pos + 1];
17 int i, j;
18 memcpy (b, a, pos);
19 for (i = 0; i < n; i++)
21 for (j = 0; j < pos; j++)
22 if (b[j] == i || b[j] == i + pos - j || i == b[j] + pos - j)
23 break;
24 if (j < pos)
25 continue;
26 if (pos == n - 1)
27 /* Found a solution. Could output it here. */
28 ++cnt;
29 else
31 b[pos] = i;
32 #pragma omp task
33 nqueens (b, n, pos + 1);
38 int
39 main (int argc, char **argv)
41 int n = 8;
42 if (argc >= 2)
43 n = strtoul (argv[1], NULL, 0);
44 if (n < 1 || n > 127)
46 fprintf (stderr, "invalid count %d\n", n);
47 return 1;
49 cnt = 0;
50 double stime = omp_get_wtime ();
51 nqueens ("", n, 0);
52 printf ("serial N %d solutions # %d time %f\n", n, cnt, omp_get_wtime () - stime);
53 #pragma omp parallel
54 cnt = 0;
55 stime = omp_get_wtime ();
56 int tempcnt = 0;
57 #pragma omp parallel reduction (+:tempcnt)
59 #pragma omp single
60 nqueens ("", n, 0);
61 tempcnt = cnt;
63 cnt = tempcnt;
64 printf ("parallel N %d solutions # %d time %f\n", n, cnt, omp_get_wtime () - stime);
65 return 0;