File: indexed_misc.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 (673 lines) | stat: -rw-r--r-- 19,880 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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
/*
 * Copyright (C) by Argonne National Laboratory
 *     See COPYRIGHT in top-level directory
 */

#include "mpi.h"
#include <stdio.h>
#include <stdlib.h>
#include "mpitestconf.h"
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#include <assert.h>
#include <limits.h>
#include "mpitest.h"

static int verbose = 1;

#define check(cond_)                                                                             \
    do {                                                                                         \
        if (!(cond_)) {                                                                          \
            if (verbose) {                                                                       \
                fprintf(stderr, "condition '%s' does not hold, at line %d\n", #cond_, __LINE__); \
            }                                                                                    \
            errs += 1;                                                                           \
        }                                                                                        \
    } while (0)

#define check_err(err_, what_failed_)                                                 \
    do {                                                                              \
        if (err_) {                                                                   \
            if (verbose) {                                                            \
                fprintf(stderr, "error: %s, at line %d\n", (what_failed_), __LINE__); \
            }                                                                         \
            errs += (err_);                                                           \
        }                                                                             \
    } while (0)

/* tests */
int indexed_contig_test(void);
int indexed_zeroblock_first_test(void);
int indexed_zeroblock_middle_test(void);
int indexed_zeroblock_last_test(void);
int indexed_contig_leading_zero_test(void);
int indexed_same_lengths(void);

/* helper functions */
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 = indexed_contig_test();
    if (err && verbose)
        fprintf(stderr, "%d errors in indexed_contig_test.\n", err);
    errs += err;

    err = indexed_zeroblock_first_test();
    if (err && verbose)
        fprintf(stderr, "%d errors in indexed_zeroblock_first_test.\n", err);
    errs += err;

    err = indexed_zeroblock_middle_test();
    if (err && verbose)
        fprintf(stderr, "%d errors in indexed_zeroblock_middle_test.\n", err);
    errs += err;

    err = indexed_zeroblock_last_test();
    if (err && verbose)
        fprintf(stderr, "%d errors in indexed_zeroblock_last_test.\n", err);
    errs += err;

    err = indexed_contig_leading_zero_test();
    if (err && verbose)
        fprintf(stderr, "%d errors in indexed_contig_leading_zero_test.\n", err);
    errs += err;

    err = indexed_same_lengths();
    if (err && verbose)
        fprintf(stderr, "%d errors in indexed_contig_leading_zero_test.\n", err);
    errs += err;

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

int indexed_zeroblock_first_test(void)
{
    int err, errs = 0;

    MPI_Datatype type;
    int len[3] = { 0, 1, 1 };
    int disp[3] = { 0, 1, 4 };
    MPI_Aint lb, ub, extent;

    err = MPI_Type_indexed(3, len, disp, MPI_INT, &type);
    if (err != MPI_SUCCESS) {
        if (verbose) {
            fprintf(stderr, "error creating indexed type in indexed_zeroblock_first_test()\n");
        }
        errs += 1;
    }

    MPI_Type_get_extent(type, &lb, &extent);
    if (lb != sizeof(int)) {
        if (verbose) {
            fprintf(stderr, "lb mismatch; is %d, should be %d\n", (int) lb, (int) sizeof(int));
        }
        errs++;
    }
    ub = lb + extent;
    if (ub != 5 * sizeof(int)) {
        if (verbose) {
            fprintf(stderr,
                    "ub mismatch; is %d, should be %d\n", (int) ub, (int) (5 * sizeof(int)));
        }
        errs++;
    }

    MPI_Type_free(&type);

    return errs;
}

int indexed_zeroblock_middle_test(void)
{
    int err, errs = 0;

    MPI_Datatype type;
    int len[3] = { 1, 0, 1 };
    int disp[3] = { 1, 2, 4 };
    MPI_Aint lb, ub, extent;

    err = MPI_Type_indexed(3, len, disp, MPI_INT, &type);
    if (err != MPI_SUCCESS) {
        if (verbose) {
            fprintf(stderr, "error creating indexed type in indexed_zeroblock_middle_test()\n");
        }
        errs += 1;
    }

    MPI_Type_get_extent(type, &lb, &extent);
    if (lb != sizeof(int)) {
        if (verbose) {
            fprintf(stderr, "lb mismatch; is %d, should be %d\n", (int) lb, (int) sizeof(int));
        }
        errs++;
    }
    ub = lb + extent;
    if (ub != 5 * sizeof(int)) {
        if (verbose) {
            fprintf(stderr,
                    "ub mismatch; is %d, should be %d\n", (int) ub, (int) (5 * sizeof(int)));
        }
        errs++;
    }

    MPI_Type_free(&type);

    return errs;
}

int indexed_zeroblock_last_test(void)
{
    int err, errs = 0;

    MPI_Datatype type;
    int len[3] = { 1, 1, 0 };
    int disp[3] = { 1, 4, 8 };
    MPI_Aint lb, ub, extent;

    err = MPI_Type_indexed(3, len, disp, MPI_INT, &type);
    if (err != MPI_SUCCESS) {
        if (verbose) {
            fprintf(stderr, "error creating indexed type in indexed_zeroblock_last_test()\n");
        }
        errs += 1;
    }

    MPI_Type_get_extent(type, &lb, &extent);
    if (lb != sizeof(int)) {
        if (verbose) {
            fprintf(stderr, "lb mismatch; is %d, should be %d\n", (int) lb, (int) sizeof(int));
        }
        errs++;
    }
    ub = lb + extent;
    if (ub != 5 * sizeof(int)) {
        if (verbose) {
            fprintf(stderr,
                    "ub mismatch; is %d, should be %d\n", (int) ub, (int) (5 * sizeof(int)));
        }
        errs++;
    }

    MPI_Type_free(&type);

    return errs;
}

/* indexed_contig_test()
 *
 * Tests behavior with an indexed array that can be compacted but should
 * continue to be stored as an indexed type.  Specifically for coverage.
 *
 * Returns the number of errors encountered.
 */
int indexed_contig_test(void)
{
    int buf[9] = { -1, 1, 2, 3, -2, 4, 5, -3, 6 };
    int err, errs = 0;

    int i, count = 5;
    int blklen[] = { 1, 2, 1, 1, 1 };
    int disp[] = { 1, 2, 5, 6, 8 };
    MPI_Datatype newtype;

    int size, int_size;

    err = MPI_Type_indexed(count, blklen, disp, MPI_INT, &newtype);
    if (err != MPI_SUCCESS) {
        if (verbose) {
            fprintf(stderr, "error creating indexed type in indexed_contig_test()\n");
        }
        errs++;
    }

    MPI_Type_size(MPI_INT, &int_size);

    err = MPI_Type_size(newtype, &size);
    if (err != MPI_SUCCESS) {
        if (verbose) {
            fprintf(stderr, "error obtaining type size in indexed_contig_test()\n");
        }
        errs++;
    }

    if (size != 6 * int_size) {
        if (verbose) {
            fprintf(stderr, "error: size != 6 * int_size in indexed_contig_test()\n");
        }
        errs++;
    }

    MPI_Type_commit(&newtype);

    err = pack_and_unpack((char *) buf, 1, newtype, 9 * sizeof(int));
    if (err != 0) {
        if (verbose) {
            fprintf(stderr, "error packing/unpacking in indexed_contig_test()\n");
        }
        errs += err;
    }

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

        switch (i) {
            case 1:
                goodval = 1;
                break;
            case 2:
                goodval = 2;
                break;
            case 3:
                goodval = 3;
                break;
            case 5:
                goodval = 4;
                break;
            case 6:
                goodval = 5;
                break;
            case 8:
                goodval = 6;
                break;
            default:
                goodval = 0;    /* pack_and_unpack() zeros before unpack */
                break;
        }
        if (buf[i] != goodval) {
            errs++;
            if (verbose)
                fprintf(stderr, "buf[%d] = %d; should be %d\n", i, buf[i], goodval);
        }
    }

    MPI_Type_free(&newtype);

    return errs;
}

/* very similar to indexed_zeroblock_first_test, but only has a single contig in
 * order to catch a particular optimization path in MPICH */
int indexed_contig_leading_zero_test(void)
{
    int err, errs = 0;

    int i;
    MPI_Datatype type = MPI_DATATYPE_NULL;
    MPI_Datatype struct_type = MPI_DATATYPE_NULL;
    MPI_Datatype types[2];
    int len[3] = { 0, 4, 0 };
    int disp[3] = { INT_MAX, 2, INT_MAX };
    MPI_Aint adisp[3];
    MPI_Aint lb, ub, extent;
    int *buf = NULL;

    err = MPI_Type_indexed(3, len, disp, MPI_INT, &type);
    check_err(err, "creating indexed type in indexed_contig_leading_zero_test()");
    err = MPI_Type_commit(&type);
    check_err(err, "committing indexed type in indexed_contig_leading_zero_test()");

    MPI_Type_get_extent(type, &lb, &extent);
    check(lb == 2 * sizeof(int));
    ub = lb + extent;
    check(ub == 6 * sizeof(int));

    /* make sure packing/unpacking works (hits a simple "is_contig" case in
     * MPICH's pack/unpack routines) */
    buf = malloc(10 * sizeof(int));
    assert(buf != NULL);
    for (i = 0; i < 10; ++i) {
        buf[i] = i + 1;
    }
    err = pack_and_unpack((char *) buf, 1, type, 10 * sizeof(int));
    check_err(err, "packing/unpacking in indexed_contig_leading_zero_test()");
    for (i = 0; i < 10; ++i) {
        int expected;
        if (i >= 2 && i < 6)
            expected = i + 1;
        else
            expected = 0;
        check(buf[i] == expected);
    }
    free(buf);

    /* -------------------------------------------------------------------- */
    /* A more rigorous test of the indexed type.  Use a hard-to-optimize struct
     * type to force a more complicated datatype processing path */
    len[0] = 1;
    len[1] = 1;
    adisp[0] = 0;
    adisp[1] = 8 * sizeof(int);
    types[0] = type;
    types[1] = MPI_INT;

    /* struct layout: xx0123xx4x ('x' indicates a hole), one char is an
     * MPI_INT */
    MPI_Type_create_struct(2, len, adisp, types, &struct_type);
    check_err(err, "creating struct type in indexed_contig_leading_zero_test()");
    err = MPI_Type_commit(&struct_type);
    check_err(err, "committing struct type in indexed_contig_leading_zero_test()");

    buf = malloc(10 * sizeof(int));
    assert(buf != NULL);
    for (i = 0; i < 10; ++i) {
        buf[i] = i + 1;
    }
    err = pack_and_unpack((char *) buf, 1, struct_type, 10 * sizeof(int));
    check_err(err, "packing/unpacking in indexed_contig_test()");

    for (i = 0; i < 10; ++i) {
        int expected;
        if ((i >= 2 && i < 6) || i == 8)
            expected = i + 1;
        else
            expected = 0;
        check(buf[i] == expected);
    }
    free(buf);

    MPI_Type_free(&struct_type);
    MPI_Type_free(&type);

    /* -------------------------------------------------------------------- */
    /* now do the same as above, but with hindexed */
    len[0] = 0;
    len[1] = 4;
    len[2] = 0;
    /* use *_MAX vars to improve our chances of hitting any pointer-casting
     * bugs in a big way (segfaults, etc.) */
    /* FIXME: This should also look at long, or use a different approach */
#if defined(HAVE_LONG_LONG) && defined(LLONG_MAX)
    if (sizeof(MPI_Aint) == sizeof(long long)) {
        adisp[0] = (MPI_Aint) LLONG_MAX;
        adisp[1] = 2 * sizeof(int);
        adisp[2] = (MPI_Aint) LLONG_MAX;
    } else
#endif
    {
        adisp[0] = (MPI_Aint) INT_MAX;
        adisp[1] = 2 * sizeof(int);
        adisp[2] = (MPI_Aint) INT_MAX;
    }

    err = MPI_Type_create_hindexed(3, len, adisp, MPI_INT, &type);
    check_err(err, "creating hindexed type in indexed_contig_leading_zero_test()");

    err = MPI_Type_commit(&type);
    check_err(err, "committing hindexed type in indexed_contig_leading_zero_test()");

    MPI_Type_get_extent(type, &lb, &extent);
    check(lb == 2 * sizeof(int));
    ub = lb + extent;
    check(ub == 6 * sizeof(int));

    buf = malloc(10 * sizeof(int));
    assert(buf != NULL);
    for (i = 0; i < 10; ++i) {
        buf[i] = i + 1;
    }
    err = pack_and_unpack((char *) buf, 1, type, 10 * sizeof(int));
    check_err(err, "packing/unpacking in indexed_contig_test()");

    for (i = 0; i < 10; ++i) {
        int expected;
        if (i >= 2 && i < 6)
            expected = i + 1;
        else
            expected = 0;
        check(buf[i] == expected);
    }
    free(buf);


    /* -------------------------------------------------------------------- */
    /* A more rigorous test of the hindexed type.  Use a hard-to-optimize struct
     * type to force a more complicated datatype processing path */
    len[0] = 1;
    len[1] = 1;
    adisp[0] = 0;
    adisp[1] = 8 * sizeof(int);

    /* struct layout: xx0123xx4x ('x' indicates a hole), one char is an
     * MPI_INT */
    err = MPI_Type_create_struct(2, len, adisp, types, &struct_type);
    check_err(err, "committing struct type in indexed_contig_leading_zero_test()");
    err = MPI_Type_commit(&struct_type);
    check_err(err, "committing struct type in indexed_contig_leading_zero_test()");

    buf = malloc(10 * sizeof(int));
    assert(buf != NULL);
    for (i = 0; i < 10; ++i) {
        buf[i] = i + 1;
    }
    /* fails in old MPICH (3.0rc1 and earlier), despite correct ub/lb
     * determination */
    err = pack_and_unpack((char *) buf, 1, struct_type, 10 * sizeof(int));
    check_err(err, "packing/unpacking in indexed_contig_test()");

    for (i = 0; i < 10; ++i) {
        int expected;
        if ((i >= 2 && i < 6) || i == 8)
            expected = i + 1;
        else
            expected = 0;
        check(buf[i] == expected);
    }
    free(buf);

    MPI_Type_free(&struct_type);
    MPI_Type_free(&type);

    return errs;
}

/* Test an indexed (and hindexed) type where the block length is the same for
 * all blocks, but with differing displacements so that it cannot directly be
 * converted to a vector type.  It is also important to add a dummy element at
 * the beginning in order to cause int/MPI_Aint misalignment for the
 * displacement of the first non-zero-width component. */
int indexed_same_lengths(void)
{
    int err, errs = 0;

    int i;
    MPI_Datatype type = MPI_DATATYPE_NULL;
    int len[4];
    int disp[4];
    MPI_Aint adisp[4];
    MPI_Aint lb, ub, extent;
    int *buf = NULL;

    len[0] = 0;
    len[1] = 1;
    len[2] = 1;
    len[3] = 1;

    disp[0] = 0;
    disp[1] = 1;
    disp[2] = 3;
    disp[3] = 8;

    err = MPI_Type_indexed(4, len, disp, MPI_INT, &type);
    check_err(err, "creating indexed type in indexed_same_lengths()");
    err = MPI_Type_commit(&type);
    check_err(err, "committing indexed type in indexed_same_lengths()");

    MPI_Type_get_extent(type, &lb, &extent);
    check(lb == 1 * sizeof(int));
    ub = lb + extent;
    check(ub == 9 * sizeof(int));

    buf = malloc(10 * sizeof(int));
    assert(buf != NULL);
    for (i = 0; i < 10; ++i) {
        buf[i] = i + 1;
    }
    err = pack_and_unpack((char *) buf, 1, type, 10 * sizeof(int));
    check_err(err, "packing/unpacking in indexed_same_lengths()");
    for (i = 0; i < 10; ++i) {
        int expected;
        if (i == 1 || i == 3 || i == 8)
            expected = i + 1;
        else
            expected = 0;
        check(buf[i] == expected);
    }
    free(buf);

    MPI_Type_free(&type);

    /* -------------------------------------------------------------------- */
    /* now do the same as above, but with hindexed */
    len[0] = 0;
    len[1] = 1;
    len[2] = 1;
    len[3] = 1;

    adisp[0] = 0 * sizeof(int);
    adisp[1] = 1 * sizeof(int);
    adisp[2] = 3 * sizeof(int);
    adisp[3] = 8 * sizeof(int);

    err = MPI_Type_create_hindexed(4, len, adisp, MPI_INT, &type);
    check_err(err, "creating hindexed type in indexed_same_lengths()");
    err = MPI_Type_commit(&type);
    check_err(err, "committing hindexed type in indexed_same_lengths()");

    MPI_Type_get_extent(type, &lb, &extent);
    check(lb == 1 * sizeof(int));
    ub = lb + extent;
    check(ub == 9 * sizeof(int));

    buf = malloc(10 * sizeof(int));
    assert(buf != NULL);
    for (i = 0; i < 10; ++i) {
        buf[i] = i + 1;
    }
    err = pack_and_unpack((char *) buf, 1, type, 10 * sizeof(int));
    check_err(err, "packing/unpacking in indexed_same_lengths()");
    for (i = 0; i < 10; ++i) {
        int expected;
        if (i == 1 || i == 3 || i == 8)
            expected = i + 1;
        else
            expected = 0;
        check(buf[i] == expected);
    }
    free(buf);

    MPI_Type_free(&type);

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

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

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