File: large_count.c

package info (click to toggle)
mpich 4.0.2-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 423,384 kB
  • sloc: ansic: 1,088,434; cpp: 71,364; javascript: 40,763; f90: 22,829; sh: 17,463; perl: 14,773; xml: 14,418; python: 10,265; makefile: 9,246; fortran: 8,008; java: 4,355; asm: 324; ruby: 176; lisp: 19; php: 8; sed: 4
file content (240 lines) | stat: -rw-r--r-- 9,736 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/*
 * Copyright (C) by Argonne National Laboratory
 *     See COPYRIGHT in top-level directory
 */

/* This test checks for large count functionality ("MPI_Count") mandated by
 * MPI-3, as well as behavior of corresponding pre-MPI-3 interfaces that now
 * have better defined behavior when an "int" quantity would overflow. */

#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
#include "mpitest.h"

/* assert-like macro that bumps the err count and emits a message */
#define check(x_)                                                                 \
    do {                                                                          \
        if (!(x_)) {                                                              \
            ++errs;                                                               \
            if (errs < 10) {                                                      \
                fprintf(stderr, "check failed: (%s), line %d\n", #x_, __LINE__); \
            }                                                                     \
        }                                                                         \
    } while (0)

int main(int argc, char *argv[])
{
    int errs = 0;
    int wrank, wsize;
    int size, elements, count;
    MPI_Aint lb, extent;
    MPI_Count size_x, lb_x, extent_x, elements_x;
    MPI_Count imx4i_true_extent;
    MPI_Datatype imax_contig = MPI_DATATYPE_NULL;
    MPI_Datatype four_ints = MPI_DATATYPE_NULL;
    MPI_Datatype imx4i = MPI_DATATYPE_NULL;
    MPI_Datatype imx4i_rsz = MPI_DATATYPE_NULL;
    MPI_Status status;

    MTest_Init(&argc, &argv);
    MPI_Comm_rank(MPI_COMM_WORLD, &wrank);
    MPI_Comm_size(MPI_COMM_WORLD, &wsize);

    check(sizeof(MPI_Count) >= sizeof(int));
    check(sizeof(MPI_Count) >= sizeof(MPI_Aint));
    check(sizeof(MPI_Count) >= sizeof(MPI_Offset));

    /* the following two checks aren't explicitly required by the standard, but
     * it's hard to imagine a world without them holding true and so most of the
     * subsequent code probably depends on them to some degree */
    check(sizeof(MPI_Aint) >= sizeof(int));
    check(sizeof(MPI_Offset) >= sizeof(int));

    /* not much point in checking for integer overflow cases if MPI_Count is
     * only as large as an int */
    if (sizeof(MPI_Count) == sizeof(int))
        goto epilogue;

    /* a very large type */
    MPI_Type_contiguous(INT_MAX, MPI_CHAR, &imax_contig);
    MPI_Type_commit(&imax_contig);

    /* a small-ish contig */
    MPI_Type_contiguous(4, MPI_INT, &four_ints);
    MPI_Type_commit(&four_ints);

    /* a type with size>INT_MAX */
    MPI_Type_vector(INT_MAX / 2, 1, 3, four_ints, &imx4i);
    MPI_Type_commit(&imx4i);
    /* don't forget, ub for dtype w/ stride doesn't include any holes at the end
     * of the type, hence the more complicated calculation below */
    imx4i_true_extent = 3LL * 4LL * sizeof(int) * ((INT_MAX / 2) - 1) + 4LL * sizeof(int);

    /* sanity check that the MPI_COUNT predefined named datatype exists */
    MPI_Send(&imx4i_true_extent, 1, MPI_COUNT, MPI_PROC_NULL, 0, MPI_COMM_SELF);

    /* the same oversized type but with goofy extents */
    MPI_Type_create_resized(imx4i, /*lb= */ INT_MAX, /*extent= */ -1024, &imx4i_rsz);
    MPI_Type_commit(&imx4i_rsz);

    /* MPI_Type_size */
    MPI_Type_size(imax_contig, &size);
    check(size == INT_MAX);
    MPI_Type_size(four_ints, &size);
    check(size == 4 * sizeof(int));
    MPI_Type_size(imx4i, &size);
    check(size == MPI_UNDEFINED);       /* should overflow an int */
    MPI_Type_size(imx4i_rsz, &size);
    check(size == MPI_UNDEFINED);       /* should overflow an int */

    /* MPI_Type_size_x */
    MPI_Type_size_x(imax_contig, &size_x);
    check(size_x == INT_MAX);
    MPI_Type_size_x(four_ints, &size_x);
    check(size_x == 4 * sizeof(int));
    MPI_Type_size_x(imx4i, &size_x);
    check(size_x == 4LL * sizeof(int) * (INT_MAX / 2)); /* should overflow an int */
    MPI_Type_size_x(imx4i_rsz, &size_x);
    check(size_x == 4LL * sizeof(int) * (INT_MAX / 2)); /* should overflow an int */

    /* MPI_Type_get_extent */
    MPI_Type_get_extent(imax_contig, &lb, &extent);
    check(lb == 0);
    check(extent == INT_MAX);
    MPI_Type_get_extent(four_ints, &lb, &extent);
    check(lb == 0);
    check(extent == 4 * sizeof(int));
    MPI_Type_get_extent(imx4i, &lb, &extent);
    check(lb == 0);
    if (sizeof(MPI_Aint) == sizeof(int))
        check(extent == MPI_UNDEFINED);
    else
        check(extent == imx4i_true_extent);

    MPI_Type_get_extent(imx4i_rsz, &lb, &extent);
    check(lb == INT_MAX);
    check(extent == -1024);

    /* MPI_Type_get_extent_x */
    MPI_Type_get_extent_x(imax_contig, &lb_x, &extent_x);
    check(lb_x == 0);
    check(extent_x == INT_MAX);
    MPI_Type_get_extent_x(four_ints, &lb_x, &extent_x);
    check(lb_x == 0);
    check(extent_x == 4 * sizeof(int));
    MPI_Type_get_extent_x(imx4i, &lb_x, &extent_x);
    check(lb_x == 0);
    check(extent_x == imx4i_true_extent);
    MPI_Type_get_extent_x(imx4i_rsz, &lb_x, &extent_x);
    check(lb_x == INT_MAX);
    check(extent_x == -1024);

    /* MPI_Type_get_true_extent */
    MPI_Type_get_true_extent(imax_contig, &lb, &extent);
    check(lb == 0);
    check(extent == INT_MAX);
    MPI_Type_get_true_extent(four_ints, &lb, &extent);
    check(lb == 0);
    check(extent == 4 * sizeof(int));
    MPI_Type_get_true_extent(imx4i, &lb, &extent);
    check(lb == 0);
    if (sizeof(MPI_Aint) == sizeof(int))
        check(extent == MPI_UNDEFINED);
    else
        check(extent == imx4i_true_extent);
    MPI_Type_get_true_extent(imx4i_rsz, &lb, &extent);
    check(lb == 0);
    if (sizeof(MPI_Aint) == sizeof(int))
        check(extent == MPI_UNDEFINED);
    else
        check(extent == imx4i_true_extent);

    /* MPI_Type_get_true_extent_x */
    MPI_Type_get_true_extent_x(imax_contig, &lb_x, &extent_x);
    check(lb_x == 0);
    check(extent_x == INT_MAX);
    MPI_Type_get_true_extent_x(four_ints, &lb_x, &extent_x);
    check(lb_x == 0);
    check(extent_x == 4 * sizeof(int));
    MPI_Type_get_true_extent_x(imx4i, &lb_x, &extent_x);
    check(lb_x == 0);
    check(extent_x == imx4i_true_extent);
    MPI_Type_get_true_extent_x(imx4i_rsz, &lb_x, &extent_x);
    check(lb_x == 0);
    check(extent_x == imx4i_true_extent);


    /* MPI_{Status_set_elements,Get_elements}{,_x} */

    /* set simple */
    MPI_Status_set_elements(&status, MPI_INT, 10);
    MPI_Get_elements(&status, MPI_INT, &elements);
    MPI_Get_elements_x(&status, MPI_INT, &elements_x);
    MPI_Get_count(&status, MPI_INT, &count);
    check(elements == 10);
    check(elements_x == 10);
    check(count == 10);

    /* set_x simple */
    MPI_Status_set_elements_x(&status, MPI_INT, 10);
    MPI_Get_elements(&status, MPI_INT, &elements);
    MPI_Get_elements_x(&status, MPI_INT, &elements_x);
    MPI_Get_count(&status, MPI_INT, &count);
    check(elements == 10);
    check(elements_x == 10);
    check(count == 10);

    /* Sets elements corresponding to count=1 of the given MPI datatype, using
     * set_elements and set_elements_x.  Checks expected values are returned by
     * get_elements, get_elements_x, and get_count (including MPI_UNDEFINED
     * clipping) */
#define check_set_elements(type_, elts_)                          \
    do {                                                          \
        elements = elements_x = count = 0xfeedface;               \
        /* can't use legacy "set" for large element counts */     \
        if ((elts_) <= INT_MAX) {                                 \
            MPI_Status_set_elements(&status, (type_), 1);         \
            MPI_Get_elements(&status, (type_), &elements);        \
            MPI_Get_elements_x(&status, (type_), &elements_x);    \
            MPI_Get_count(&status, (type_), &count);              \
            check(elements == (int) (elts_));                     \
            check(elements_x == (elts_));                         \
            check(count == 1);                                    \
        }                                                         \
                                                                  \
        elements = elements_x = count = 0xfeedface;               \
        MPI_Status_set_elements_x(&status, (type_), 1);           \
        MPI_Get_elements(&status, (type_), &elements);            \
        MPI_Get_elements_x(&status, (type_), &elements_x);        \
        MPI_Get_count(&status, (type_), &count);                  \
        if ((elts_) > INT_MAX) {                                  \
            check(elements == MPI_UNDEFINED);                     \
        }                                                         \
        else {                                                    \
            check(elements == (int) (elts_));                           \
        }                                                         \
        check(elements_x == (elts_));                             \
        check(count == 1);                                        \
    } while (0)                                                   \

    check_set_elements(imax_contig, INT_MAX);
    check_set_elements(four_ints, 4);
    check_set_elements(imx4i, 4LL * (INT_MAX / 2));
    check_set_elements(imx4i_rsz, 4LL * (INT_MAX / 2));

  epilogue:
    if (imax_contig != MPI_DATATYPE_NULL)
        MPI_Type_free(&imax_contig);
    if (four_ints != MPI_DATATYPE_NULL)
        MPI_Type_free(&four_ints);
    if (imx4i != MPI_DATATYPE_NULL)
        MPI_Type_free(&imx4i);
    if (imx4i_rsz != MPI_DATATYPE_NULL)
        MPI_Type_free(&imx4i_rsz);

    MTest_Finalize(errs);

    return MTestReturnValue(errs);
}