MCSR_logo.jpg (56K)
Home My MCSR Supercomputers Software Research Education
Login
Quick Links:

FAQs
Minicamp
Chemistry
insideHPC Blog
Current Jobs
Job History
Mathematica
Training
News
Contact Us

Solutions to MPI Examples 1, 2, & 3

/*Exercise 1 Solution*/

#include < stdio.h >
/* INCLUDE THE mpi.h HEADER FILE */
#include < mpi.h >

int main(int argc, char * argv[])
{

int myid, numprocs;


MPI_Init(&argc,&argv);


MPI_Comm_size(MPI_COMM_WORLD, &numprocs);


MPI_Comm_rank (MPI_COMM_WORLD,&myid);


if (myid == 0)
printf("Hello slaves, my old friends! I am your master .\n");
else
printf("Hello from your humble servant, process #%d\n", myid);

/* CALL MPI_Finalize() TO CLEAN UP PROCESSES */
MPI_Finalize();


}

/*Exercise 2 Solution*/

#include "mpi.h"
#include < stdio.h >
#include < math.h >
#define MAXSIZE 1000

int main(int argc, char *argv [])
{
int myid, numprocs; /* To store current process id and total num processes */
int data[MAXSIZE], i; /* Declara array to hold the numbers to be summed. */
int result; /* Store total sum (sum of each process' partial sum) */
int myresult; /* Store partial sum from summing my portion of array nbrs */
int x = 0; /* Store nbr of ints this process is respbl. for adding */
int low = 0; /* Store index of the first nbr in my portion of array */
int high = 0; /* Store index of the last number in my portion of array */

MPI_Init(&argc,&argv); /* INITIALIZE MPI SESSION */
MPI_Comm_size(MPI_COMM_WORLD,&numprocs); /* TELL MPI # PROCESSES/PROCESSORS TO USE */
MPI_Comm_rank(MPI_COMM_WORLD,&myid); /* FIND OUT WHICH PROCESS I AM */

if (myid == 0) /* IF IM MASTER */
{
for(i = 0; i < MAXSIZE; i++)
data[i] = i; /* INITIALIZE THE ARRAY */
}

/* broadcast data */
MPI_Bcast(data, MAXSIZE, MPI_INT, 0, MPI_COMM_WORLD); /* SEND THE ARRAY */
/* TO ALL PROCESSES */
/* Add my portion Of data */
x = MAXSIZE/numprocs; /* DETERMINE X, the NBR OF NBRS THIS PROCESS NEEDS TO ADD */
low = myid * x; /* DETERMINE THE STARTING NUMBER IN THE DATA ARRAY */
high = low + x; /* DETERMINE THE ENDING NUMBER IN THE DATA ARRAY */


for(i = low; i < high; i++) { /* FOR EACH OF THE NUMBERS IN MY PART OF THE ARRAY */
myresult += data[i]; /* ADD THE NUMBER TO MY RESULT */
}
printf("I am process %d: I start adding at index %d ",myid, low );
printf("and stop at index %d\n", high); /* print my start,stop points */

/* Compute global sum */ /* SUM THE PARTIAL RESULTS */
MPI_Reduce(&myresult, &result, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);

if (myid == 0) printf("The sum is %d.\n", result); /* IF IM THE MASTER */

MPI_Finalize();

return 0;
}

/*Exercise 3 Solution*/


#include "mpi.h"
#include
#include
#define MAXNBR 1000

int main(int argc, char *argv [])
{
int myid, numprocs; /* To store current process id and total num processes */
int numslaves,masterid; /* The number of slave processes */
int buf[1], i; /* Declara buf to store partial sums to be messaged */
int result; /* Store total sum (sum of each process' partial sum) */
int myresult; /* Store partial sum from summing my portion of array nbrs */
int x = 0; /* Store nbr of ints this process is respbl. for adding */
int low = 0; /* Store index of the first nbr in my portion of array */
int high = 0; /* Store index of the last number in my portion of array */
MPI_Status status;

MPI_Init(&argc,&argv); /* INITIALIZE MPI SESSION */
MPI_Comm_size(MPI_COMM_WORLD,&numprocs); /* TELL MPI # PROCESSES/PROCESSORS TO USE */
MPI_Comm_rank(MPI_COMM_WORLD,&myid); /* FIND OUT WHICH PROCESS I AM */

numslaves = numprocs - 1;
masterid = numprocs - 1;

/* JUST LET NON-MASTER NODES ADD THEIR DATA */
if (myid != (numprocs - 1))
{
/* Add my portion of the numbers */
x = MAXNBR/numslaves; /* DETERMINE X, the NBR OF NBRS THIS PROCESS NEEDS TO ADD */
low = myid * x; /* DETERMINE THE NUMBER TO START ADDING WITH */
high = low + x; /* DETERMINE THE LAST NUMBER TO ADD */

for(i = low; i < high; i++) { /* FOR EACH OF THE NUMBERS IN MY PART OF THE SEQUENCE */
myresult += i; /* ADD THE NUMBER TO MY RESULT */
}

printf("I am process %d: I start adding at number %d ",myid, low );
printf("and stop at number %d\n", high); /* print my start,stop points */

/* ADD CODE TO MAKE THE SLAVE NODES SEND RESULTS TO THE MASTER NODE */
buf[0] = myresult;
MPI_Send(buf,1, MPI_INT, masterid, 99, MPI_COMM_WORLD);
}
else


/* ADD CODE TO MAKE THE MASTER NODE RECEIVE THE RESULTS FROM TEH SLAVE NODES */

/* Compute global sum */ /* SUM THE PARTIAL RESULTS */
{
for (i = 0; i < (numprocs -1); i++)
{
printf("Master receiving partial sum from slave #%d =",i );
MPI_Recv(buf,1,MPI_INT, i, 99, MPI_COMM_WORLD, &status);
printf("%d\n",buf[0]);
result += buf[0];
}

printf("The sum is %d.\n", result); /* IF IM THE MASTER */
}

MPI_Finalize();

return 0;
}



Last Modified:June 08, 2007 10:31:55.   Copyright © 1997-2012 The Mississippi Center for Supercomputing Research. All Rights Reserved.   The University of Mississippi
Valid RSS