#include #include #include #include static void err(int status, int lineno) { fprintf(stderr, "Error at line %d: %s\n", lineno, ncmpi_strerror(status)); MPI_Abort(MPI_COMM_WORLD, 1); } int main(int argc, char **argv) { int ret, ncid=0, nprocs, rank, dimid1, dimid2, varid1=0, varid2=0, ndims=2; char buf[30] = "Collective read example\n"; int dims[2]; MPI_Offset sd[2]; MPI_Init(&argc, &argv); MPI_Comm_rank(MPI_COMM_WORLD, &rank); MPI_Comm_size(MPI_COMM_WORLD, &nprocs); if (argc != 2) { if (rank == 0) printf("Usage: %s filename\n", argv[0]); MPI_Finalize(); exit(-1); } // open the file (is in define mode by default) ret = ncmpi_open(MPI_COMM_WORLD, argv[1], NC_NOWRITE, MPI_INFO_NULL, &ncid); if (ret != NC_NOERR) err(ret, __LINE__); // query and check is the variable "v" is present ret = ncmpi_inq_varid(ncid, "v", &varid1); if (ret != NC_NOERR) err(ret, __LINE__); // query for the length of dimensions of the variable "v" ret = ncmpi_inq_dimlen(ncid, 0, &sd[0]); if (ret != NC_NOERR) err(ret, __LINE__); ret = ncmpi_inq_dimlen(ncid, 1, &sd[1]); if (ret != NC_NOERR) err(ret, __LINE__); printf("Dimensions: (%d, %d)\n", sd[0], sd[1]); MPI_Offset start[2], count[2]; int *data = (int*)malloc(sizeof(int)*sd[0]*sd[1]/nprocs); int i; start[0] = (sd[0]/nprocs) * rank; start[1] = 0; count[0] = sd[0]/nprocs; count[1] = sd[1]; // read variable using a collective (default) ret = ncmpi_get_vara_int_all(ncid, varid1, start, count, data); if (ret != NC_NOERR) err(ret, __LINE__); // close the file ncmpi_close(ncid); printf("rank: %d, data[0]: %d\n", rank, data[0]); free(data); MPI_Finalize(); return 0; }