File: darray_pack.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 (327 lines) | stat: -rw-r--r-- 9,939 bytes parent folder | download | duplicates (4)
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/*
 * Copyright (C) by Argonne National Laboratory
 *     See COPYRIGHT in top-level directory
 */

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

/*
   The default behavior of the test routines should be to briefly indicate
   the cause of any errors - in this test, that means that verbose needs
   to be set. Verbose should turn on output that is independent of error
   levels.
*/
static int verbose = 1;

/* tests */
int darray_2d_c_test1(void);
int darray_4d_c_test1(void);

/* helper functions */
static int parse_args(int argc, char **argv);
static int pack_and_unpack(char *typebuf, int count, MPI_Datatype datatype, int typebufsz);

int main(int argc, char **argv)
{
    int err, errs = 0;

    MTest_Init(&argc, &argv);
    parse_args(argc, argv);

    /* To improve reporting of problems about operations, we
     * change the error handler to errors return */
    MPI_Comm_set_errhandler(MPI_COMM_WORLD, MPI_ERRORS_RETURN);

    /* perform some tests */
    err = darray_2d_c_test1();
    if (err && verbose)
        fprintf(stderr, "%d errors in 2d darray c test 1.\n", err);
    errs += err;

    err = darray_4d_c_test1();
    if (err && verbose)
        fprintf(stderr, "%d errors in 4d darray c test 1.\n", err);
    errs += err;

    /* print message and exit */
    /* Allow the use of more than one process - some MPI implementations
     * (including IBM's) check that the number of processes given to
     * Type_create_darray is no larger than MPI_COMM_WORLD */

    MTest_Finalize(errs);
    return MTestReturnValue(errs);
}

/* darray_2d_test1()
 *
 * Performs a sequence of tests building darrays with single-element
 * blocks, running through all the various positions that the element might
 * come from.
 *
 * Returns the number of errors encountered.
 */
int darray_2d_c_test1(void)
{
    MPI_Datatype darray;
    int array[9];               /* initialized below */
    int array_size[2] = { 3, 3 };
    int array_distrib[2] = { MPI_DISTRIBUTE_BLOCK, MPI_DISTRIBUTE_BLOCK };
    int array_dargs[2] = { MPI_DISTRIBUTE_DFLT_DARG, MPI_DISTRIBUTE_DFLT_DARG };
    int array_psizes[2] = { 3, 3 };

    int i, rank, err, errs = 0, sizeoftype;

    /* pretend we are each rank, one at a time */
    for (rank = 0; rank < 9; rank++) {
        /* set up buffer */
        for (i = 0; i < 9; i++) {
            array[i] = i;
        }

        /* set up type */
        err = MPI_Type_create_darray(9, /* size */
                                     rank, 2,   /* dims */
                                     array_size,
                                     array_distrib,
                                     array_dargs, array_psizes, MPI_ORDER_C, MPI_INT, &darray);
        if (err != MPI_SUCCESS) {
            errs++;
            if (verbose) {
                fprintf(stderr,
                        "error in MPI_Type_create_darray call; aborting after %d errors\n", errs);
            }
            MTestPrintError(err);
            return errs;
        }

        MPI_Type_commit(&darray);

        MPI_Type_size(darray, &sizeoftype);
        if (sizeoftype != sizeof(int)) {
            errs++;
            if (verbose)
                fprintf(stderr, "size of type = %d; should be %d\n", sizeoftype, (int) sizeof(int));
            return errs;
        }

        err = pack_and_unpack((char *) array, 1, darray, 9 * sizeof(int));

        for (i = 0; i < 9; i++) {

            if ((i == rank) && (array[i] != rank)) {
                errs++;
                if (verbose)
                    fprintf(stderr, "[2d array rank=%d]:array[%d] = %d; should be %d\n",
                            rank, i, array[i], rank);
            } else if ((i != rank) && (array[i] != 0)) {
                errs++;
                if (verbose)
                    fprintf(stderr, "[2d array rank=%d]:array[%d] = %d; should be %d\n",
                            rank, i, array[i], 0);
            }
        }
        MPI_Type_free(&darray);
    }

    return errs;
}

/* darray_4d_c_test1()
 *
 * Returns the number of errors encountered.
 */
int darray_4d_c_test1(void)
{
    MPI_Datatype darray;
    int array[72];
    int array_size[4] = { 6, 3, 2, 2 };
    int array_distrib[4] = { MPI_DISTRIBUTE_BLOCK,
        MPI_DISTRIBUTE_BLOCK,
        MPI_DISTRIBUTE_NONE,
        MPI_DISTRIBUTE_NONE
    };
    int array_dargs[4] = { MPI_DISTRIBUTE_DFLT_DARG,
        MPI_DISTRIBUTE_DFLT_DARG,
        MPI_DISTRIBUTE_DFLT_DARG,
        MPI_DISTRIBUTE_DFLT_DARG
    };
    int array_psizes[4] = { 6, 3, 1, 1 };

    int i, rank, err, errs = 0, sizeoftype;

    for (rank = 0; rank < 18; rank++) {
        /* set up array */
        for (i = 0; i < 72; i++) {
            array[i] = i;
        }

        /* set up type */
        err = MPI_Type_create_darray(18,        /* size */
                                     rank, 4,   /* dims */
                                     array_size,
                                     array_distrib,
                                     array_dargs, array_psizes, MPI_ORDER_C, MPI_INT, &darray);
        if (err != MPI_SUCCESS) {
            errs++;
            if (verbose) {
                fprintf(stderr,
                        "error in MPI_Type_create_darray call; aborting after %d errors\n", errs);
            }
            MTestPrintError(err);
            return errs;
        }

        MPI_Type_commit(&darray);

        /* verify the size of the type */
        MPI_Type_size(darray, &sizeoftype);
        if (sizeoftype != 4 * sizeof(int)) {
            errs++;
            if (verbose)
                fprintf(stderr, "size of type = %d; should be %d\n",
                        sizeoftype, (int) (4 * sizeof(int)));
            return errs;
        }

        /* pack and unpack the type, zero'ing out all other values */
        err = pack_and_unpack((char *) array, 1, darray, 72 * sizeof(int));

        for (i = 0; i < 4 * rank; i++) {
            if (array[i] != 0) {
                errs++;
                if (verbose)
                    fprintf(stderr, "[4d array rank=%d]:array[%d] = %d; should be %d\n",
                            rank, i, array[i], 0);
            }
        }

        for (i = 4 * rank; i < 4 * rank + 4; i++) {
            if (array[i] != i) {
                errs++;
                if (verbose)
                    fprintf(stderr, "[4d array rank=%d]:array[%d] = %d; should be %d\n",
                            rank, i, array[i], i);
            }
        }
        for (i = 4 * rank + 4; i < 72; i++) {
            if (array[i] != 0) {
                errs++;
                if (verbose)
                    fprintf(stderr, "[4d array rank=%d]:array[%d] = %d; should be %d\n",
                            rank, i, array[i], 0);
            }
        }

        MPI_Type_free(&darray);
    }
    return errs;
}

/******************************************************************/

/* pack_and_unpack()
 *
 * Perform packing and unpacking of a buffer for the purposes of checking
 * to see if we are processing a type correctly.  Zeros the buffer between
 * these two operations, so the data described by the type should be in
 * place upon return but all other regions of the buffer should be zero.
 *
 * Parameters:
 * typebuf - pointer to buffer described by datatype and count that
 *           will be packed and then unpacked into
 * count, datatype - description of typebuf
 * typebufsz - size of typebuf; used specifically to zero the buffer
 *             between the pack and unpack steps
 *
 */
static int pack_and_unpack(char *typebuf, int count, MPI_Datatype datatype, int typebufsz)
{
    char *packbuf;
    int err, errs = 0, pack_size, type_size, position;

    err = MPI_Type_size(datatype, &type_size);
    if (err != MPI_SUCCESS) {
        errs++;
        if (verbose) {
            fprintf(stderr, "error in MPI_Type_size call; aborting after %d errors\n", errs);
        }
        MTestPrintError(err);
        return errs;
    }

    type_size *= count;

    err = MPI_Pack_size(count, datatype, MPI_COMM_SELF, &pack_size);
    if (err != MPI_SUCCESS) {
        errs++;
        if (verbose) {
            fprintf(stderr, "error in MPI_Pack_size call; aborting after %d errors\n", errs);
        }
        MTestPrintError(err);
        return errs;
    }
    packbuf = (char *) malloc(pack_size);
    if (packbuf == NULL) {
        errs++;
        if (verbose) {
            fprintf(stderr, "error in malloc call; aborting after %d errors\n", errs);
        }
        return errs;
    }

    /* FIXME: the pack size returned need not be the type_size - this will
     * only be true if the pack routine simply moves the bytes but does
     * no other transformations of the data */
    position = 0;
    err = MPI_Pack(typebuf, count, datatype, packbuf, type_size, &position, MPI_COMM_SELF);

    if (position != type_size) {
        errs++;
        if (verbose)
            fprintf(stderr, "position = %d; should be %d (pack)\n", position, type_size);
    }

    memset(typebuf, 0, typebufsz);
    position = 0;
    err = MPI_Unpack(packbuf, type_size, &position, typebuf, count, datatype, MPI_COMM_SELF);
    if (err != MPI_SUCCESS) {
        errs++;
        if (verbose) {
            fprintf(stderr, "error in MPI_Unpack call; aborting after %d errors\n", errs);
        }
        MTestPrintError(err);
        return errs;
    }
    free(packbuf);

    if (position != type_size) {
        errs++;
        if (verbose)
            fprintf(stderr, "position = %d; should be %d (unpack)\n", position, type_size);
    }

    return errs;
}

static int parse_args(int argc, char **argv)
{
    /*
     * int ret;
     *
     * while ((ret = getopt(argc, argv, "v")) >= 0)
     * {
     * switch (ret) {
     * case 'v':
     * verbose = 1;
     * break;
     * }
     * }
     */
    if (argc > 1 && strcmp(argv[1], "-v") == 0)
        verbose = 1;
    return 0;
}