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 <stdlib.h>
8 #include <stdio.h>
9 #include <mpi.h>
10 
main(int argc,char ** argv)11 int main(int argc, char **argv)
12 {
13     int size;
14     int rank;
15     int * msg = NULL;
16     int msg_sz = 0;
17     int niter = 1;
18     int iter;
19     int i;
20 
21     if (MPI_Init(&argc, &argv) != MPI_SUCCESS)
22     {
23         printf("ERROR: problem with MPI_Init\n"); fflush(stdout);
24     }
25 
26     if (MPI_Comm_size(MPI_COMM_WORLD, &size) != MPI_SUCCESS)
27     {
28 	printf("ERROR: problem with MPI_Comm_size\n"); fflush(stdout);
29     }
30 
31     if (MPI_Comm_rank(MPI_COMM_WORLD, &rank) != MPI_SUCCESS)
32     {
33 	printf("ERROR: problem with MPI_Comm_rank\n"); fflush(stdout);
34     }
35 
36     printf("sr: size %d rank %d\n", size, rank); fflush(stdout);
37 
38     if (size < 2)
39     {
40 	printf("ERROR: needs to be run with at least 2 procs\n");
41 	fflush(stdout);
42     }
43 
44     if (argc > 1)
45     {
46 	sscanf(argv[1], "%d", &msg_sz);
47     }
48 
49     if (argc > 2)
50     {
51 	sscanf(argv[2], "%d", &niter);
52     }
53 
54     if (rank == 0)
55     {
56 	printf("msg_sz=%d, niter=%d\n", msg_sz, niter);
57 	fflush(stdout);
58     }
59 
60     if (msg_sz > 0)
61     {
62 	msg = (int *) malloc(msg_sz * sizeof(int));
63     }
64 
65     if (rank == 0)
66     {
67 	/* usleep(10000); */
68 
69 	for (iter = 0; iter < niter; iter++)
70 	{
71 	    for (i = 0; i < msg_sz; i++)
72 	    {
73 		msg[i] = iter * msg_sz + i;
74 	    }
75 
76 	    if (MPI_Send(msg, msg_sz, MPI_INT, 1, iter, MPI_COMM_WORLD)
77 		!= MPI_SUCCESS)
78 	    {
79 		printf("ERROR: problem with MPI_Send\n"); fflush(stdout);
80 	    }
81 	}
82 
83     }
84     else if (rank == 1)
85     {
86 	MPI_Status status;
87 
88 	for (iter = 0; iter < niter; iter++)
89 	{
90 	    if (MPI_Recv(msg, msg_sz, MPI_INT, 0, iter, MPI_COMM_WORLD,
91 			 &status) != MPI_SUCCESS)
92 	    {
93 		printf("ERROR: problem with MPI_Recv\n"); fflush(stdout);
94 	    }
95 
96 	    for (i = 0; i < msg_sz; i++)
97 	    {
98 		if (msg[i] != iter * msg_sz + i)
99 		{
100 		    printf("ERROR: %d != %d, i=%d iter=%d\n", msg[i],
101 			   iter * msg_sz + i, i, iter);
102 		    fflush(stdout);
103 		    abort();
104 		}
105 	    }
106 	}
107 
108 	printf("All messages successfully received!\n");
109 	fflush(stdout);
110     }
111 
112     printf("sr: process %d finished\n", rank); fflush(stdout);
113 
114     if (MPI_Finalize() != MPI_SUCCESS)
115     {
116         printf("ERROR: problem with MPI_Finalize\n"); fflush(stdout);
117     }
118 
119     return 0;
120 }
121