Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
MPI_Abort can theorically fail. Add a call to exit() to ensure that the program...
[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         exit(1);
42     }
43     idx = 0;
44     for (i=0; i<size; i++) {
45         for (j=0; j<mycount; j++) {
46             sendbuf[idx++] = rank + i;
47         }
48     }
49     recvbuf = (int *)malloc( mycount * sizeof(int) );
50     if (!recvbuf) {
51         fprintf( stderr, "Could not allocate %d ints for recvbuf\n", 
52                  mycount );
53         MPI_Abort( MPI_COMM_WORLD, 1 );
54         exit(1);
55     }
56
57     MPI_Reduce_scatter_block( sendbuf, recvbuf, mycount, MPI_INT, MPI_SUM, 
58                               comm );
59
60     sumval = size * rank + ((size - 1) * size)/2;
61     /* recvbuf should be size * (rank + i) */
62     for (i=0; i<mycount; i++) {
63         if (recvbuf[i] != sumval) {
64             err++;
65             fprintf( stdout, "Did not get expected value for reduce scatter\n" );
66             fprintf( stdout, "[%d] Got %d expected %d\n", rank, recvbuf[i], sumval );
67         }
68     }
69
70     MPI_Reduce_scatter_block( MPI_IN_PLACE, sendbuf, mycount, MPI_INT, MPI_SUM, 
71                         comm );
72
73     sumval = size * rank + ((size - 1) * size)/2;
74     /* recv'ed values for my process should be size * (rank + i) */
75     for (i=0; i<mycount; i++) {
76         if (sendbuf[rank*mycount+i] != sumval) {
77             err++;
78             fprintf( stdout, "Did not get expected value for reduce scatter (in place)\n" );
79             fprintf( stdout, "[%d] Got %d expected %d\n", rank, sendbuf[rank*mycount+i], sumval );
80         }
81     }
82
83     free(sendbuf);
84     free(recvbuf);
85        
86     MTest_Finalize( err );
87
88     MPI_Finalize( );
89
90     return 0;
91 }