Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Include directory is in source_dir, not in binary_dir.
[simgrid.git] / teshsuite / smpi / mpich3-test / datatype / tfree.c
1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
2 /*
3  *
4  *  (C) 2003 by Argonne National Laboratory.
5  *      See COPYRIGHT in top-level directory.
6  */
7 #include "mpi.h"
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include "mpitest.h"
11
12 /*
13 static char MTEST_Descrip[] = "Test that freed datatypes have reference count semantics";
14 */
15
16 #define VEC_NELM 128
17 #define VEC_STRIDE 8
18
19 int main( int argc, char *argv[] )
20 {
21     int errs = 0;
22     int rank, size, source, dest, i;
23     MPI_Comm      comm;
24     MPI_Status    status;
25     MPI_Request   req;
26     MPI_Datatype  strideType;
27     MPI_Datatype  tmpType[1024];
28     int           *buf = 0;
29
30     MTest_Init( &argc, &argv );
31
32     comm = MPI_COMM_WORLD;
33
34     MPI_Comm_rank( comm, &rank );
35     MPI_Comm_size( comm, &size );
36
37     if (size < 2) {
38         fprintf( stderr, "This test requires at least two processes." );
39         MPI_Abort( MPI_COMM_WORLD, 1 );
40     }
41
42     source  = 0;
43     dest    = size - 1;
44
45     /* 
46        The idea here is to create a simple but non-contig datatype,
47        perform an irecv with it, free it, and then create 
48        many new datatypes.  While not a complete test, if the datatype
49        was freed and the space was reused, this test may detect 
50        that error 
51        A similar test for sends might work by sending a large enough message
52        to force the use of rendezvous send. 
53     */
54     MPI_Type_vector( VEC_NELM, 1, VEC_STRIDE, MPI_INT, &strideType );
55     MPI_Type_commit( &strideType );
56
57     if (rank == dest) {
58         buf = (int *)malloc( VEC_NELM * VEC_STRIDE * sizeof(int) );
59         for (i=0; i<VEC_NELM*VEC_STRIDE; i++) buf[i] = -i;
60         MPI_Irecv( buf, 1, strideType, source, 0, comm, &req );
61         MPI_Type_free( &strideType );
62
63         for (i=0; i<1024; i++) {
64             MPI_Type_vector( VEC_NELM, 1, 1, MPI_INT, &tmpType[i] );
65             MPI_Type_commit( &tmpType[i] );
66         }
67
68         MPI_Sendrecv( 0, 0, MPI_INT, source, 1, 
69                       0, 0, MPI_INT, source, 1, comm, &status );
70
71         MPI_Wait( &req, &status );
72         for (i=0; i<VEC_NELM; i++) {
73             if (buf[VEC_STRIDE*i] != i) {
74                 errs++;
75                 if (errs < 10) {
76                     printf( "buf[%d] = %d, expected %d\n", VEC_STRIDE*i, 
77                             buf[VEC_STRIDE*i], i );
78                 }
79             }
80         }
81         for (i=0; i<1024; i++) {
82             MPI_Type_free( &tmpType[i] );
83         }
84         free( buf );
85     }
86     else if (rank == source) {
87         buf = (int *)malloc( VEC_NELM * sizeof(int) );
88         for (i=0; i<VEC_NELM; i++) buf[i] = i;
89         /* Synchronize with the receiver */
90         MPI_Sendrecv( 0, 0, MPI_INT, dest, 1, 
91                       0, 0, MPI_INT, dest, 1, comm, &status );
92         MPI_Send( buf, VEC_NELM, MPI_INT, dest, 0, comm );
93         free( buf );
94     }
95
96     /* Clean up the strideType */
97     if (rank != dest) {
98         MPI_Type_free( &strideType );
99     }
100
101
102     MTest_Finalize( errs );
103     MPI_Finalize();
104     return 0;
105 }