Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
5fb81e510b5759d1aa2bf042bba9dd1904b17163
[simgrid.git] / teshsuite / smpi / mpich3-test / coll / redscatblk3.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 int main( int argc, char **argv )
22 {
23     int      err = 0;
24     int      *sendbuf, *recvbuf;
25     int      size, rank, i, j, idx, mycount, sumval;
26     MPI_Comm comm;
27
28
29     MTest_Init( &argc, &argv );
30     comm = MPI_COMM_WORLD;
31
32     MPI_Comm_size( comm, &size );
33     MPI_Comm_rank( comm, &rank );
34     mycount = (1024 * 1024) / size;
35
36     sendbuf = (int *) malloc( mycount * size * sizeof(int) );
37     if (!sendbuf) {
38         fprintf( stderr, "Could not allocate %d ints for sendbuf\n", 
39                  mycount * size );
40         MPI_Abort( MPI_COMM_WORLD, 1 );
41     }
42     idx = 0;
43     for (i=0; i<size; i++) {
44         for (j=0; j<mycount; j++) {
45             sendbuf[idx++] = rank + i;
46         }
47     }
48     recvbuf = (int *)malloc( mycount * sizeof(int) );
49     if (!recvbuf) {
50         fprintf( stderr, "Could not allocate %d ints for recvbuf\n", 
51                  mycount );
52         MPI_Abort( MPI_COMM_WORLD, 1 );
53     }
54
55     MPI_Reduce_scatter_block( sendbuf, recvbuf, mycount, MPI_INT, MPI_SUM, 
56                               comm );
57
58     sumval = size * rank + ((size - 1) * size)/2;
59     /* recvbuf should be size * (rank + i) */
60     for (i=0; i<mycount; i++) {
61         if (recvbuf[i] != sumval) {
62             err++;
63             fprintf( stdout, "Did not get expected value for reduce scatter\n" );
64             fprintf( stdout, "[%d] Got %d expected %d\n", rank, recvbuf[i], sumval );
65         }
66     }
67
68     MPI_Reduce_scatter_block( MPI_IN_PLACE, sendbuf, mycount, MPI_INT, MPI_SUM, 
69                         comm );
70
71     sumval = size * rank + ((size - 1) * size)/2;
72     /* recv'ed values for my process should be size * (rank + i) */
73     for (i=0; i<mycount; i++) {
74         if (sendbuf[rank*mycount+i] != sumval) {
75             err++;
76             fprintf( stdout, "Did not get expected value for reduce scatter (in place)\n" );
77             fprintf( stdout, "[%d] Got %d expected %d\n", rank, sendbuf[rank*mycount+i], sumval );
78         }
79     }
80
81     free(sendbuf);
82     free(recvbuf);
83        
84     MTest_Finalize( err );
85
86     MPI_Finalize( );
87
88     return 0;
89 }