Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
remove warning with mc
[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         exit(1);
53       }
54
55       /* Get the neighbors */
56       left  = (rank - 1 + size) % size;
57       right = (rank + 1) % size;
58
59       /* Set the defaults */
60       for (i=0; i<size; i++) {
61           sendcounts[i] = 0;
62           recvcounts[i] = 0;
63           rdispls[i]    = 0;
64           sdispls[i]    = 0;
65       }
66
67       for (length=1; length < 66000; length = length*2+1 ) {
68           /* Get the buffers */
69           sbuf = (int *)malloc( 2 * length * sizeof(int) );
70           rbuf = (int *)malloc( 2 * length * sizeof(int) );
71           if (!sbuf || !rbuf) {
72               fprintf( stderr, "Could not allocate buffers!\n" );
73               MPI_Abort( comm, 1 );
74               exit(1);
75           }
76           
77           /* Load up the buffers */
78           for (i=0; i<length; i++) {
79               sbuf[i]        = i + 100000*rank;
80               sbuf[i+length] = i + 100000*rank;
81               rbuf[i]        = -i;
82               rbuf[i+length] = -i-length;
83           }
84           sendcounts[left]  = length;
85           sendcounts[right] = length;
86           recvcounts[left]  = length;
87           recvcounts[right] = length;
88           rdispls[left]     = 0;
89           rdispls[right]    = length;
90           sdispls[left]     = 0;
91           sdispls[right]    = length;
92       
93           MPI_Alltoallv( sbuf, sendcounts, sdispls, MPI_INT,
94                          rbuf, recvcounts, rdispls, MPI_INT, comm );
95       
96           /* Check rbuf */
97           p = rbuf;          /* left */
98
99           for (i=0; i<length; i++) {
100               if (p[i] != i + 100000 * left) {
101                   if (err < 10) {
102                       fprintf( stderr, "[%d from %d] got %d expected %d for %dth\n", 
103                                rank, left, p[i], i + 100000 * left, i );
104                   }
105                   err++;
106               }
107           }
108
109           p = rbuf + length; /* right */
110           for (i=0; i<length; i++) {
111               if (p[i] != i + 100000 * right) {
112                   if (err < 10) {
113                       fprintf( stderr, "[%d from %d] got %d expected %d for %dth\n", 
114                                rank, right, p[i], i + 100000 * right, i );
115                   }
116                   err++;
117               }
118           }
119
120           free( rbuf );
121           free( sbuf );
122       }
123           
124       free( sdispls );
125       free( rdispls );
126       free( recvcounts );
127       free( sendcounts );
128       MTestFreeComm( &comm );
129     }
130
131     MTest_Finalize( err );
132     MPI_Finalize();
133     return 0;
134 }