Next Up Previous Contents Index
A program

1 A program

language=C,escapechar=!

This program computes the maimum of all prefixes of given sequence. It is assumed that sequence is read from .

The program is implemented using the protocol. The following libraries are used by the program: stdio, stdlib, mpi.

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

A macro is defined to select the greatest of two numbers.

#define !!(x,y) (x<y?y:x)

The main program is

int main(int argc, char *argv[]) int rank,size; int v,oldv,j,*vec; MPIStatus st;

MPIInit(argc, argv); oldv=v = rand(); MPICommrank(MPICOMMWORLD,rank); MPICommsize(MPICOMMWORLD,size); vec=(int *) malloc(size*sizeof(int));

A set of n numbers (where n is the number of available processors) is read by process 0 and scattered through all processes.

if (rank==0) for (j=0;j<size;j++) vec[j]=rand() MPIScatter(vec,1,MPIINT, v,1,MPIINT,0,MPICOMMWORLD);

oldv=v; printf("start: if (rank<size-1) MPISend(v,1,MPIINT, rank+1,1,MPICOMMWORLD);

for (j=1;j<size;j*=2) int l; printf(" if (rank-j>=0) MPIRecv(l,1,MPIINT,rank-j,1,MPICOMMWORLD,st); v=max(v,l); if (j+rank<size) MPISend(v,1,MPIINT,rank+j,1,MPICOMMWORLD); printf("rank

MPIFinalize();

Identifiers


Next Up Previous Contents Index