| 1 | # include <stdlib.h>
|
|---|
| 2 | # include <stdio.h>
|
|---|
| 3 | # include <omp.h>
|
|---|
| 4 |
|
|---|
| 5 | int main ( int argc, char *argv[] );
|
|---|
| 6 | void prime_number_sweep ( int n_lo, int n_hi, int n_factor );
|
|---|
| 7 | int prime_number ( int n );
|
|---|
| 8 |
|
|---|
| 9 | /******************************************************************************/
|
|---|
| 10 |
|
|---|
| 11 | int main ( int argc, char *argv[] )
|
|---|
| 12 |
|
|---|
| 13 | /******************************************************************************/
|
|---|
| 14 | /*
|
|---|
| 15 | Purpose:
|
|---|
| 16 |
|
|---|
| 17 | MAIN is the main program for PRIME_OPENMP.
|
|---|
| 18 |
|
|---|
| 19 | Discussion:
|
|---|
| 20 |
|
|---|
| 21 | This program calls a version of PRIME_NUMBER that includes
|
|---|
| 22 | OpenMP directives for parallel processing.
|
|---|
| 23 |
|
|---|
| 24 | Licensing:
|
|---|
| 25 |
|
|---|
| 26 | This code is distributed under the GNU LGPL license.
|
|---|
| 27 |
|
|---|
| 28 | Modified:
|
|---|
| 29 |
|
|---|
| 30 | 06 August 2009
|
|---|
| 31 |
|
|---|
| 32 | Author:
|
|---|
| 33 |
|
|---|
| 34 | John Burkardt
|
|---|
| 35 | */
|
|---|
| 36 | {
|
|---|
| 37 | int n_factor;
|
|---|
| 38 | int n_hi;
|
|---|
| 39 | int n_lo;
|
|---|
| 40 |
|
|---|
| 41 | printf ( "\n" );
|
|---|
| 42 | printf ( "PRIME_OPENMP\n" );
|
|---|
| 43 | printf ( " C/OpenMP version\n" );
|
|---|
| 44 |
|
|---|
| 45 | printf ( "\n" );
|
|---|
| 46 | printf ( " Number of processors available = %d\n", omp_get_num_procs ( ) );
|
|---|
| 47 | printf ( " Number of threads = %d\n", omp_get_max_threads ( ) );
|
|---|
| 48 |
|
|---|
| 49 | n_lo = 1;
|
|---|
| 50 | n_hi = 500;
|
|---|
| 51 | n_factor = 2;
|
|---|
| 52 |
|
|---|
| 53 | prime_number_sweep ( n_lo, n_hi, n_factor );
|
|---|
| 54 |
|
|---|
| 55 | n_lo = 5;
|
|---|
| 56 | n_hi = 500000;
|
|---|
| 57 | n_factor = 10;
|
|---|
| 58 |
|
|---|
| 59 | //prime_number_sweep ( n_lo, n_hi, n_factor );
|
|---|
| 60 | /*
|
|---|
| 61 | Terminate.
|
|---|
| 62 | */
|
|---|
| 63 | printf ( "\n" );
|
|---|
| 64 | printf ( "PRIME_OPENMP\n" );
|
|---|
| 65 | printf ( " Normal end of execution.\n" );
|
|---|
| 66 |
|
|---|
| 67 | return 0;
|
|---|
| 68 | }
|
|---|
| 69 | /******************************************************************************/
|
|---|
| 70 |
|
|---|
| 71 | void prime_number_sweep ( int n_lo, int n_hi, int n_factor )
|
|---|
| 72 |
|
|---|
| 73 | /******************************************************************************/
|
|---|
| 74 | /*
|
|---|
| 75 | Purpose:
|
|---|
| 76 |
|
|---|
| 77 | PRIME_NUMBER_SWEEP does repeated calls to PRIME_NUMBER.
|
|---|
| 78 |
|
|---|
| 79 | Licensing:
|
|---|
| 80 |
|
|---|
| 81 | This code is distributed under the GNU LGPL license.
|
|---|
| 82 |
|
|---|
| 83 | Modified:
|
|---|
| 84 |
|
|---|
| 85 | 06 August 2009
|
|---|
| 86 |
|
|---|
| 87 | Author:
|
|---|
| 88 |
|
|---|
| 89 | John Burkardt
|
|---|
| 90 |
|
|---|
| 91 | Parameters:
|
|---|
| 92 |
|
|---|
| 93 | Input, int N_LO, the first value of N.
|
|---|
| 94 |
|
|---|
| 95 | Input, int N_HI, the last value of N.
|
|---|
| 96 |
|
|---|
| 97 | Input, int N_FACTOR, the factor by which to increase N after
|
|---|
| 98 | each iteration.
|
|---|
| 99 | */
|
|---|
| 100 | {
|
|---|
| 101 | int i;
|
|---|
| 102 | int n;
|
|---|
| 103 | int primes;
|
|---|
| 104 | double wtime;
|
|---|
| 105 |
|
|---|
| 106 | printf ( "\n" );
|
|---|
| 107 | printf ( "TEST01\n" );
|
|---|
| 108 | printf ( " Call PRIME_NUMBER to count the primes from 1 to N.\n" );
|
|---|
| 109 | printf ( "\n" );
|
|---|
| 110 | printf ( " N Pi Time\n" );
|
|---|
| 111 | printf ( "\n" );
|
|---|
| 112 |
|
|---|
| 113 | n = n_lo;
|
|---|
| 114 |
|
|---|
| 115 | while ( n <= n_hi )
|
|---|
| 116 | {
|
|---|
| 117 | wtime = omp_get_wtime ( );
|
|---|
| 118 |
|
|---|
| 119 | primes = prime_number ( n );
|
|---|
| 120 |
|
|---|
| 121 | wtime = omp_get_wtime ( ) - wtime;
|
|---|
| 122 |
|
|---|
| 123 | printf ( " %8d %8d %14f\n", n, primes, wtime );
|
|---|
| 124 |
|
|---|
| 125 | n = n * n_factor;
|
|---|
| 126 | }
|
|---|
| 127 |
|
|---|
| 128 | return;
|
|---|
| 129 | }
|
|---|
| 130 | /******************************************************************************/
|
|---|
| 131 |
|
|---|
| 132 | int prime_number ( int n )
|
|---|
| 133 |
|
|---|
| 134 | /******************************************************************************/
|
|---|
| 135 | /*
|
|---|
| 136 | Purpose:
|
|---|
| 137 |
|
|---|
| 138 | PRIME_NUMBER returns the number of primes between 1 and N.
|
|---|
| 139 |
|
|---|
| 140 | Discussion:
|
|---|
| 141 |
|
|---|
| 142 | A naive algorithm is used.
|
|---|
| 143 |
|
|---|
| 144 | Mathematica can return the number of primes less than or equal to N
|
|---|
| 145 | by the command PrimePi[N].
|
|---|
| 146 |
|
|---|
| 147 | N PRIME_NUMBER
|
|---|
| 148 |
|
|---|
| 149 | 1 0
|
|---|
| 150 | 10 4
|
|---|
| 151 | 100 25
|
|---|
| 152 | 1,000 168
|
|---|
| 153 | 10,000 1,229
|
|---|
| 154 | 100,000 9,592
|
|---|
| 155 | 1,000,000 78,498
|
|---|
| 156 | 10,000,000 664,579
|
|---|
| 157 | 100,000,000 5,761,455
|
|---|
| 158 | 1,000,000,000 50,847,534
|
|---|
| 159 |
|
|---|
| 160 | Licensing:
|
|---|
| 161 |
|
|---|
| 162 | This code is distributed under the GNU LGPL license.
|
|---|
| 163 |
|
|---|
| 164 | Modified:
|
|---|
| 165 |
|
|---|
| 166 | 21 May 2009
|
|---|
| 167 |
|
|---|
| 168 | Author:
|
|---|
| 169 |
|
|---|
| 170 | John Burkardt
|
|---|
| 171 |
|
|---|
| 172 | Parameters:
|
|---|
| 173 |
|
|---|
| 174 | Input, int N, the maximum number to check.
|
|---|
| 175 |
|
|---|
| 176 | Output, int PRIME_NUMBER, the number of prime numbers up to N.
|
|---|
| 177 | */
|
|---|
| 178 | {
|
|---|
| 179 | int i;
|
|---|
| 180 | int j;
|
|---|
| 181 | int prime;
|
|---|
| 182 | int total = 0;
|
|---|
| 183 |
|
|---|
| 184 | # pragma omp parallel \
|
|---|
| 185 | shared ( n ) \
|
|---|
| 186 | private ( i, j, prime )
|
|---|
| 187 |
|
|---|
| 188 |
|
|---|
| 189 | # pragma omp for reduction ( + : total )
|
|---|
| 190 | for ( i = 2; i <= n; i++ )
|
|---|
| 191 | {
|
|---|
| 192 | prime = 1;
|
|---|
| 193 |
|
|---|
| 194 | for ( j = 2; j < i; j++ )
|
|---|
| 195 | {
|
|---|
| 196 | if ( i % j == 0 )
|
|---|
| 197 | {
|
|---|
| 198 | prime = 0;
|
|---|
| 199 | break;
|
|---|
| 200 | }
|
|---|
| 201 | }
|
|---|
| 202 | total = total + prime;
|
|---|
| 203 | }
|
|---|
| 204 |
|
|---|
| 205 | return total;
|
|---|
| 206 | }
|
|---|