Question
-> PLEASE READ CAREFULLY =20 then make sure tasks per node is fixed at 20 i n your script when you experiment. 2. Bonus (2
-> PLEASE READ CAREFULLY <-
Write a C program using MPI to implement parallel odd - even sort for the given total values of elements and cores . Your code will support 2 m elements to sort , that is, the number of elements to sort is even . The number of cores p will be chosen such that 2m/p is even . That is, each core will have an even number of elements (this ensures that p hase 1 requires no inter - process communication). The total numbe r of cores p wont necessarily be even, but you are guaranteed that 2m/p is even. Each core will be assigned 2m/p elements. You can either use one large array (of size 2m) that each core will have a copy of, or define arrays of si ze 2m/p for each core. In phase 1, each core will do a compare - exchange on elements that are assigned to it. For example if there a re 4 elements per core (stored in an array A) , then core 0 will compare - exchange A[0] and A[1], then A[2] and A[3] and so on for the other cores. In phase 2, each core will do a compare - exchange on the its own elements but also a compare - ex change between the next core. For example if there are 4 elements per core (stored in a rray A) then core 0 will compare - exchange A[1] and A[ 2], then A[3] and A[4]. Note that A[4] is assigned to core 1 in this case. If there are 12 elements in total and 3 cores, then in phase 2: core 0 will compare - exchange its A[3] with core 1 s A[4], core 1 will co mpare - exchange A[7] with A[8 ] in core 2. You need to generalize this for the scenario where there is an even number of elements and each core has an even number of elements assigned to it. Make sure you start with unsorted arrays. After m rounds of p hase 1 and phase 2 i.e. 2m phases in total, the el ements should be sorted. After this is complete, have each core send their current value s to core 0 (in order) , and have core 0 print the values i n sorted order (see Hello2.c example in slides). Use a for loop to receive values from each core and print as you receive them, this ensures they will be printed in order of rank. Note: if ntasks >=20 then make sure tasks per node is fixed at 20 i n your script when you experiment. 2. Bonus (2 points). Generalize your code in part 1 to take into account an odd number of elements, and possibly an odd number of elements per core. 3. Bonus (2 points). Generalize your code from part 2 to take into account when the number of elements is not divisible by the number of cores i.e. there m ay be a remainder. For example your code should handle when there are 15 elements and 4 cores
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