Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
I blame someone else for this
[simgrid.git] / teshsuite / smpi / struct_test.c
1 #include <stdio.h>
2 #include "mpi.h"
3
4 int main( argc, argv )
5 int argc;
6 char **argv;
7 {
8     int          rank;
9     struct { int a;int c; double b;int tab[2][3];} value;
10     MPI_Datatype mystruct;
11     int          blocklens[3];
12     MPI_Aint     indices[3];
13     MPI_Datatype old_types[3], type2;
14     int i,j;
15
16     MPI_Init( &argc, &argv );
17
18     MPI_Comm_rank( MPI_COMM_WORLD, &rank );
19
20     int tab[2][3]={{1*rank,2*rank,3*rank},{7*rank,8*rank,9*rank}}; 
21     MPI_Type_contiguous(3, MPI_INT, &type2);
22     MPI_Type_commit(&type2);
23
24     /* One value of each type, and two for the contiguous one */
25     blocklens[0] = 1;
26     blocklens[1] = 1;
27     blocklens[2] = 2;
28     /* The base types */
29     old_types[0] = MPI_INT;
30     old_types[1] = MPI_DOUBLE;
31     old_types[2] = type2;
32     /* The locations of each element */
33     MPI_Address( &value.a, &indices[0] );
34     MPI_Address( &value.b, &indices[1] );
35     MPI_Address( &tab, &indices[2] );
36     /* Make relative */
37     indices[2] = indices[2] - indices[0];
38     indices[1] = indices[1] - indices[0];
39     indices[0] = 0;
40
41     MPI_Type_struct( 3, blocklens, indices, old_types, &mystruct );
42     MPI_Type_commit( &mystruct );
43
44     if (rank == 0){
45       value.a=-2;
46       value.b=8.0;
47     }else{
48       value.a=10000;
49       value.b=5.0; 
50     }
51
52     MPI_Bcast( &value, 1, mystruct, 0, MPI_COMM_WORLD );
53
54     printf( "Process %d got %d (-2?) and %f (8.0?), tab (should be all 0): ", rank, value.a, value.b );
55    
56     for(j=0; j<2;j++ )
57       for(i=0; i<3;i++ )
58       printf("%d ", tab[j][i]);
59
60       printf("\n");
61
62
63     /* Clean up the type */
64     MPI_Type_free( &mystruct );
65     MPI_Type_free( &type2 );
66     MPI_Finalize( );
67     return 0;
68 }