File: simple_collective.c

package info (click to toggle)
mpich 4.0.2-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 423,384 kB
  • sloc: ansic: 1,088,434; cpp: 71,364; javascript: 40,763; f90: 22,829; sh: 17,463; perl: 14,773; xml: 14,418; python: 10,265; makefile: 9,246; fortran: 8,008; java: 4,355; asm: 324; ruby: 176; lisp: 19; php: 8; sed: 4
file content (153 lines) | stat: -rw-r--r-- 3,906 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
145
146
147
148
149
150
151
152
153
/*
 * Copyright (C) by Argonne National Laboratory
 *     See COPYRIGHT in top-level directory
 */


/* this deceptively simple test uncovered a bug in the way certain file systems
 * dealt with tuning parameters.  See
 * https://github.com/open-mpi/ompi/issues/158 and
 * http://trac.mpich.org/projects/mpich/ticket/2261
 *
 * originally uncovered in Darshan:
 * http://lists.mcs.anl.gov/pipermail/darshan-users/2015-February/000256.html
 *
 * to really exercise the bug in simple_collective,
 * we'd have to run on a Lustre or Panasas file system.
 *
 * I am surprised src/mpi/romio/test/create_excl.c did not uncover the bug
 */

#include "mpitest.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <sys/time.h>
#include <mpi.h>
#include <errno.h>
#include <getopt.h>

static char *opt_file = NULL;

static int parse_args(int rank, int argc, char **argv);
static void usage(const char *prog);

static int test_write(char *file, int nprocs, int rank, MPI_Info info)
{
    double stime, etime, wtime, w_elapsed, w_slowest, elapsed, slowest;
    MPI_File fh;
    int ret;
    char buffer[700] = { 0 };
    MPI_Status status;
    int verbose = 0;

    MPI_Barrier(MPI_COMM_WORLD);
    stime = MPI_Wtime();

    ret = MPI_File_open(MPI_COMM_WORLD, file,
                        MPI_MODE_CREATE | MPI_MODE_WRONLY | MPI_MODE_EXCL, info, &fh);

    if (ret != 0) {
        fprintf(stderr, "Error: failed to open %s\n", file);
        return 1;
    }

    etime = MPI_Wtime();

    ret = MPI_File_write_at_all(fh, rank * 700, buffer, 700, MPI_BYTE, &status);
    if (ret != 0) {
        fprintf(stderr, "Error: failed to write %s\n", file);
        return 1;
    }

    wtime = MPI_Wtime();

    MPI_File_close(&fh);

    elapsed = etime - stime;
    w_elapsed = wtime - etime;
    MPI_Reduce(&elapsed, &slowest, 1, MPI_DOUBLE, MPI_MAX, 0, MPI_COMM_WORLD);
    MPI_Reduce(&w_elapsed, &w_slowest, 1, MPI_DOUBLE, MPI_MAX, 0, MPI_COMM_WORLD);

    if (rank == 0) {
        unlink(file);

        slowest *= 1000.0;
        w_slowest *= 1000.0;
        if (verbose == 1) {
            printf("file: %s, nprocs: %d, open_time: %f ms, write_time: %f ms\n",
                   file, nprocs, slowest, w_slowest);
        }
    }
    return 0;
}

int main(int argc, char **argv)
{
    int rank = -1;
    int nprocs;
    char file[256];
    MPI_Info info;
    int nr_errors = 0;

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

    /* parse the command line arguments */
    parse_args(rank, argc, argv);

    sprintf(file, "%s", opt_file);
    MPI_Info_create(&info);
    nr_errors += test_write(file, nprocs, rank, info);
    /* actual value does not matter.  test only writes a small amount of data */
    MPI_Info_set(info, "striping_factor", "50");
    nr_errors += test_write(file, nprocs, rank, info);
    MPI_Info_free(&info);

    MTest_Finalize(nr_errors);
    return MTestReturnValue(nr_errors);
}

static int parse_args(int rank, int argc, char **argv)
{
    int c;

    while ((c = getopt(argc, argv, "e")) != EOF) {
        switch (c) {
            case 'h':
                if (rank == 0)
                    usage(argv[0]);
                exit(0);
            case '?':  /* unknown */
                if (rank == 0)
                    usage(argv[0]);
                exit(1);
            default:
                break;
        }
    }

    if (argc - optind != 1) {
        if (rank == 0)
            usage(argv[0]);
        exit(1);
    }

    opt_file = strdup(argv[optind]);
    assert(opt_file);

    return (0);
}

static void usage(const char *prog)
{
    printf("Usage: %s [<OPTIONS>...] <FILE NAME>\n", prog);
    printf("\n<OPTIONS> is one or more of\n");
    printf(" -h       print this help\n");
}