Lab 2: Lab exercise for MPI Groups and Communicator Management
The attached below is an incomplete C-code. In this lab asignment, you are asked to:
- Read through the program and identify which sections of code correspond to each of the elements of the program structure listed below.
- Insert the appropriate calls to MPI routines at the points indicated.
- Check if there is any error in the code.
- Compile the program.
- Specify how many nodes you wish to use (3).
- Run the program.'
- Check the correctness of your work by comparing the even-ranked output with file even.data and odd-ranked with file odd.data.
There are 2 parts to the project:
- Build 2 groups, one for even-numbered tasks (starting with task 0), one for odd-numbered tasks.
- Do a sum reduction for each group.
The comments in the lab code will direct you towards different solutions.The program structure is as follows:
- Each node determines its rank in MPI_COMM_WORLD
- Two groups/communicators are created -- one consisting of even-ranked tasks and one of odd-ranked tasks. (Lab project)
- Each task calculates a floating-point value xsin
- The sum of xsin is accumulated in task 0 for each new communicator. (Lab project)
- The results for the even-ranked tasks is written to the file even.rep; for the odd-ranked tasks to odd.rep.
void set_group(MPI_Comm[], MPI_Group[], int);
main(int
argc, char **argv)
{
int ntask,
rank_in_world, rank_in_newgrp;
MPI_Comm
newcomm[2];
MPI_Group
newgrp[2];
float xsin,
xsum;
FILE *fpeven,
*fpodd;
MPI_Init(&argc,
&argv);
/*
==================================================================
Establish number of
tasks and rank of this task in MPI_COMM_WORLD
==================================================================
*/
MPI_Comm_size(MPI_COMM_WORLD,&ntask);
MPI_Comm_rank(MPI_COMM_WORLD,&rank_in_world);
/*
==============================
Call routine to set
up groups
==============================
*/
set_group(newcomm,
newgrp, ntask);
xsin =
fabs(sin(151.*(float)(rank_in_world+10)));
/*
==================================================================
Project: Sum
all values of xsin on even-numbered tasks
First:
Learn your rank in this new group (MPI_UNDEFINED if
you are not a member), assign to the variable
rank_in_newgrp
Second:
Members accumulate the sum across the group, and store
it as xsum on the task with rank 0
==================================================================
*/
/*
=====> Check membership here */
if (rank_in_newgrp != MPI_UNDEFINED) {
/* =====> Accumulate sum here */
if (rank_in_newgrp == 0) {
fpeven = fopen("even.rep","w");
fprintf(fpeven,"Sum of even tasks: %12.5f\n",xsum);
fclose (fpeven); }
}
/*
==================================================================
Project: Sum
all values of xsin on odd-numbered tasks
First:
Learn your rank in this new group (MPI_UNDEFINED if
you are not a member), assign to the variable
rank_in_newgrp
Second:
Members accumulate the sum across the group, and store
it as xsum on the task with rank 0
==================================================================
*/
/*
=====> Check membership here */
if (rank_in_newgrp
!= MPI_UNDEFINED) {
/* =====> Accumulate sum here */
if (rank_in_newgrp == 0) {
fpodd = fopen("odd.rep","w");
fprintf(fpodd,"Sum of odd tasks: %12.5f\n",xsum);
fclose (fpodd); }
}
MPI_Finalize();
}
void set_group(MPI_Comm
newcomm[], MPI_Group newgrp[], int ntask)
{
int list[100],
ktr, i;
MPI_Group
base_grp;
/*
==============================================================
Project: Get
base group from MPI_COMM_WORLD communicator
using the variable base_grp to hold the information
==============================================================
*/
/*
=====> Put code here */
/*
================================================================
Project: Build
a group containing even-numbered tasks
and create the corresponding communicator.
Put the new group information in the variable
newgrp[0] and the communicator in variable newcomm[0]
================================================================
*/
ktr = 0;
for(i=0
; i < ntask ; i=i+2) {
list[ktr] = i;
ktr ++;
}
/*
=====> Put code here */
/*
================================================================
Project: Build
a group containing odd-numbered tasks
and create the corresponding communicator.
Put the new group information in the variable
newgrp[1] and the communicator in variable newcomm[1]
================================================================
*/
/*
=====> Put code here */
}