Logo AND Algorithmique Numérique Distribuée

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