File: openerr.c

package info (click to toggle)
mpich 4.3.0%2Breally4.2.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, trixie
  • size: 419,120 kB
  • sloc: ansic: 1,215,557; cpp: 74,755; javascript: 40,763; f90: 20,649; sh: 18,463; xml: 14,418; python: 14,397; perl: 13,772; makefile: 9,279; fortran: 8,063; java: 4,553; asm: 324; ruby: 176; lisp: 19; php: 8; sed: 4
file content (144 lines) | stat: -rw-r--r-- 4,458 bytes parent folder | download | duplicates (3)
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 "mpitestconf.h"
#include <stdio.h>

#ifdef HAVE_STRING_H
#include <string.h>
#endif
#include "mpitest.h"

/*
 * openerr - Test that errors on file open are handled correctly and that the
 * returned error message is accessible
 */
#define BUFLEN 10

int main(int argc, char *argv[])
{
    MPI_File fh;
    char emsg[MPI_MAX_ERROR_STRING];
    int emsglen, err, ec, errs = 0;
    int amode, rank;
    const char *name = 0;
    MPI_Status st;
    char outbuf[BUFLEN], inbuf[BUFLEN];

    MTest_Init(&argc, &argv);

    name = "not-a-file-to-use";
    /* Try to open a file that does/should not exist */
    /* Note that no error message should be printed by MPI_File_open,
     * even when there is an error */
    err = MPI_File_open(MPI_COMM_WORLD, name, MPI_MODE_RDONLY, MPI_INFO_NULL, &fh);
    if (err == MPI_SUCCESS) {
        errs++;
        printf("Did not return error when opening a file that does not exist\n");
        MPI_File_close(&fh);
        MPI_File_delete(name, MPI_INFO_NULL);
    } else {
        MPI_Error_class(err, &ec);
        MPI_Error_string(err, emsg, &emsglen);
        MTestPrintfMsg(2, "Error msg from open: %s\n", emsg);
        if (ec != MPI_ERR_NO_SUCH_FILE && ec != MPI_ERR_IO) {
            errs++;
            printf("Did not return class ERR_NO_SUCH_FILE or ERR_IO\n");
            printf("Returned class %d, message %s\n", ec, emsg);
        }
    }

    /* Now, create a file, write data into it; close, then reopen as
     * read only and try to write to it */

    amode = MPI_MODE_CREATE | MPI_MODE_WRONLY;
    name = "mpio-test-openerrs";

    err = MPI_File_open(MPI_COMM_WORLD, name, amode, MPI_INFO_NULL, &fh);
    if (err) {
        errs++;
        MTestPrintErrorMsg("Unable to open file for writing", err);
    } else {
        MPI_Comm_rank(MPI_COMM_WORLD, &rank);
        memset(outbuf, 'A' + rank, BUFLEN);

        err = MPI_File_write_at(fh, rank * BUFLEN, outbuf, BUFLEN, MPI_BYTE, &st);
        if (err) {
            errs++;
            MTestPrintErrorMsg("Unable to write file", err);
        }
        MPI_File_close(&fh);
    }

    /* Now, open for read only, and delete on close */
    amode = MPI_MODE_RDONLY | MPI_MODE_DELETE_ON_CLOSE;

    err = MPI_File_open(MPI_COMM_WORLD, name, amode, MPI_INFO_NULL, &fh);
    if (err) {
        errs++;
        MTestPrintErrorMsg("Unable to reopen file for reading", err);
    } else {
        /* Try to read it */

        /* Clear buffer before reading into it */

        memset(inbuf, 0, BUFLEN);

        err = MPI_File_read_at(fh, rank * BUFLEN, inbuf, BUFLEN, MPI_BYTE, &st);
        if (err) {
            errs++;
            MTestPrintErrorMsg("Unable to read file", err);
        }

        /* Try to write it (should fail) */
        err = MPI_File_write_at(fh, rank * BUFLEN, outbuf, BUFLEN, MPI_BYTE, &st);
        if (err == MPI_SUCCESS) {
            errs++;
            printf("Write operation succeeded to read-only file\n");
        } else {
            /* Look at error class */
            MPI_Error_class(err, &ec);
            if (ec != MPI_ERR_READ_ONLY && ec != MPI_ERR_ACCESS) {
                errs++;
                printf("Unexpected error class %d when writing to read-only file\n", ec);
                MTestPrintErrorMsg("Error msg is: ", err);
            }
        }
        err = MPI_File_close(&fh);
        if (err) {
            errs++;
            MTestPrintErrorMsg("Failed to close", err);
        }

        /* No MPI_Barrier is required here */

        /*
         *  Test open without CREATE to see if DELETE_ON_CLOSE worked.
         *  This should fail if file was deleted correctly.
         */

        amode = MPI_MODE_RDONLY;
        err = MPI_File_open(MPI_COMM_WORLD, name, amode, MPI_INFO_NULL, &fh);
        if (err == MPI_SUCCESS) {
            errs++;
            printf("File was not deleted!\n");
            MPI_File_close(&fh);
        } else {
            MPI_Error_class(err, &ec);
            if (ec != MPI_ERR_NO_SUCH_FILE && ec != MPI_ERR_IO) {
                errs++;
                printf("Did not return class ERR_NO_SUCH_FILE or ERR_IO\n");
                printf("Returned class %d, message %s\n", ec, emsg);
            }
        }
    }
    /* */

    /* Find out how many errors we saw */
    MTest_Finalize(errs);

    return MTestReturnValue(errs);
}