int main(int argc, char** argv) { extern int optind; char *filename="testfile.nc"; int i, rank, verbose=1, err; int ncid, cmode, omode; MPI_Init(&argc, &argv); MPI_Comm_rank(MPI_COMM_WORLD, &rank); /* get command-line arguments */ while ((i = getopt(argc, argv, "hq")) != EOF) switch(i) { case 'q': verbose = 0; break; case 'h': default: if (rank==0) usage(argv[0]); MPI_Finalize(); return 0; } argc -= optind; argv += optind; if (argc == 1) filename = argv[0]; /* optional argument */ if (verbose && rank == 0) printf("%s: example of file create and open\n",__FILE__); /* create a new file using clobber mode ----------------------------------*/ cmode = NC_CLOBBER; err = ncmpi_create(MPI_COMM_WORLD, filename, cmode, MPI_INFO_NULL, &ncid); ERR /* close file */ err = ncmpi_close(ncid); ERR /* open the newly created file for read only -----------------------------*/ omode = NC_NOWRITE; err = ncmpi_open(MPI_COMM_WORLD, filename, omode, MPI_INFO_NULL, &ncid); ERR /* close file */ err = ncmpi_close(ncid); ERR /* check if there is any PnetCDF internal malloc residue */ MPI_Offset malloc_size, sum_size; err = ncmpi_inq_malloc_size(&malloc_size); if (err == NC_NOERR) { MPI_Reduce(&malloc_size, &sum_size, 1, MPI_OFFSET, MPI_SUM, 0, MPI_COMM_WORLD); if (rank == 0 && sum_size > 0) printf("heap memory allocated by PnetCDF internally has %lld bytes yet to be freed\n", sum_size); } MPI_Finalize(); return 0; }
int main(int argc, char **argv) { char filename[256]; int err, nerrs=0, ncid, cmode, rank, nprocs; MPI_Init(&argc, &argv); MPI_Comm_size(MPI_COMM_WORLD, &nprocs); MPI_Comm_rank(MPI_COMM_WORLD, &rank); if (argc > 2) { if (!rank) printf("Usage: %s [filename]\n",argv[0]); MPI_Finalize(); return 0; } strcpy(filename, "testfile.nc"); if (argc == 2) strcpy(filename, argv[1]); MPI_Bcast(filename, 256, MPI_CHAR, 0, MPI_COMM_WORLD); if (rank == 0) { char cmd_str[256]; sprintf(cmd_str, "*** TESTING C %s for NC_NOCLOBBER and NC_EEXIST ", argv[0]); printf("%-66s ------ ", cmd_str); fflush(stdout); } /* create a file if it does not exist */ cmode = NC_CLOBBER; err = ncmpi_create(MPI_COMM_WORLD, filename, cmode, MPI_INFO_NULL, &ncid); ERR err = ncmpi_close(ncid); ERR /* now the file exists, test if PnetCDF can return correct error code */ cmode = NC_NOCLOBBER; err = ncmpi_create(MPI_COMM_WORLD, filename, cmode, MPI_INFO_NULL, &ncid); if (err != NC_EEXIST) /* err == NC_EOFILE */ nerrs++; /* check if PnetCDF freed all internal malloc */ MPI_Offset malloc_size, sum_size; err = ncmpi_inq_malloc_size(&malloc_size); if (err == NC_NOERR) { MPI_Reduce(&malloc_size, &sum_size, 1, MPI_OFFSET, MPI_SUM, 0, MPI_COMM_WORLD); if (rank == 0 && sum_size > 0) printf("heap memory allocated by PnetCDF internally has %lld bytes yet to be freed\n", sum_size); } MPI_Allreduce(MPI_IN_PLACE, &nerrs, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD); if (rank == 0) { if (nerrs) printf(FAIL_STR,nerrs); else printf(PASS_STR); } MPI_Finalize(); return 0; }
int main(int argc, char **argv) { char filename[256]; int rank, nprocs, cmode, err, nerrs=0; MPI_Init(&argc, &argv); MPI_Comm_size(MPI_COMM_WORLD, &nprocs); MPI_Comm_rank(MPI_COMM_WORLD, &rank); if (argc > 2) { if (!rank) printf("Usage: %s [filename]\n",argv[0]); MPI_Finalize(); return 0; } if (argc == 2) snprintf(filename, 256, "%s", argv[1]); else strcpy(filename, "testfile.nc"); MPI_Bcast(filename, 256, MPI_CHAR, 0, MPI_COMM_WORLD); char *cmd_str = (char*)malloc(strlen(argv[0]) + 256); sprintf(cmd_str, "*** TESTING C %s for emulating netCDF tst_norm ", basename(argv[0])); if (rank == 0) printf("%-66s ------ ", cmd_str); free(cmd_str); /*---- testing UTF-8 normalization ----*/ /* test CDF-2 format */ cmode = NC_CLOBBER | NC_64BIT_OFFSET; nerrs += tst_norm(filename, cmode); /* test CDF-5 format */ cmode = NC_CLOBBER | NC_64BIT_DATA; nerrs += tst_norm(filename, cmode); /* check if PnetCDF freed all internal malloc */ MPI_Offset malloc_size, sum_size; err = ncmpi_inq_malloc_size(&malloc_size); if (err == NC_NOERR) { MPI_Reduce(&malloc_size, &sum_size, 1, MPI_OFFSET, MPI_SUM, 0, MPI_COMM_WORLD); if (rank == 0 && sum_size > 0) printf("heap memory allocated by PnetCDF internally has %lld bytes yet to be freed\n", sum_size); } MPI_Allreduce(MPI_IN_PLACE, &nerrs, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD); if (rank == 0) { if (nerrs) printf(FAIL_STR,nerrs); else printf(PASS_STR); } MPI_Finalize(); return 0; }
int main(int argc, char** argv) { char filename[256]; int rank, nprocs, err, nerrs=0; MPI_Init(&argc, &argv); MPI_Comm_rank(MPI_COMM_WORLD, &rank); MPI_Comm_size(MPI_COMM_WORLD, &nprocs); if (argc > 2) { if (!rank) printf("Usage: %s [filename]\n",argv[0]); MPI_Finalize(); return 0; } strcpy(filename, "testfile.nc"); if (argc == 2) strcpy(filename, argv[1]); MPI_Bcast(filename, 256, MPI_CHAR, 0, MPI_COMM_WORLD); if (rank == 0) { char cmd_str[256]; sprintf(cmd_str, "*** TESTING C %s for last large var in CDF-1/2", argv[0]); printf("%-66s ------ ", cmd_str); fflush(stdout); } nerrs += check_last_var(filename); nerrs += check_rec_var(filename); nerrs += check_not_last_var(filename); nerrs += check_add_var(filename); nerrs += check_var_offset(filename); /* check if PnetCDF freed all internal malloc */ MPI_Offset malloc_size, sum_size; err = ncmpi_inq_malloc_size(&malloc_size); if (err == NC_NOERR) { MPI_Reduce(&malloc_size, &sum_size, 1, MPI_OFFSET, MPI_SUM, 0, MPI_COMM_WORLD); if (rank == 0 && sum_size > 0) printf("heap memory allocated by PnetCDF internally has %lld bytes yet to be freed\n", sum_size); } MPI_Allreduce(MPI_IN_PLACE, &nerrs, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD); if (rank == 0) { if (nerrs) printf(FAIL_STR,nerrs); else printf(PASS_STR); } MPI_Finalize(); return 0; }
/*----< main() >--------------------------------------------------------------*/ int main(int argc, char **argv) { char *filename="testfile.nc"; int rank, nerrs=0; MPI_Init(&argc, &argv); MPI_Comm_rank(MPI_COMM_WORLD, &rank); if (argc > 2) { if (!rank) printf("Usage: %s [filename]\n",argv[0]); MPI_Finalize(); return 0; } if (argc == 2) filename = argv[1]; if (rank == 0) { char cmd_str[256]; sprintf(cmd_str, "*** TESTING C %s for CDF-5 type in CDF-1 and 2 ", argv[0]); printf("%-66s ------ ", cmd_str); } nerrs += test_attr_types(filename, 0); nerrs += test_attr_types(filename, NC_64BIT_OFFSET); nerrs += test_var_types(filename, 0); nerrs += test_var_types(filename, NC_64BIT_OFFSET); MPI_Offset malloc_size, sum_size; int err = ncmpi_inq_malloc_size(&malloc_size); if (err == NC_NOERR) { MPI_Reduce(&malloc_size, &sum_size, 1, MPI_OFFSET, MPI_SUM, 0, MPI_COMM_WORLD); if (rank == 0 && sum_size > 0) printf("heap memory allocated by PnetCDF internally has %lld bytes yet to be freed\n", sum_size); } if (rank == 0) { if (nerrs) printf(FAIL_STR,nerrs); else printf(PASS_STR); } MPI_Finalize(); return 0; }
int main(int argc, char **argv) { char dir_name[256], filename[256]; int err, rank, nerrs=0, format, ncid; MPI_Init(&argc, &argv); MPI_Comm_rank(MPI_COMM_WORLD, &rank); if (argc != 2) { if (!rank) printf("Usage: %s dir_name\n",argv[0]); MPI_Finalize(); return 0; } strcpy(dir_name, argv[1]); MPI_Bcast(dir_name, 256, MPI_CHAR, 0, MPI_COMM_WORLD); if (rank == 0) { char cmd_str[256]; sprintf(cmd_str, "*** TESTING C %s for inquiring CDF file formats ", argv[0]); printf("%-66s ------ ", cmd_str); } /* test CDF-1 -----------------------------------------------------------*/ sprintf(filename,"%s/test_cdf1.nc",dir_name); err = ncmpi_open(MPI_COMM_WORLD, filename, 0, MPI_INFO_NULL, &ncid); ERR err = ncmpi_inq_format(ncid, &format); ERR if (format != NC_FORMAT_CLASSIC) { printf("Error (line=%d): expecting CDF-1 format for file %s but got %d\n", __LINE__,filename,format); nerrs++; } err = ncmpi_close(ncid); ERR err = ncmpi_inq_file_format(filename, &format); ERR if (format != NC_FORMAT_CLASSIC) { printf("Error (line=%d): expecting CDF-1 format for file %s but got %d\n", __LINE__,filename,format); nerrs++; } /* test CDF-2 -----------------------------------------------------------*/ sprintf(filename,"%s/test_cdf2.nc",dir_name); err = ncmpi_open(MPI_COMM_WORLD, filename, 0, MPI_INFO_NULL, &ncid); ERR err = ncmpi_inq_format(ncid, &format); ERR if (format != NC_FORMAT_CDF2) { printf("Error (line=%d): expecting CDF-2 format for file %s but got %d\n", __LINE__,filename,format); nerrs++; } err = ncmpi_close(ncid); ERR err = ncmpi_inq_file_format(filename, &format); ERR if (format != NC_FORMAT_CDF2) { printf("Error (line=%d): expecting CDF-2 format for file %s but got %d\n", __LINE__,filename,format); nerrs++; } /* test CDF-5 -----------------------------------------------------------*/ sprintf(filename,"%s/test_cdf5.nc",dir_name); err = ncmpi_open(MPI_COMM_WORLD, filename, 0, MPI_INFO_NULL, &ncid); ERR err = ncmpi_inq_format(ncid, &format); ERR if (format != NC_FORMAT_CDF5) { printf("Error (line=%d): expecting CDF-5 format for file %s but got %d\n", __LINE__,filename,format); nerrs++; } err = ncmpi_close(ncid); ERR err = ncmpi_inq_file_format(filename, &format); ERR if (format != NC_FORMAT_CDF5) { printf("Error (line=%d): expecting CDF-5 format for file %s but got %d\n", __LINE__,filename,format); nerrs++; } MPI_Offset malloc_size, sum_size; err = ncmpi_inq_malloc_size(&malloc_size); if (err == NC_NOERR) { MPI_Reduce(&malloc_size, &sum_size, 1, MPI_OFFSET, MPI_SUM, 0, MPI_COMM_WORLD); if (rank == 0 && sum_size > 0) printf("heap memory allocated by PnetCDF internally has %lld bytes yet to be freed\n", sum_size); } MPI_Allreduce(MPI_IN_PLACE, &nerrs, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD); if (rank == 0) { if (nerrs) printf(FAIL_STR,nerrs); else printf(PASS_STR); } MPI_Finalize(); return 0; }
int main(int argc, char** argv) { char filename[256]; int rank, nprocs, err, flag, nerrs=0; int log_enabled; int ncid; MPI_Info info, infoused; char hint[MPI_MAX_INFO_VAL]; MPI_Init(&argc, &argv); MPI_Comm_rank(MPI_COMM_WORLD, &rank); MPI_Comm_size(MPI_COMM_WORLD, &nprocs); if (argc > 2) { if (!rank) printf("Usage: %s [filename]\n",argv[0]); MPI_Finalize(); return 1; } if (argc == 2) snprintf(filename, 256, "%s", argv[1]); else strcpy(filename, "testfile.nc"); MPI_Bcast(filename, 256, MPI_CHAR, 0, MPI_COMM_WORLD); if (rank == 0) { char *cmd_str = (char*)malloc(strlen(argv[0]) + 256); sprintf(cmd_str, "*** TESTING C %s for checking offsets of new variables ", basename(argv[0])); printf("%-66s ------ ", cmd_str); fflush(stdout); free(cmd_str); } MPI_Info_create(&info); MPI_Info_set(info, "nc_dw_overwrite", "enable"); MPI_Info_set(info, "nc_dw_del_on_close", "disable"); MPI_Info_set(info, "nc_dw_flush_buffer_size", "256"); /* MPI_Info_set(info, "nc_dw_dirname", "()@^$@!(_&$)@(#%%&)(*#$"); */ err = ncmpi_create(MPI_COMM_WORLD, filename, NC_CLOBBER, info, &ncid); CHECK_ERR err = ncmpi_inq_file_info(ncid, &infoused); CHECK_ERR MPI_Info_get(infoused, "nc_dw", MPI_MAX_INFO_VAL - 1, hint, &flag); if (flag && strcasecmp(hint, "enable") == 0) log_enabled = 1; else log_enabled = 0; if (log_enabled) { MPI_Info_get(infoused, "nc_dw_overwrite", MPI_MAX_INFO_VAL - 1, hint, &flag); if (flag) { if (strcmp(hint, "enable") != 0) { printf("Error at line %d: unexpected nc_dw_overwrite = %s, but got %s\n", __LINE__, "enable", hint); nerrs++; } } else{ printf("Error at line %d: nc_dw_overwrite is not set\n", __LINE__); nerrs++; } MPI_Info_get(infoused, "nc_dw_del_on_close", MPI_MAX_INFO_VAL - 1, hint, &flag); if (flag) { if (strcmp(hint, "disable") != 0) { printf("Error at line %d: unexpected nc_dw_del_on_close = %s, but got %s\n", __LINE__, "disable", hint); nerrs++; } } else{ printf("Error at line %d: nc_dw_del_on_close is not set\n", __LINE__); nerrs++; } MPI_Info_get(infoused, "nc_dw_flush_buffer_size", MPI_MAX_INFO_VAL - 1, hint, &flag); if (flag) { if (strcmp(hint, "256") != 0) { printf("Error at line %d: unexpected nc_dw_flush_buffer_size = %s, but got %s\n", __LINE__, "256", hint); nerrs++; } } else{ printf("Error at line %d: nc_dw_flush_buffer_size is not set\n", __LINE__); nerrs++; } } err = ncmpi_enddef(ncid); CHECK_ERR err = ncmpi_close(ncid); CHECK_ERR MPI_Info_free(&info); MPI_Info_free(&infoused); /* check if PnetCDF freed all internal malloc */ MPI_Offset malloc_size, sum_size; err = ncmpi_inq_malloc_size(&malloc_size); if (err == NC_NOERR) { MPI_Reduce(&malloc_size, &sum_size, 1, MPI_OFFSET, MPI_SUM, 0, MPI_COMM_WORLD); if (rank == 0 && sum_size > 0) printf("heap memory allocated by PnetCDF internally has %lld bytes yet to be freed\n", sum_size); } MPI_Allreduce(MPI_IN_PLACE, &nerrs, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD); if (rank == 0) { if (nerrs) printf(FAIL_STR,nerrs); else printf(PASS_STR); } MPI_Finalize(); return (nerrs > 0); }
int main(int argc, char** argv) { char filename[256]; int i, j, rank, nprocs, err, nerrs=0; int ncid, varid, dimid[2], req, st; MPI_Offset start[2], count[2], stride[2]; unsigned char buffer[NY][NX]; MPI_Init(&argc, &argv); MPI_Comm_rank(MPI_COMM_WORLD, &rank); MPI_Comm_size(MPI_COMM_WORLD, &nprocs); if (argc > 2) { if (!rank) printf("Usage: %s [filename]\n",argv[0]); MPI_Finalize(); return 1; } if (argc == 2) snprintf(filename, 256, "%s", argv[1]); else strcpy(filename, "testfile.nc"); MPI_Bcast(filename, 256, MPI_CHAR, 0, MPI_COMM_WORLD); if (rank == 0) { char *cmd_str = (char*)malloc(strlen(argv[0]) + 256); sprintf(cmd_str, "*** TESTING C %s for ncmpi_end_indep_data ", basename(argv[0])); printf("%-66s ------ ",cmd_str); free(cmd_str); } err = ncmpi_create(MPI_COMM_WORLD, filename, NC_CLOBBER|NC_64BIT_DATA, MPI_INFO_NULL, &ncid); CHECK_ERR err = ncmpi_def_dim(ncid, "Y", NC_UNLIMITED, &dimid[0]); CHECK_ERR err = ncmpi_def_dim(ncid, "X", NX*nprocs, &dimid[1]); CHECK_ERR err = ncmpi_def_var(ncid, "var", NC_UBYTE, NDIMS, dimid, &varid); CHECK_ERR err = ncmpi_enddef(ncid); CHECK_ERR for (i=0; i<NY; i++) for (j=0; j<NX; j++) buffer[i][j] = rank+10; start[0] = 0; start[1] = NX*rank; count[0] = NY/2; count[1] = NX/2; stride[0] = 2; stride[1] = 2; err = ncmpi_buffer_attach(ncid, NY*NX); CHECK_ERR err = ncmpi_begin_indep_data(ncid); CHECK_ERR err = ncmpi_bput_vars_uchar(ncid, varid, start, count, stride, &buffer[0][0], &req); CHECK_ERR /* check if write buffer contents have been altered */ for (i=0; i<NY; i++) for (j=0; j<NX; j++) { if (buffer[i][j] != rank+10) { printf("Error at line %d in %s: put buffer[%d][%d]=%hhu altered, should be %d\n", __LINE__,__FILE__,i,j,buffer[i][j],rank+10); nerrs++; } } err = ncmpi_end_indep_data(ncid); CHECK_ERR /* calling wait API after exiting independent data mode on purpose */ err = ncmpi_wait_all(ncid, 1, &req, &st); CHECK_ERR err = st; CHECK_ERR /* check if write buffer contents have been altered */ for (i=0; i<NY; i++) for (j=0; j<NX; j++) { if (buffer[i][j] != rank+10) { printf("Error at line %d in %s: put buffer[%d][%d]=%hhu altered, should be %d\n", __LINE__,__FILE__,i,j,buffer[i][j],rank+10); nerrs++; } } err = ncmpi_buffer_detach(ncid); CHECK_ERR err = ncmpi_close(ncid); CHECK_ERR /* check if PnetCDF freed all internal malloc */ MPI_Offset malloc_size, sum_size; err = ncmpi_inq_malloc_size(&malloc_size); if (err == NC_NOERR) { MPI_Reduce(&malloc_size, &sum_size, 1, MPI_OFFSET, MPI_SUM, 0, MPI_COMM_WORLD); if (rank == 0 && sum_size > 0) { printf("heap memory allocated by PnetCDF internally has %lld bytes yet to be freed\n", sum_size); ncmpi_inq_malloc_list(); } } MPI_Allreduce(MPI_IN_PLACE, &nerrs, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD); if (rank == 0) { if (nerrs) printf(FAIL_STR,nerrs); else printf(PASS_STR); } MPI_Finalize(); return (nerrs > 0); }
int main(int argc, char** argv) { extern int optind; char filename[256]; int i, j, rank, nprocs, verbose=1, err, nerrs=0; int ncid, cmode, varid, dimid[2], buf[NY][NX]; char str_att[128]; float float_att[100]; MPI_Offset global_ny, global_nx; MPI_Offset start[2], count[2]; MPI_Init(&argc, &argv); MPI_Comm_rank(MPI_COMM_WORLD, &rank); MPI_Comm_size(MPI_COMM_WORLD, &nprocs); /* get command-line arguments */ while ((i = getopt(argc, argv, "hq")) != EOF) switch(i) { case 'q': verbose = 0; break; case 'h': default: if (rank==0) usage(argv[0]); MPI_Finalize(); return 1; } if (argv[optind] == NULL) strcpy(filename, "testfile.nc"); else snprintf(filename, 256, "%s", argv[optind]); MPI_Bcast(filename, 256, MPI_CHAR, 0, MPI_COMM_WORLD); if (verbose && rank == 0) printf("%s: example of using put_vara APIs\n",__FILE__); /* create a new file for writing ----------------------------------------*/ cmode = NC_CLOBBER | NC_64BIT_DATA; err = ncmpi_create(MPI_COMM_WORLD, filename, cmode, MPI_INFO_NULL, &ncid); ERR /* the global array is NY * (NX * nprocs) */ global_ny = NY; global_nx = NX * nprocs; for (i=0; i<NY; i++) for (j=0; j<NX; j++) buf[i][j] = rank; /* add a global attribute: a time stamp at rank 0 */ time_t ltime = time(NULL); /* get the current calendar time */ asctime_r(localtime(<ime), str_att); /* make sure the time string are consistent among all processes */ MPI_Bcast(str_att, strlen(str_att), MPI_CHAR, 0, MPI_COMM_WORLD); err = ncmpi_put_att_text(ncid, NC_GLOBAL, "history", strlen(str_att), &str_att[0]); ERR /* define dimensions x and y */ err = ncmpi_def_dim(ncid, "Y", global_ny, &dimid[0]); ERR err = ncmpi_def_dim(ncid, "X", global_nx, &dimid[1]); ERR /* define a 2D variable of integer type */ err = ncmpi_def_var(ncid, "var", NC_INT, 2, dimid, &varid); ERR /* add attributes to the variable */ strcpy(str_att, "example attribute of type text."); err = ncmpi_put_att_text(ncid, varid, "str_att_name", strlen(str_att), &str_att[0]); ERR for (i=0; i<8; i++) float_att[i] = i; err = ncmpi_put_att_float(ncid, varid, "float_att_name", NC_FLOAT, 8, &float_att[0]); ERR long long int64_att=10000000000LL; err = ncmpi_put_att_longlong(ncid, varid, "int64_att_name", NC_INT64, 1, &int64_att); ERR /* do not forget to exit define mode */ err = ncmpi_enddef(ncid); ERR /* now we are in data mode */ start[0] = 0; start[1] = NX * rank; count[0] = NY; count[1] = NX; err = ncmpi_put_vara_int_all(ncid, varid, start, count, &buf[0][0]); ERR err = ncmpi_close(ncid); ERR /* check if there is any PnetCDF internal malloc residue */ MPI_Offset malloc_size, sum_size; err = ncmpi_inq_malloc_size(&malloc_size); if (err == NC_NOERR) { MPI_Reduce(&malloc_size, &sum_size, 1, MPI_OFFSET, MPI_SUM, 0, MPI_COMM_WORLD); if (rank == 0 && sum_size > 0) printf("heap memory allocated by PnetCDF internally has %lld bytes yet to be freed\n", sum_size); } MPI_Finalize(); return (nerrs > 0); }
/*----< main() >------------------------------------------------------------*/ int main(int argc, char **argv) { char filename[256]; int i, j, err, ncid, varid0, varid1, varid2, dimids[2], nerrs=0; int rank, nprocs, debug=0, blocklengths[2], **buf, *bufptr; int array_of_sizes[2], array_of_subsizes[2], array_of_starts[2]; MPI_Offset start[2], count[2]; MPI_Aint a0, a1, disps[2]; MPI_Datatype buftype, ghost_buftype, rec_filetype, fix_filetype; MPI_Init(&argc, &argv); MPI_Comm_size(MPI_COMM_WORLD, &nprocs); MPI_Comm_rank(MPI_COMM_WORLD, &rank); if (argc > 2) { if (!rank) printf("Usage: %s [filename]\n",argv[0]); MPI_Finalize(); return 0; } strcpy(filename, "testfile.nc"); if (argc == 2) strcpy(filename, argv[1]); MPI_Bcast(filename, 256, MPI_CHAR, 0, MPI_COMM_WORLD); if (rank == 0) { char cmd_str[256]; sprintf(cmd_str, "*** TESTING C %s for flexible put and get ", argv[0]); printf("%-66s ------ ", cmd_str); fflush(stdout); } buf = (int**)malloc(NY * sizeof(int*)); buf[0] = (int*)malloc(NY * NX * sizeof(int)); for (i=1; i<NY; i++) buf[i] = buf[i-1] + NX; /* construct various MPI derived data types */ /* construct an MPI derived data type for swapping 1st row with 2nd row */ blocklengths[0] = blocklengths[1] = NX; MPI_Get_address(buf[1], &a0); MPI_Get_address(buf[0], &a1); disps[0] = 0; disps[1] = a1 - a0; bufptr = buf[1]; err = MPI_Type_create_hindexed(2, blocklengths, disps, MPI_INT, &buftype); if (err != MPI_SUCCESS) printf("MPI error MPI_Type_create_hindexed\n"); MPI_Type_commit(&buftype); start[0] = 0; start[1] = NX*rank; count[0] = 2; count[1] = NX; if (debug) printf("put start=%lld %lld count=%lld %lld\n",start[0],start[1],count[0],count[1]); /* create a file type for the fixed-size variable */ array_of_sizes[0] = 2; array_of_sizes[1] = NX*nprocs; array_of_subsizes[0] = count[0]; array_of_subsizes[1] = count[1]; array_of_starts[0] = start[0]; array_of_starts[1] = start[1]; MPI_Type_create_subarray(2, array_of_sizes, array_of_subsizes, array_of_starts, MPI_ORDER_C, MPI_INT, &fix_filetype); MPI_Type_commit(&fix_filetype); /* create a buftype with ghost cells on each side */ array_of_sizes[0] = count[0]+4; array_of_sizes[1] = count[1]+4; array_of_subsizes[0] = count[0]; array_of_subsizes[1] = count[1]; array_of_starts[0] = 2; array_of_starts[1] = 2; MPI_Type_create_subarray(2, array_of_sizes, array_of_subsizes, array_of_starts, MPI_ORDER_C, MPI_INT, &ghost_buftype); MPI_Type_commit(&ghost_buftype); /* create a new file for write */ err = ncmpi_create(MPI_COMM_WORLD, filename, NC_CLOBBER, MPI_INFO_NULL, &ncid); ERR /* define a 2D array */ err = ncmpi_def_dim(ncid, "REC_DIM", NC_UNLIMITED, &dimids[0]); ERR err = ncmpi_def_dim(ncid, "X", NX*nprocs, &dimids[1]); ERR err = ncmpi_def_var(ncid, "rec_var", NC_INT, 2, dimids, &varid0); ERR err = ncmpi_def_var(ncid, "dummy_rec", NC_INT, 2, dimids, &varid2); ERR err = ncmpi_def_dim(ncid, "FIX_DIM", 2, &dimids[0]); ERR err = ncmpi_def_var(ncid, "fix_var", NC_INT, 2, dimids, &varid1); ERR err = ncmpi_enddef(ncid); ERR /* create a file type for the record variable */ int *array_of_blocklengths=(int*) malloc(count[0]*sizeof(int)); MPI_Aint *array_of_displacements=(MPI_Aint*) malloc(count[0]*sizeof(MPI_Aint)); MPI_Offset recsize; err = ncmpi_inq_recsize(ncid, &recsize); for (i=0; i<count[0]; i++) { array_of_blocklengths[i] = count[1]; array_of_displacements[i] = start[1]*sizeof(int) + recsize * i; } MPI_Type_create_hindexed(2, array_of_blocklengths, array_of_displacements, MPI_INT, &rec_filetype); MPI_Type_commit(&rec_filetype); free(array_of_blocklengths); free(array_of_displacements); /* initialize the contents of the array */ for (j=0; j<NY; j++) for (i=0; i<NX; i++) buf[j][i] = rank*100 + j*10 + i; /* write the record variable */ err = ncmpi_put_vard_all(ncid, varid0, rec_filetype, bufptr, 1, buftype); ERR /* check if the contents of buf are altered */ CHECK_VALUE /* check if root process can write to file header in data mode */ err = ncmpi_rename_var(ncid, varid0, "rec_VAR"); ERR /* write the fixed-size variable */ err = ncmpi_put_vard_all(ncid, varid1, fix_filetype, bufptr, 1, buftype); ERR /* check if the contents of buf are altered */ CHECK_VALUE /* check if root process can write to file header in data mode */ err = ncmpi_rename_var(ncid, varid0, "rec_var"); ERR /* test the same routines in independent data mode */ err = ncmpi_begin_indep_data(ncid); ERR err = ncmpi_put_vard(ncid, varid0, rec_filetype, bufptr, 1, buftype); ERR CHECK_VALUE err = ncmpi_rename_var(ncid, varid0, "rec_VAR"); ERR err = ncmpi_put_vard(ncid, varid1, fix_filetype, bufptr, 1, buftype); ERR CHECK_VALUE err = ncmpi_rename_var(ncid, varid0, "rec_var"); ERR err = ncmpi_end_indep_data(ncid); ERR err = ncmpi_close(ncid); ERR /* open the same file and read back for validate */ err = ncmpi_open(MPI_COMM_WORLD, filename, NC_NOWRITE, MPI_INFO_NULL, &ncid); ERR err = ncmpi_inq_varid(ncid, "rec_var", &varid0); ERR err = ncmpi_inq_varid(ncid, "fix_var", &varid1); ERR nerrs += get_var_and_verify(ncid, varid0, start, count, buf, buftype, ghost_buftype, rec_filetype); nerrs += get_var_and_verify(ncid, varid1, start, count, buf, buftype, ghost_buftype, fix_filetype); err = ncmpi_close(ncid); ERR MPI_Type_free(&rec_filetype); MPI_Type_free(&fix_filetype); MPI_Type_free(&buftype); MPI_Type_free(&ghost_buftype); free(buf[0]); free(buf); /* check if PnetCDF freed all internal malloc */ MPI_Offset malloc_size, sum_size; err = ncmpi_inq_malloc_size(&malloc_size); if (err == NC_NOERR) { MPI_Reduce(&malloc_size, &sum_size, 1, MPI_OFFSET, MPI_SUM, 0, MPI_COMM_WORLD); if (rank == 0 && sum_size > 0) printf("heap memory allocated by PnetCDF internally has %lld bytes yet to be freed\n", sum_size); } MPI_Allreduce(MPI_IN_PLACE, &nerrs, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD); if (rank == 0) { if (nerrs) printf(FAIL_STR,nerrs); else printf(PASS_STR); } MPI_Finalize(); return 0; }
/*----< main() >------------------------------------------------------------*/ int main(int argc, char **argv) { int i, j, err, nerrs=0, rank, nprocs; int ncid, dimid[2], varid, req, status; MPI_Offset start[2], count[2], stride[2], imap[2]; int var[6][4]; float k, rh[4][6]; signed char varT[4][6]; char filename[256]; MPI_Init(&argc, &argv); MPI_Comm_rank(MPI_COMM_WORLD, &rank); MPI_Comm_size(MPI_COMM_WORLD, &nprocs); if (argc > 2) { if (!rank) printf("Usage: %s [filename]\n",argv[0]); MPI_Finalize(); return 1; } if (argc == 2) snprintf(filename, 256, "%s", argv[1]); else strcpy(filename, "testfile.nc"); if (rank == 0) { char *cmd_str = (char*)malloc(strlen(argv[0]) + 256); sprintf(cmd_str, "*** TESTING C %s for get/put varm ", basename(argv[0])); printf("%-66s ------ ", cmd_str); fflush(stdout); free(cmd_str); } #ifdef DEBUG if (nprocs > 1 && rank == 0) printf("Warning: %s is designed to run on 1 process\n", argv[0]); #endif err = ncmpi_create(MPI_COMM_WORLD, filename, NC_CLOBBER | NC_64BIT_DATA, MPI_INFO_NULL, &ncid); CHECK_ERR /* define a variable of a 6 x 4 integer array in the nc file */ err = ncmpi_def_dim(ncid, "Y", 6, &dimid[0]); CHECK_ERR err = ncmpi_def_dim(ncid, "X", 4, &dimid[1]); CHECK_ERR err = ncmpi_def_var(ncid, "var", NC_INT, 2, dimid, &varid); CHECK_ERR err = ncmpi_enddef(ncid); CHECK_ERR /* create a 6 x 4 integer variable in the file with contents: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23 */ for (j=0; j<6; j++) for (i=0; i<4; i++) var[j][i] = j*4+i; start[0] = 0; start[1] = 0; count[0] = 6; count[1] = 4; if (rank > 0) count[0] = count[1] = 0; err = ncmpi_put_vara_int_all(ncid, varid, start, count, &var[0][0]); CHECK_ERR if (nprocs > 1) MPI_Barrier(MPI_COMM_WORLD); err = ncmpi_close(ncid); CHECK_ERR err = ncmpi_open(MPI_COMM_WORLD, filename, NC_NOWRITE, MPI_INFO_NULL, &ncid); CHECK_ERR err = ncmpi_inq_varid(ncid, "var", &varid); CHECK_ERR /* read the variable back in the matrix transposed way, rh is 4 x 6 */ count[0] = 6; count[1] = 4; stride[0] = 1; stride[1] = 1; imap[0] = 1; imap[1] = 6; /* would be {4, 1} if not transposing */ for (i=0; i<6; i++) for (j=0; j<4; j++) rh[j][i] = -1.0; err = ncmpi_iget_varm_float(ncid, varid, start, count, stride, imap, &rh[0][0], &req); CHECK_ERR err = ncmpi_wait_all(ncid, 1, &req, &status); CHECK_ERR err = status; CHECK_ERR /* check the contents of read */ k = 0.0; for (i=0; i<6; i++) { for (j=0; j<4; j++) { if (rh[j][i] != k) { #ifdef PRINT_ERR_ON_SCREEN printf("Error at line %d in %s: expecting rh[%d][%d]=%f but got %f\n", __LINE__,__FILE__,j,i,k,rh[j][i]); #endif nerrs++; break; } k += 1.0; } } #ifdef PRINT_ON_SCREEN /* print the contents of read */ for (j=0; j<4; j++) { printf("[%2d]: ",j); for (i=0; i<6; i++) { printf("%5.1f",rh[j][i]); } printf("\n"); } #endif /* the stdout should be: [ 0]: 0.0 4.0 8.0 12.0 16.0 20.0 [ 1]: 1.0 5.0 9.0 13.0 17.0 21.0 [ 2]: 2.0 6.0 10.0 14.0 18.0 22.0 [ 3]: 3.0 7.0 11.0 15.0 19.0 23.0 */ for (i=0; i<6; i++) for (j=0; j<4; j++) rh[j][i] = -1.0; err = ncmpi_get_varm_float_all(ncid, varid, start, count, stride, imap, &rh[0][0]); CHECK_ERR /* check the contents of read */ k = 0.0; for (i=0; i<6; i++) { for (j=0; j<4; j++) { if (rh[j][i] != k) { #ifdef PRINT_ERR_ON_SCREEN printf("Error at line %d in %s: expecting rh[%d][%d]=%f but got %f\n", __LINE__,__FILE__,j,i,k,rh[j][i]); #endif nerrs++; break; } k += 1.0; } } #ifdef PRINT_ON_SCREEN /* print the contents of read */ for (j=0; j<4; j++) { printf("[%2d]: ",j); for (i=0; i<6; i++) { printf("%5.1f",rh[j][i]); } printf("\n"); } #endif /* the stdout should be: [ 0]: 0.0 4.0 8.0 12.0 16.0 20.0 [ 1]: 1.0 5.0 9.0 13.0 17.0 21.0 [ 2]: 2.0 6.0 10.0 14.0 18.0 22.0 [ 3]: 3.0 7.0 11.0 15.0 19.0 23.0 */ err = ncmpi_close(ncid); CHECK_ERR err = ncmpi_open(MPI_COMM_WORLD, filename, NC_WRITE, MPI_INFO_NULL, &ncid); CHECK_ERR err = ncmpi_inq_varid(ncid, "var", &varid); CHECK_ERR /* testing get_varm(), first zero-out the variable in the file */ memset(&var[0][0], 0, 6*4*sizeof(int)); start[0] = 0; start[1] = 0; count[0] = 6; count[1] = 4; if (rank > 0) count[0] = count[1] = 0; err = ncmpi_put_vara_int_all(ncid, varid, start, count, &var[0][0]); CHECK_ERR /* set the contents of the write buffer varT, a 4 x 6 char array 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73 */ for (j=0; j<4; j++) for (i=0; i<6; i++) varT[j][i] = j*6+i + 50; /* write varT to the NC variable in the matrix transposed way */ start[0] = 0; start[1] = 0; count[0] = 6; count[1] = 4; stride[0] = 1; stride[1] = 1; imap[0] = 1; imap[1] = 6; /* would be {4, 1} if not transposing */ if (rank > 0) count[0] = count[1] = 0; err = ncmpi_iput_varm_schar(ncid, varid, start, count, stride, imap, &varT[0][0], &req); CHECK_ERR err = ncmpi_wait_all(ncid, 1, &req, &status); CHECK_ERR err = status; CHECK_ERR /* the output from command "ncmpidump -v var test.nc" should be: var = 50, 56, 62, 68, 51, 57, 63, 69, 52, 58, 64, 70, 53, 59, 65, 71, 54, 60, 66, 72, 55, 61, 67, 73 ; */ /* check if the contents of write buffer have been altered */ for (j=0; j<4; j++) { for (i=0; i<6; i++) { if (varT[j][i] != j*6+i + 50) { #ifdef PRINT_ERR_ON_SCREEN /* this error is a pnetcdf internal error, if occurs */ printf("Error at line %d in %s: expecting varT[%d][%d]=%d but got %d\n", __LINE__,__FILE__,j,i,j*6+i + 50,varT[j][i]); #endif nerrs++; break; } } } err = ncmpi_put_varm_schar_all(ncid, varid, start, count, stride, imap, &varT[0][0]); CHECK_ERR /* check if the contents of write buffer have been altered */ for (j=0; j<4; j++) { for (i=0; i<6; i++) { if (varT[j][i] != j*6+i + 50) { #ifdef PRINT_ERR_ON_SCREEN /* this error is a pnetcdf internal error, if occurs */ printf("Error at line %d in %s: expecting varT[%d][%d]=%d but got %d\n", __LINE__,__FILE__,j,i,j*6+i + 50,varT[j][i]); #endif nerrs++; break; } } } err = ncmpi_close(ncid); CHECK_ERR /* check if PnetCDF freed all internal malloc */ MPI_Offset malloc_size, sum_size; err = ncmpi_inq_malloc_size(&malloc_size); if (err == NC_NOERR) { MPI_Reduce(&malloc_size, &sum_size, 1, MPI_OFFSET, MPI_SUM, 0, MPI_COMM_WORLD); if (rank == 0 && sum_size > 0) printf("heap memory allocated by PnetCDF internally has %lld bytes yet to be freed\n", sum_size); if (malloc_size > 0) ncmpi_inq_malloc_list(); } MPI_Allreduce(MPI_IN_PLACE, &nerrs, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD); if (rank == 0) { if (nerrs) printf(FAIL_STR,nerrs); else printf(PASS_STR); } MPI_Finalize(); return (nerrs > 0); }
int main(int argc, char **argv) { int rank, nprocs, ncid, pres_varid, temp_varid; int lat_varid, lon_varid; /* The start and count arrays will tell the netCDF library where to read our data. */ MPI_Offset start[NDIMS], count[NDIMS]; /* Program variables to hold the data we will read. We will only need enough space to hold one timestep of data; one record. */ float **pres_in=NULL; /* [NLVL/nprocs][NLAT][NLON] */ float **temp_in=NULL; /* [NLVL/nprocs][NLAT][NLON] */ /* These program variables hold the latitudes and longitudes. */ float lats[NLAT], lons[NLON]; /* Loop indexes. */ int lvl, lat, lon, rec, i = 0; /* Error handling. */ int err, nerrs=0; char *filename = FILE_NAME; MPI_Init(&argc, &argv); MPI_Comm_size(MPI_COMM_WORLD, &nprocs); MPI_Comm_rank(MPI_COMM_WORLD, &rank); if (argc > 2) { if (!rank) printf("Usage: %s [filename]\n",argv[0]); MPI_Finalize(); return 1; } if (argc == 2) filename = argv[1]; if (rank == 0) { char *cmd_str = (char*)malloc(strlen(argv[0]) + 256); sprintf(cmd_str, "*** TESTING C %s for reading file", basename(argv[0])); printf("%-66s ------ ", cmd_str); free(cmd_str); } /* Open the file. */ err = ncmpi_open(MPI_COMM_WORLD, filename, NC_NOWRITE, MPI_INFO_NULL, &ncid); CHECK_ERR /* Get the varids of the latitude and longitude coordinate * variables. */ err = ncmpi_inq_varid(ncid, LAT_NAME, &lat_varid); CHECK_ERR err = ncmpi_inq_varid(ncid, LON_NAME, &lon_varid); CHECK_ERR /* Read the coordinate variable data. */ memset(lats, 0, sizeof(float)*NLAT); memset(lons, 0, sizeof(float)*NLON); err = ncmpi_get_var_float_all(ncid, lat_varid, &lats[0]); CHECK_ERR err = ncmpi_get_var_float_all(ncid, lon_varid, &lons[0]); CHECK_ERR /* Check the coordinate variable data. */ for (lat = 0; lat < NLAT; lat++) if (lats[lat] != START_LAT + 5.*lat) { printf("\nError at line %d in %s: expect %e but got %e\n", __LINE__,__FILE__, START_LAT+5.*lat,lats[lat]); nerrs++; goto fn_exit; } for (lon = 0; lon < NLON; lon++) if (lons[lon] != START_LON + 5.*lon) { printf("\nError at line %d in %s: expect %e but got %e\n", __LINE__,__FILE__, START_LON+5.*lon,lons[lon]); nerrs++; goto fn_exit; } /* Get the varids of the pressure and temperature netCDF * variables. */ err = ncmpi_inq_varid(ncid, PRES_NAME, &pres_varid); CHECK_ERR err = ncmpi_inq_varid(ncid, TEMP_NAME, &temp_varid); CHECK_ERR /* Read the data. Since we know the contents of the file we know * that the data arrays in this program are the correct size to * hold one timestep. */ count[0] = 1; count[2] = NLAT; count[3] = NLON; start[2] = 0; start[3] = 0; /* divide NLVL dimension among processes */ count[1] = NLVL / nprocs; start[1] = count[1] * rank; if (rank < NLVL % nprocs) { start[1] += rank; count[1]++; } else { start[1] += NLVL % nprocs; } if (count[1] == 0) start[1] = 0; /* allocate read buffers */ pres_in = (float**) malloc(count[1]*2 * sizeof(float*)); temp_in = pres_in + count[1]; if (count[1] > 0) { pres_in[0] = (float*) malloc(count[1]*2 * NLAT*NLON * sizeof(float)); temp_in[0] = pres_in[0] + count[1] * NLAT*NLON; for (i=1; i<count[1]; i++) { pres_in[i] = pres_in[i-1] + NLAT*NLON; temp_in[i] = temp_in[i-1] + NLAT*NLON; } } /* Read and check one record at a time. */ for (rec = 0; rec < NREC; rec++) { start[0] = rec; err = ncmpi_get_vara_float_all(ncid, pres_varid, start, count, &pres_in[0][0]); CHECK_ERR err = ncmpi_get_vara_float_all(ncid, temp_varid, start, count, &temp_in[0][0]); CHECK_ERR /* Check the data. */ i = (int)start[1] * NLAT * NLON; for (lvl=0; lvl<count[1]; lvl++) for (lat = 0; lat < NLAT; lat++) for (lon = 0; lon < NLON; lon++) { if (pres_in[lvl][lat*NLON+lon] != SAMPLE_PRESSURE + i) { printf("\nError at line %d in %s: expect %e but got %e\n", __LINE__,__FILE__, SAMPLE_PRESSURE+i,pres_in[lvl][lat*NLON+lon]); nerrs++; goto fn_exit; } if (temp_in[lvl][lat*NLON+lon] != SAMPLE_TEMP + i) { printf("\nError at line %d in %s: expect %e but got %e\n", __LINE__,__FILE__, SAMPLE_TEMP+i,temp_in[lvl][lat*NLON+lon]); nerrs++; goto fn_exit; } i++; } } /* next record */ fn_exit: /* Close the file. */ err = ncmpi_close(ncid); CHECK_ERR if (pres_in != NULL) { if (pres_in[0] != NULL) free(pres_in[0]); free(pres_in); } /* check if there is any malloc residue */ MPI_Offset malloc_size, sum_size; err = ncmpi_inq_malloc_size(&malloc_size); if (err == NC_NOERR) { MPI_Reduce(&malloc_size, &sum_size, 1, MPI_OFFSET, MPI_SUM, 0, MPI_COMM_WORLD); if (rank == 0 && sum_size > 0) printf("heap memory allocated by PnetCDF internally has %lld bytes yet to be freed\n", sum_size); } MPI_Allreduce(MPI_IN_PLACE, &nerrs, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD); if (rank == 0) { if (nerrs) printf(FAIL_STR,nerrs); else printf(PASS_STR); } MPI_Finalize(); return (nerrs > 0); }
int main(int argc, char** argv) { extern int optind; char filename[256]; int i, j, verbose=1, rank, nprocs, err, nerrs=0; int myNX, G_NX, myOff, num_reqs; int ncid, cmode, varid, dimid[2], *reqs, *sts, **buf; MPI_Offset start[2], count[2]; MPI_Info info; MPI_Init(&argc, &argv); MPI_Comm_rank(MPI_COMM_WORLD, &rank); MPI_Comm_size(MPI_COMM_WORLD, &nprocs); /* get command-line arguments */ while ((i = getopt(argc, argv, "hq")) != EOF) switch(i) { case 'q': verbose = 0; break; case 'h': default: if (rank==0) usage(argv[0]); MPI_Finalize(); return 1; } if (argv[optind] == NULL) strcpy(filename, "testfile.nc"); else snprintf(filename, 256, "%s", argv[optind]); /* set an MPI-IO hint to disable file offset alignment for fixed-size * variables */ MPI_Info_create(&info); MPI_Info_set(info, "nc_var_align_size", "1"); cmode = NC_CLOBBER | NC_64BIT_DATA; err = ncmpi_create(MPI_COMM_WORLD, filename, cmode, info, &ncid); ERR MPI_Info_free(&info); /* the global array is NY * (NX * nprocs) */ G_NX = NX * nprocs; myOff = NX * rank; myNX = NX; if (verbose) printf("%2d: myOff=%3d myNX=%3d\n",rank,myOff,myNX); err = ncmpi_def_dim(ncid, "Y", NY, &dimid[0]); ERR err = ncmpi_def_dim(ncid, "X", G_NX, &dimid[1]); ERR err = ncmpi_def_var(ncid, "var", NC_INT, 2, dimid, &varid); ERR err = ncmpi_enddef(ncid); ERR /* First, fill the entire array with zeros, using a blocking I/O. Every process writes a subarray of size NY * myNX */ buf = (int**) malloc(myNX * sizeof(int*)); buf[0] = (int*) calloc(NY * myNX, sizeof(int)); start[0] = 0; start[1] = myOff; count[0] = NY; count[1] = myNX; err = ncmpi_put_vara_int_all(ncid, varid, start, count, buf[0]); free(buf[0]); /* initialize the buffer with rank ID. Also make the case interesting, by allocating buffers separately */ for (i=0; i<myNX; i++) { buf[i] = (int*) malloc(NY * sizeof(int)); for (j=0; j<NY; j++) buf[i][j] = rank; } reqs = (int*) malloc(myNX * sizeof(int)); sts = (int*) malloc(myNX * sizeof(int)); /* each proc writes myNX single columns of the 2D array */ start[0] = 0; start[1] = rank; count[0] = NY; count[1] = 1; if (verbose) printf("%2d: start=%3lld %3lld count=%3lld %3lld\n", rank, start[0],start[1], count[0],count[1]); num_reqs = 0; for (i=0; i<myNX; i++) { err = ncmpi_iput_vara_int(ncid, varid, start, count, buf[i], &reqs[num_reqs++]); ERR start[1] += nprocs; } err = ncmpi_wait_all(ncid, num_reqs, reqs, sts); ERR /* check status of all requests */ for (i=0; i<num_reqs; i++) if (sts[i] != NC_NOERR) printf("Error at line %d in %s: nonblocking write fails on request %d (%s)\n", __LINE__,__FILE__,i, ncmpi_strerror(sts[i])); err = ncmpi_close(ncid); ERR /* read back using the same access pattern */ err = ncmpi_open(MPI_COMM_WORLD, filename, NC_NOWRITE, info, &ncid); ERR err = ncmpi_inq_varid(ncid, "var", &varid); ERR for (i=0; i<myNX; i++) for (j=0; j<NY; j++) buf[i][j] = -1; /* each proc reads myNX single columns of the 2D array */ start[0] = 0; start[1] = rank; count[0] = NY; count[1] = 1; num_reqs = 0; for (i=0; i<myNX; i++) { err = ncmpi_iget_vara_int(ncid, varid, start, count, buf[i], &reqs[num_reqs++]); ERR start[1] += nprocs; } err = ncmpi_wait_all(ncid, num_reqs, reqs, sts); ERR /* check status of all requests */ for (i=0; i<num_reqs; i++) if (sts[i] != NC_NOERR) printf("Error at line %d in %s: nonblocking write fails on request %d (%s)\n", __LINE__,__FILE__,i, ncmpi_strerror(sts[i])); for (i=0; i<myNX; i++) { for (j=0; j<NY; j++) if (buf[i][j] != rank) printf("Error at line %d in %s: expect buf[%d][%d]=%d but got %d\n", __LINE__,__FILE__,i,j,rank,buf[i][j]); } err = ncmpi_close(ncid); ERR free(sts); free(reqs); for (i=0; i<myNX; i++) free(buf[i]); free(buf); /* check if there is any PnetCDF internal malloc residue */ MPI_Offset malloc_size, sum_size; err = ncmpi_inq_malloc_size(&malloc_size); if (err == NC_NOERR) { MPI_Reduce(&malloc_size, &sum_size, 1, MPI_OFFSET, MPI_SUM, 0, MPI_COMM_WORLD); if (rank == 0 && sum_size > 0) printf("heap memory allocated by PnetCDF internally has %lld bytes yet to be freed\n", sum_size); } MPI_Finalize(); return (nerrs > 0); }
int main(int argc, char** argv) { extern int optind; char *filename="testfile.nc"; int i, rank, nprocs, verbose=1, err; int ncid, cmode, varid, dimid[2], num_reqs, *buffer, **bufs, *nvarids; MPI_Offset w_len, **starts, **counts, *bufcounts; MPI_Datatype *datatypes; MPI_Info info; MPI_Init(&argc, &argv); MPI_Comm_rank(MPI_COMM_WORLD, &rank); MPI_Comm_size(MPI_COMM_WORLD, &nprocs); /* get command-line arguments */ while ((i = getopt(argc, argv, "hq")) != EOF) switch(i) { case 'q': verbose = 0; break; case 'h': default: if (rank==0) usage(argv[0]); MPI_Finalize(); return 0; } argc -= optind; argv += optind; if (argc == 1) filename = argv[0]; /* optional argument */ if (nprocs != 4 && rank == 0 && verbose) printf("Warning: this program is intended to run on 4 processes\n"); /* set an MPI-IO hint to disable file offset alignment for fix-sized * variables */ MPI_Info_create(&info); MPI_Info_set(info, "nc_var_align_size", "1"); /* create a new file for writing ----------------------------------------*/ cmode = NC_CLOBBER | NC_64BIT_DATA; err = ncmpi_create(MPI_COMM_WORLD, filename, cmode, info, &ncid); ERR MPI_Info_free(&info); /* create a global array of size NY * NX */ err = ncmpi_def_dim(ncid, "Y", NY, &dimid[0]); ERR err = ncmpi_def_dim(ncid, "X", NX, &dimid[1]); ERR err = ncmpi_def_var(ncid, "var", NC_INT, NDIMS, dimid, &varid); ERR err = ncmpi_enddef(ncid); ERR /* pick arbitrary numbers of requests for 4 processes */ num_reqs = 0; if (rank == 0) num_reqs = 4; else if (rank == 1) num_reqs = 6; else if (rank == 2) num_reqs = 5; else if (rank == 3) num_reqs = 4; starts = (MPI_Offset**) malloc(num_reqs * sizeof(MPI_Offset*)); counts = (MPI_Offset**) malloc(num_reqs * sizeof(MPI_Offset*)); starts[0] = (MPI_Offset*) calloc(num_reqs * NDIMS, sizeof(MPI_Offset)); counts[0] = (MPI_Offset*) calloc(num_reqs * NDIMS, sizeof(MPI_Offset)); for (i=1; i<num_reqs; i++) { starts[i] = starts[i-1] + NDIMS; counts[i] = counts[i-1] + NDIMS; } /* assign arbitrary starts and counts */ const int y=0, x=1; if (rank == 0) { starts[0][y] = 0; starts[0][x] = 5; counts[0][y] = 1; counts[0][x] = 2; starts[1][y] = 1; starts[1][x] = 0; counts[1][y] = 1; counts[1][x] = 1; starts[2][y] = 2; starts[2][x] = 6; counts[2][y] = 1; counts[2][x] = 2; starts[3][y] = 3; starts[3][x] = 0; counts[3][y] = 1; counts[3][x] = 3; /* rank 0 is writing the followings: ("-" means skip) - - - - - 0 0 - - - 0 - - - - - - - - - - - - - - - 0 0 - - 0 0 0 - - - - - - - */ } else if (rank ==1) { starts[0][y] = 0; starts[0][x] = 3; counts[0][y] = 1; counts[0][x] = 2; starts[1][y] = 0; starts[1][x] = 8; counts[1][y] = 1; counts[1][x] = 2; starts[2][y] = 1; starts[2][x] = 5; counts[2][y] = 1; counts[2][x] = 2; starts[3][y] = 2; starts[3][x] = 0; counts[3][y] = 1; counts[3][x] = 2; starts[4][y] = 2; starts[4][x] = 8; counts[4][y] = 1; counts[4][x] = 2; starts[5][y] = 3; starts[5][x] = 4; counts[5][y] = 1; counts[5][x] = 3; /* rank 1 is writing the followings: ("-" means skip) - - - 1 1 - - - 1 1 - - - - - 1 1 - - - 1 1 - - - - - - 1 1 - - - - 1 1 1 - - - */ } else if (rank ==2) { starts[0][y] = 0; starts[0][x] = 7; counts[0][y] = 1; counts[0][x] = 1; starts[1][y] = 1; starts[1][x] = 1; counts[1][y] = 1; counts[1][x] = 3; starts[2][y] = 1; starts[2][x] = 7; counts[2][y] = 1; counts[2][x] = 3; starts[3][y] = 2; starts[3][x] = 2; counts[3][y] = 1; counts[3][x] = 1; starts[4][y] = 3; starts[4][x] = 3; counts[4][y] = 1; counts[4][x] = 1; /* rank 2 is writing the followings: ("-" means skip) - - - - - - - 2 - - - 2 2 2 - - - 2 2 2 - - 2 - - - - - - - - - - 2 - - - - - - */ } else if (rank ==3) { starts[0][y] = 0; starts[0][x] = 0; counts[0][y] = 1; counts[0][x] = 3; starts[1][y] = 1; starts[1][x] = 4; counts[1][y] = 1; counts[1][x] = 1; starts[2][y] = 2; starts[2][x] = 3; counts[2][y] = 1; counts[2][x] = 3; starts[3][y] = 3; starts[3][x] = 7; counts[3][y] = 1; counts[3][x] = 3; /* rank 3 is writing the followings: ("-" means skip) 3 3 3 - - - - - - - - - - - 3 - - - - - - - - 3 3 3 - - - - - - - - - - - 3 3 3 */ } nvarids = (int*) malloc(num_reqs * sizeof(int)); bufcounts = (MPI_Offset*) malloc(num_reqs * sizeof(MPI_Offset)); datatypes = (MPI_Datatype*) malloc(num_reqs * sizeof(MPI_Datatype)); w_len = 0; for (i=0; i<num_reqs; i++) { nvarids[i] = varid; bufcounts[i] = counts[i][x]; datatypes[i] = MPI_INT; w_len += bufcounts[i]; } /* allocate I/O buffer and initialize its contents */ buffer = (int*) malloc(w_len * sizeof(int)); for (i=0; i<w_len; i++) buffer[i] = rank; /* set the buffer pointers to different offsets to the I/O buffer */ bufs = (int**) malloc(num_reqs * sizeof(int*)); bufs[0] = buffer; for (i=1; i<num_reqs; i++) bufs[i] = bufs[i-1] + bufcounts[i-1]; err = ncmpi_mput_vara_all(ncid, num_reqs, nvarids, starts, counts, (void**)bufs, bufcounts, datatypes); ERR err = ncmpi_close(ncid); ERR free(buffer); free(bufs); free(nvarids); free(bufcounts); free(datatypes); free(starts[0]); free(counts[0]); free(starts); free(counts); /* check if there is any PnetCDF internal malloc residue */ MPI_Offset malloc_size, sum_size; err = ncmpi_inq_malloc_size(&malloc_size); if (err == NC_NOERR) { MPI_Reduce(&malloc_size, &sum_size, 1, MPI_OFFSET, MPI_SUM, 0, MPI_COMM_WORLD); if (rank == 0 && sum_size > 0) printf("heap memory allocated by PnetCDF internally has %lld bytes yet to be freed\n", sum_size); } MPI_Finalize(); return 0; }
int main(int argc, char **argv) { char filename[256]; int err, nerrs=0, ncid, dimid[NDIMS], varid[5], ndims=NDIMS; int i, j, k, nprocs, rank, req, *buf; MPI_Offset start[NDIMS] = {0}; MPI_Offset count[NDIMS] = {0}; MPI_Offset stride[NDIMS] = {0}; MPI_Init(&argc, &argv); MPI_Comm_rank(MPI_COMM_WORLD, &rank); MPI_Comm_size(MPI_COMM_WORLD, &nprocs); if (argc > 2) { if (!rank) printf("Usage: %s [filename]\n",argv[0]); MPI_Finalize(); return 1; } if (argc == 2) snprintf(filename, 256, "%s", argv[1]); else strcpy(filename, "testfile.nc"); if (rank == 0) { char *cmd_str = (char*)malloc(strlen(argv[0]) + 256); sprintf(cmd_str, "*** TESTING C %s for NULL stride ", basename(argv[0])); printf("%-66s ------ ", cmd_str); fflush(stdout); free(cmd_str); } err = ncmpi_create(MPI_COMM_WORLD, filename, 0, MPI_INFO_NULL, &ncid); CHECK_ERR err = ncmpi_def_dim(ncid, "Y", NY, &dimid[0]); CHECK_ERR err = ncmpi_def_dim(ncid, "X", nprocs*NX, &dimid[1]); CHECK_ERR err = ncmpi_def_var(ncid, "v0", NC_INT, ndims, dimid, &varid[0]); CHECK_ERR err = ncmpi_def_var(ncid, "v1", NC_INT, ndims, dimid, &varid[1]); CHECK_ERR err = ncmpi_def_var(ncid, "v2", NC_INT, ndims, dimid, &varid[2]); CHECK_ERR err = ncmpi_def_var(ncid, "v3", NC_INT, ndims, dimid, &varid[3]); CHECK_ERR err = ncmpi_def_var(ncid, "v4", NC_INT, ndims, dimid, &varid[4]); CHECK_ERR err = ncmpi_enddef(ncid); CHECK_ERR start[0] = 0; start[1] = rank*NX; count[0] = NY; count[1] = NX; buf = (int*) malloc((size_t)NY * NX * sizeof(int)); for (i=0; i<NY*NX; i++) buf[i] = rank+10; err = ncmpi_put_vara_int_all(ncid, varid[0], start, count, buf); CHECK_ERR CHECK_PUT_BUF err = ncmpi_put_vars_int_all(ncid, varid[1], start, count, NULL, buf); CHECK_ERR CHECK_PUT_BUF start[0] = 0; start[1] = rank; count[0] = NY; count[1] = NX; stride[0] = 1; stride[1] = nprocs; err = ncmpi_put_vars_int_all(ncid, varid[2], start, count, stride, buf); CHECK_ERR CHECK_PUT_BUF /* test bput_vars */ err = ncmpi_buffer_attach(ncid, NY*NX*sizeof(int)); CHECK_ERR start[0] = 0; start[1] = rank*NX; count[0] = NY; count[1] = NX; err = ncmpi_bput_vars_int(ncid, varid[3], start, count, NULL, buf, &req); CHECK_ERR err = ncmpi_wait_all(ncid, 1, &req, NULL); CHECK_ERR CHECK_PUT_BUF start[0] = 0; start[1] = rank; count[0] = NY; count[1] = NX; stride[0] = 1; stride[1] = nprocs; err = ncmpi_bput_vars_int(ncid, varid[4], start, count, stride, buf, &req); CHECK_ERR err = ncmpi_wait_all(ncid, 1, &req, NULL); CHECK_ERR CHECK_PUT_BUF free(buf); err = ncmpi_buffer_detach(ncid); CHECK_ERR buf = (int*) malloc((size_t)NY * NX * nprocs * sizeof(int)); memset(buf, 0, (size_t)NY * NX * nprocs * sizeof(int)); err = ncmpi_get_var_int_all(ncid, varid[0], buf); CHECK_ERR /* check read buffer contents */ /* v0 = * 10, 10, 11, 11, 12, 12, 13, 13, * 10, 10, 11, 11, 12, 12, 13, 13, * 10, 10, 11, 11, 12, 12, 13, 13, * 10, 10, 11, 11, 12, 12, 13, 13 ; */ for (i=0; i<NY; i++) { for (j=0; j<nprocs; j++) { for (k=0; k<NX; k++) { if (buf[i*nprocs*NX+j*NX+k] != j+10) { printf("Error at line %d in %s: expected buffer[%d]=%d but got %d\n", __LINE__,__FILE__,i*nprocs*NX+j*NX+k, j+10, buf[i*nprocs*NX+j*NX+k]); nerrs++; } } } } memset(buf, 0, (size_t)NY * NX * nprocs * sizeof(int)); err = ncmpi_get_var_int_all(ncid, varid[1], buf); CHECK_ERR /* check read buffer contents */ /* v1 = * 10, 10, 11, 11, 12, 12, 13, 13, * 10, 10, 11, 11, 12, 12, 13, 13, * 10, 10, 11, 11, 12, 12, 13, 13, * 10, 10, 11, 11, 12, 12, 13, 13 ; */ for (i=0; i<NY; i++) { for (j=0; j<nprocs; j++) { for (k=0; k<NX; k++) { if (buf[i*nprocs*NX+j*NX+k] != j+10) { printf("Error at line %d in %s: expected buffer[%d]=%d but got %d\n", __LINE__,__FILE__,i*nprocs*NX+j*NX+k, j+10, buf[i*nprocs*NX+j*NX+k]); nerrs++; } } } } memset(buf, 0, (size_t)NY * NX * nprocs * sizeof(int)); err = ncmpi_get_var_int_all(ncid, varid[2], buf); CHECK_ERR /* check read buffer contents */ /* v2 = * 10, 11, 12, 13, 10, 11, 12, 13, * 10, 11, 12, 13, 10, 11, 12, 13, * 10, 11, 12, 13, 10, 11, 12, 13, * 10, 11, 12, 13, 10, 11, 12, 13 ; */ for (i=0; i<NY; i++) { for (k=0; k<NX; k++) { for (j=0; j<nprocs; j++) { if (buf[i*nprocs*NX+k*nprocs+j] != j+10) { printf("Error at line %d in %s: expected buffer[%d]=%d but got %d\n", __LINE__,__FILE__,i*nprocs*NX+k*nprocs+j, j+10, buf[i*nprocs*NX+k*nprocs+j]); nerrs++; } } } } memset(buf, 0, (size_t)NY * NX * nprocs * sizeof(int)); err = ncmpi_get_var_int_all(ncid, varid[3], buf); CHECK_ERR /* check read buffer contents */ /* v3 = * 10, 10, 11, 11, 12, 12, 13, 13, * 10, 10, 11, 11, 12, 12, 13, 13, * 10, 10, 11, 11, 12, 12, 13, 13, * 10, 10, 11, 11, 12, 12, 13, 13 ; */ for (i=0; i<NY; i++) { for (j=0; j<nprocs; j++) { for (k=0; k<NX; k++) { if (buf[i*nprocs*NX+j*NX+k] != j+10) { printf("Error at line %d in %s: expected buffer[%d]=%d but got %d\n", __LINE__,__FILE__,i*nprocs*NX+j*NX+k, j+10, buf[i*nprocs*NX+j*NX+k]); nerrs++; } } } } memset(buf, 0, (size_t)NY * NX * nprocs * sizeof(int)); err = ncmpi_get_var_int_all(ncid, varid[4], buf); CHECK_ERR /* check read buffer contents */ /* v4 = * 10, 11, 12, 13, 10, 11, 12, 13, * 10, 11, 12, 13, 10, 11, 12, 13, * 10, 11, 12, 13, 10, 11, 12, 13, * 10, 11, 12, 13, 10, 11, 12, 13 ; */ for (i=0; i<NY; i++) { for (k=0; k<NX; k++) { for (j=0; j<nprocs; j++) { if (buf[i*nprocs*NX+k*nprocs+j] != j+10) { printf("Error at line %d in %s: expected buffer[%d]=%d but got %d\n", __LINE__,__FILE__,i*nprocs*NX+k*nprocs+j, j+10, buf[i*nprocs*NX+k*nprocs+j]); nerrs++; } } } } err = ncmpi_close(ncid); CHECK_ERR free(buf); /* check if PnetCDF freed all internal malloc */ MPI_Offset malloc_size, sum_size; err = ncmpi_inq_malloc_size(&malloc_size); if (err == NC_NOERR) { MPI_Reduce(&malloc_size, &sum_size, 1, MPI_OFFSET, MPI_SUM, 0, MPI_COMM_WORLD); if (rank == 0 && sum_size > 0) printf("heap memory allocated by PnetCDF internally has %lld bytes yet to be freed\n", sum_size); if (malloc_size > 0) ncmpi_inq_malloc_list(); } MPI_Allreduce(MPI_IN_PLACE, &nerrs, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD); if (rank == 0) { if (nerrs) printf(FAIL_STR,nerrs); else printf(PASS_STR); } MPI_Finalize(); return (nerrs > 0); }
int main(int argc, char** argv) { char filename[256]; int i, j, rank, nprocs, err, nerrs=0, expected; int ncid, cmode, varid[2], dimid[2], req[4], st[4], *buf; int *buf0, *buf1, *buf2; size_t len; MPI_Offset start[2], count[2]; MPI_Info info; MPI_Init(&argc, &argv); MPI_Comm_rank(MPI_COMM_WORLD, &rank); MPI_Comm_size(MPI_COMM_WORLD, &nprocs); /* this program is intended to run on one process */ if (rank) goto fn_exit; /* get command-line arguments */ if (argc > 2) { if (!rank) printf("Usage: %s [filename]\n",argv[0]); MPI_Finalize(); return 1; } if (argc == 2) snprintf(filename, 256, "%s", argv[1]); else strcpy(filename, "testfile.nc"); if (rank == 0) { char *cmd_str = (char*)malloc(strlen(argv[0]) + 256); sprintf(cmd_str, "*** TESTING C %s for writing interleaved fileviews ", basename(argv[0])); printf("%-66s ------ ", cmd_str); free(cmd_str); } MPI_Info_create(&info); MPI_Info_set(info, "romio_cb_write", "disable"); MPI_Info_set(info, "ind_wr_buffer_size", "8"); /* these 2 hints are required to cause a core dump if r1758 fix is not * presented */ /* create a new file for writing ----------------------------------------*/ cmode = NC_CLOBBER | NC_64BIT_DATA; err = ncmpi_create(MPI_COMM_SELF, filename, cmode, info, &ncid); CHECK_ERR MPI_Info_free(&info); /* define dimensions Y and X */ err = ncmpi_def_dim(ncid, "Y", NY, &dimid[0]); CHECK_ERR err = ncmpi_def_dim(ncid, "X", NX, &dimid[1]); CHECK_ERR /* define 2D variables of integer type */ err = ncmpi_def_var(ncid, "var0", NC_INT, 2, dimid, &varid[0]); CHECK_ERR err = ncmpi_def_var(ncid, "var1", NC_INT, 2, dimid, &varid[1]); CHECK_ERR /* enable fill mode */ err = ncmpi_set_fill(ncid, NC_FILL, NULL); CHECK_ERR /* do not forget to exit define mode */ err = ncmpi_enddef(ncid); CHECK_ERR /* now we are in data mode */ buf = (int*) malloc(NY*NX * sizeof(int)); /* fill the entire variable var0 with -1s */ for (i=0; i<NY*NX; i++) buf[i] = -1; err = ncmpi_put_var_int_all(ncid, varid[0], buf); CHECK_ERR /* write 8 x 2 elements so this only interleaves the next two * iput requests */ start[0] = 0; start[1] = 3; count[0] = 8; count[1] = 2; len = (size_t)(count[0] * count[1]); buf0 = (int*) malloc(len * sizeof(int)); for (i=0; i<len; i++) buf0[i] = 50+i; err = ncmpi_iput_vara_int(ncid, varid[0], start, count, buf0, &req[0]); CHECK_ERR /* write 1 x 3 elements */ start[0] = 1; start[1] = 8; count[0] = 1; count[1] = 5; len = (size_t)(count[0] * count[1]); buf1 = (int*) malloc(len * sizeof(int)); for (i=0; i<len; i++) buf1[i] = 60+i; err = ncmpi_iput_vara_int(ncid, varid[0], start, count, buf1, &req[1]); CHECK_ERR /* write 1 x 3 elements */ start[0] = 3; start[1] = 7; count[0] = 1; count[1] = 5; len = (size_t)(count[0] * count[1]); buf2 = (int*) malloc(len * sizeof(int)); for (i=0; i<len; i++) buf2[i] = 70+i; err = ncmpi_iput_vara_int(ncid, varid[0], start, count, buf2, &req[2]); CHECK_ERR err = ncmpi_wait_all(ncid, 3, req, st); CHECK_ERR free(buf0); free(buf1); free(buf2); /* fill the entire variable var1 with -1s */ for (i=0; i<NY*NX; i++) buf[i] = -1; err = ncmpi_put_var_int_all(ncid, varid[1], buf); CHECK_ERR /* write 8 x 2 elements so this only interleaves the next two iput * requests */ start[0] = 0; start[1] = 3; count[0] = 8; count[1] = 2; len = (size_t)(count[0] * count[1]); buf0 = (int*) malloc(len * sizeof(int)); for (i=0; i<count[0]*count[1]; i++) buf0[i] = 50+i; err = ncmpi_iput_vara_int(ncid, varid[1], start, count, buf0, &req[0]); CHECK_ERR /* rearrange buffer contents, as buf is 2D */ for (i=0; i<5; i++) buf[i] = 10 + i; for (i=5; i<10; i++) buf[i] = 10 + i + 5; for (i=10; i<15; i++) buf[i] = 10 + i + 10; start[0] = 6; start[1] = 7; count[0] = 3; count[1] = 5; err = ncmpi_iput_vara_int(ncid, varid[1], start, count, buf, &req[1]); CHECK_ERR for (i=15; i<20; i++) buf[i] = 10 + i - 10; for (i=20; i<25; i++) buf[i] = 10 + i - 5; start[0] = 6; start[1] = 12; count[0] = 2; count[1] = 5; err = ncmpi_iput_vara_int(ncid, varid[1], start, count, buf+15, &req[2]); CHECK_ERR for (i=25; i<30; i++) buf[i] = 10 + i; start[0] = 8; start[1] = 12; count[0] = 1; count[1] = 5; err = ncmpi_iput_vara_int(ncid, varid[1], start, count, buf+25, &req[3]); CHECK_ERR err = ncmpi_wait_all(ncid, 4, req, st); CHECK_ERR /* check if write buffer contents have been altered */ for (i=0; i<16; i++) CHECK_CONTENTS(buf0, 50 + i) for (i=0; i<5; i++) CHECK_CONTENTS(buf, 10 + i) for (i=5; i<10; i++) CHECK_CONTENTS(buf, 10 + i + 5) for (i=10; i<15; i++) CHECK_CONTENTS(buf, 10 + i + 10) for (i=15; i<20; i++) CHECK_CONTENTS(buf, 10 + i - 10) for (i=20; i<25; i++) CHECK_CONTENTS(buf, 10 + i - 5) for (i=25; i<30; i++) CHECK_CONTENTS(buf, 10 + i) err = ncmpi_close(ncid); CHECK_ERR free(buf0); /* open the same file and read back for validate */ err = ncmpi_open(MPI_COMM_SELF, filename, NC_NOWRITE, MPI_INFO_NULL, &ncid); CHECK_ERR err = ncmpi_inq_varid(ncid, "var0", &varid[0]); CHECK_ERR err = ncmpi_inq_varid(ncid, "var1", &varid[1]); CHECK_ERR /* read the entire array */ for (i=0; i<NY*NX; i++) buf[i] = -1; err = ncmpi_get_var_int_all(ncid, varid[0], buf); CHECK_ERR /* check if the contents of buf are expected */ expected = 50; for (j=0; j<8; j++) { for (i=3; i<5; i++) { if (buf[j*NX+i] != expected) { printf("%d: Unexpected read buf[%d][%d]=%d, should be %d\n", rank, j, i, buf[j*NX+i], expected); nerrs++; } expected++; } } expected = 60; j = 1; for (i=8; i<13; i++) { if (buf[j*NX+i] != expected) { printf("%d: Unexpected read buf[%d][%d]=%d, should be %d\n", rank, j, i, buf[j*NX+i], expected); nerrs++; } expected++; } expected = 70; j = 3; for (i=7; i<12; i++) { if (buf[j*NX+i] != expected) { printf("%d: Unexpected read buf[%d][%d]=%d, should be %d\n", rank, j, i, buf[j*NX+i], expected); nerrs++; } expected++; } /* initialize the contents of the array to a different value */ for (i=0; i<NY*NX; i++) buf[i] = -1; /* read the entire array */ err = ncmpi_get_var_int_all(ncid, varid[1], buf); CHECK_ERR /* check if the contents of buf are expected */ expected = 10; for (j=6; j<9; j++) { for (i=7; i<17; i++) { if (buf[j*NX+i] != expected) { printf("%d: Unexpected read buf[%d]=%d, should be %d\n", rank, i, buf[j*NX+i], expected); nerrs++; } expected++; } } expected = 50; for (j=0; j<8; j++) { for (i=3; i<5; i++) { if (buf[j*NX+i] != expected) { printf("%d: Unexpected read buf[%d][%d]=%d, should be %d\n", rank, j, i, buf[j*NX+i], expected); nerrs++; } expected++; } } err = ncmpi_close(ncid); CHECK_ERR free(buf); /* check if PnetCDF freed all internal malloc */ MPI_Offset malloc_size; err = ncmpi_inq_malloc_size(&malloc_size); if (err == NC_NOERR && malloc_size > 0) { printf("heap memory allocated by PnetCDF internally has %lld bytes yet to be freed\n", malloc_size); ncmpi_inq_malloc_list(); } fn_exit: MPI_Allreduce(MPI_IN_PLACE, &nerrs, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD); if (rank == 0) { if (nerrs) printf(FAIL_STR,nerrs); else printf(PASS_STR); } MPI_Finalize(); return (nerrs > 0); }
int main(int argc, char** argv) { char filename[256]; int rank, nprocs, nerrs=0; int err, ncid; #if NC_MAX_VAR_DIMS < INT_MAX int i, varid, *dimid; #endif MPI_Init(&argc, &argv); MPI_Comm_rank(MPI_COMM_WORLD, &rank); MPI_Comm_size(MPI_COMM_WORLD, &nprocs); if (argc > 2) { if (!rank) printf("Usage: %s [filename]\n",argv[0]); MPI_Finalize(); return 1; } if (argc == 2) snprintf(filename, 256, "%s", argv[1]); else strcpy(filename, "testfile.nc"); MPI_Bcast(filename, 256, MPI_CHAR, 0, MPI_COMM_WORLD); if (rank == 0) { char *cmd_str = (char*)malloc(strlen(argv[0]) + 256); sprintf(cmd_str, "*** TESTING C %s for checking NC_MAX_VAR_DIMS ", basename(argv[0])); printf("%-66s ------ ", cmd_str); fflush(stdout); free(cmd_str); } #if NC_MAX_VAR_DIMS < INT_MAX err = ncmpi_create(MPI_COMM_WORLD, filename, NC_CLOBBER, MPI_INFO_NULL, &ncid); CHECK_ERR /* define dimensions */ dimid = (int*) malloc((NC_MAX_VAR_DIMS+2) * sizeof(int)); err = ncmpi_def_dim(ncid, "dim0", NC_UNLIMITED, &dimid[0]); CHECK_ERR err = ncmpi_def_dim(ncid, "dim1", 1, &dimid[1]); CHECK_ERR for (i=2; i<NC_MAX_VAR_DIMS+2; i++) dimid[i] = dimid[1]; /* define variables */ err = ncmpi_def_var(ncid, "v0", NC_INT, NC_MAX_VAR_DIMS+1, &dimid[0], &varid); EXP_ERR(NC_EMAXDIMS) err = ncmpi_def_var(ncid, "v1", NC_INT, NC_MAX_VAR_DIMS+1, &dimid[1], &varid); EXP_ERR(NC_EMAXDIMS) err = ncmpi_set_fill(ncid, NC_NOFILL, NULL); CHECK_ERR err = ncmpi_close(ncid); CHECK_ERR free(dimid); /* check if PnetCDF freed all internal malloc */ MPI_Offset malloc_size, sum_size; err = ncmpi_inq_malloc_size(&malloc_size); if (err == NC_NOERR) { MPI_Reduce(&malloc_size, &sum_size, 1, MPI_OFFSET, MPI_SUM, 0, MPI_COMM_WORLD); if (rank == 0 && sum_size > 0) printf("heap memory allocated by PnetCDF internally has %lld bytes yet to be freed\n", sum_size); if (malloc_size > 0) ncmpi_inq_malloc_list(); } MPI_Allreduce(MPI_IN_PLACE, &nerrs, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD); if (rank == 0) { if (nerrs) printf(FAIL_STR,nerrs); else printf(PASS_STR); } #else err = ncmpi_create(MPI_COMM_WORLD, filename, NC_CLOBBER, MPI_INFO_NULL, &ncid); CHECK_ERR err = ncmpi_close(ncid); CHECK_ERR if (rank == 0) printf(SKIP_STR); #endif MPI_Finalize(); return (nerrs > 0); }
int main(int argc, char *argv[]) { char filename[256]; int i, rank, nprocs, err, nerrs=0; int cmode[NUM_FORMATS]={0, NC_64BIT_OFFSET, NC_64BIT_DATA}; MPI_Init(&argc, &argv); MPI_Comm_size(MPI_COMM_WORLD, &nprocs); MPI_Comm_rank(MPI_COMM_WORLD, &rank); if (argc > 2) { if (!rank) printf("Usage: %s [filename]\n",argv[0]); MPI_Finalize(); return 0; } if (argc == 2) snprintf(filename, 256, "%s", argv[1]); else strcpy(filename, "testfile.nc"); MPI_Bcast(filename, 256, MPI_CHAR, 0, MPI_COMM_WORLD); char *cmd_str = (char*)malloc(strlen(argv[0]) + 256); sprintf(cmd_str, "*** TESTING C %s for emulating netCDF tst_small ", basename(argv[0])); if (rank == 0) printf("%-66s ------ ", cmd_str); free(cmd_str); for (i=0; i<NUM_FORMATS; i++) { #ifdef DEBUG printf("*** testing simple small file with a global attribute..."); #endif nerrs += test_small_atts(filename, cmode[i]|NC_CLOBBER); #ifdef DEBUG printf("*** testing simple small file with fixed dimensions..."); #endif nerrs += test_small_fixed(filename, cmode[i]|NC_CLOBBER); #ifdef DEBUG printf("*** testing simple small file with an unlimited dimension..."); #endif nerrs += test_small_unlim(filename, cmode[i]|NC_CLOBBER); #ifdef DEBUG printf("*** testing small file with one variable..."); #endif nerrs += test_small_one(filename, cmode[i]|NC_CLOBBER); #ifdef DEBUG printf("*** testing small file with one variable and one att..."); #endif nerrs += test_one_with_att(filename, cmode[i]|NC_CLOBBER); #ifdef DEBUG printf("*** testing small file with one record variable, which grows..."); #endif nerrs += test_one_growing(filename, cmode[i]|NC_CLOBBER); #ifdef DEBUG printf("*** testing small file with one growing record " "variable, with attributes added..."); #endif nerrs += test_one_growing_with_att(filename, cmode[i]|NC_CLOBBER); #ifdef DEBUG if (verbose) printf("*** testing small file with two growing record " "variables, with attributes added..."); #endif nerrs += test_two_growing_with_att(filename, cmode[i]|NC_CLOBBER); } /* check if PnetCDF freed all internal malloc */ MPI_Offset malloc_size, sum_size; err = ncmpi_inq_malloc_size(&malloc_size); if (err == NC_NOERR) { MPI_Reduce(&malloc_size, &sum_size, 1, MPI_OFFSET, MPI_SUM, 0, MPI_COMM_WORLD); if (rank == 0 && sum_size > 0) printf("heap memory allocated by PnetCDF internally has %lld bytes yet to be freed\n", sum_size); } MPI_Allreduce(MPI_IN_PLACE, &nerrs, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD); if (rank == 0) { if (nerrs) printf(FAIL_STR,nerrs); else printf(PASS_STR); } MPI_Finalize(); return 0; }
/*----< main() >------------------------------------------------------------*/ int main(int argc, char **argv) { extern int optind; char filename[256]; int i, nerrs=0, err, rank, nprocs, ncid, varid[2], dimids[2]; MPI_Init(&argc, &argv); MPI_Comm_size(MPI_COMM_WORLD, &nprocs); MPI_Comm_rank(MPI_COMM_WORLD, &rank); if (rank == 0) { char *cmd_str = (char*)malloc(strlen(argv[0]) + 256); sprintf(cmd_str, "*** TESTING C %s for profiling ", basename(argv[0])); printf("%-66s ------ ", cmd_str); fflush(stdout); free(cmd_str); } err = verbose = 0; if (rank == 0) { /* get command-line arguments */ while ((i = getopt(argc, argv, "vh")) != EOF) { switch(i) { case 'v': verbose = 1; break; case 'h': default: printf("Usage: %s [-v] [filename]\n",argv[0]); err = 1; } } if (argv[optind] == NULL) strcpy(filename, "testfile.nc"); else snprintf(filename, 256, "%s", argv[optind]); } MPI_Bcast(&err, 1, MPI_INT, 0, MPI_COMM_WORLD); if (err == 1) { MPI_Finalize(); return 1; } MPI_Bcast(&verbose, 1, MPI_INT, 0, MPI_COMM_WORLD); MPI_Bcast(filename, 256, MPI_CHAR, 0, MPI_COMM_WORLD); /* create a new file for write */ TRC(ncmpi_create)(MPI_COMM_WORLD, filename, NC_CLOBBER, MPI_INFO_NULL, &ncid); CHECK_ERR if (verbose) printf("%d: ---- after ncmpi_create\n",rank); /* define a 2D array */ err = ncmpi_def_dim(ncid, "REC_DIM", NC_UNLIMITED, &dimids[0]); CHECK_ERR err = ncmpi_def_dim(ncid, "X", NX*nprocs, &dimids[1]); CHECK_ERR err = ncmpi_def_var(ncid, "rec_var", NC_INT, 2, dimids, &varid[0]); CHECK_ERR err = ncmpi_def_dim(ncid, "Y", 2, &dimids[0]); CHECK_ERR err = ncmpi_def_var(ncid, "fix_var", NC_INT, 2, dimids, &varid[1]); CHECK_ERR /* add attributes to the variable */ err = ncmpi_put_att_text(ncid, varid[0], "att_name", 14, "attribute text"); CHECK_ERR TRC(ncmpi_enddef)(ncid); CHECK_ERR // nerrs += test_vara(ncid, varid); // nerrs += test_ivara(ncid, varid); // nerrs += test_vard(ncid, varid); // nerrs += test_varn(ncid); nerrs += test_ivarn(ncid); TRC(ncmpi_close)(ncid); CHECK_ERR /* check if PnetCDF freed all internal malloc */ MPI_Offset malloc_size, sum_size; err = ncmpi_inq_malloc_size(&malloc_size); if (err == NC_NOERR) { MPI_Reduce(&malloc_size, &sum_size, 1, MPI_OFFSET, MPI_SUM, 0, MPI_COMM_WORLD); if (rank == 0 && sum_size > 0) printf("heap memory allocated by PnetCDF internally has %lld bytes yet to be freed\n", sum_size); if (malloc_size > 0) ncmpi_inq_malloc_list(); } MPI_Allreduce(MPI_IN_PLACE, &nerrs, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD); if (rank == 0) { if (nerrs) printf(FAIL_STR,nerrs); else printf(PASS_STR); } MPI_Finalize(); return (nerrs > 0); }