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

courses.cs.purdue.edu java threads YouTu cs18000 fall 15 labs lab10 [Computer Sc

ID: 3758775 • Letter: C

Question

courses.cs.purdue.edu java threads YouTu cs18000 fall 15 labs lab10 [Computer Science Courses] Run the program again. Do you get the same outpu Putting it All Together For this part, you will be using the concepts in concurrent programming you learned in this lab so far to write a multi-threaded program that finds the total number of values between 1 and 3000 that are divisible by 7 Create a class Divider that can be used to instantiate threads. The class should implement the interface Runnable Divider should have a variable named counter shared by all threads. Each thread should be given a start and end integer when constructed Each thread should check every number from start to end and should increment counter every time it finds a number divisible by 7 Create and start 3 threads to find how many numbers between 1 and 3000 are divisible by 7 as follows: 1. Thread t1 should check from 1 to 1000 2. Thread t2 should check from 1001 to 2000. 3. Thread t3 should check from 2001 to 3000. Use the join method to be sure that all three threads have finished running. Then, print the value of counter. If a problem happens while waiting on the threads to finish, catch an InterruptedException by simply printing "Big problem" Turning in Your Work 1. Open the MATE terminal

Explanation / Answer

omp_prime.c

#include <stdio.h>
#include <math.h>
#include <omp.h>

#define THREADS 4
#define N 3000

int primes[N];
int pflag[N];

int is_prime(int v)
{
int i;
int bound = floor(sqrt ((double)v)) + 1;
  
for (i = 2; i < bound; i++) {
/* No need to check against known composites */
if (!pflag[i])
continue;
if (v % i == 0) {
pflag[v] = 0;
return 0;
}
}
return (v > 1);
}

int main(int argn, char **argv)
{
int i;
int total = 0;

#ifdef _OPENMP
omp_set_num_threads(THREADS);
omp_set_dynamic(0);
#endif

for (i = 0; i < N; i++) {
pflag[i] = 1;
}

#pragma omp parallel for
for (i = 2; i < N; i++) {
if ( is_prime(i) ) {
primes[total] = i;
total++;
}
}
printf("Number of prime numbers between 2 and %d: %d ",
N, total);
for (i = 0; i < total; i++) {
printf("%d ", primes[i]);
}

return 0;
}

pthr_prime.c

#include <stdio.h>
#include <math.h>
#include <pthread.h>

#define THREADS 4
#define N 3000
int primes[N];
int pflag[N];
int total = 0;

int is_prime(int v)
{
int i;
int bound = floor(sqrt ((double)v)) + 1;

for (i = 2; i < bound; i++) {
/* No need to check against known composites */
if (!pflag[i])
continue;
if (v % i == 0) {
pflag[v] = 0;
return 0;
}
}
return (v > 1);
}

void *work(void *arg)
{
int start;
int end;
int i;

start = (N/THREADS) * (*(int *)arg) ;
end = start + N/THREADS;
for (i = start; i < end; i++) {
if ( is_prime(i) ) {
primes[total] = i;
total++;
}
}
return NULL;
}

int main(int argn, char **argv)
{
int i;
pthread_t tids[THREADS-1];

for (i = 0; i < N; i++) {
pflag[i] = 1;
}

for (i = 0; i < THREADS-1; i++) {
pthread_create(&tids[i], NULL, work, (void *)&i);
}

i = THREADS-1;
work((void *)&i);
  
printf("Number of prime numbers between 2 and %d: %d ",
N, total);
for (i = 0; i < total; i++) {
printf("%d ", primes[i]);
}
  
return 0;
}