Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Initialize arrays at declaration, and remove use of deprecated bzero().
[simgrid.git] / teshsuite / smpi / reduce_scatter_coll.c
1 /* 
2  * Test of reduce scatter.
3  *
4  * Each processor contributes its rank + the index to the reduction, 
5  * then receives the ith sum
6  *
7  * Can be called with any number of processors.
8  */
9
10 #include "mpi.h"
11 #include <stdio.h>
12 #include <stdlib.h>
13
14 int main( int argc, char **argv )
15 {
16     int      err = 0, toterr;
17     int      *sendbuf, *recvbuf, *recvcounts;
18     int      size, rank, i, sumval;
19     MPI_Comm comm;
20
21
22     MPI_Init( &argc, &argv );
23     comm = MPI_COMM_WORLD;
24
25     MPI_Comm_size( comm, &size );
26     MPI_Comm_rank( comm, &rank );
27     sendbuf = (int *) malloc( size * sizeof(int) );
28     for (i=0; i<size; i++) 
29         sendbuf[i] = rank + i;
30     recvcounts = (int *)malloc( size * sizeof(int) );
31     recvbuf = (int *)malloc( size * sizeof(int) );
32     for (i=0; i<size; i++) 
33     recvcounts[i] = 1;
34     MPI_Reduce_scatter( sendbuf, recvbuf, recvcounts, MPI_INT, MPI_SUM, comm );
35     sumval = size * rank + ((size - 1) * size)/2;
36 /* recvbuf should be size * (rank + i) */
37     if (recvbuf[0] != sumval) {
38         err++;
39         fprintf( stdout, "Did not get expected value for reduce scatter\n" );
40         fprintf( stdout, "[%d] Got %d expected %d\n", rank, recvbuf[0], sumval );
41     }
42
43     MPI_Allreduce( &err, &toterr, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD );
44     if (rank == 0 && toterr == 0) {
45         printf( " No Errors\n" );
46     }
47     free(sendbuf);
48     free(recvcounts);
49     free(recvbuf);
50     
51     MPI_Finalize( );
52
53     return toterr;
54 }