Logo AND Algorithmique Numérique Distribuée

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