Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge tag 'v3_9_90' into hypervisor
[simgrid.git] / teshsuite / smpi / mpich3-test / coll / redscatinter.c
1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
2 /*
3  *  (C) 2011 by Argonne National Laboratory.
4  *      See COPYRIGHT in top-level directory.
5  */
6 /* 
7  * Test of reduce scatter with large data on an intercommunicator
8  * (needed in MPICH to trigger the long-data algorithm)
9  *
10  * Each processor contributes its rank + the index to the reduction, 
11  * then receives the ith sum
12  *
13  * Can be called with any number of processors.
14  */
15
16 #include "mpi.h"
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include "mpitest.h"
20
21 int main( int argc, char **argv )
22 {
23     int      err = 0;
24     int      *recvcounts;
25     int      size, rsize, rank, i;
26     int      recvcount, /* Each process receives this much data */
27              sendcount, /* Each process contributes this much data */
28              basecount; /* Unit of elements - basecount *rsize is recvcount, 
29                            etc. */
30     int      isLeftGroup;
31     long long *sendbuf, *recvbuf;
32     long long sumval;
33     MPI_Comm comm;
34
35
36     MTest_Init( &argc, &argv );
37     comm = MPI_COMM_WORLD;
38
39     basecount = 1024;
40
41     while (MTestGetIntercomm( &comm, &isLeftGroup, 2 )) {
42         if (comm == MPI_COMM_NULL) continue;
43
44         MPI_Comm_remote_size( comm, &rsize );
45         MPI_Comm_size( comm, &size );
46         MPI_Comm_rank( comm, &rank );
47
48         if (0) {
49             printf( "[%d] %s (%d,%d) remote %d\n", rank, 
50                     isLeftGroup ? "L" : "R", 
51                     rank, size, rsize );
52         }
53
54         recvcount = basecount * rsize;
55         sendcount = basecount * rsize * size;
56
57         recvcounts = (int *)malloc( size * sizeof(int) );
58         if (!recvcounts) {
59             fprintf( stderr, "Could not allocate %d int for recvcounts\n", 
60                      size );
61             MPI_Abort( MPI_COMM_WORLD, 1 );
62         }
63         for (i=0; i<size; i++) 
64             recvcounts[i] = recvcount;
65         
66         sendbuf = (long long *) malloc( sendcount * sizeof(long long) );
67         if (!sendbuf) {
68             fprintf( stderr, "Could not allocate %d ints for sendbuf\n", 
69                      sendcount );
70             MPI_Abort( MPI_COMM_WORLD, 1 );
71         }
72
73         for (i=0; i<sendcount; i++) {
74             sendbuf[i] = (long long)(rank*sendcount + i);
75         }
76         recvbuf = (long long *)malloc( recvcount * sizeof(long long) );
77         if (!recvbuf) {
78             fprintf( stderr, "Could not allocate %d ints for recvbuf\n", 
79                      recvcount );
80             MPI_Abort( MPI_COMM_WORLD, 1 );
81         }
82         for (i=0; i<recvcount; i++) {
83             recvbuf[i] = (long long)(-i);
84         }
85         
86         MPI_Reduce_scatter( sendbuf, recvbuf, recvcounts, MPI_LONG_LONG, MPI_SUM,
87                             comm );
88
89         /* Check received data */
90         for (i=0; i<recvcount; i++) {
91             sumval = (long long)(sendcount) * (long long)((rsize * (rsize-1))/2) +
92                 (long long)(i + rank * rsize * basecount) * (long long)rsize;
93             if (recvbuf[i] != sumval) {
94                 err++;
95                 if (err < 4) {
96                     fprintf( stdout, "Did not get expected value for reduce scatter\n" );
97                     fprintf( stdout, "[%d] %s recvbuf[%d] = %lld, expected %lld\n",
98                              rank, 
99                              isLeftGroup ? "L" : "R", 
100                              i, recvbuf[i], sumval );
101                 }
102             }
103         }
104         
105         free(sendbuf);
106         free(recvbuf);
107         free(recvcounts);
108
109         MTestFreeComm( &comm );
110     }
111
112     MTest_Finalize( err );
113
114     MPI_Finalize( );
115
116     return 0;
117 }