Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' into depencencies
[simgrid.git] / teshsuite / smpi / mpich3-test / io / simple_collective.c
1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
2 /*
3  *  (C) 2015 by Argonne National Laboratory.
4  *      See COPYRIGHT in top-level directory.
5  */
6
7
8 /* this deceptively simple test uncovered a bug in the way certain file systems
9  * dealt with tuning parmeters.  See
10  * https://github.com/open-mpi/ompi/issues/158 and
11  * http://trac.mpich.org/projects/mpich/ticket/2261
12  *
13  * originally uncovered in Darshan:
14  * http://lists.mcs.anl.gov/pipermail/darshan-users/2015-February/000256.html
15  *
16  * to really exercise the bug in simple_collective,
17  * we'd have to run on a Lustre or Panasas file system.
18  *
19  * I am surprised src/mpi/romio/test/create_excl.c did not uncover the bug
20  */
21
22 #include <assert.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <fcntl.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <unistd.h>
29 #include <string.h>
30 #include <sys/time.h>
31 #include <mpi.h>
32 #include <errno.h>
33 #include <getopt.h>
34
35 #include "simgrid/plugins/file_system.h"
36
37 static char *opt_file = NULL;
38 static int rank = -1;
39
40 static int parse_args(int argc, char **argv);
41 static void usage(const char *prog);
42 int test_write(char *file, int nprocs, int rank, MPI_Info info);
43 int test_write(char *file, int nprocs, int rank, MPI_Info info)
44 {
45     double stime, etime, wtime, w_elapsed, w_slowest, elapsed, slowest;
46     MPI_File fh;
47     int ret;
48     char buffer[700] = { 0 };
49     MPI_Status status;
50     int verbose = 0;
51
52     MPI_Barrier(MPI_COMM_WORLD);
53     stime = MPI_Wtime();
54
55     ret = MPI_File_open(MPI_COMM_WORLD, file,
56                         MPI_MODE_CREATE | MPI_MODE_WRONLY | MPI_MODE_EXCL, info, &fh);
57
58     if (ret != 0) {
59         fprintf(stderr, "Error: failed to open %s\n", file);
60         return 1;
61     }
62
63     etime = MPI_Wtime();
64
65     ret = MPI_File_write_at_all(fh, rank * 700, buffer, 700, MPI_BYTE, &status);
66     if (ret != 0) {
67         fprintf(stderr, "Error: failed to write %s\n", file);
68         return 1;
69     }
70
71     wtime = MPI_Wtime();
72
73     MPI_File_close(&fh);
74
75     elapsed = etime - stime;
76     w_elapsed = wtime - etime;
77     MPI_Reduce(&elapsed, &slowest, 1, MPI_DOUBLE, MPI_MAX, 0, MPI_COMM_WORLD);
78     MPI_Reduce(&w_elapsed, &w_slowest, 1, MPI_DOUBLE, MPI_MAX, 0, MPI_COMM_WORLD);
79
80     if (rank == 0) {
81         //unlink(file);
82         //to emulate unlink, use sg_file_unlink
83         sg_file_t fd = sg_file_open(file, NULL);
84         sg_file_unlink(fd);
85         slowest *= 1000.0;
86         w_slowest *= 1000.0;
87         if (verbose == 1) {
88             printf("file: %s, nprocs: %d, open_time: %f ms, write_time: %f ms\n",
89                    file, nprocs, slowest, w_slowest);
90         }
91     }
92     return 0;
93 }
94
95 int main(int argc, char **argv)
96 {
97     int nprocs;
98     char file[256];
99     MPI_Info info;
100     int nr_errors = 0;
101
102
103     MPI_Init(&argc, &argv);
104     MPI_Comm_rank(MPI_COMM_WORLD, &rank);
105     MPI_Comm_size(MPI_COMM_WORLD, &nprocs);
106
107     /* parse the command line arguments */
108     parse_args(argc, argv);
109
110     sprintf(file, "%s", opt_file);
111     MPI_Info_create(&info);
112     nr_errors += test_write(file, nprocs, rank, info);
113     /* acutal value does not matter.  test only writes a small amount of data */
114     MPI_Info_set(info, "striping_factor", "50");
115     nr_errors += test_write(file, nprocs, rank, info);
116     MPI_Info_free(&info);
117
118     MPI_Finalize();
119     if (!rank && nr_errors == 0) {
120         printf(" No Errors\n");
121     }
122     return (-nr_errors);
123 }
124
125 static int parse_args(int argc, char **argv)
126 {
127     int c;
128
129     while ((c = getopt(argc, argv, "e")) != EOF) {
130         switch (c) {
131         case 'h':
132             if (rank == 0)
133                 usage(argv[0]);
134             exit(0);
135         case '?':      /* unknown */
136             if (rank == 0)
137                 usage(argv[0]);
138             exit(1);
139         default:
140             break;
141         }
142     }
143
144     if (argc - optind != 1) {
145         if (rank == 0)
146             usage(argv[0]);
147         exit(1);
148     }
149
150     opt_file = strdup(argv[optind]);
151     assert(opt_file!=NULL);
152
153     return (0);
154 }
155
156 static void usage(const char *prog)
157 {
158     printf("Usage: %s [<OPTIONS>...] <FILE NAME>\n", prog);
159     printf("\n<OPTIONS> is one or more of\n");
160     printf(" -h       print this help\n");
161 }
162
163 /*
164  * vim: ts=8 sts=4 sw=4 noexpandtab
165  */