File: rd_end.c

package info (click to toggle)
mpich 4.3.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 101,184 kB
  • sloc: ansic: 1,040,629; cpp: 82,270; javascript: 40,763; perl: 27,933; python: 16,041; sh: 14,676; xml: 14,418; f90: 12,916; makefile: 9,270; fortran: 8,046; java: 4,635; asm: 324; ruby: 103; awk: 27; lisp: 19; php: 8; sed: 4
file content (79 lines) | stat: -rw-r--r-- 2,130 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
#include "mpitest.h"
#include "mpi.h"
#include <stdio.h>

/* Test case contributed by Pascal Deveze <Pascal.Deveze@bull.net>
 *
 * This test uses MPI_File_read_all to read a file with less data than requested.
 * Run with two processors.
 */

#define IO_SIZE 10
static const char *filename = "test.ord";

int main(int argc, char **argv)
{
    int errs = 0;
    MPI_File fh;
    MPI_Status status;
    unsigned char buffer[IO_SIZE];
    unsigned char cmp[IO_SIZE];

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

    if (size != 2) {
        printf("This test require 2 processes\n");
        goto fn_exit;
    }

    MPI_File_open(MPI_COMM_WORLD, filename, MPI_MODE_CREATE | MPI_MODE_RDWR, MPI_INFO_NULL, &fh);
    MPI_File_set_errhandler(fh, MPI_ERRORS_ARE_FATAL);
    for (int i = 0; i < IO_SIZE; i++) {
        buffer[i] = i;
        cmp[i] = i;
    }
    if (rank == 0) {
        MPI_File_write(fh, buffer, IO_SIZE - 5, MPI_CHAR, &status);
    }

    for (int i = 0; i < IO_SIZE; i++) {
        buffer[i] = 99;
    }
    for (int i = IO_SIZE - 5; i < IO_SIZE; i++) {
        cmp[i] = 99;
    }

    /* strictly speaking, this sync/barrier/sync closes one write access and
     * ensures subsequent read access sees latest data */
    MPI_File_sync(fh);
    MPI_Barrier(MPI_COMM_WORLD);
    MPI_File_sync(fh);

    MPI_File_seek(fh, 0, MPI_SEEK_SET);
    /* everybody reads the first IO_SIZE bytes from the file, but the file is
     * only IO_SIZE-5 bytes big.  */
    MPI_File_read_all(fh, buffer, IO_SIZE, MPI_BYTE, &status);

    int count;
    MPI_Get_count(&status, MPI_CHAR, &count);
    if (count != IO_SIZE - 5) {
        fprintf(stderr, "%d: count was %d; expected %d\n", rank, count, IO_SIZE - 5);
        errs++;
    }

    for (int i = 0; i < IO_SIZE; i++) {
        if (buffer[i] != cmp[i]) {
            fprintf(stderr, "%d: buffer[%d] = %d; expected %d\n", rank, i, buffer[i], cmp[i]);
            errs++;
        }
    }

    MPI_File_close(&fh);

  fn_exit:
    MTest_Finalize(errs);
    return MTestReturnValue(errs);
}