Logo AND Algorithmique Numérique Distribuée

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