Lesson 12

Date: 4/21/2010
Computational clusters
Linux for Engineering and IT applications


Computing Partial Sums

It queries the user for a seed value, then uses this to fill an initial value array which is subsequently broadcast to all processes, computes a local sum using the received initial value array, and sends each sum to the root or master node (the node of rank zero), which computes a cumulative total. Each node prints its local sum, and the root node prints the cumulative total.

#include "mpi.h"
#include <stdio.h>

#define ASIZE 100
#define PI    3.141592653589793238462643

int main(argc, argv)
int argc;
char *argv[];
{
   int rank, numtasks, namelen, rc;
   char processor_name[MPI_MAX_PROCESSOR_NAME];
   int i;
   double seed, init_val[ASIZE], val[ASIZE], sum, tsum;
   MPI_Status status;

   MPI_Init(&argc, &argv);
   MPI_Comm_size(MPI_COMM_WORLD, &numtasks);
   MPI_Comm_rank(MPI_COMM_WORLD, &rank);
   MPI_Get_processor_name(processor_name, &namelen);

   if (rank == 0) {   /* Only the first process in the group */
      printf("Enter some kind of seed value:\n");
      scanf("%lf", &seed);
      for (i = 0; i < ASIZE; i++)
         init_val[i] = (double)i * seed * PI;
   }
    rc = MPI_Bcast(init_val, ASIZE, MPI_DOUBLE, 0, MPI_COMM_WORLD); 
   /* Broadcast computed initial values to all other processes */

    if (rc != MPI_SUCCESS) {
     fprintf(stderr, "Oops! An error occurred in MPI_Bcast()\n");
     MPI_Abort(MPI_COMM_WORLD, rc);
            }


   for (i = 0, sum = 0.0; i < ASIZE; i++) {
      val[i] = init_val[i] * rank;
      sum += val[i];
   }
   printf("%d: My sum is %lf\n", rank, sum);

   /* Send sum back to the first process */
if (rank != 0){/* All processes except the one of rank 0 */
      MPI_Send(&sum, 1, MPI_DOUBLE, 0, 1, MPI_COMM_WORLD);
   }
   else {
      tsum = sum;
      for (i = 1; i < numtasks; i++) {
         MPI_Recv(&sum, 1, MPI_DOUBLE, MPI_ANY_SOURCE, 1,
            MPI_COMM_WORLD, &status);
         tsum += sum;
      }
      printf("%d: Total sum is %lf\n", rank, tsum);
   }

   MPI_Finalize();
}

Sample program output:

Enter some kind of seed value:
35
0: My sum is 0.000000
0: Total sum is 544280.927234
1: My sum is 544280.927234



Take me to the Course Website