Question

Write an MPI program, countprimes which will count the number of prime numbers in the numbers...

Write an MPI program, countprimes which will count the number of prime numbers in the numbers from 1 to n inclusive where n is a long integer. The value for n which can be set in the program using a constant should be 50,000. Each process will test its share of the cases. Each process should print out any primes that it finds in a readable manner indicating which process found it and the actual prime. The master process should end with printing a total for the count of the number of primes and the total amount of time taken to find all the primes. Please follow the following instructions carefully before submitting your work: Before submission, make sure you clean up the directories so that no miscellaneous files are kept around in the submission. (Grade would be deducted if useless files are found in the homework directories.) Include the source code and the Makefile in the submission.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

C Program using MPI for the given Question:

#include <math.h>

#include <mpi.h>

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

int main(int argc, char *argv[] );

int prime_number (int n, int id, int p);

void timestamp ();

int main (int argc , char *argv[] )

//purpose: main is the main program for PRIME_MPI.

{

int i,id,err,n, n_factor, n_hi, n_lo, p, primes, primes_part;

double wtime;

n_lo = 1;

n_hi = 262144;

n_factor = 2;

//Initialize MPI.

ierr = MPI_init ( &argc, &argv );

//Get the number of proesses.

ierr = MPI_comm_size ( MPI_COMM_WORLD, &p );

//determine this processes's rank.

ierr = MPI_COMM_RANK (MPI_COMM_WORLD, &id );

if ( id == 0){

timestamp ( );

printf ( "\n" );

printf ( "PRIME_MPI\n" );

printf ( " C/MPI version\n" );

printf ( "\n" );

printf ( "An MPI example program to count number of primes.\n" );

printf ( "The numberof processes is %d\n", p );

printf ( "\n" );

printf ( " N pi Time\n" );

printf ( "\n" );

}

n = n_lo;

while ( n <= n_hi ){

if ( id == 0){

wtime = MPI_Wtime ( );

}

ierr = MPI_Bcast ( &n, 1, MPI_INT, 0, MPI_COMM_WORLD );

primes_part = prime-number ( n, id, p );

ierr = MPI_Reduce ( &primes_part, &primes, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD );

if ( id == 0 ){

wtime = MPI_Wtime ( ) - wtime;

printf ( " %8d %8d %14f\n", n, primes, wtime );

}

n = n * n_factor;

}

//Terminate MPI.

ierr = MPI_Finalize ( );

//Terminate.

if ( id == 0 ){

printf ( "\n" );

printf ( "PRIME_MPI - Master process: \n");

printf ( " Normal end of execution. \n");

timestamp ( );

}

return 0;

}

{

int i;

int j;

int prime;

int total;

total = 0;

for ( i = 2 + id; i <= n; i = i + p ){

prime = 1;

for ( j = 2; j < i; j ++ ){

if ( ( i% j ) == 0 ){

prime = 0;

break;

}

}

total = total + prime;

}

return total;

}b

Add a comment
Know the answer?
Add Answer to:
Write an MPI program, countprimes which will count the number of prime numbers in the numbers...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Prime Number Programing in C Note: The program is to be written using the bitwise operation....

    Prime Number Programing in C Note: The program is to be written using the bitwise operation. Use an integer array (not a Boolean array) and a bit length (for instance 32 bits). In this program you will write a C program to find all prime numbers less than 10,000. You should use 10,000 bits to correspond to the 10,000 integers under test. You should initially turn all bits on (off) and when a number is found that is not prime,...

  • Write a C program which computes the count of the prime and perfect numbers within a...

    Write a C program which computes the count of the prime and perfect numbers within a given limit Land U, representing the lower and upper limit respectively. Prime numbers are numbers that have only 2 factors: 1 and themselves (1 is not prime). An integer number is said to be perfect number if its factors, including 1 (but not the number itself), sum to the number. Sample Inputi: 1 100 Sample Outputs: Prime: 25 Perfect: 2 Sample Input2: 101 10000...

  • Write and test a MIPS assembly language program to compute and display the first prime numbers...

    Write and test a MIPS assembly language program to compute and display the first prime numbers up to n where n is given. Set n to be 19 but the program should work of any value of n. For the program to identify primes, the easiest way is to use the algorithm: for (i = 2; i < x; i++)       if ((x % i) == 0) break;   //break out, not prime where x is the number you are checking...

  • Program Requirements First, find all the prime numbers between 2 and a user-inputted number from the...

    Program Requirements First, find all the prime numbers between 2 and a user-inputted number from the console (inclusive). The identified prime numbers must be stored in an array . The inputted number should be between 0 and 1000 (inclusive). array is large enough to o Make sure your hold all the prim e numbers. Dynamic memory allocation is not required for this assignment, so you can have unused space in your array Make sure you can handle the cases of...

  • Prime Numbers Write a program that will determine whether a set of randomly-generated numbers are prime...

    Prime Numbers Write a program that will determine whether a set of randomly-generated numbers are prime numbers. To write your program, you will need to use the following programming features: The random library (module) A function A variable that will prompt the user to say how many random numbers the program should generate A for loop with a range function A randint function taken from the random library (module); the randint function takes two arguments – the beginning and the...

  • The Sieve of Eratosthenes is a simple, ancient algorithm for finding all prime numbers up to...

    The Sieve of Eratosthenes is a simple, ancient algorithm for finding all prime numbers up to any given limit. It does so by iteratively marking as composite lie., not prime) the multiples of each prime, starting with the multiples of 2 The sieve of Eratosthenes can be expressed in pseudocode, as follows: Input: an integer n Let A be an array of 8oo1ean values, indexed by integers 2 to n, initially all set to true. for t - 2, 3,...

  • JAVA please! Prime numbers are interesting numbers. A prime number is one that is only divisible...

    JAVA please! Prime numbers are interesting numbers. A prime number is one that is only divisible by 1 and itself. For hundreds of years mathematicians have looked for the largest prime number. In the year 1456 the largest known prime was 8191. By the year 1588 it was a 6-digit number: 131,071. The famous mathematician Leonhard Euler discovered the 10-digit number 2,147,483,647 in 1772. Over the years larger and larger primes have been found. There are contests to find the...

  • Homework No.2 CSC 222 Write a shell script program called "countf.sh" that will count how many...

    Homework No.2 CSC 222 Write a shell script program called "countf.sh" that will count how many files or directories recursively. beside the file counts, also report how many files for each types (directory or regular files). It will either take arguments for specific directories and no argument. If no argument, it will read the current working directory as the starting point. Please also provide -h option to print out how to use your countf.sh program % ./countf.sh % ./countf.sh file1...

  • in visual studio build a masm program that prints out the prime numbers in a array...

    in visual studio build a masm program that prints out the prime numbers in a array L1001-Sieve of Eratosthenes Please use your textbook as a reference. Goal: Use what we have learned to generate prime numbers. Prime numbers have many applications in computer science and as such, efficient ways to discover prime numbers can be very useful. Mathematicians have been intrigued by the concept for ages including the Greek mathematician, Eratosthenes of Cyrene (famous for calculating the circumference o the...

  • use simple C++ please Exercise 3: Write a program that reads a positive number and checks...

    use simple C++ please Exercise 3: Write a program that reads a positive number and checks if the number can be expressed as a sum of two prime numbers. If yes, write all possible ways of expressing the number as sum of primes. Note: n is a prime if it is divisible only by 1 and itself, hence not divisible by any integer in the range from 2 to sqrt(n), both inclusive. Write a function is Prime that take a...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT