1 /*
2  * Copyright (C) by Argonne National Laboratory
3  *     See COPYRIGHT in top-level directory
4  */
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 
main(int argc,char ** argv)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, "testfile");
43         /*
44          * fprintf(stderr, "\n*#  Usage: resized -fname filename\n\n");
45          * MPI_Abort(MPI_COMM_WORLD, 1);
46          */
47     } else {
48         argv++;
49         len = (int) strlen(*argv);
50         filename = (char *) malloc(len + 1);
51         strcpy(filename, *argv);
52     }
53 
54     MPI_File_delete(filename, MPI_INFO_NULL);
55 
56     /* create a resized type comprising an integer with an lb at sizeof(int) and extent = 3*sizeof(int) */
57     lb = sizeof(int);
58     extent = 3 * sizeof(int);
59     MPI_Type_create_resized(MPI_INT, lb, extent, &newtype1);
60 
61     MPI_Type_commit(&newtype1);
62     MPI_Type_create_resized(newtype1, lb, extent, &newtype);
63     MPI_Type_commit(&newtype);
64 
65     /* initialize the file */
66     MPI_File_open(MPI_COMM_WORLD, filename, MPI_MODE_CREATE | MPI_MODE_RDWR, MPI_INFO_NULL, &fh);
67     for (i = 0; i < 4; i++)
68         newbuf[i] = 55;
69     MPI_File_write(fh, newbuf, 4, MPI_INT, MPI_STATUS_IGNORE);
70     MPI_File_close(&fh);
71 
72     /* write 2 ints into file view with resized type */
73 
74     buf[0] = 10;
75     buf[1] = 20;
76 
77     MPI_File_open(MPI_COMM_WORLD, filename, MPI_MODE_CREATE | MPI_MODE_RDWR, MPI_INFO_NULL, &fh);
78 
79     mpi_errno = MPI_File_set_view(fh, 0, MPI_INT, newtype, (char *) "native", MPI_INFO_NULL);
80     if (mpi_errno != MPI_SUCCESS)
81         errs++;
82 
83     MPI_File_write(fh, buf, 2, MPI_INT, MPI_STATUS_IGNORE);
84 
85     MPI_File_close(&fh);
86 
87 
88     /* read back file view with resized type  and verify */
89 
90     MPI_File_open(MPI_COMM_WORLD, filename, MPI_MODE_RDONLY, MPI_INFO_NULL, &fh);
91 
92     mpi_errno = MPI_File_set_view(fh, 0, MPI_INT, newtype, (char *) "native", MPI_INFO_NULL);
93     if (mpi_errno != MPI_SUCCESS)
94         errs++;
95 
96     for (i = 0; i < 4; i++)
97         newbuf[i] = 100;
98     MPI_File_read(fh, newbuf, 2, MPI_INT, MPI_STATUS_IGNORE);
99     if ((newbuf[0] != 10) || (newbuf[1] != 20) || (newbuf[2] != 100) || (newbuf[3] != 100)) {
100         errs++;
101         fprintf(stderr,
102                 "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",
103                 newbuf[0], newbuf[1], newbuf[2], newbuf[3]);
104     }
105 
106     MPI_File_close(&fh);
107 
108     /* read file back and verify */
109 
110     MPI_File_open(MPI_COMM_WORLD, filename, MPI_MODE_RDONLY, MPI_INFO_NULL, &fh);
111 
112     MPI_File_get_size(fh, &size);
113     if (size != 4 * sizeof(int)) {
114         errs++;
115         fprintf(stderr, "file size is %lld, should be %d\n", (long long) size,
116                 (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     return MTestReturnValue(errs);
136 }
137