Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

A. You are to write a parallel program using MPI in which the mean value of a Ma

ID: 3840413 • Letter: A

Question

A. You are to write a parallel program using MPI in which the mean value of a Matrix is to be calculated. The full matrix is residing in the memory of process 0, and the mean value need to be calculated and made available at all executing processes. Do not write the program but state the sequence of all MPI calls needed in your program in the sequence by which they should appear. You do not have to write the full call with all of its parameters, just the name of the call.

B.

i)   Does the following code have a deadlock? _____________

Why? _______________________      (4 pts)

ii) If you answer is “Yes”, then rewrite the code using the same MPI calls below to fix the problem.

MPI_Comm_rank(MPI_COMM_WORLD, &myrank);

if (myrank == 0) {

    MPI_Send(a, 10, MPI_INT, 1, 1, MPI_COMM_WORLD);

    MPI_Send(b, 10, MPI_INT, 1, 2, MPI_COMM_WORLD);

}

else if (myrank == 1) {

    MPI_Recv(b, 10, MPI_INT, 0, 2, MPI_COMM_WORLD);

    MPI_Recv(a, 10, MPI_INT, 0, 1, MPI_COMM_WORLD);

}

Explanation / Answer

int MPI_Send(void* message /* in */,
int count /* in */,
MPI_Datatype datatype /* in */,
int dest /* in */,
int tag /* in */,
MPI_Comm comm /* in */)
int MPI_Recv(void* message /* out */,
int count /* in */,
MPI_Datatype datatype /* in */,
int source /* in */,
int tag /* in */,
MPI_Comm comm /* in */,
MPI_Status* status /* out */)
19
Message Bodies
“void* message”: the starting location in memory where the data is to be
found
“int count “: number of items to be sent.
“MPI_Datatype datatype ”: the type of data to be sent.
20
MPI Datatypes
MPI defines its own data type that
correspond to typical datatypes in C or
Fortran
This allows to code to be portable
between systems
Users are allowed to build their own
datatypes in MPI
MPI Datatype C Datatype
MPI_CHAR signed char
MPI_SHORT signed short int
MPI_INT signed int
MPI_LONG Signed long int
MPI_UNSIGNED_CHAR unsigned char
MPI_UNSIGNED_SHORT unsigned short int
MPI_UNSIGNED unsigned int
MPI_FLOAT Float
MPI_DOUBLE Double
MPI_LONG_DOUBLE Long double

Message Envelope
What else is needed for A to send a message to B in a communicator?
Example. Process A can send both floats to be printed and floats to be
stored. How is process B to distinguish between the two different
types?
• We now know where to deliver and where to get message, number of elements
in the message and their type, and destination and source IDs.
• Additionally, we also use a message identifier “tag”.
-- It allows program to label classes of messages (e.g. one for printing data,
another for storing data, etc.)
-- A tag is an int specified by the programmer that the system adds to the
message envelope.
-- MPI guarantees that the integers 0 – 32767 can be used as tags.
21
Blocking vs. Non-Blocking Communication
Blocking: blocking send or receive routines does not
return until operation is complete.
-- blocking sends ensure that it is safe to overwrite the
sent data
-- blocking receives make sure that the data has arrived
and is ready for use
Non-blocking: Non-blocking send or receive routines
returns immediately, with no information about
completion.
-- User should test for success or failure of communication.
-- In between, the process is free to handle other tasks.
-- It is less likely to form deadlocking code
-- It is used with MPI_Wait() or MPI_Test()

Type of Communication MPI Function
blocking send MPI_Send
non-blocking send MPI_Isend
blocking receive MPI_Recv
non-blocking receive MPI_Irecv
C Code
#include <stdio.h>
#include "mpi.h"
main(int argc, char** argv)
{
int my_rank, numbertoreceive, numbertosend=77;
MPI_Status status;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
if (my_rank==0){
MPI_Recv( &numbertoreceive, 1, MPI_INT, MPI_ANY_SOURCE,
MPI_ANY_TAG, MPI_COMM_WORLD, &status);
printf("Number received is: %d ", numbertoreceive);
}
else if(my_rank == 1)
MPI_Send( &numbertosend, 1, MPI_INT, 0, 10, MPI_COMM_WORLD);
MPI_Finalize();
}
Goal: Process 0 sends a number 77 to process 1.
24
MPI_Send( &numbertosend, 1, MPI_INT, 0, 10, MPI_COMM_WORLD)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote