Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'toufic' of github.com:Takishipp/simgrid
[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;
20     int toterr;
21     int size;
22     int rank;
23     int i;
24     MPI_Comm comm;
25
26     MPI_Init( &argc, &argv );
27     comm = MPI_COMM_WORLD;
28
29     MPI_Comm_size( comm, &size );
30     MPI_Comm_rank( comm, &rank );
31     int* sendbuf = (int *) malloc( size * sizeof(int) );
32     for (i=0; i<size; i++) 
33       sendbuf[i] = rank + i;
34     int* recvcounts = (int*) malloc (size * sizeof(int));
35     int* recvbuf    = (int*) malloc (size * sizeof(int));
36     for (i=0; i<size; i++) 
37       recvcounts[i] = 1;
38     MPI_Reduce_scatter( sendbuf, recvbuf, recvcounts, MPI_INT, MPI_SUM, comm );
39     int sumval = size * rank + ((size - 1) * size)/2;
40     /* recvbuf should be size * (rank + i) */
41     if (recvbuf[0] != sumval) {
42       err++;
43       fprintf( stdout, "Did not get expected value for reduce scatter\n" );
44       fprintf( stdout, "[%d] Got %d expected %d\n", rank, recvbuf[0], sumval );
45     }
46
47     MPI_Allreduce( &err, &toterr, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD );
48     if (rank == 0 && toterr == 0) {
49       printf( " No Errors\n" );
50     }
51     free(sendbuf);
52     free(recvcounts);
53     free(recvbuf);
54
55     MPI_Finalize();
56
57     return toterr;
58 }