File: struct_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 (392 lines) | stat: -rw-r--r-- 9,889 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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
/*
 * Copyright (C) by Argonne National Laboratory
 *     See COPYRIGHT in top-level directory
 */

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

static int verbose = 0;

int main(int argc, char *argv[]);
int parse_args(int argc, char **argv);
int single_struct_test(void);
int array_of_structs_test(void);
int struct_of_structs_test(void);

struct test_struct_1 {
    int a, b;
    char c, d;
    int e;
};

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

    /* Initialize MPI */
    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);

    err = single_struct_test();
    if (verbose && err)
        fprintf(stderr, "error in single_struct_test\n");
    errs += err;

    err = array_of_structs_test();
    if (verbose && err)
        fprintf(stderr, "error in array_of_structs_test\n");
    errs += err;

    err = struct_of_structs_test();
    if (verbose && err)
        fprintf(stderr, "error in struct_of_structs_test\n");
    errs += err;

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

int single_struct_test(void)
{
    int err, errs = 0;
    int bufsize, position = 0;
    struct test_struct_1 ts1, ts2;
    MPI_Datatype mystruct;
    char *buffer;

    MPI_Aint disps[3] = { 0, 2 * sizeof(int), 3 * sizeof(int) };        /* guessing... */
    int blks[3] = { 2, 2, 1 };
    MPI_Datatype types[3] = { MPI_INT, MPI_CHAR, MPI_INT };

    ts1.a = 1;
    ts1.b = 2;
    ts1.c = 3;
    ts1.d = 4;
    ts1.e = 5;

    err = MPI_Type_create_struct(3, blks, disps, types, &mystruct);
    if (err != MPI_SUCCESS) {
        errs++;
        if (verbose) {
            fprintf(stderr, "MPI_Type_create_struct returned error\n");
        }
    }

    MPI_Type_commit(&mystruct);

    MPI_Pack_size(1, mystruct, MPI_COMM_WORLD, &bufsize);
    buffer = (char *) malloc(bufsize);

    err = MPI_Pack(&ts1, 1, mystruct, buffer, bufsize, &position, MPI_COMM_WORLD);
    if (err != MPI_SUCCESS) {
        errs++;
        if (verbose) {
            fprintf(stderr, "MPI_Pack returned error\n");
        }
    }

    position = 0;
    err = MPI_Unpack(buffer, bufsize, &position, &ts2, 1, mystruct, MPI_COMM_WORLD);
    if (err != MPI_SUCCESS) {
        errs++;
        if (verbose) {
            fprintf(stderr, "MPI_Unpack returned error\n");
        }
    }

    MPI_Type_free(&mystruct);
    free(buffer);

    if (ts1.a != ts2.a) {
        errs++;
        if (verbose) {
            fprintf(stderr, "ts2.a = %d; should be %d\n", ts2.a, ts1.a);
        }
    }
    if (ts1.b != ts2.b) {
        errs++;
        if (verbose) {
            fprintf(stderr, "ts2.b = %d; should be %d\n", ts2.b, ts1.b);
        }
    }
    if (ts1.c != ts2.c) {
        errs++;
        if (verbose) {
            fprintf(stderr, "ts2.c = %d; should be %d\n", (int) ts2.c, (int) ts1.c);
        }
    }
    if (ts1.d != ts2.d) {
        errs++;
        if (verbose) {
            fprintf(stderr, "ts2.d = %d; should be %d\n", (int) ts2.d, (int) ts1.d);
        }
    }
    if (ts1.e != ts2.e) {
        errs++;
        if (verbose) {
            fprintf(stderr, "ts2.e = %d; should be %d\n", ts2.e, ts1.e);
        }
    }

    return errs;
}

int array_of_structs_test(void)
{
    int i, err, errs = 0;
    int bufsize, position = 0;
    struct test_struct_1 ts1[10], ts2[10];
    MPI_Datatype mystruct;
    char *buffer;

    MPI_Aint disps[3] = { 0, 2 * sizeof(int), 3 * sizeof(int) };        /* guessing... */
    int blks[3] = { 2, 2, 1 };
    MPI_Datatype types[3] = { MPI_INT, MPI_CHAR, MPI_INT };

    for (i = 0; i < 10; i++) {
        ts1[i].a = 10 * i + 1;
        ts1[i].b = 10 * i + 2;
        ts1[i].c = 10 * i + 3;
        ts1[i].d = 10 * i + 4;
        ts1[i].e = 10 * i + 5;

        ts2[i].a = -13;
        ts2[i].b = -13;
        ts2[i].c = -13;
        ts2[i].d = -13;
        ts2[i].e = -13;
    }

    err = MPI_Type_create_struct(3, blks, disps, types, &mystruct);
    if (err != MPI_SUCCESS) {
        errs++;
        if (verbose) {
            fprintf(stderr, "MPI_Type_create_struct returned error\n");
        }
    }

    MPI_Type_commit(&mystruct);

    MPI_Pack_size(10, mystruct, MPI_COMM_WORLD, &bufsize);
    buffer = (char *) malloc(bufsize);

    err = MPI_Pack(ts1, 10, mystruct, buffer, bufsize, &position, MPI_COMM_WORLD);
    if (err != MPI_SUCCESS) {
        errs++;
        if (verbose) {
            fprintf(stderr, "MPI_Pack returned error\n");
        }
    }

    position = 0;
    err = MPI_Unpack(buffer, bufsize, &position, ts2, 10, mystruct, MPI_COMM_WORLD);
    if (err != MPI_SUCCESS) {
        errs++;
        if (verbose) {
            fprintf(stderr, "MPI_Unpack returned error\n");
        }
    }

    MPI_Type_free(&mystruct);
    free(buffer);

    for (i = 0; i < 10; i++) {
        if (ts1[i].a != ts2[i].a) {
            errs++;
            if (verbose) {
                fprintf(stderr, "ts2[%d].a = %d; should be %d\n", i, ts2[i].a, ts1[i].a);
            }
        }
        if (ts1[i].b != ts2[i].b) {
            errs++;
            if (verbose) {
                fprintf(stderr, "ts2[%d].b = %d; should be %d\n", i, ts2[i].b, ts1[i].b);
            }
        }
        if (ts1[i].c != ts2[i].c) {
            errs++;
            if (verbose) {
                fprintf(stderr, "ts2[%d].c = %d; should be %d\n",
                        i, (int) ts2[i].c, (int) ts1[i].c);
            }
        }
        if (ts1[i].d != ts2[i].d) {
            errs++;
            if (verbose) {
                fprintf(stderr, "ts2[%d].d = %d; should be %d\n",
                        i, (int) ts2[i].d, (int) ts1[i].d);
            }
        }
        if (ts1[i].e != ts2[i].e) {
            errs++;
            if (verbose) {
                fprintf(stderr, "ts2[%d].e = %d; should be %d\n", i, ts2[i].e, ts1[i].e);
            }
        }
    }

    return errs;
}

int struct_of_structs_test(void)
{
    int i, j, err, errs = 0, bufsize, position;

    char buf[50], buf2[50], *packbuf;

    MPI_Aint disps[2] = { 0, 3 };
    int blks[2] = { 2, 1 };
    MPI_Datatype types[2], chartype, tiletype1, tiletype2, tmptype, finaltype;

    /* build a contig of one char to try to keep optimizations
     * from being applied.
     */
    err = MPI_Type_contiguous(1, MPI_CHAR, &chartype);
    if (err != MPI_SUCCESS) {
        errs++;
        if (verbose) {
            fprintf(stderr, "chartype create failed\n");
        }
        return errs;
    }

    /* build a type that we can tile a few times */
    types[0] = MPI_CHAR;
    types[1] = chartype;

    err = MPI_Type_create_struct(2, blks, disps, types, &tiletype1);
    if (err != MPI_SUCCESS) {
        errs++;
        if (verbose) {
            fprintf(stderr, "tiletype1 create failed\n");
        }
        return errs;
    }

    /* build the same type again, again to avoid optimizations */
    err = MPI_Type_create_struct(2, blks, disps, types, &tiletype2);
    if (err != MPI_SUCCESS) {
        errs++;
        if (verbose) {
            fprintf(stderr, "tiletype2 create failed\n");
        }
        return errs;
    }

    /* build a combination of those two tiletypes */
    disps[0] = 0;
    disps[1] = 5;
    blks[0] = 1;
    blks[1] = 1;
    types[0] = tiletype1;
    types[1] = tiletype2;
    err = MPI_Type_create_struct(2, blks, disps, types, &tmptype);
    if (err != MPI_SUCCESS) {
        errs++;
        if (verbose) {
            fprintf(stderr, "tmptype create failed\n");
        }
        return errs;
    }

    err = MPI_Type_create_resized(tmptype, 0, 10, &finaltype);
    if (err != MPI_SUCCESS) {
        errs++;
        if (verbose) {
            fprintf(stderr, "finaltype create failed\n");
        }
        return errs;
    }

    MPI_Type_commit(&finaltype);
    MPI_Type_free(&chartype);
    MPI_Type_free(&tiletype1);
    MPI_Type_free(&tiletype2);
    MPI_Type_free(&tmptype);

    MPI_Pack_size(5, finaltype, MPI_COMM_WORLD, &bufsize);

    packbuf = malloc(bufsize);
    if (packbuf == NULL) {
        errs++;
        if (verbose) {
            fprintf(stderr, "pack buffer allocation (%d bytes) failed\n", bufsize);
        }
        return errs;
    }

    for (j = 0; j < 10; j++) {
        for (i = 0; i < 5; i++) {
            if (i == 2 || i == 4)
                buf[5 * j + i] = 0;
            else
                buf[5 * j + i] = i;
        }
    }

    position = 0;
    err = MPI_Pack(buf, 5, finaltype, packbuf, bufsize, &position, MPI_COMM_WORLD);
    if (err != MPI_SUCCESS) {
        errs++;
        if (verbose) {
            fprintf(stderr, "pack failed\n");
        }
        return errs;
    }

    memset(buf2, 0, 50);
    position = 0;
    err = MPI_Unpack(packbuf, bufsize, &position, buf2, 5, finaltype, MPI_COMM_WORLD);
    if (err != MPI_SUCCESS) {
        errs++;
        if (verbose) {
            fprintf(stderr, "unpack failed\n");
        }
        return errs;
    }

    for (j = 0; j < 10; j++) {
        for (i = 0; i < 5; i++) {
            if (buf[5 * j + i] != buf2[5 * j + i]) {
                errs++;
                if (verbose) {
                    fprintf(stderr,
                            "buf2[%d] = %d; should be %d\n",
                            5 * j + i, (int) buf2[5 * j + i], (int) buf[5 * j + i]);
                }
            }
        }
    }

    free(packbuf);
    MPI_Type_free(&finaltype);
    return errs;
}

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;
}