Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' into depencencies
[simgrid.git] / teshsuite / smpi / mpich3-test / io / resized.c
1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
2 /*
3  *  (C) 2008 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 "mpitest.h"
11
12 /*
13 static char MTEST_Descrip[] = "Test file views with MPI_Type_create_resized";
14 */
15
16 int main(int argc, char **argv)
17 {
18     int i, nprocs, len, mpi_errno, buf[2], newbuf[4];
19     int errs = 0;
20     MPI_Offset size;
21     MPI_Aint lb, extent;
22     MPI_File fh;
23     char *filename;
24     MPI_Datatype newtype;
25
26     MTest_Init(&argc, &argv);
27     MPI_Comm_size(MPI_COMM_WORLD, &nprocs);
28
29     if (nprocs != 1) {
30         fprintf(stderr, "Run this program on 1 process\n");
31         MPI_Abort(MPI_COMM_WORLD, 1);
32     }
33
34     i = 1;
35     while ((i < argc) && strcmp("-fname", *argv)) {
36         i++;
37         argv++;
38     }
39     if (i >= argc) {
40         len = 8;
41         filename = (char *) malloc(len + 10);
42         strcpy(filename, "testfile");
43         /*
44          * fprintf(stderr, "\n*#  Usage: resized -fname filename\n\n");
45          * MPI_Abort(MPI_COMM_WORLD, 1);
46          */
47     }
48     else {
49         argv++;
50         len = (int) strlen(*argv);
51         filename = (char *) malloc(len + 1);
52         strcpy(filename, *argv);
53     }
54
55     MPI_File_delete(filename, MPI_INFO_NULL);
56
57     /* create a resized type comprising an integer with an lb at sizeof(int) and extent = 3*sizeof(int) */
58     lb = sizeof(int);
59     extent = 3 * sizeof(int);
60     MPI_Type_create_resized(MPI_INT, lb, extent, &newtype);
61
62     MPI_Type_commit(&newtype);
63
64     /* initialize the file */
65     MPI_File_open(MPI_COMM_WORLD, filename, MPI_MODE_CREATE | MPI_MODE_RDWR, MPI_INFO_NULL, &fh);
66     for (i = 0; i < 4; i++)
67         newbuf[i] = 55;
68     MPI_File_write(fh, newbuf, 4, MPI_INT, MPI_STATUS_IGNORE);
69     MPI_File_close(&fh);
70
71     /* write 2 ints into file view with resized type */
72
73     buf[0] = 10;
74     buf[1] = 20;
75
76     MPI_File_open(MPI_COMM_WORLD, filename, MPI_MODE_CREATE | MPI_MODE_RDWR, MPI_INFO_NULL, &fh);
77
78     mpi_errno = MPI_File_set_view(fh, 0, MPI_INT, newtype, (char *) "native", MPI_INFO_NULL);
79     if (mpi_errno != MPI_SUCCESS)
80         errs++;
81
82     MPI_File_write(fh, buf, 2, MPI_INT, MPI_STATUS_IGNORE);
83
84     MPI_File_close(&fh);
85
86
87     /* read back file view with resized type  and verify */
88
89     MPI_File_open(MPI_COMM_WORLD, filename, MPI_MODE_RDONLY, MPI_INFO_NULL, &fh);
90
91     mpi_errno = MPI_File_set_view(fh, 0, MPI_INT, newtype, (char *) "native", MPI_INFO_NULL);
92     if (mpi_errno != MPI_SUCCESS)
93         errs++;
94
95     for (i = 0; i < 4; i++)
96         newbuf[i] = 100;
97     MPI_File_read(fh, newbuf, 2, MPI_INT, MPI_STATUS_IGNORE);
98 /*    if ((newbuf[0] != 10) || (newbuf[1] != 20) || (newbuf[2] != 100) || (newbuf[3] != 100)) {
99         errs++;
100         fprintf(stderr,
101                 "newbuf[0] is %d, should be 10,\n newbuf[1] is %d, should be 20\n newbuf[2] is %d, should be 100,\n newbuf[3] is %d, should be 100,\n",
102                 newbuf[0], newbuf[1], newbuf[2], newbuf[3]);
103     }*/
104
105     MPI_File_close(&fh);
106
107     /* read file back and verify */
108
109     MPI_File_open(MPI_COMM_WORLD, filename, MPI_MODE_RDONLY, MPI_INFO_NULL, &fh);
110
111     MPI_File_get_size(fh, &size);
112     if (size != 4 * sizeof(int)) {
113         errs++;
114         fprintf(stderr, "file size is %lld, should be %d\n", size, (int) (4 * sizeof(int)));
115     }
116
117     for (i = 0; i < 4; i++)
118         newbuf[i] = 100;
119     MPI_File_read(fh, newbuf, 4, MPI_INT, MPI_STATUS_IGNORE);
120 /*    if ((newbuf[0] != 10) || (newbuf[3] != 20) || (newbuf[1] != 55) || (newbuf[2] != 55)) {
121         errs++;
122         fprintf(stderr,
123                 "newbuf[0] is %d, should be 10,\n newbuf[1] is %d, should be 55,\n newbuf[2] is %d, should be 55,\n newbuf[3] is %d, should be 20\n",
124                 newbuf[0], newbuf[1], newbuf[2], newbuf[3]);
125     }
126 */
127     MPI_File_close(&fh);
128
129     MPI_Type_free(&newtype);
130     free(filename);
131     MTest_Finalize(errs);
132     MPI_Finalize();
133     return 0;
134 }