Created
August 21, 2017 08:49
-
-
Save grvgr/40ee4b043121a48e0cc4557278b3706c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* compile: gcc prime.c -o prime -fopenmp */ | |
#include <stdio.h> | |
#include <time.h> | |
#include <omp.h> | |
int main(int argc, char *argv[]) { | |
double start, end, runTime; | |
int num = 1, primes = 0, limit; | |
start = omp_get_wtime(); | |
limit = argv[1] ? atoi(argv[1]) : 100000; | |
#pragma omp parallel for schedule(dynamic) reduction(+ : primes) | |
for (num = 1; num <= limit; num++) { | |
int i = 2; | |
while(i <= num) { | |
if(num % i == 0) | |
break; | |
i++; | |
} | |
if(i == num) | |
primes++; | |
} | |
end = omp_get_wtime(); | |
runTime = end - start; | |
/* if(argv[1]) */ | |
/* printf("%g\n", runTime); */ | |
/* else */ | |
printf("This machine calculated all %d prime numbers under %d in %g seconds\n", primes, limit, runTime); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment