Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'hypervisor' of git+ssh://scm.gforge.inria.fr//gitroot/simgrid/simgrid...
[simgrid.git] / teshsuite / smpi / mpich3-test / coll / alltoallv0.c
1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
2 /*
3  *  (C) 2001 by Argonne National Laboratory.
4  *      See COPYRIGHT in top-level directory.
5  */
6 #include "mpi.h"
7 #include "mpitest.h"
8 #include <stdlib.h>
9 #include <stdio.h>
10
11 /*
12   This program tests MPI_Alltoallv by having processor each process 
13   send data to two neighbors only, using counts of 0 for the other processes.
14   This idiom is sometimes used for halo exchange operations.
15
16   Because there are separate send and receive types to alltoallv,
17   there need to be tests to rearrange data on the fly.  Not done yet.
18   
19   Currently, the test uses only MPI_INT; this is adequate for testing systems
20   that use point-to-point operations
21  */
22
23 int main( int argc, char **argv )
24 {
25
26     MPI_Comm comm;
27     int      *sbuf, *rbuf;
28     int      rank, size;
29     int      *sendcounts, *recvcounts, *rdispls, *sdispls;
30     int      i, *p, err;
31     int      left, right, length;
32     
33     MTest_Init( &argc, &argv );
34     err = 0;
35     
36     while (MTestGetIntracommGeneral( &comm, 2, 1 )) {
37       if (comm == MPI_COMM_NULL) continue;
38
39       MPI_Comm_size( comm, &size );
40       MPI_Comm_rank( comm, &rank );
41       
42       if (size < 3) continue;
43
44       /* Create and load the arguments to alltoallv */
45       sendcounts = (int *)malloc( size * sizeof(int) );
46       recvcounts = (int *)malloc( size * sizeof(int) );
47       rdispls    = (int *)malloc( size * sizeof(int) );
48       sdispls    = (int *)malloc( size * sizeof(int) );
49       if (!sendcounts || !recvcounts || !rdispls || !sdispls) {
50         fprintf( stderr, "Could not allocate arg items!\n" );
51         MPI_Abort( comm, 1 );
52       }
53
54       /* Get the neighbors */
55       left  = (rank - 1 + size) % size;
56       right = (rank + 1) % size;
57
58       /* Set the defaults */
59       for (i=0; i<size; i++) {
60           sendcounts[i] = 0;
61           recvcounts[i] = 0;
62           rdispls[i]    = 0;
63           sdispls[i]    = 0;
64       }
65
66       for (length=1; length < 66000; length = length*2+1 ) {
67           /* Get the buffers */
68           sbuf = (int *)malloc( 2 * length * sizeof(int) );
69           rbuf = (int *)malloc( 2 * length * sizeof(int) );
70           if (!sbuf || !rbuf) {
71               fprintf( stderr, "Could not allocate buffers!\n" );
72               MPI_Abort( comm, 1 );
73           }
74           
75           /* Load up the buffers */
76           for (i=0; i<length; i++) {
77               sbuf[i]        = i + 100000*rank;
78               sbuf[i+length] = i + 100000*rank;
79               rbuf[i]        = -i;
80               rbuf[i+length] = -i-length;
81           }
82           sendcounts[left]  = length;
83           sendcounts[right] = length;
84           recvcounts[left]  = length;
85           recvcounts[right] = length;
86           rdispls[left]     = 0;
87           rdispls[right]    = length;
88           sdispls[left]     = 0;
89           sdispls[right]    = length;
90       
91           MPI_Alltoallv( sbuf, sendcounts, sdispls, MPI_INT,
92                          rbuf, recvcounts, rdispls, MPI_INT, comm );
93       
94           /* Check rbuf */
95           p = rbuf;          /* left */
96
97           for (i=0; i<length; i++) {
98               if (p[i] != i + 100000 * left) {
99                   if (err < 10) {
100                       fprintf( stderr, "[%d from %d] got %d expected %d for %dth\n", 
101                                rank, left, p[i], i + 100000 * left, i );
102                   }
103                   err++;
104               }
105           }
106
107           p = rbuf + length; /* right */
108           for (i=0; i<length; i++) {
109               if (p[i] != i + 100000 * right) {
110                   if (err < 10) {
111                       fprintf( stderr, "[%d from %d] got %d expected %d for %dth\n", 
112                                rank, right, p[i], i + 100000 * right, i );
113                   }
114                   err++;
115               }
116           }
117
118           free( rbuf );
119           free( sbuf );
120       }
121           
122       free( sdispls );
123       free( rdispls );
124       free( recvcounts );
125       free( sendcounts );
126       MTestFreeComm( &comm );
127     }
128
129     MTest_Finalize( err );
130     MPI_Finalize();
131     return 0;
132 }