1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
3 * (C) 2008 by Argonne National Laboratory.
4 * See COPYRIGHT in top-level directory.
13 static char MTEST_Descrip[] = "Test file views with MPI_Type_create_resized";
16 int main(int argc, char **argv)
18 int i, nprocs, len, mpi_errno, buf[2], newbuf[4];
24 MPI_Datatype newtype, newtype1;
26 MTest_Init(&argc, &argv);
27 MPI_Comm_size(MPI_COMM_WORLD, &nprocs);
30 fprintf(stderr, "Run this program on 1 process\n");
31 MPI_Abort(MPI_COMM_WORLD, 1);
35 while ((i < argc) && strcmp("-fname", *argv)) {
41 filename = (char *) malloc(len + 10);
42 strcpy(filename, "/scratch/testfile");
44 * fprintf(stderr, "\n*# Usage: resized -fname filename\n\n");
45 * MPI_Abort(MPI_COMM_WORLD, 1);
50 len = (int) strlen(*argv);
51 filename = (char *) malloc(len + 1);
52 strcpy(filename, *argv);
55 MPI_File_delete(filename, MPI_INFO_NULL);
57 /* create a resized type comprising an integer with an lb at sizeof(int) and extent = 3*sizeof(int) */
59 extent = 3 * sizeof(int);
60 MPI_Type_create_resized(MPI_INT, lb, extent, &newtype1);
62 MPI_Type_commit(&newtype1);
63 MPI_Type_create_resized(newtype1, lb, extent, &newtype);
64 MPI_Type_commit(&newtype);
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++)
70 MPI_File_write(fh, newbuf, 4, MPI_INT, MPI_STATUS_IGNORE);
73 /* write 2 ints into file view with resized type */
78 MPI_File_open(MPI_COMM_WORLD, filename, MPI_MODE_CREATE | MPI_MODE_RDWR, MPI_INFO_NULL, &fh);
80 mpi_errno = MPI_File_set_view(fh, 0, MPI_INT, newtype, (char *) "native", MPI_INFO_NULL);
81 if (mpi_errno != MPI_SUCCESS)
84 MPI_File_write(fh, buf, 2, MPI_INT, MPI_STATUS_IGNORE);
89 /* read back file view with resized type and verify */
91 MPI_File_open(MPI_COMM_WORLD, filename, MPI_MODE_RDONLY, MPI_INFO_NULL, &fh);
93 mpi_errno = MPI_File_set_view(fh, 0, MPI_INT, newtype, (char *) "native", MPI_INFO_NULL);
94 if (mpi_errno != MPI_SUCCESS)
97 for (i = 0; i < 4; i++)
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)) {*/
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]);*/
109 /* read file back and verify */
111 MPI_File_open(MPI_COMM_WORLD, filename, MPI_MODE_RDONLY, MPI_INFO_NULL, &fh);
113 MPI_File_get_size(fh, &size);
114 if (size != 4 * sizeof(int)) {
116 fprintf(stderr, "file size is %lld, should be %d\n", size, (int) (4 * sizeof(int)));
119 for (i = 0; i < 4; i++)
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)) {*/
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]);*/
131 MPI_Type_free(&newtype1);
132 MPI_Type_free(&newtype);
134 MTest_Finalize(errs);