Hello I am stuck on this problem Can you help me out Everything is one question
ID: 3860505 • Letter: H
Question
Hello I am stuck on this problem Can you help me out Everything is one question and the template is included Can you make the program work So I can show my colleagues how is done Not 3 of us could figure it out lol Please code simple as possible Lab for Chapter 5 -Pipelined Insertion Sort This is the lab exercise for Chapter 5-Pipelined Computation. The task is to implement pipelined insertion sort using MPI. The guidelines for the implementation include the following: 1. Use one process to implement one function unit in the pipeline. Communications between processes must follow the pipeline structure, i.e., process i only communicates with process i-1 and process i+1 where Ocixp-1, process O only communicates with process 1 and process p-1 only communicates with process p-2, etc Use the master process, Process 0, to generate random numbers to sort and start the sorting process. The number of numbers to sort should be equal to the number of processes. Process 0 should use a dynamically allocated array to store generated numbers. Process O prints the generated numbers before the sorting starts. After sorting, numbers must be transferred back to Process O using the pipeline structure and Process 0 prints out the sorted numbers. 2. 3. A template of the program is provided. Demonstrate your program in the class.
Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "mpi.h"
int main(int argc, char* argv[])
{
MPI_Init(&argc, &argv);
int process_rank;
int process_count;
MPI_Comm_rank(MPI_COMM_WORLD, &process_rank); // Find process rank
MPI_Comm_size(MPI_COMM_WORLD, &process_count); // Stores the number of processes
int maxNumber = 100; // Range of numbers to generate
int listSize = process_count; // Quantity of numbers to generate
int runs = 0;
int numberToHold;
int numberToPass;
int tempNumber;
int sender = process_rank - 1;
int receiver = process_rank + 1;
int* theList = malloc(sizeof(int) * listSize);
// Generate random numbers in process 0 to fill the list and to be sorted
if(process_rank == 0)
{
printf(" Generating %d random numbers, ranging from 0 to %d.", listSize, maxNumber - 1);
printf(" The unsorted list is: ");
srand(time(NULL));
for(runs = 0; runs < listSize; runs++)
{
theList[runs] = rand() % maxNumber;
printf("%d ", theList[runs]);
}
}
// In each process, pass along a number if it is less than the current one it is holding.
for(runs = 0; runs < process_count - process_rank; runs++)
{
if(process_rank == 0)
{
if(runs == 0)
numberToHold = theList[runs];
else
{
numberToPass = theList[runs];
if(numberToHold < numberToPass)
{
tempNumber = numberToHold;
numberToHold = numberToPass;
numberToPass = tempNumber;
}
MPI_Send(&numberToPass, 1, MPI_INT, receiver, 0, MPI_COMM_WORLD);
}
}
else
{
if(runs == 0)
MPI_Recv(&numberToHold, 1, MPI_INT, sender, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
else
{
MPI_Recv(&numberToPass, 1, MPI_INT, sender, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
if(numberToHold < numberToPass)
{
tempNumber = numberToHold;
numberToHold = numberToPass;
numberToPass = tempNumber;
}
MPI_Send(&numberToPass, 1, MPI_INT, receiver, 0, MPI_COMM_WORLD);
}
}
}
numberToPass = 0;
if(process_rank == 0)
printf(" The numbers in order, sorted by the pipelined insertion sort are: ");
// Pipeline all numbers back to process 0 to print.
for(runs = 0; runs < process_count - process_rank; runs++)
{
if(process_rank == 0)
{
if(runs == 0)
{
printf("%d ", numberToHold);
}
else
{
MPI_Recv(&numberToPass, 1, MPI_INT, receiver, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
printf("%d ", numberToPass);
}
}
else
{
if(runs == 0)
MPI_Send(&numberToHold, 1, MPI_INT, sender, 0, MPI_COMM_WORLD);
else
{
MPI_Recv(&numberToPass, 1, MPI_INT, receiver, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
MPI_Send(&numberToPass, 1, MPI_INT, sender, 0, MPI_COMM_WORLD);
}
}
}
if(process_rank == 0)
printf(" ");
MPI_Finalize();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.