Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add new entry in Release_Notes.
[simgrid.git] / teshsuite / smpi / mpich3-test / io / resized2.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, newtype1;
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, "/scratch/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, &newtype1);
61
62     MPI_Type_commit(&newtype1);
63     MPI_Type_create_resized(newtype1, lb, extent, &newtype);
64     MPI_Type_commit(&newtype);
65
66     /* initialize the file */
67     MPI_File_open(MPI_COMM_WORLD, filename, MPI_MODE_CREATE | MPI_MODE_RDWR, MPI_INFO_NULL, &fh);
68     for (i = 0; i < 4; i++)
69         newbuf[i] = 55;
70     MPI_File_write(fh, newbuf, 4, MPI_INT, MPI_STATUS_IGNORE);
71     MPI_File_close(&fh);
72
73     /* write 2 ints into file view with resized type */
74
75     buf[0] = 10;
76     buf[1] = 20;
77
78     MPI_File_open(MPI_COMM_WORLD, filename, MPI_MODE_CREATE | MPI_MODE_RDWR, MPI_INFO_NULL, &fh);
79
80     mpi_errno = MPI_File_set_view(fh, 0, MPI_INT, newtype, (char *) "native", MPI_INFO_NULL);
81     if (mpi_errno != MPI_SUCCESS)
82         errs++;
83
84     MPI_File_write(fh, buf, 2, MPI_INT, MPI_STATUS_IGNORE);
85
86     MPI_File_close(&fh);
87
88
89     /* read back file view with resized type  and verify */
90
91     MPI_File_open(MPI_COMM_WORLD, filename, MPI_MODE_RDONLY, MPI_INFO_NULL, &fh);
92
93     mpi_errno = MPI_File_set_view(fh, 0, MPI_INT, newtype, (char *) "native", MPI_INFO_NULL);
94     if (mpi_errno != MPI_SUCCESS)
95         errs++;
96
97     for (i = 0; i < 4; i++)
98         newbuf[i] = 100;
99     MPI_File_read(fh, newbuf, 2, MPI_INT, MPI_STATUS_IGNORE);
100 /*    if ((newbuf[0] != 10) || (newbuf[1] != 20) || (newbuf[2] != 100) || (newbuf[3] != 100)) {*/
101 /*        errs++;*/
102 /*        fprintf(stderr,*/
103 /*                "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",*/
104 /*                newbuf[0], newbuf[1], newbuf[2], newbuf[3]);*/
105 /*    }*/
106
107     MPI_File_close(&fh);
108
109     /* read file back and verify */
110
111     MPI_File_open(MPI_COMM_WORLD, filename, MPI_MODE_RDONLY, MPI_INFO_NULL, &fh);
112
113     MPI_File_get_size(fh, &size);
114     if (size != 4 * sizeof(int)) {
115         errs++;
116         fprintf(stderr, "file size is %lld, should be %d\n", size, (int) (4 * sizeof(int)));
117     }
118
119     for (i = 0; i < 4; i++)
120         newbuf[i] = 100;
121     MPI_File_read(fh, newbuf, 4, MPI_INT, MPI_STATUS_IGNORE);
122 /*    if ((newbuf[0] != 10) || (newbuf[3] != 20) || (newbuf[1] != 55) || (newbuf[2] != 55)) {*/
123 /*        errs++;*/
124 /*        fprintf(stderr,*/
125 /*                "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",*/
126 /*                newbuf[0], newbuf[1], newbuf[2], newbuf[3]);*/
127 /*    }*/
128
129     MPI_File_close(&fh);
130
131     MPI_Type_free(&newtype1);
132     MPI_Type_free(&newtype);
133     free(filename);
134     MTest_Finalize(errs);
135     MPI_Finalize();
136     return 0;
137 }