Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
More fixes to VPATH testing
[simgrid.git] / examples / smpi / sendrecv.c
1 #include "mpi.h"
2 #include <stdio.h>
3
4 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_Request request;
17     MPI_Status status;
18
19     for (i=0;i<BUFLEN;i++) {
20                 buffer[i]=myid;
21     }
22
23     right = (myid + 1) % numprocs;
24     left = myid - 1;
25     if (left < 0)
26         left = numprocs - 1;
27
28     /* performs a right-to-left shift of vectors */ 
29     MPI_Sendrecv(buffer, 10, MPI_INT, left, TAG_SND, buffer2, 10, MPI_INT, right, TAG_RCV, MPI_COMM_WORLD, &status);
30  
31     for (i=0;i<BUFLEN;i++) {
32                 if (buffer2[i]!=((myid+1)%numprocs)) {
33                           fprintf(stderr,"[%d] error: should have values %d, has %d\n",myid,myid-1,buffer[i]);
34                           return(0);
35               }
36     }
37     return(1);
38 }
39  
40 int main(int argc, char *argv[])
41 {
42
43    int myid, numprocs;
44
45     MPI_Init(&argc,&argv);
46     MPI_Comm_size(MPI_COMM_WORLD, &numprocs);
47     MPI_Comm_rank(MPI_COMM_WORLD, &myid);
48
49
50     if (0==myid) 
51                 printf("\n    *** MPI_Sendrecv test ***\n\n");
52
53     if ( test(myid,numprocs)) {
54                 fprintf(stderr,"[%d] ok.\n",myid);
55     }
56     else {
57                 fprintf(stderr,"[%d] failed.\n",myid);
58     }
59
60
61     MPI_Finalize();
62     return 0;
63 }