Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote