Convert the following MPI program to a sequent program
$10-30 USD
Closed
Posted about 1 year ago
$10-30 USD
Paid on delivery
Write the program to behave like:
How many processes?
5
Then make a shared memory location of type int of the size of: 5
The function (like f) that would be the one parameter of m_fork() should do the following:
Generates a random integer.
Check to see:
if it is a prime number, saves the number in the array that its index is the rank of the process.
If it is not a prime number, saves its negate in in the array that its index is the rank of the process.
Now the process of rank zero prints the array such that:
if the content of a location of the array is positive (like n), outputs n is a prime number.
if the content of a location of the array is negative (like -n) outputs n is not a prime number.
Note when process with rank zero is going to print the array, all the processes (including rank zero) have saved their number to array.
Use the file sequent.h given and then convert the given mpi code to sequent
//File name: prime.c
#include <mpi.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int isPrime(int n){
int i;
for(i = 2; i < n; i++)
if(n % i == 0)
return 0;
return 1;
}
int main(){
int comm_sz, my_rank, q, n;
MPI_Init(NULL, NULL);
MPI_Comm_size(MPI_COMM_WORLD, &comm_sz);
MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
srandom((unsigned)time(NULL) + my_rank);
n = 1 + random()%10000; //The program should work for any positive number.
if(isPrime(n))
MPI_Send(&n, 1, MPI_INT, 0, 0, MPI_COMM_WORLD);
else{
n = -n;
MPI_Send(&n, 1, MPI_INT, 0, 0, MPI_COMM_WORLD);
}
//To make sure all the processes obtained a number, and sent either n or -n to process 0
MPI_Barrier(MPI_COMM_WORLD);
if(my_rank == 0)
for (q = 0; q < comm_sz; q++)
{
MPI_Recv(&n, 1, MPI_INT, q, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
if(n < 0)
printf("Receiving from process: %d, %d is NOT a prime number\n", q, -n);
else
printf("Receiving from process: %d, %d is a prime number\n", q, n);
}
MPI_Finalize();
return 0;
}
Hello, my intention is to be able to help each other and dedicate a large part of my time to the project in order to satisfy the proposed needs, in addition to striving to deliver it as quickly as possible.
This program first creates a memory space with 5 elements. Then it generates an integer for each operation and checks if that integer is prime. If the integer is prime, it is assigned to the index of that process in the stored memory area. If the integer is prime, it is assigned a negative value.
Next, the sequential program checks the units in the memory segments, outputting "x is NOT a prime number" if the element is negative. If the element is positive it will output "x is a prime number" I can turn this code into a sequential program if you are interested and contact