Logo AND Algorithmique Numérique Distribuée

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