Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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.

image text in transcribed

Here is my code so far:

#include #include #include #include //looks in the include directory static bool isPrime(long val) { if (val

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!

Parallelize the FOR loop that removes the multiples in the sieve code using a cyclic assignment of iterations to processes. Follow these steps when writing the MPI code 1. Base your code on the serial code from Project l 2. Make the code print "MPI" instead of "serial". 3. Include the MPI header file. 4. Initialize and finalize the MPI library. 5. Every process is allowed to read in the parameters from the command line. 6. All normal program output needs to be produced by process 0 exclusively 7. All processes must allocate the full Boolean array. In addition, process 0 must allocate a second Boolean array of the same size. 8. Execute a barrier just before the start time is taken. 9. very process has to initialize its entire array. 10. Change the FOR loop header to "for (long i- 2 my rank; (i i) top; i comm sz)" to obtain a cyclic assignment. 11. Reduce the results of the local arrays into the second array that only process 0 has. Use "MPI :BOOL" as the data type and the correct operator from Slide Cho4.46. 12. Make sure the verification code checks the proper array 13. Your code must run correctly for different numbers of processes, i e., it must produce the same result as the serial code and pass the verification. 4. Free all dynamically allocated memory before normal program termination. The timed code section should be the same as in the serial code except it also includes the reduc- tion. Dynamic memory allocation and freeing should not be timed, nor should the MPI initializa- tion or finalization. Parallelize the FOR loop that removes the multiples in the sieve code using a cyclic assignment of iterations to processes. Follow these steps when writing the MPI code 1. Base your code on the serial code from Project l 2. Make the code print "MPI" instead of "serial". 3. Include the MPI header file. 4. Initialize and finalize the MPI library. 5. Every process is allowed to read in the parameters from the command line. 6. All normal program output needs to be produced by process 0 exclusively 7. All processes must allocate the full Boolean array. In addition, process 0 must allocate a second Boolean array of the same size. 8. Execute a barrier just before the start time is taken. 9. very process has to initialize its entire array. 10. Change the FOR loop header to "for (long i- 2 my rank; (i i) top; i comm sz)" to obtain a cyclic assignment. 11. Reduce the results of the local arrays into the second array that only process 0 has. Use "MPI :BOOL" as the data type and the correct operator from Slide Cho4.46. 12. Make sure the verification code checks the proper array 13. Your code must run correctly for different numbers of processes, i e., it must produce the same result as the serial code and pass the verification. 4. Free all dynamically allocated memory before normal program termination. The timed code section should be the same as in the serial code except it also includes the reduc- tion. Dynamic memory allocation and freeing should not be timed, nor should the MPI initializa- tion or finalization

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Moving Objects Databases

Authors: Ralf Hartmut Güting, Markus Schneider

1st Edition

0120887991, 978-0120887996

More Books

Students also viewed these Databases questions

Question

Describe the major job evaluation systems.

Answered: 1 week ago

Question

What are the stages of project management? Write it in items.

Answered: 1 week ago