Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
This used to work by accident
[simgrid.git] / teshsuite / smpi / mpich-test / coll / alltoallv.c
1 #include "mpi.h"
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include "test.h"
5
6 /*
7   This program tests MPI_Alltoallv by having processor i send different
8   amounts of data to each processor.
9
10   Because there are separate send and receive types to alltoallv,
11   there need to be tests to rearrange data on the fly.  Not done yet.
12   
13   The first test sends i items to processor i from all processors.
14
15   Currently, the test uses only MPI_INT; this is adequate for testing systems
16   that use point-to-point operations
17  */
18
19 int main( int argc, char **argv )
20 {
21
22     MPI_Comm comm;
23     int      *sbuf, *rbuf;
24     int      rank, size;
25     int      *sendcounts, *recvcounts, *rdispls, *sdispls;
26     int      i, j, *p, err, toterr;
27     
28     MPI_Init( &argc, &argv );
29     err = 0;
30     
31     comm = MPI_COMM_WORLD;
32
33     /* Create the buffer */
34     MPI_Comm_size( comm, &size );
35     MPI_Comm_rank( comm, &rank );
36     sbuf = (int *)malloc( size * size * sizeof(int) );
37     rbuf = (int *)malloc( size * size * sizeof(int) );
38     if (!sbuf || !rbuf) {
39         fprintf( stderr, "Could not allocated buffers!\n" );
40         MPI_Abort( comm, 1 );
41     }
42     
43     /* Load up the buffers */
44     for (i=0; i<size*size; i++) {
45         sbuf[i] = i + 100*rank;
46         rbuf[i] = -i;
47     }
48
49     /* Create and load the arguments to alltoallv */
50     sendcounts = (int *)malloc( size * sizeof(int) );
51     recvcounts = (int *)malloc( size * sizeof(int) );
52     rdispls    = (int *)malloc( size * sizeof(int) );
53     sdispls    = (int *)malloc( size * sizeof(int) );
54     if (!sendcounts || !recvcounts || !rdispls || !sdispls) {
55         fprintf( stderr, "Could not allocate arg items!\n" );
56         MPI_Abort( comm, 1 );
57     }
58     for (i=0; i<size; i++) {
59         sendcounts[i] = i;
60         recvcounts[i] = rank;
61         rdispls[i]    = i * rank;
62         sdispls[i]    = (i * (i+1))/2;
63     }
64     MPI_Alltoallv( sbuf, sendcounts, sdispls, MPI_INT,
65                    rbuf, recvcounts, rdispls, MPI_INT, comm );
66
67     /* Check rbuf */
68     for (i=0; i<size; i++) {
69         p = rbuf + rdispls[i];
70         for (j=0; j<rank; j++) {
71             if (p[j] != i * 100 + (rank*(rank+1))/2 + j) {
72                 fprintf( stderr, "[%d] got %d expected %d for %dth\n",
73                          rank, p[j],(i*(i+1))/2 + j, j );
74                 err++;
75             }
76         }
77     }
78
79     free( sdispls );
80     free( rdispls );
81     free( recvcounts );
82     free( sendcounts );
83     free( rbuf );
84     free( sbuf );
85
86     MPI_Allreduce( &err, &toterr, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD );
87     MPI_Comm_rank( MPI_COMM_WORLD, &rank );
88     if (rank == 0) {
89         if (toterr > 0) 
90             fprintf( stderr, "Test FAILED with %d errors\n", toterr );
91         else
92             fprintf( stderr, " No Errors\n" );
93     }
94         
95     MPI_Finalize();
96     return 0;
97 }