All pastes #182696 Raw Edit

Miscellany

public c v1 · immutable
#182696 ·published 2006-09-26 08:22 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;    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 fromelement %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;}"prog5.c" 91 lines, 1775 characters/oact2/scuola21> mpcc prog5.c1506-507 (W) No licenses available. Contact your program supplier to add additio nal users. Compilation will proceed shortly./oact2/scuola21> poe ./a.out -procs 8 -nodes 1 -hostfile hostfile_sp4319600 +322800 = 642400 on processor #0 of processors #8959600 +962800 = 1922400 on processor #1 of processors #8I'm processor #1 and I have sent my subtotal of 800 elements from element 800 to  element 1599 to processor #01599600 +1602800 = 3202400 on processor #2 of processors #8I'm processor #2 and I have sent my subtotal of 800 elements from element 1600 t o element 2399 to processor #02239600 +2242800 = 4482400 on processor #3 of processors #8I'm processor #3 and I have sent my subtotal of 800 elements from element 2400 t o element 3199 to processor #02879600 +2882800 = 5762400 on processor #4 of processors #8I'm processor #4 and I have sent my subtotal of 800 elements from element 3200 t o element 3999 to processor #03519600 +3522800 = 7042400 on processor #5 of processors #8I'm processor #5 and I have sent my subtotal of 800 elements from element 4000 t o element 4799 to processor #04159600 +4162800 = 8322400 on processor #6 of processors #8I'm processor #6 and I have sent my subtotal of 800 elements from element 4800 t o element 5599 to processor #04799600 +4802800 = 9602400 on processor #7 of processors #8I'm processor #7 and I have sent my subtotal of 800 elements from element 5600 t o element 6399 to processor #0Master processor #0 has all results as following:642400 1922400 3202400 4482400 5762400 7042400 8322400 9602400While receiveing subtotals of 800 elements, processor #0 has computed the total of 6400 (800 * 8 processors) elements from element 0 to element 6399 that is 409 79200/oact2/scuola21> tar -cf prog.tar prog*.c/oact2/scuola21> cat prog5.c#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;}