File: large_count.c

package info (click to toggle)
mpich 4.3.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 101,184 kB
  • sloc: ansic: 1,040,629; cpp: 82,270; javascript: 40,763; perl: 27,933; python: 16,041; sh: 14,676; xml: 14,418; f90: 12,916; makefile: 9,270; fortran: 8,046; java: 4,635; asm: 324; ruby: 103; awk: 27; lisp: 19; php: 8; sed: 4
file content (183 lines) | stat: -rw-r--r-- 5,899 bytes parent folder | download | duplicates (2)
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

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <mpi.h>


static int Errors;

#define ERROR(fname) \
    if (err != MPI_SUCCESS) { \
        int errorStringLen; \
        char errorString[MPI_MAX_ERROR_STRING]; \
        MPI_Error_string(err, errorString, &errorStringLen); \
	Errors++; \
        printf("Error at line %d when calling %s: %s\n",__LINE__,fname,errorString); \
    }

#define LEN 2048
#define NVARS 1100

/*----< main() >------------------------------------------------------------*/
int main(int argc, char **argv)
{
    char *filename;
    size_t i, buf_len;
    int err, rank, verbose = 1, nprocs, psize[2], gsize[2], count[2], start[2];
    char *buf;
    MPI_File fh;
    MPI_Datatype subType, filetype, buftype;
    MPI_Status status;
    MPI_Offset fsize;
    int array_of_blocklengths[NVARS];
    MPI_Aint array_of_displacements[NVARS];
    MPI_Datatype array_of_types[NVARS];

    MPI_Init(&argc, &argv);
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    MPI_Comm_size(MPI_COMM_WORLD, &nprocs);

    if (argc != 2) {
        if (!rank)
            printf("Usage: %s filename\n", argv[0]);
        MPI_Finalize();
        exit(1);
    }
    filename = argv[1];

    /* Creates a division of processors in a cartesian grid */
    psize[0] = psize[1] = 0;
    err = MPI_Dims_create(nprocs, 2, psize);
    ERROR("MPI_Dims_create");

    /* each 2D variable is of size gsizes[0] x gsizes[1] bytes */
    gsize[0] = LEN * psize[0];
    gsize[1] = LEN * psize[1];

    /* set subarray offset and length */
    start[0] = LEN * (rank / psize[1]);
    start[1] = LEN * (rank % psize[1]);
    count[0] = LEN - 1; /* -1 to create holes */
    count[1] = LEN - 1;

    fsize = (MPI_Offset) gsize[0] * gsize[1] * NVARS - (LEN + 1);
    if (verbose) {
        buf_len = (size_t) NVARS *(LEN - 1) * (LEN - 1);
        if (rank == 0) {
            printf("nprocs=%d NVARS=%d LEN=%d\n", nprocs, NVARS, LEN);
            printf("Expecting file size=%lld bytes (%.1f MB, %.1f GB)\n",
                   (long long) fsize, (float) fsize / 1048576, (float) fsize / 1073741824);
            printf("Each global variable is of size %d bytes (%.1f MB)\n",
                   gsize[0] * gsize[1], (float) gsize[0] * gsize[1] / 1048576);
            printf("Each process writes %zd bytes (%.1f MB, %.1f GB)\n",
                   buf_len, (float) buf_len / 1048576, (float) buf_len / 1073741824);
        }
        printf("rank %3d: gsize=%4d %4d start=%4d %4d count=%4d %4d\n", rank,
               gsize[0], gsize[1], start[0], start[1], count[0], count[1]);
    }


    /* create 2D subarray datatype for fileview */
    err = MPI_Type_create_subarray(2, gsize, count, start, MPI_ORDER_C, MPI_BYTE, &subType);
    ERROR("MPI_Type_create_subarray");
    err = MPI_Type_commit(&subType);
    ERROR("MPI_Type_commit");

    /* create a filetype by concatenating NVARS subType */
    for (i = 0; i < NVARS; i++) {
        array_of_blocklengths[i] = 1;
        array_of_displacements[i] = gsize[0] * gsize[1] * i;
        array_of_types[i] = subType;
    }
    err = MPI_Type_create_struct(NVARS, array_of_blocklengths,
                                 array_of_displacements, array_of_types, &filetype);
    ERROR("MPI_Type_create_struct");
    err = MPI_Type_commit(&filetype);
    ERROR("MPI_Type_commit");
    err = MPI_Type_free(&subType);
    ERROR("MPI_Type_free");

    /* Create local buffer datatype: each 2D variable is of size LEN x LEN */
    gsize[0] = LEN;
    gsize[1] = LEN;
    start[0] = 0;
    start[1] = 0;
    count[0] = LEN - 1; /* -1 to create holes */
    count[1] = LEN - 1;

    err = MPI_Type_create_subarray(2, gsize, count, start, MPI_ORDER_C, MPI_BYTE, &subType);
    ERROR("MPI_Type_create_subarray");
    err = MPI_Type_commit(&subType);
    ERROR("MPI_Type_commit");

    /* concatenate NVARS subType into a buftype */
    for (i = 0; i < NVARS; i++) {
        array_of_blocklengths[i] = 1;
        array_of_displacements[i] = LEN * LEN * i;
        array_of_types[i] = subType;
    }

    /* create a buftype by concatenating NVARS subTypes */
    err = MPI_Type_create_struct(NVARS, array_of_blocklengths,
                                 array_of_displacements, array_of_types, &buftype);
    ERROR("MPI_Type_create_struct");
    err = MPI_Type_commit(&buftype);
    ERROR("MPI_Type_commit");
    err = MPI_Type_free(&subType);
    ERROR("MPI_Type_free");

    /* allocate a local buffer */
    buf_len = (size_t) NVARS *LEN * LEN;
    buf = (char *) malloc(buf_len);
    for (i = 0; i < buf_len; i++)
        buf[i] = (char) rank;

    /* open to create a file */
    err =
        MPI_File_open(MPI_COMM_WORLD, filename, MPI_MODE_CREATE | MPI_MODE_RDWR, MPI_INFO_NULL,
                      &fh);
    ERROR("MPI_File_open");

    /* set the file view */
    err = MPI_File_set_view(fh, 0, MPI_BYTE, filetype, "native", MPI_INFO_NULL);
    ERROR("MPI_File_set_view");

    /* MPI collective write */
    err = MPI_File_write_all(fh, buf, 1, buftype, &status);
    ERROR("MPI_File_write_all");

    /* do it again but use the non-blocking collectives */
    err = MPI_File_seek(fh, 0, MPI_SEEK_SET);
    ERROR("MPI_File_seek");
    MPI_Request req;
    err = MPI_File_iwrite_all(fh, buf, 1, buftype, &req);
    ERROR("MPI_File_write_all");
    MPI_Wait(&req, &status);

    err = MPI_File_read_all(fh, buf, 1, buftype, &status);
    ERROR("MPI_File_read_all");

    err = MPI_File_seek(fh, 0, MPI_SEEK_SET);
    ERROR("MPI_File_seek");
    err = MPI_File_iread_all(fh, buf, 1, buftype, &req);
    MPI_Wait(&req, &status);


    MPI_File_close(&fh);
    free(buf);

    err = MPI_Type_free(&filetype);
    ERROR("MPI_Type_free");
    err = MPI_Type_free(&buftype);
    ERROR("MPI_Type_free");

    if (Errors > 0) {
        printf(" Found %d errors\n", Errors);
    } else {
        printf(" No Errors\n");
    }

    MPI_Finalize();
    return 0;
}