Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' into depencencies
[simgrid.git] / teshsuite / smpi / mpich3-test / io / external32-derived-dtype.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 #include <stdlib.h>
8 #include <stdio.h>
9 #include "mpi.h"
10
11 static void read_file(const char *name, void *buf, MPI_Datatype dt)
12 {
13     int rank, rc;
14     MPI_File fh;
15     char datarep[] = "external32";
16     int amode = MPI_MODE_RDONLY;
17     MPI_Status status;
18     MPI_Offset offset;
19
20     /* get our rank */
21     MPI_Comm_rank(MPI_COMM_WORLD, &rank);
22
23     /* open file */
24     rc = MPI_File_open(MPI_COMM_WORLD, (char *) name,
25             amode, MPI_INFO_NULL, &fh);
26     if (rc != MPI_SUCCESS) {
27         printf("Rank %d: Failed to open file %s\n", rank, name);
28         fflush(stdout);
29         MPI_Abort(MPI_COMM_WORLD, 1);
30         return;
31     }
32
33     /* set file view to be sequence of datatypes past header */
34     MPI_File_set_view(fh, 0, dt, dt, datarep, MPI_INFO_NULL);
35
36     /* issue a collective read: In 3.2 and older the external32 code
37      * path had a bug that would cause an overlapping memcopy and crash
38      */
39     offset = rank;
40     MPI_File_read_at_all(fh, offset, buf, 1, dt, &status);
41
42     /* close file */
43     MPI_File_close(&fh);
44
45     return;
46 }
47
48 static void write_file(const char *name, void *buf, MPI_Datatype dt)
49 {
50     int rank, amode;
51     char datarep[] = "external32";
52     MPI_Status status;
53     MPI_File fh;
54     MPI_Offset offset;
55
56     /* get our rank in job */
57     MPI_Comm_rank(MPI_COMM_WORLD, &rank);
58
59     /* open file */
60     amode = MPI_MODE_WRONLY | MPI_MODE_CREATE;
61     MPI_File_open(MPI_COMM_WORLD, (char *) name, amode, MPI_INFO_NULL, &fh);
62
63     /* truncate file to 0 bytes */
64     MPI_File_set_size(fh, 0);
65
66     /* set file view to be sequence of datatypes past header */
67     MPI_File_set_view(fh, 0, dt, dt, datarep, MPI_INFO_NULL);
68
69     /* collective write of file info */
70     offset = rank;
71     MPI_File_write_at_all(fh, offset, buf, 1, dt, &status);
72
73     /* close file */
74     MPI_File_close(&fh);
75
76     return;
77 }
78
79 /* write and read a file in which each process writes one int
80  * in rank order */
81 int main(int argc, char *argv[])
82 {
83
84     char buf[2] = "a";
85     MPI_Datatype dt;
86     int blocks[2] = { 1, 1 };
87     int disps[2] = { 0, 1 };
88
89     MPI_Init(&argc, &argv);
90     MPI_Type_indexed(2, blocks, disps, MPI_CHAR, &dt);
91     MPI_Type_commit(&dt);
92
93     write_file("testfile", buf, dt);
94
95     read_file("testfile", buf, dt);
96
97     MPI_Type_free(&dt);
98
99     /* if we get this far, then we've passed.  No verification in this test at
100      * this time. */
101     fprintf(stdout, " No Errors\n");
102
103     MPI_Finalize();
104
105     return 0;
106 }