Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
remove few tests which may never finish, and change one that used too much stack...
[simgrid.git] / teshsuite / smpi / mpich-test / pt2pt / hindexed.c
1 #include "mpi.h"
2 #include <stdio.h>
3 /* stdlib.h needed for malloc declaration */
4 #include <stdlib.h>
5
6 #if defined(NEEDS_STDLIB_PROTOTYPES)
7 #include "protofix.h"
8 #endif
9
10 /*
11  * This file tests MPI_Type_hindexed by describing parts of a triangular
12  * matrix, stored in a square matrix, and sending sending it.
13  * 
14  * The matrix is stored in column-major, and the tests use
15  * MPI_Type_vector or MPI_Type_struct to define the elements that are sent
16  */
17
18 int main( int argc, char **argv )
19 {
20     MPI_Datatype rowtype, mattype;
21     int          *sbuf, *rbuf;
22     int          rank, mat_n;
23     static int   blens[2] = { 1, 1 };
24     MPI_Datatype types[2] = { MPI_INT, MPI_UB };
25     int          *mat_blens, i ;
26     MPI_Aint     *mat_displs;
27     MPI_Aint     displs[2];
28     MPI_Status   status;
29     int          err, row, col;
30
31     MPI_Init( &argc, &argv );
32
33     err = 0;
34     MPI_Comm_rank( MPI_COMM_WORLD, &rank );
35     mat_n = 10;
36     sbuf = (int *) malloc( mat_n * mat_n * sizeof(int) );
37     rbuf = (int *) malloc( mat_n * mat_n * sizeof(int) );
38     if (!sbuf || !rbuf) {
39         MPI_Abort( MPI_COMM_WORLD, 1 );
40     }
41
42     /* Define a row type based on a strided struct type */
43     displs[0] = 0;
44     displs[1] = mat_n*sizeof(int);
45     MPI_Type_struct( 2, blens, displs, types, &rowtype );
46      MPI_Type_commit( &rowtype ); 
47
48     /* Define an hindexed type that defines all of the rows of the
49        triangular part of sbuf */
50     
51     mat_blens = (int *)malloc( mat_n * sizeof(int) );
52     mat_displs = (MPI_Aint *)malloc( mat_n * sizeof(MPI_Aint) );
53     for (i=0; i<mat_n; i++) {
54         mat_blens[i] = mat_n - i;
55         MPI_Address( &sbuf[i + i * mat_n], &mat_displs[i] );
56         if (i != 0)
57             mat_displs[i] = mat_displs[i] - mat_displs[0];
58     }
59     mat_displs[0] = 0;
60     MPI_Type_hindexed( mat_n, mat_blens, mat_displs, rowtype, &mattype );
61     MPI_Type_commit( &mattype );
62     MPI_Type_free( &rowtype );
63
64     /* Load up the data */
65     for (i=0; i<mat_n * mat_n; i++) {
66         sbuf[i] = i;
67         rbuf[i] = -i;
68     }
69     
70     /* Send it and receive it in the same order */
71     MPI_Sendrecv( sbuf, 1, mattype, rank, 0, rbuf, 1, mattype, rank, 0, 
72                   MPI_COMM_WORLD, &status );
73
74     for (row = 0; row<mat_n; row++) {
75         for (col = row; col<mat_n; col++) {
76             if (rbuf[row + col*mat_n] != sbuf[row + col*mat_n]) {
77                 err++;
78                 fprintf( stderr, "mat(%d,%d) = %d, not %d\n",
79                          row, col, rbuf[row+col*mat_n], sbuf[row+col*mat_n] );
80             }
81         }
82     }
83
84     /* Send hindexed and receive contiguous */
85     MPI_Sendrecv( sbuf, 1, mattype, rank, 1, 
86                   rbuf, (mat_n * (mat_n + 1))/2, MPI_INT, rank, 1, 
87                   MPI_COMM_WORLD, &status );
88     i = 0;
89     for (row = 0; row<mat_n; row++) {
90         for (col = row; col<mat_n; col++) {
91             if (rbuf[i] != sbuf[row + col*mat_n]) {
92                 err++;
93                 fprintf( stderr, "rbuf(%d,%d) = %d, not %d\n",
94                          row, col, rbuf[i], sbuf[row+col*mat_n] );
95             }
96             i++;
97         }
98     }
99
100     MPI_Type_free( &mattype );
101     if (err == 0) printf( "Test passed\n" );
102     else          printf( "Test failed with %d errors\n", err );
103
104     MPI_Finalize();
105     return 0;
106 }