Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'mc-perf' into mc
[simgrid.git] / teshsuite / smpi / mpich3-test / coll / redscat3.c
1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
2 /*
3  *  (C) 2010 by Argonne National Laboratory.
4  *      See COPYRIGHT in top-level directory.
5  */
6 /* 
7  * Test of reduce scatter with large data (needed in MPICH to trigger the
8  * 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 /* Limit the number of error reports */
22 #define MAX_ERRORS 10
23
24 int main( int argc, char **argv )
25 {
26     int      err = 0;
27     int      *sendbuf, *recvbuf, *recvcounts;
28     int      size, rank, i, j, idx, mycount, sumval;
29     MPI_Comm comm;
30
31
32     MTest_Init( &argc, &argv );
33     comm = MPI_COMM_WORLD;
34
35     MPI_Comm_size( comm, &size );
36     MPI_Comm_rank( comm, &rank );
37     recvcounts = (int *)malloc( size * sizeof(int) );
38     if (!recvcounts) {
39         fprintf( stderr, "Could not allocate %d ints for recvcounts\n", 
40                  size );
41         MPI_Abort( MPI_COMM_WORLD, 1 );
42         exit(1);
43     }
44     mycount = (1024 * 1024) / size;
45     for (i=0; i<size; i++) 
46         recvcounts[i] = mycount;
47     sendbuf = (int *) malloc( mycount * size * sizeof(int) );
48     if (!sendbuf) {
49         fprintf( stderr, "Could not allocate %d ints for sendbuf\n", 
50                  mycount * size );
51         MPI_Abort( MPI_COMM_WORLD, 1 );
52         exit(1);
53     }
54     idx = 0;
55     for (i=0; i<size; i++) {
56         for (j=0; j<mycount; j++) {
57             sendbuf[idx++] = rank + i;
58         }
59     }
60     recvbuf = (int *)malloc( mycount * sizeof(int) );
61     if (!recvbuf) {
62         fprintf( stderr, "Could not allocate %d ints for recvbuf\n", 
63                  mycount );
64         MPI_Abort( MPI_COMM_WORLD, 1 );
65         exit(1);
66     }
67     for (i=0; i<mycount; i++) {
68         recvbuf[i] = -1;
69     }
70
71     MPI_Reduce_scatter( sendbuf, recvbuf, recvcounts, MPI_INT, MPI_SUM, comm );
72
73     sumval = size * rank + ((size - 1) * size)/2;
74     /* recvbuf should be size * (rank + i) */
75     for (i=0; i<mycount; i++) {
76         if (recvbuf[i] != sumval) {
77             err++;
78             if (err < MAX_ERRORS) {
79                 fprintf( stdout, "Did not get expected value for reduce scatter\n" );
80                 fprintf( stdout, "[%d] Got recvbuf[%d] = %d expected %d\n",
81                          rank, i, recvbuf[i], sumval );
82             }
83         }
84     }
85
86     MPI_Reduce_scatter( MPI_IN_PLACE, sendbuf, recvcounts, MPI_INT, MPI_SUM, 
87                         comm );
88
89     sumval = size * rank + ((size - 1) * size)/2;
90     /* recv'ed values for my process should be size * (rank + i) */
91     for (i=0; i<mycount; i++) {
92         if (sendbuf[i] != sumval) {
93             err++;
94             if (err < MAX_ERRORS) {
95                 fprintf( stdout, "Did not get expected value for reduce scatter (in place)\n" );
96                 fprintf( stdout, "[%d] Got buf[%d] = %d expected %d\n", 
97                          rank, i, sendbuf[rank*mycount+i], sumval );
98             }
99         }
100     }
101
102     free(sendbuf);
103     free(recvbuf);
104     free(recvcounts);
105        
106     MTest_Finalize( err );
107
108     MPI_Finalize( );
109
110     return 0;
111 }