Question
I am in Parallel Programming and was assigned to convert a serial code into mpi. The program is sieve of Eratosthenes and can find all
I am in Parallel Programming and was assigned to convert a serial code into mpi. The program is sieve of Eratosthenes and can find all primes within a given limit. I am having a difficult time getting my sieve_mpi.cpp to compile and run correctly in mpi. I believe I have divided the problem correctly between processes and think that my major problem is something to do with my reduction. So I have two arrays (array,array2); array is allocated to all processes, while array2 is allocated only to process 0. So after removing the multiples from array, I want to use MPI_Reduce to place all values on array2 that only process 0 has access to. I don't clearly understand on what reduce operation I could use in the parameters that will reduce an array boolean elements. Im certain I would use one of the logical operations but I don't understand how it would reduce. I also asked my professor if he could help but all he said was that "all processes have to initialize the entire array. You are not using a cyclic assignment. You allocate too much memory. You dont neet the '&' before the array names. The reduction should not be in a loop"
Here are the requirements to complete.
Here is my code so far:
#include int main(int argc, char *argv[]) { int comm_sz; int my_rank; MPI_Init(NULL,NULL); //initializes MPI MPI_Comm_size(MPI_COMM_WORLD, &comm_sz); MPI_Comm_rank(MPI_COMM_WORLD, &my_rank); if (my_rank == 0){ printf("Prime Sieve v1.0 [MPI] "); //changed "serial" to "MPI" } // check command line if (argc != 2) {fprintf(stderr, "usage: %s maximum ", argv[0]); exit(-1);} const long top = atol(argv[1]); if (top const int my_start = my_rank * (long)top / comm_sz; const int my_end = (my_rank+1) * (long)top / comm_sz; // allocate array bool* array = new bool[top]; // start time MPI_Barrier(MPI_COMM_WORLD);//barrier implemented before time start timeval start, end; gettimeofday(&start, NULL); // initialize array for (long i = 2 + my_rank; (i*i) // remove multiples for (long i = my_start; (i * i) // reduction MPI_Reduce(array,array2,1,MPI::BOOL,MPI_LXOR,0,MPI_COMM_WORLD);/ot sure on the operator or the for loo // end time gettimeofday(&end, NULL); double runtime = end.tv_sec + end.tv_usec / 1000000.0 - start.tv_sec - start.tv_usec / 1000000.0; printf("compute time: %.4f s ", runtime); // print part of result from array2 if (my_rank == 0){ for (long i = 2; i // verify result if (top Thanks!
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started