2 * Based on the example listed on the following web page:
3 * http://developers.sun.com/sunstudio/downloads/ssx/tha/tha_using.html
12 #include <unistd.h> // getopt()
13 #include "../../drd/drd.h"
16 static int is_prime(int* const pflag
, int v
)
19 int bound
= floor(sqrt ((double)v
)) + 1;
21 for (i
= 2; i
< bound
; i
++)
23 /* No need to check against known composites */
35 int main(int argc
, char **argv
)
47 while ((optchar
= getopt(argc
, argv
, "qt:v")) != EOF
)
55 num_threads
= atoi(optarg
);
61 fprintf(stderr
, "Error: unknown option '%c'.\n", optchar
);
66 if (optind
+ 1 != argc
)
68 fprintf(stderr
, "Error: wrong number of arguments.\n");
71 n
= atoi(argv
[optind
]);
73 // Not the most user-friendly way to do error checking, but better than
76 assert(num_threads
>= 1);
78 primes
= malloc(n
* sizeof(primes
[0]));
79 pflag
= malloc(n
* sizeof(pflag
[0]));
81 omp_set_num_threads(num_threads
);
84 for (i
= 0; i
< n
; i
++) {
91 #pragma omp parallel for
92 for (i
= 2; i
< n
; i
++)
94 if (is_prime(pflag
, i
))
102 printf("Number of prime numbers between 2 and %d: %d\n",
104 for (i
= 0; i
< total
; i
++)
106 printf("%d\n", primes
[i
]);