Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' into depencencies
[simgrid.git] / teshsuite / smpi / mpich3-test / io / i_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 #include <mpi.h>
7 #include <stdio.h>
8 #include <string.h>
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <assert.h>
12
13 //#define NUM_X 536870911
14 #define NUM_X 536870912
15 #define NUM_Y 1
16
17 //#define BIGDT 2147483643
18 #define BIGDT 2147483647
19
20 /*
21 static char MTEST_Descrip[] = "Nonblocking file read/write for bigtype";
22 */
23
24 int main(int argc, char **argv)
25 {
26     MPI_File fh;
27     int i, j;
28     size_t k;
29     MPI_Datatype inner_type, rem_type, mem_type;
30     MPI_Datatype int_type, file_type;
31     int *buf_write, *buf_read;
32     int rc;
33     MPI_Aint disp[2];
34     int block_len[2];
35     MPI_Datatype type[2];
36     MPI_Status status;
37     MPI_Request request;
38
39     MPI_Init(&argc, &argv);
40
41     if (sizeof(MPI_Aint) <= sizeof(int)) {
42         /* can't test on this platform... */
43         goto exit;
44     }
45
46     k = 0;
47     /* create a large buffer 2 */
48     buf_write = malloc(NUM_X * NUM_Y * sizeof(int));
49     buf_read = malloc(NUM_X * NUM_Y * sizeof(int));
50     if (buf_write == NULL || 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     rc = MPI_File_open(MPI_COMM_WORLD, "testfile",
93                        MPI_MODE_RDWR | MPI_MODE_CREATE, MPI_INFO_NULL, &fh);
94     if (rc != MPI_SUCCESS) {
95         fprintf(stderr, "Can't open file: %s\n", "testfile");
96         exit(1);
97     }
98
99     rc = MPI_File_set_view(fh, 2144, MPI_BYTE, file_type, "native", MPI_INFO_NULL);
100     if (rc != MPI_SUCCESS) {
101         fprintf(stderr, "ERROR SET VIEW\n");
102         exit(1);
103     }
104
105     /* write everything */
106     rc = MPI_File_iwrite_at_all(fh, 0, buf_write, 1, mem_type, &request);
107     if (rc != MPI_SUCCESS) {
108         fprintf(stderr, "%d ERROR IWRITE AT ALL\n", rc);
109         exit(1);
110     }
111     MPI_Wait(&request, &status);
112
113     rc = MPI_File_set_view(fh, 2144, MPI_BYTE, file_type, "native", MPI_INFO_NULL);
114     if (rc != MPI_SUCCESS) {
115         fprintf(stderr, "ERROR SET VIEW\n");
116         exit(1);
117     }
118
119     /* read everything */
120     rc = MPI_File_iread_at_all(fh, 0, buf_read, 1, mem_type, &request);
121     if (rc != MPI_SUCCESS) {
122         fprintf(stderr, "%d ERROR IREAD AT ALL\n", rc);
123         exit(1);
124     }
125     MPI_Wait(&request, &status);
126
127     for (k = 0; k < NUM_X * NUM_Y; k++) {
128         if (buf_read[k] != buf_write[k]) {
129             fprintf(stderr, "Verfiy Failed index %zu: expected %d found %d\n",
130                     k, buf_write[k], buf_read[k]);
131             assert(0);
132         }
133     }
134
135     free(buf_write);
136     free(buf_read);
137     MPI_File_close(&fh);
138
139     MPI_Type_free(&mem_type);
140     MPI_Type_free(&file_type);
141
142   exit:
143     MPI_Finalize();
144     printf(" No Errors\n");
145
146     return 0;
147 }