Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Reduce the size of partial shared malloc tests.
[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++)
60             buf[i] = -i;
61         MPI_Irecv(buf, 1, strideType, source, 0, comm, &req);
62         MPI_Type_free(&strideType);
63
64         for (i = 0; i < 1024; i++) {
65             MPI_Type_vector(VEC_NELM, 1, 1, MPI_INT, &tmpType[i]);
66             MPI_Type_commit(&tmpType[i]);
67         }
68
69         MPI_Sendrecv(NULL, 0, MPI_INT, source, 1, NULL, 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, buf[VEC_STRIDE * i], i);
77                 }
78             }
79         }
80         for (i = 0; i < 1024; i++) {
81             MPI_Type_free(&tmpType[i]);
82         }
83         free(buf);
84     }
85     else if (rank == source) {
86         buf = (int *) malloc(VEC_NELM * sizeof(int));
87         for (i = 0; i < VEC_NELM; i++)
88             buf[i] = i;
89         /* Synchronize with the receiver */
90         MPI_Sendrecv(NULL, 0, MPI_INT, dest, 1, NULL, 0, MPI_INT, dest, 1, comm, &status);
91         MPI_Send(buf, VEC_NELM, MPI_INT, dest, 0, comm);
92         free(buf);
93     }
94
95     /* Clean up the strideType */
96     if (rank != dest) {
97         MPI_Type_free(&strideType);
98     }
99
100
101     MTest_Finalize(errs);
102     MPI_Finalize();
103     return 0;
104 }