#include #include #include /*----< main() >------------------------------------------------------------*/ int main(int argc, char **argv) { int i, err, rank, np, buf_size, *buf; int sizes[2], starts[2], counts[2]; MPI_File fh; MPI_Datatype ftype; MPI_Status status; MPI_Init(&argc, &argv); MPI_Comm_rank(MPI_COMM_WORLD, &rank); MPI_Comm_size(MPI_COMM_WORLD, &np); if (np != 4) { printf("number of processes must be 4, exiting ... \n"); MPI_Finalize(); return 1; } /* a 8x10 2D array is block-* partitioned among 4 processes */ sizes[0] = sizes[1] = 10; counts[0] = 2; counts[1] = sizes[1]; starts[0] = 2 * rank; starts[1] = 0; /* create file type */ MPI_Type_create_subarray(2, sizes, counts, starts, MPI_ORDER_C, MPI_INT, &ftype) ; MPI_Type_commit(&ftype); /* open the file */ err = MPI_File_open(MPI_COMM_WORLD, "testfile", MPI_MODE_CREATE | MPI_MODE_WRONLY, MPI_INFO_NULL, &fh); if (err != MPI_SUCCESS) { printf("Error: MPI_File_open() filename %s\n",argv[1]); MPI_Abort(MPI_COMM_WORLD, -1); exit(1); } /* set the file view */ MPI_File_set_view(fh, 0, MPI_INT, ftype, "native", MPI_INFO_NULL); MPI_Type_free(&ftype); buf_size = counts[0] * counts[1]; buf = (int*) malloc(buf_size*sizeof(int)); /* MPI collective write */ MPI_File_write_all(fh, buf, buf_size, MPI_INT, &status); MPI_File_close(&fh); free(buf); MPI_Finalize(); return 0; }