All pastes #826360 Raw Edit

Something

public text v1 · immutable
#826360 ·published 2007-12-21 04:13 UTC
rendered paste body
/*
 *  mpich5.c
 *  $Id$
 *
 *  Copyright (C) 2007 Andrew Smirnoff
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 *  
 */

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#include "mpi.h"

#define PERPROC 4

int main(int argc, char *argv[])
{
     int numproc;
     int rank;
     int *va;
     int sum1, sum2;
     int buf[PERPROC];
     int i,j,k;
     int res;
     int max;
     MPI_Status status;

     srandom(time(NULL));
     
     MPI_Init(&argc, &argv);
     MPI_Comm_size(MPI_COMM_WORLD, &numproc);
     if (numproc < 3)
     {
	  fprintf(stderr, "Process count must be greater that 3!\n");
	  MPI_Finalize();
	  exit(0);
     }
     MPI_Comm_rank(MPI_COMM_WORLD, &rank);

     if (rank == 0)
     {
	  va = (int *)malloc(numproc * PERPROC * sizeof(int));
	  for (i = 0; i < numproc * PERPROC; i++)
	       va[i] = random() % 100;
	  printf ("va = ");
	  for (i = 0; i < numproc * PERPROC; i++)
	       printf("%d ", va[i]);
	  printf("\n");
	  fflush(stdin);

	  printf("Sending vectors to processes...\n");
	  fflush(stdin);

	  k = 1;
	  for(i = 1 ; i < numproc; i++)
	  {
	       for(j = 0; j < PERPROC; j++)
		    buf[j] = va[i*PERPROC + j];
	       MPI_Send(buf, PERPROC, MPI_INT, i, 0, MPI_COMM_WORLD);
	  }
	  max = va[0];
	  for (i = 1; i < PERPROC; i++)
	  {
	       MPI_Sendrecv(&max, 1, MPI_INT, 1, 0, &res, 1, MPI_INT, numproc - 1, 0, MPI_COMM_WORLD, &status);
	       if (va[i] > res)
		    res = va[i];
	       if (res > max)
		    max = res;
	  }
	  for (i = 0; i < numproc - 1; i++)
	  {
	       MPI_Recv(&res, 1, MPI_INT, numproc - 1, 0, MPI_COMM_WORLD, &status);
	       if (res > max)
		    max = res;
	  }
	  printf("Sending done!\nGetting result...\n");
	  fflush(stdin);
	  sum1 = va[0];
	  for (i = 0; i < numproc * PERPROC; i++)
	       if (sum1 < va[i])
		    sum1 = va[i];
	  printf("Result: %d (%d)\n", max, sum1);
     }
     else
     {
	  MPI_Recv(buf, PERPROC, MPI_INT, 0, 0, MPI_COMM_WORLD, &status);
	  printf("%d: va: ", rank);
	  for (i = 0; i < PERPROC; i++)
	       printf("%d ", buf[i]);
	  printf("\n");
	  fflush(stdout);
	  max = buf[0];
	  for (i = 1; i < PERPROC; i++)
	  {
	       MPI_Sendrecv(&max, 1, MPI_INT, (rank + 1 >= numproc) ? 0 : rank + 1, 0, &res, 1, MPI_INT, rank - 1, 0, MPI_COMM_WORLD, &status);
	       if (buf[i]> res)
		    res = buf[i];
	       if (res > max)
		    max = res;
	  }
	  for (i = 0; i < rank - 1; i++)
	  {
	       MPI_Sendrecv(&max, 1, MPI_INT, (rank + 1 >= numproc) ? 0 : rank + 1, 0, &res, 1, MPI_INT, rank - 1, 0, MPI_COMM_WORLD, &status);
	       if (res > max)
		    max = res;
	  }
	  MPI_Send(&max, 1, MPI_INT, (rank + 1 >= numproc) ? 0 : rank + 1, 0, MPI_COMM_WORLD);
     }
     if (rank == 0)
     	free(va);
     MPI_Finalize();

     return 0;
}