All pastes #182697 Raw Edit

Untitled

public c v1 · immutable
#182697 ·published 2006-09-26 08:23 UTC
rendered paste body
#include <stdio.h>#include "mpi.h"#define N 800int main( argc, argv )int  argc;char **argv;{    int ierr;    int rank, size;    MPI_Init( &argc, &argv );    MPI_Comm_size( MPI_COMM_WORLD, &size );    MPI_Comm_rank( MPI_COMM_WORLD, &rank );    int a[size*N];    int b[size*N];    int c[size*N];    for (int i=0;i<size*N;i++)    {     b[i]=i;     c[i]=i+4;    }    int ista,iend;    ista = N * rank;    iend = ista + N;    int A=0,B=0,C=0;    for (int i=ista;i<iend;i++)    {     a[i] = b[i] + c[i];     A+=a[i];     B+=b[i];     C+=c[i];    }    printf("%d +%d = %d on processor #%d of processors #%d\n",B,C,A,rank,size);    int V[size];    int R;    MPI_Status status;    int received;    if (rank == 0) {      received = 1;    }    V[rank]=A;    int T;    if (rank == 0) {      T=V[0];      while (received<size) {      ierr = MPI_Recv(&R,1,MPI_INT,                      MPI_ANY_SOURCE,MPI_ANY_TAG,MPI_COMM_WORLD,                      &status);      V[status.MPI_SOURCE]=R;      received++;      T+=R;      }    } else {      ierr = MPI_Send(&A,1,MPI_INT,0,1,MPI_COMM_WORLD);      printf("I'm processor #%d and I have sent my subtotal of %d elements from element %d to element %d to processor #0\n",rank,N,ista,iend-1);    }    if (rank == 0 ) {      printf("Master processor #0 has all results as following:\n");      for (int i=0;i<size;i++) {        printf("%d ",V[i]);      }      printf("\n");      printf("While receiveing subtotals of %d elements, processor #0 has computed the total of %d (%d * %d processors) elements from element %d to element %d that is %d\n",N,N*size,N,size,0,N*size-1,T);    }    MPI_Finalize();    return 0;}