Compute the following code so that you are able to the runtime of the parallel l
ID: 3910367 • Letter: C
Question
Compute the following code so that you are able to the runtime of the parallel loop using omp_get_wtime
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <omp.h>
int is_prime(int n);
int main(int argc, char** argv){
int n=10000000;
int numprimes = 0;
int i;
#pragma parallel
for (i = 1; i <= n; i++)
{
if (is_prime(i) == 1)
numprimes ++;
}
printf("Number of Primes: %d ",numprimes);
}
int is_prime(int n)
{
/* handle special cases */
if (n == 0) return 0;
else if (n == 1) return 0;
else if (n == 2) return 1;
int i;
for(i=2;i<=(int)(sqrt((double) n));i++)
if (n%i==0) return 0;
return 1;
}
Explanation / Answer
/*The code modification is as follows:
Compilation command
gcc file.c -lm -lgomp
*/
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <omp.h>
int is_prime(int n);
int main(int argc, char** argv){
int n=10000000;
int numprimes = 0;
int i;
double start_time = omp_get_wtime();
#pragma parallel
for (i = 1; i <= n; i++)
{
if (is_prime(i) == 1)
numprimes ++;
}
double time = omp_get_wtime() - start_time;
printf("%lf ",time);
printf("Number of Primes: %d ",numprimes);
}
int is_prime(int n)
{
/* handle special cases */
if (n == 0) return 0;
else if (n == 1) return 0;
else if (n == 2) return 1;
int i;
for(i=2;i<=(int)(sqrt((double) n));i++)
if (n%i==0) return 0;
return 1;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.