1 /* -*- Mode: C; c-basic-offset:4 ; -*- */
2 /*
3  *  (C) 2001 by Argonne National Laboratory.
4  *      See COPYRIGHT in top-level directory.
5  */
6 
7 #include "mpi.h"
8 #include <stdio.h>
9 #include <string.h>
10 
11 #define BUFLEN 512
12 
main(int argc,char * argv[])13 int main(int argc, char *argv[])
14 {
15     int myid, numprocs, next, namelen;
16     char buffer[BUFLEN], processor_name[MPI_MAX_PROCESSOR_NAME];
17     MPI_Status status;
18 
19     MPI_Init(&argc,&argv);
20     MPI_Comm_size(MPI_COMM_WORLD,&numprocs);
21     MPI_Comm_rank(MPI_COMM_WORLD,&myid);
22     MPI_Get_processor_name(processor_name,&namelen);
23 
24     fprintf(stderr,"Process %d on %s\n", myid, processor_name);
25     fprintf(stderr,"Process %d of %d\n", myid, numprocs);
26     strcpy(buffer,"hello there");
27     if (myid == numprocs-1)
28 	next = 0;
29     else
30 	next = myid+1;
31 
32     if (myid == 0)
33     {
34         printf("%d sending '%s' \n",myid,buffer);fflush(stdout);
35 	MPI_Send(buffer, strlen(buffer)+1, MPI_CHAR, next, 99, MPI_COMM_WORLD);
36 	printf("%d receiving \n",myid);fflush(stdout);
37 	MPI_Recv(buffer, BUFLEN, MPI_CHAR, MPI_ANY_SOURCE, 99, MPI_COMM_WORLD,
38 		 &status);
39 	printf("%d received '%s' \n",myid,buffer);fflush(stdout);
40 	/* mpdprintf(001,"%d receiving \n",myid); */
41     }
42     else
43     {
44         printf("%d receiving  \n",myid);fflush(stdout);
45 	MPI_Recv(buffer, BUFLEN, MPI_CHAR, MPI_ANY_SOURCE, 99, MPI_COMM_WORLD,
46 		 &status);
47 	printf("%d received '%s' \n",myid,buffer);fflush(stdout);
48 	/* mpdprintf(001,"%d receiving \n",myid); */
49 	MPI_Send(buffer, strlen(buffer)+1, MPI_CHAR, next, 99, MPI_COMM_WORLD);
50 	printf("%d sent '%s' \n",myid,buffer);fflush(stdout);
51     }
52     MPI_Barrier(MPI_COMM_WORLD);
53     MPI_Finalize();
54     return (0);
55 }
56