Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Cleanup.
[simgrid.git] / examples / smpi / sendrecv.c
1 #include "mpi.h"
2 #include <stdio.h>
3
4 static int test(int myid, int numprocs) {
5 // The tags should match on the sender and receiver side.
6 // The distinction between sendtag and recvtag is mainly
7 // useful to make some other Recv or Send calls match the sendrecv. 
8 #define TAG_RCV 999
9 #define TAG_SND 999
10
11
12 #define BUFLEN 10
13     int left, right;
14     int buffer[BUFLEN], buffer2[BUFLEN];
15     int i;
16     MPI_Status status;
17
18     for (i=0;i<BUFLEN;i++) {
19                 buffer[i]=myid;
20     }
21
22     right = (myid + 1) % numprocs;
23     left = myid - 1;
24     if (left < 0)
25         left = numprocs - 1;
26
27     /* performs a right-to-left shift of vectors */ 
28     MPI_Sendrecv(buffer, 10, MPI_INT, left, TAG_SND, buffer2, 10, MPI_INT, right, TAG_RCV, MPI_COMM_WORLD, &status);
29  
30     for (i=0;i<BUFLEN;i++) {
31                 if (buffer2[i]!=((myid+1)%numprocs)) {
32                           fprintf(stderr,"[%d] error: should have values %d, has %d\n",myid,myid-1,buffer[i]);
33                           return(0);
34               }
35     }
36     return(1);
37 }
38  
39 int main(int argc, char *argv[])
40 {
41
42    int myid, numprocs;
43
44     MPI_Init(&argc,&argv);
45     MPI_Comm_size(MPI_COMM_WORLD, &numprocs);
46     MPI_Comm_rank(MPI_COMM_WORLD, &myid);
47
48
49     if (0==myid) 
50                 printf("\n    *** MPI_Sendrecv test ***\n\n");
51
52     if ( test(myid,numprocs)) {
53                 fprintf(stderr,"[%d] ok.\n",myid);
54     }
55     else {
56                 fprintf(stderr,"[%d] failed.\n",myid);
57     }
58
59
60     MPI_Finalize();
61     return 0;
62 }