From 100231bba8f19b4deb3c9e73911bc7bcaca8fbd8 Mon Sep 17 00:00:00 2001 From: Lars Pastewka Date: Sun, 1 Oct 2017 14:21:09 +0200 Subject: [PATCH] ENH: Enable multi file writes. --- src/USER-NETCDF/dump_netcdf.cpp | 35 ++++++++++++++++++++----- src/USER-NETCDF/dump_netcdf_mpiio.cpp | 37 +++++++++++++++++++++------ 2 files changed, 57 insertions(+), 15 deletions(-) diff --git a/src/USER-NETCDF/dump_netcdf.cpp b/src/USER-NETCDF/dump_netcdf.cpp index aa2a4700a3..85a2d40935 100644 --- a/src/USER-NETCDF/dump_netcdf.cpp +++ b/src/USER-NETCDF/dump_netcdf.cpp @@ -88,8 +88,8 @@ DumpNetCDF::DumpNetCDF(LAMMPS *lmp, int narg, char **arg) : if (multiproc) error->all(FLERR,"Multi-processor writes are not supported."); - if (multifile) - error->all(FLERR,"Multiple files are not supported."); + if (append_flag && multifile) + error->all(FLERR,"Cannot append when writing to multiple files."); perat = new nc_perat_t[nfield]; @@ -224,6 +224,24 @@ DumpNetCDF::~DumpNetCDF() void DumpNetCDF::openfile() { + char *filecurrent = filename; + if (multifile && !singlefile_opened) { + char *filestar = filecurrent; + filecurrent = new char[strlen(filestar) + 16]; + char *ptr = strchr(filestar,'*'); + *ptr = '\0'; + if (padflag == 0) + sprintf(filecurrent,"%s" BIGINT_FORMAT "%s", + filestar,update->ntimestep,ptr+1); + else { + char bif[8],pad[16]; + strcpy(bif,BIGINT_FORMAT); + sprintf(pad,"%%s%%0%d%s%%s",padflag,&bif[1]); + sprintf(filecurrent,pad,filestar,update->ntimestep,ptr+1); + } + *ptr = '*'; + } + if (thermo && !singlefile_opened) { if (thermovar) delete [] thermovar; thermovar = new int[output->thermo->nfield]; @@ -268,14 +286,14 @@ void DumpNetCDF::openfile() ntotalgr = group->count(igroup); if (filewriter) { - if (append_flag && access(filename, F_OK) != -1) { + if (append_flag && !multifile && access(filecurrent, F_OK) != -1) { // Fixme! Perform checks if dimensions and variables conform with // data structure standard. if (singlefile_opened) return; singlefile_opened = 1; - NCERRX( nc_open(filename, NC_WRITE, &ncid), filename ); + NCERRX( nc_open(filecurrent, NC_WRITE, &ncid), filecurrent ); // dimensions NCERRX( nc_inq_dimid(ncid, NC_FRAME_STR, &frame_dim), NC_FRAME_STR ); @@ -348,8 +366,8 @@ void DumpNetCDF::openfile() if (singlefile_opened) return; singlefile_opened = 1; - NCERRX( nc_create(filename, NC_64BIT_DATA, &ncid), - filename ); + NCERRX( nc_create(filecurrent, NC_64BIT_DATA, &ncid), + filecurrent ); // dimensions NCERRX( nc_def_dim(ncid, NC_FRAME_STR, NC_UNLIMITED, &frame_dim), @@ -601,7 +619,10 @@ void DumpNetCDF::closefile() // append next time DumpNetCDF::openfile is called append_flag = 1; // write to next frame upon next open - framei++; + if (multifile) + framei = 1; + else + framei++; } } diff --git a/src/USER-NETCDF/dump_netcdf_mpiio.cpp b/src/USER-NETCDF/dump_netcdf_mpiio.cpp index e054772c41..271f963a4e 100644 --- a/src/USER-NETCDF/dump_netcdf_mpiio.cpp +++ b/src/USER-NETCDF/dump_netcdf_mpiio.cpp @@ -88,8 +88,8 @@ DumpNetCDFMPIIO::DumpNetCDFMPIIO(LAMMPS *lmp, int narg, char **arg) : if (multiproc) error->all(FLERR,"Multi-processor writes are not supported."); - if (multifile) - error->all(FLERR,"Multiple files are not supported."); + if (append_flag && multifile) + error->all(FLERR,"Cannot append when writing to multiple files."); perat = new nc_perat_t[nfield]; @@ -217,6 +217,24 @@ DumpNetCDFMPIIO::~DumpNetCDFMPIIO() void DumpNetCDFMPIIO::openfile() { + char *filecurrent = filename; + if (multifile && !singlefile_opened) { + char *filestar = filecurrent; + filecurrent = new char[strlen(filestar) + 16]; + char *ptr = strchr(filestar,'*'); + *ptr = '\0'; + if (padflag == 0) + sprintf(filecurrent,"%s" BIGINT_FORMAT "%s", + filestar,update->ntimestep,ptr+1); + else { + char bif[8],pad[16]; + strcpy(bif,BIGINT_FORMAT); + sprintf(pad,"%%s%%0%d%s%%s",padflag,&bif[1]); + sprintf(filecurrent,pad,filestar,update->ntimestep,ptr+1); + } + *ptr = '*'; + } + if (thermo && !singlefile_opened) { if (thermovar) delete [] thermovar; thermovar = new int[output->thermo->nfield]; @@ -260,7 +278,7 @@ void DumpNetCDFMPIIO::openfile() // get total number of atoms ntotalgr = group->count(igroup); - if (append_flag && access(filename, F_OK) != -1) { + if (append_flag && !multifile && access(filecurrent, F_OK) != -1) { // Fixme! Perform checks if dimensions and variables conform with // data structure standard. @@ -270,8 +288,8 @@ void DumpNetCDFMPIIO::openfile() if (singlefile_opened) return; singlefile_opened = 1; - NCERRX( ncmpi_open(MPI_COMM_WORLD, filename, NC_WRITE, MPI_INFO_NULL, - &ncid), filename ); + NCERRX( ncmpi_open(MPI_COMM_WORLD, filecurrent, NC_WRITE, MPI_INFO_NULL, + &ncid), filecurrent ); // dimensions NCERRX( ncmpi_inq_dimid(ncid, NC_FRAME_STR, &frame_dim), NC_FRAME_STR ); @@ -344,8 +362,8 @@ void DumpNetCDFMPIIO::openfile() if (singlefile_opened) return; singlefile_opened = 1; - NCERRX( ncmpi_create(MPI_COMM_WORLD, filename, NC_64BIT_DATA, - MPI_INFO_NULL, &ncid), filename ); + NCERRX( ncmpi_create(MPI_COMM_WORLD, filecurrent, NC_64BIT_DATA, + MPI_INFO_NULL, &ncid), filecurrent ); // dimensions NCERRX( ncmpi_def_dim(ncid, NC_FRAME_STR, NC_UNLIMITED, &frame_dim), @@ -577,7 +595,10 @@ void DumpNetCDFMPIIO::closefile() // append next time DumpNetCDFMPIIO::openfile is called append_flag = 1; // write to next frame upon next open - framei++; + if (multifile) + framei = 1; + else + framei++; } }