This section shows the source code for prime_omp.c as follows:
1 /* 2 * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All Rights Reserved. 3 * @(#)prime_omp.c 1.3 (Oracle) 10/03/26 4 */ 5 6 #include <stdio.h> 7 #include <math.h> 8 #include <omp.h> 9 10 #define THREADS 4 11 #define N 10000 12 13 int primes[N]; 14 int pflag[N]; 15 16 int is_prime(int v) 17 { 18 int i; 19 int bound = floor(sqrt(v)) + 1; 20 21 for (i = 2; i < bound; i++) { 22 /* no need to check against known composites */ 23 if (!pflag[i]) 24 continue; 25 if (v % i == 0) { 26 pflag[v] = 0; 27 return 0; 28 } 29 } 30 return (v > 1); 31 } 32 33 int main(int argn, char **argv) 34 { 35 int i; 36 int total = 0; 37 38 #ifdef _OPENMP 39 omp_set_dynamic(0); 40 omp_set_num_threads(THREADS); 41 #endif 42 43 for (i = 0; i < N; i++) { 44 pflag[i] = 1; 45 } 46 47 #pragma omp parallel for 48 for (i = 2; i < N; i++) { 49 if ( is_prime(i) ) { 50 primes[total] = i; 51 total++; 52 } 53 } 54 55 printf("Number of prime numbers between 2 and %d: %d\n", 56 N, total); 57 58 return 0; 59 }