Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' into adrien
[simgrid.git] / teshsuite / smpi / mpich3-test / io / userioerr.c
1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
2 /*
3  *
4  *  (C) 2006 by Argonne National Laboratory.
5  *      See COPYRIGHT in top-level directory.
6  */
7 #include "mpi.h"
8 #include <stdio.h>
9 #include "mpitest.h"
10 #include "mpitestconf.h"
11
12 int verbose = 0;
13
14 int handlerCalled = 0;
15
16 /* Prototype to suppress compiler warnings */
17 void user_handler(MPI_File * fh, int *errcode, ...);
18
19 void user_handler(MPI_File * fh, int *errcode, ...)
20 {
21     if (*errcode != MPI_SUCCESS) {
22         handlerCalled++;
23         if (verbose) {
24             printf("In user_handler with code %d\n", *errcode);
25         }
26     }
27 }
28
29 int main(int argc, char *argv[])
30 {
31     int rank, errs = 0, rc;
32     MPI_Errhandler ioerr_handler;
33     MPI_Status status;
34     MPI_File fh;
35     char inbuf[80];
36
37     MTest_Init(&argc, &argv);
38
39     MPI_Comm_rank(MPI_COMM_WORLD, &rank);
40
41     /* Create a file to which to attach the handler */
42     rc = MPI_File_open(MPI_COMM_WORLD, (char *) "/scratch/test.txt",
43                        MPI_MODE_CREATE | MPI_MODE_WRONLY | MPI_MODE_DELETE_ON_CLOSE,
44                        MPI_INFO_NULL, &fh);
45     if (rc) {
46         errs++;
47         printf("Unable to open test.txt for writing\n");
48     }
49
50     rc = MPI_File_create_errhandler(user_handler, &ioerr_handler);
51     if (rc) {
52         errs++;
53         printf("MPI_File_create_Errhandler returned an error code: %d\n", rc);
54     }
55
56     rc = MPI_File_set_errhandler(fh, ioerr_handler);
57     if (rc) {
58         errs++;
59         printf("MPI_File_set_errhandler returned an error code: %d\n", rc);
60     }
61
62     /* avoid leaking the errhandler, safe because they have refcount semantics */
63     rc = MPI_Errhandler_free(&ioerr_handler);
64     if (rc) {
65         errs++;
66         printf("MPI_Errhandler_free returned an error code: %d\n", rc);
67     }
68
69     /* This should generate an error because the file mode is WRONLY */
70     rc = MPI_File_read_at(fh, 0, inbuf, 80, MPI_BYTE, &status);
71     if (handlerCalled != 1) {
72         errs++;
73         printf("User-defined error handler was not called\n");
74     }
75
76     rc = MPI_File_close(&fh);
77     if (rc) {
78         errs++;
79         printf("MPI_File_close returned an error code: %d\n", rc);
80     }
81
82     MTest_Finalize(errs);
83     MPI_Finalize();
84     return 0;
85 }