File: dataalign.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 (102 lines) | stat: -rw-r--r-- 3,079 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
/*
 * Copyright (C) by Argonne National Laboratory
 *     See COPYRIGHT in top-level directory
 */

#include "mpi.h"
#include <stdio.h>
/* The next is for isprint */
#include <ctype.h>
#include "mpitest.h"

int main(int argc, char *argv[])
{
    struct a {
        int i;
        char c;
    } s[10], s1[10];
    int j;
    int errs = 0;
    int rank, size, tsize;
    MPI_Aint text, tmp_lb;
    int blens[2];
    MPI_Aint disps[2];
    MPI_Datatype bases[2];
    MPI_Datatype str, con;
    MPI_Status status;

    MTest_Init(&argc, &argv);

    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    MPI_Comm_size(MPI_COMM_WORLD, &size);

    for (j = 0; j < 10; j++) {
        s[j].i = j + rank;
        s[j].c = j + rank + 'a';
    }

    blens[0] = blens[1] = 1;
    disps[0] = 0;
    disps[1] = sizeof(int);
    bases[0] = MPI_INT;
    bases[1] = MPI_CHAR;
    MPI_Type_create_struct(2, blens, disps, bases, &str);
    MPI_Type_commit(&str);
    MPI_Type_contiguous(10, str, &con);
    MPI_Type_commit(&con);
    MPI_Type_size(con, &tsize);
    MPI_Type_get_extent(con, &tmp_lb, &text);

    MTestPrintfMsg(0, "Size of MPI array is %d, extent is %d\n", tsize, text);

    /* The following block of code is only for verbose-level output */
    {
        void *p1, *p2;
        p1 = s;
        p2 = s + 10;
        MTestPrintfMsg(0,
                       "C array starts at %p and ends at %p for a length of %d\n",
                       s, &(s[9].c), (char *) p2 - (char *) p1);
    }

    MPI_Type_get_extent(str, &tmp_lb, &text);
    MPI_Type_size(str, &tsize);
    MTestPrintfMsg(0, "Size of MPI struct is %d, extent is %d\n", tsize, (int) text);
    MTestPrintfMsg(0, "Size of C struct is %d\n", sizeof(struct a));
    if (text != sizeof(struct a)) {
        fprintf(stderr,
                "Extent of struct a (%d) does not match sizeof (%d)\n",
                (int) text, (int) sizeof(struct a));
        errs++;
    }

    MPI_Send(s, 1, con, rank ^ 1, 0, MPI_COMM_WORLD);
    MPI_Recv(s1, 1, con, rank ^ 1, 0, MPI_COMM_WORLD, &status);

    for (j = 0; j < 10; j++) {
        MTestPrintfMsg(0, "%d Sent: %d %c, Got: %d %c\n", rank, s[j].i, s[j].c, s1[j].i, s1[j].c);
        if (s1[j].i != j + status.MPI_SOURCE) {
            errs++;
            fprintf(stderr, "Got s[%d].i = %d; expected %d\n", j, s1[j].i, j + status.MPI_SOURCE);
        }
        if (s1[j].c != (char) ('a' + j + status.MPI_SOURCE)) {
            errs++;
            /* If the character is not a printing character,
             * this can generate a file that diff, for example,
             * believes is a binary file */
            if (isprint((int) (s1[j].c))) {
                fprintf(stderr, "Got s[%d].c = %c; expected %c\n",
                        j, s1[j].c, j + status.MPI_SOURCE + 'a');
            } else {
                fprintf(stderr, "Got s[%d].c = %x; expected %c\n",
                        j, (int) s1[j].c, j + status.MPI_SOURCE + 'a');
            }
        }
    }

    MPI_Type_free(&str);
    MPI_Type_free(&con);

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