File: resized.c

package info (click to toggle)
mpich 5.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 251,828 kB
  • sloc: ansic: 1,323,147; cpp: 82,869; f90: 72,420; javascript: 40,763; perl: 28,296; sh: 19,399; python: 16,191; xml: 14,418; makefile: 9,468; fortran: 8,046; java: 4,635; pascal: 352; asm: 324; ruby: 176; awk: 27; lisp: 19; php: 8; sed: 4
file content (144 lines) | stat: -rw-r--r-- 4,409 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
/*
 * Copyright (C) by Argonne National Laboratory
 *     See COPYRIGHT in top-level directory
 */

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

/*
static char MTEST_Descrip[] = "Test file views with MPI_Type_create_resized";
*/

int main(int argc, char **argv)
{
    int i, nprocs, len, mpi_errno, buf[2], newbuf[4];
    int errs = 0;
    MPI_Offset size;
    MPI_Aint lb, extent;
    MPI_File fh;
    char *filename;
    MPI_Datatype newtype;

    MTest_Init(&argc, &argv);
    MPI_Comm_size(MPI_COMM_WORLD, &nprocs);

    if (nprocs != 1) {
        fprintf(stderr, "Run this program on 1 process\n");
        MPI_Abort(MPI_COMM_WORLD, 1);
    }

    i = 1;
    while ((i < argc) && strcmp("-fname", *argv)) {
        i++;
        argv++;
    }
    if (i >= argc) {
        len = 8;
        filename = (char *) malloc(len + 10);
        strcpy(filename, "testfile");
        /*
         * fprintf(stderr, "\n*#  Usage: resized -fname filename\n\n");
         * MPI_Abort(MPI_COMM_WORLD, 1);
         */
    } else {
        argv++;
        len = (int) strlen(*argv);
        filename = (char *) malloc(len + 1);
        strcpy(filename, *argv);
    }

    /* delete the file in case it exists. If it exists, it should return MPI_ERR_NO_SUCH_FILE */
    mpi_errno = MPI_File_delete(filename, MPI_INFO_NULL);
    if (mpi_errno != MPI_SUCCESS) {
        int error_class = 0;
        MPI_Error_class(mpi_errno, &error_class);
        if (error_class != MPI_ERR_NO_SUCH_FILE) {
            printf
                ("MPI_File_delete returned error code %x, error class %x, expect error class MPI_ERR_NO_SUCH_FILE\n",
                 mpi_errno, error_class);
            errs++;
        }
    }

    /* create a resized type comprising an integer with an lb at sizeof(int) and extent = 3*sizeof(int) */
    lb = sizeof(int);
    extent = 3 * sizeof(int);
    MPI_Type_create_resized(MPI_INT, lb, extent, &newtype);

    MPI_Type_commit(&newtype);

    /* initialize the file */
    MPI_File_open(MPI_COMM_WORLD, filename, MPI_MODE_CREATE | MPI_MODE_RDWR, MPI_INFO_NULL, &fh);
    for (i = 0; i < 4; i++)
        newbuf[i] = 55;
    MPI_File_write(fh, newbuf, 4, MPI_INT, MPI_STATUS_IGNORE);
    MPI_File_close(&fh);

    /* write 2 ints into file view with resized type */

    buf[0] = 10;
    buf[1] = 20;

    MPI_File_open(MPI_COMM_WORLD, filename, MPI_MODE_CREATE | MPI_MODE_RDWR, MPI_INFO_NULL, &fh);

    mpi_errno = MPI_File_set_view(fh, 0, MPI_INT, newtype, (char *) "native", MPI_INFO_NULL);
    if (mpi_errno != MPI_SUCCESS)
        errs++;

    MPI_File_write(fh, buf, 2, MPI_INT, MPI_STATUS_IGNORE);

    MPI_File_close(&fh);


    /* read back file view with resized type  and verify */

    MPI_File_open(MPI_COMM_WORLD, filename, MPI_MODE_RDONLY, MPI_INFO_NULL, &fh);

    mpi_errno = MPI_File_set_view(fh, 0, MPI_INT, newtype, (char *) "native", MPI_INFO_NULL);
    if (mpi_errno != MPI_SUCCESS)
        errs++;

    for (i = 0; i < 4; i++)
        newbuf[i] = 100;
    MPI_File_read(fh, newbuf, 2, MPI_INT, MPI_STATUS_IGNORE);
    if ((newbuf[0] != 10) || (newbuf[1] != 20) || (newbuf[2] != 100) || (newbuf[3] != 100)) {
        errs++;
        fprintf(stderr,
                "newbuf[0] is %d, should be 10,\n newbuf[1] is %d, should be 20\n newbuf[2] is %d, should be 100,\n newbuf[3] is %d, should be 100,\n",
                newbuf[0], newbuf[1], newbuf[2], newbuf[3]);
    }

    MPI_File_close(&fh);

    /* read file back and verify */

    MPI_File_open(MPI_COMM_WORLD, filename, MPI_MODE_RDONLY, MPI_INFO_NULL, &fh);

    MPI_File_get_size(fh, &size);
    if (size != 4 * sizeof(int)) {
        errs++;
        fprintf(stderr, "file size is %lld, should be %d\n", (long long) size,
                (int) (4 * sizeof(int)));
    }

    for (i = 0; i < 4; i++)
        newbuf[i] = 100;
    MPI_File_read(fh, newbuf, 4, MPI_INT, MPI_STATUS_IGNORE);
    if ((newbuf[0] != 10) || (newbuf[3] != 20) || (newbuf[1] != 55) || (newbuf[2] != 55)) {
        errs++;
        fprintf(stderr,
                "newbuf[0] is %d, should be 10,\n newbuf[1] is %d, should be 55,\n newbuf[2] is %d, should be 55,\n newbuf[3] is %d, should be 20\n",
                newbuf[0], newbuf[1], newbuf[2], newbuf[3]);
    }

    MPI_File_close(&fh);

    MPI_Type_free(&newtype);
    free(filename);
    MTest_Finalize(errs);
    return MTestReturnValue(errs);
}