Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
activate tests.
[simgrid.git] / teshsuite / smpi / mpich3-test / io / bigtype.c
1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
2 /*
3  *  (C) 2014 by Argonne National Laboratory.
4  *      See COPYRIGHT in top-level directory.
5  */
6
7 #include <mpi.h>
8 #include <stdio.h>
9 #include <string.h>
10 #include <stdlib.h>
11 #include <unistd.h>
12 #include <assert.h>
13
14 //#define NUM_X 536870911
15 #define NUM_X 53687091
16 #define NUM_Y 1
17
18 //#define BIGDT 2147483643
19 #define BIGDT 214748364
20
21 int main(int argc, char **argv)
22 {
23
24     MPI_File fh;
25     int i, j;
26     size_t k;
27     MPI_Datatype inner_type, rem_type, mem_type;
28     MPI_Datatype int_type, file_type;
29     int *buf_write, *buf_read;
30     int rc;
31     MPI_Aint disp[2];
32     int block_len[2];
33     MPI_Datatype type[2];
34
35     MPI_Init(&argc, &argv);
36
37     if (sizeof(MPI_Aint) <= sizeof(int)) {
38         /* can't test on this platform... */
39         goto exit;
40     }
41
42     k = 0;
43     /* create a large buffer 2 */
44     buf_write = malloc(NUM_X * NUM_Y * sizeof(int));
45     if (buf_write == NULL) {
46         fprintf(stderr, "not enough memory\n");
47         exit(1);
48     }
49     buf_read = malloc(NUM_X * NUM_Y * sizeof(int));
50     if (buf_read == NULL) {
51         fprintf(stderr, "not enough memory\n");
52         exit(1);
53     }
54     memset(buf_read, 0, NUM_X * NUM_Y * sizeof(int));
55
56     for (i = 0; i < NUM_X; i++) {
57         for (j = 0; j < NUM_Y; j++) {
58             buf_write[k] = k;
59             k++;
60         }
61     }
62
63     /* Big Datatype (2^31 - 1 bytes) */
64     MPI_Type_contiguous(BIGDT, MPI_BYTE, &inner_type);
65     /* Small Datatype (1 byte) */
66     MPI_Type_contiguous(1, MPI_BYTE, &rem_type);
67
68     type[0] = inner_type;
69     type[1] = rem_type;
70     block_len[0] = 1;
71     block_len[1] = 1;
72     disp[0] = 0;
73     disp[1] = BIGDT;
74
75     /* combine both types */
76     MPI_Type_struct(2, block_len, disp, type, &mem_type);
77
78     MPI_Type_commit(&mem_type);
79     MPI_Type_free(&rem_type);
80     MPI_Type_free(&inner_type);
81
82     MPI_Type_contiguous(4, MPI_BYTE, &int_type);
83     {
84         /* This creates a big type that is actually contituous, touching an
85          * optimization that was at one point buggy  */
86         MPI_Type_vector(1, NUM_X, 1, int_type, &file_type);
87     }
88
89     MPI_Type_commit(&file_type);
90     MPI_Type_free(&int_type);
91
92     if (MPI_File_open(MPI_COMM_WORLD, "testfile", MPI_MODE_RDWR | MPI_MODE_CREATE,
93                       MPI_INFO_NULL, &fh) != 0) {
94         fprintf(stderr, "Can't open file: %s\n", "testfile");
95         exit(1);
96     }
97
98     if (MPI_SUCCESS != MPI_File_set_view(fh, 2144, MPI_BYTE, file_type, "native", MPI_INFO_NULL)) {
99         fprintf(stderr, "ERROR SET VIEW\n");
100         exit(1);
101     }
102
103     /* write everything */
104     rc = MPI_File_write_at_all(fh, 0, buf_write, 1, mem_type, MPI_STATUS_IGNORE);
105     if (rc != MPI_SUCCESS) {
106         fprintf(stderr, "%d ERROR WRITE AT ALL\n", rc);
107         exit(1);
108     }
109
110     if (MPI_SUCCESS != MPI_File_set_view(fh, 2144, MPI_BYTE, file_type, "native", MPI_INFO_NULL)) {
111         fprintf(stderr, "ERROR SET VIEW\n");
112         exit(1);
113     }
114
115     /* read everything */
116     rc = MPI_File_read_at_all(fh, 0, buf_read, 1, mem_type, MPI_STATUS_IGNORE);
117     if (rc != MPI_SUCCESS) {
118         fprintf(stderr, "%d ERROR READ AT ALL\n", rc);
119         exit(1);
120     }
121
122 /*    for (k = 0; k < NUM_X * NUM_Y; k++) {
123         if (buf_read[k] != buf_write[k]) {
124             fprintf(stderr, "Verfiy Failed index %zu: expected %d found %d\n",
125                     k, buf_write[k], buf_read[k]);
126             assert(0);
127         }
128     }
129 */
130     free(buf_write);
131     free(buf_read);
132     MPI_File_close(&fh);
133
134     MPI_Type_free(&mem_type);
135     MPI_Type_free(&file_type);
136
137   exit:
138     MPI_Finalize();
139     printf(" No Errors\n");
140
141     return 0;
142 }