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];
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, "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, &newtype);
62 MPI_Type_commit(&newtype);
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++)
68 MPI_File_write(fh, newbuf, 4, MPI_INT, MPI_STATUS_IGNORE);
71 /* write 2 ints into file view with resized type */
76 MPI_File_open(MPI_COMM_WORLD, filename, MPI_MODE_CREATE | MPI_MODE_RDWR, MPI_INFO_NULL, &fh);
78 mpi_errno = MPI_File_set_view(fh, 0, MPI_INT, newtype, (char *) "native", MPI_INFO_NULL);
79 if (mpi_errno != MPI_SUCCESS)
82 MPI_File_write(fh, buf, 2, MPI_INT, MPI_STATUS_IGNORE);
87 /* read back file view with resized type and verify */
89 MPI_File_open(MPI_COMM_WORLD, filename, MPI_MODE_RDONLY, MPI_INFO_NULL, &fh);
91 mpi_errno = MPI_File_set_view(fh, 0, MPI_INT, newtype, (char *) "native", MPI_INFO_NULL);
92 if (mpi_errno != MPI_SUCCESS)
95 for (i = 0; i < 4; i++)
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)) {
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]);
107 /* read file back and verify */
109 MPI_File_open(MPI_COMM_WORLD, filename, MPI_MODE_RDONLY, MPI_INFO_NULL, &fh);
111 MPI_File_get_size(fh, &size);
112 if (size != 4 * sizeof(int)) {
114 fprintf(stderr, "file size is %lld, should be %d\n", size, (int) (4 * sizeof(int)));
117 for (i = 0; i < 4; i++)
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)) {
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]);
129 MPI_Type_free(&newtype);
131 MTest_Finalize(errs);