Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
3bf3d919304c6de11f5df13b334c5268a127547e
[simgrid.git] / teshsuite / smpi / coll-reduce-scatter / coll-reduce-scatter.c
1 /* Copyright (c) 2013-2014. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 /* 
8  * Test of reduce scatter.
9  * Each processor contributes its rank + the index to the reduction,  then receives the ith sum
10  * Can be called with any number of processors.
11  */
12
13 #include "mpi.h"
14 #include <stdio.h>
15 #include <stdlib.h>
16
17 int main( int argc, char **argv )
18 {
19     int      err = 0, toterr;
20     int      *sendbuf, *recvbuf, *recvcounts;
21     int      size, rank, i, sumval;
22     MPI_Comm comm;
23
24     MPI_Init( &argc, &argv );
25     comm = MPI_COMM_WORLD;
26
27     MPI_Comm_size( comm, &size );
28     MPI_Comm_rank( comm, &rank );
29     sendbuf = (int *) malloc( size * sizeof(int) );
30     for (i=0; i<size; i++) 
31       sendbuf[i] = rank + i;
32     recvcounts = (int *)malloc( size * sizeof(int) );
33     recvbuf = (int *)malloc( size * sizeof(int) );
34     for (i=0; i<size; i++) 
35       recvcounts[i] = 1;
36     MPI_Reduce_scatter( sendbuf, recvbuf, recvcounts, MPI_INT, MPI_SUM, comm );
37     sumval = size * rank + ((size - 1) * size)/2;
38     /* recvbuf should be size * (rank + i) */
39     if (recvbuf[0] != sumval) {
40       err++;
41       fprintf( stdout, "Did not get expected value for reduce scatter\n" );
42       fprintf( stdout, "[%d] Got %d expected %d\n", rank, recvbuf[0], sumval );
43     }
44
45     MPI_Allreduce( &err, &toterr, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD );
46     if (rank == 0 && toterr == 0) {
47       printf( " No Errors\n" );
48     }
49     free(sendbuf);
50     free(recvcounts);
51     free(recvbuf);
52
53     MPI_Finalize();
54
55     return toterr;
56 }