Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Include directory is in source_dir, not in binary_dir.
[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     }
43     mycount = (1024 * 1024) / size;
44     for (i=0; i<size; i++) 
45         recvcounts[i] = mycount;
46     sendbuf = (int *) malloc( mycount * size * sizeof(int) );
47     if (!sendbuf) {
48         fprintf( stderr, "Could not allocate %d ints for sendbuf\n", 
49                  mycount * size );
50         MPI_Abort( MPI_COMM_WORLD, 1 );
51     }
52     idx = 0;
53     for (i=0; i<size; i++) {
54         for (j=0; j<mycount; j++) {
55             sendbuf[idx++] = rank + i;
56         }
57     }
58     recvbuf = (int *)malloc( mycount * sizeof(int) );
59     if (!recvbuf) {
60         fprintf( stderr, "Could not allocate %d ints for recvbuf\n", 
61                  mycount );
62         MPI_Abort( MPI_COMM_WORLD, 1 );
63     }
64     for (i=0; i<mycount; i++) {
65         recvbuf[i] = -1;
66     }
67
68     MPI_Reduce_scatter( sendbuf, recvbuf, recvcounts, MPI_INT, MPI_SUM, comm );
69
70     sumval = size * rank + ((size - 1) * size)/2;
71     /* recvbuf should be size * (rank + i) */
72     for (i=0; i<mycount; i++) {
73         if (recvbuf[i] != sumval) {
74             err++;
75             if (err < MAX_ERRORS) {
76                 fprintf( stdout, "Did not get expected value for reduce scatter\n" );
77                 fprintf( stdout, "[%d] Got recvbuf[%d] = %d expected %d\n",
78                          rank, i, recvbuf[i], sumval );
79             }
80         }
81     }
82
83     MPI_Reduce_scatter( MPI_IN_PLACE, sendbuf, recvcounts, MPI_INT, MPI_SUM, 
84                         comm );
85
86     sumval = size * rank + ((size - 1) * size)/2;
87     /* recv'ed values for my process should be size * (rank + i) */
88     for (i=0; i<mycount; i++) {
89         if (sendbuf[i] != sumval) {
90             err++;
91             if (err < MAX_ERRORS) {
92                 fprintf( stdout, "Did not get expected value for reduce scatter (in place)\n" );
93                 fprintf( stdout, "[%d] Got buf[%d] = %d expected %d\n", 
94                          rank, i, sendbuf[rank*mycount+i], sumval );
95             }
96         }
97     }
98
99     free(sendbuf);
100     free(recvbuf);
101     free(recvcounts);
102        
103     MTest_Finalize( err );
104
105     MPI_Finalize( );
106
107     return 0;
108 }