Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'v3_8_x'
[simgrid.git] / teshsuite / smpi / mpich-test / topol / cartmap.c
1 #include "mpi.h"
2 #include <stdio.h>
3 /* stdlib.h Needed for malloc declaration */
4 #include <stdlib.h>
5 #include "test.h"
6
7 #define NUM_DIMS 2
8
9 int main( int argc, char **argv )
10 {
11     int              rank, size, i;
12     int              errors=0;
13     int              dims[NUM_DIMS];
14     int              periods[NUM_DIMS];
15     int              *rbuf, *sbuf;
16     int              new_rank;
17
18     MPI_Init( &argc, &argv );
19
20     MPI_Comm_rank( MPI_COMM_WORLD, &rank );
21     MPI_Comm_size( MPI_COMM_WORLD, &size );
22
23     /* Clear dims array and get dims for topology */
24     for(i=0;i<NUM_DIMS;i++) { dims[i] = 0; periods[i] = 0; }
25     MPI_Dims_create ( size, NUM_DIMS, dims );
26
27     /* Look at what rankings a cartesian topology MIGHT have */
28     MPI_Cart_map( MPI_COMM_WORLD, 2, dims, periods, &new_rank );
29
30     /* Check that all new ranks are used exactly once */
31     rbuf = (int *)malloc( size * sizeof(int) );
32     sbuf = (int *)malloc( size * sizeof(int) );
33     if (!rbuf || !sbuf) {
34         MPI_Abort( MPI_COMM_WORLD, 1 );
35     }
36     for (i=0; i<size; i++) 
37         sbuf[i] = 0;
38     sbuf[new_rank] = 1;
39     MPI_Reduce( sbuf, rbuf, size, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD );
40     if (rank == 0) {
41         for (i=0; i<size; i++) {
42             if (rbuf[i] != 1) {
43                 errors++;
44                 fprintf( stderr, "Rank %d used %d times\n", i, rbuf[i] );
45             }
46         }
47         if (errors == 0) 
48             printf( "Cart map test passed\n" );
49     }
50
51     free( rbuf );
52     free( sbuf );
53     MPI_Finalize();
54     return 0;
55 }