Skip to content

Instantly share code, notes, and snippets.

@grvgr
Created August 21, 2017 08:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save grvgr/40ee4b043121a48e0cc4557278b3706c to your computer and use it in GitHub Desktop.
Save grvgr/40ee4b043121a48e0cc4557278b3706c to your computer and use it in GitHub Desktop.
/* 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