add -nonbuf / -nb command line flag to disable buffering for screen and logfile

This commit is contained in:
Axel Kohlmeyer
2022-08-09 15:52:47 -04:00
parent ba7507c9e5
commit 5c589dbe20
2 changed files with 35 additions and 1 deletions

View File

@ -14,6 +14,7 @@ letter abbreviation can be used:
* :ref:`-m or -mpicolor <mpicolor>` * :ref:`-m or -mpicolor <mpicolor>`
* :ref:`-c or -cite <cite>` * :ref:`-c or -cite <cite>`
* :ref:`-nc or -nocite <nocite>` * :ref:`-nc or -nocite <nocite>`
* :ref:`-nb or -nonbuf <nonbuf>`
* :ref:`-pk or -package <package>` * :ref:`-pk or -package <package>`
* :ref:`-p or -partition <partition>` * :ref:`-p or -partition <partition>`
* :ref:`-pl or -plog <plog>` * :ref:`-pl or -plog <plog>`
@ -257,6 +258,23 @@ Disable generating a citation reminder (see above) at all.
---------- ----------
.. _nonbuf:
**-nonbuf**
Turn off buffering for screen and logfile output. For performance
reasons, output to the screen and logfile is usually buffered, i.e.
output is only generated if a buffer - typically 4096 bytes - has been
filled. However, when LAMMPS crashes, that can mean that there is
important output missing. When using this flag, this buffering is
turned off (only for screen and logfile output). Note that when running
in parallel with MPI, the screen output may still be buffered by the MPI
library which cannot be changed by LAMMPS. This flag should only be
used for debugging and not for production simulations as the performance
impact can be significant, especially for large parallel runs.
----------
.. _package: .. _package:
**-package style args ....** **-package style args ....**

View File

@ -196,6 +196,7 @@ LAMMPS::LAMMPS(int narg, char **arg, MPI_Comm communicator) :
int citelogfile = CiteMe::VERBOSE; int citelogfile = CiteMe::VERBOSE;
char *citefile = nullptr; char *citefile = nullptr;
int helpflag = 0; int helpflag = 0;
int nonbufflag = 0;
suffix = suffix2 = suffixp = nullptr; suffix = suffix2 = suffixp = nullptr;
suffix_enable = 0; suffix_enable = 0;
@ -298,6 +299,11 @@ LAMMPS::LAMMPS(int narg, char **arg, MPI_Comm communicator) :
citeflag = 0; citeflag = 0;
iarg++; iarg++;
} else if (strcmp(arg[iarg],"-nonbuf") == 0 ||
strcmp(arg[iarg],"-nb") == 0) {
nonbufflag = 1;
iarg++;
} else if (strcmp(arg[iarg],"-package") == 0 || } else if (strcmp(arg[iarg],"-package") == 0 ||
strcmp(arg[iarg],"-pk") == 0) { strcmp(arg[iarg],"-pk") == 0) {
if (iarg+2 > narg) if (iarg+2 > narg)
@ -520,7 +526,6 @@ LAMMPS::LAMMPS(int narg, char **arg, MPI_Comm communicator) :
utils::flush_buffers(this); utils::flush_buffers(this);
} }
// universe is one or more worlds, as setup by partition switch // universe is one or more worlds, as setup by partition switch
// split universe communicator into separate world communicators // split universe communicator into separate world communicators
// set world screen, logfile, communicator, infile // set world screen, logfile, communicator, infile
@ -540,6 +545,7 @@ LAMMPS::LAMMPS(int narg, char **arg, MPI_Comm communicator) :
screen = fopen(str.c_str(),"w"); screen = fopen(str.c_str(),"w");
if (screen == nullptr) if (screen == nullptr)
error->one(FLERR,"Cannot open screen file {}: {}",str,utils::getsyserror()); error->one(FLERR,"Cannot open screen file {}: {}",str,utils::getsyserror());
setvbuf(screen, NULL, _IONBF, 0);
} else if (strcmp(arg[screenflag],"none") == 0) { } else if (strcmp(arg[screenflag],"none") == 0) {
screen = nullptr; screen = nullptr;
} else { } else {
@ -563,6 +569,7 @@ LAMMPS::LAMMPS(int narg, char **arg, MPI_Comm communicator) :
logfile = fopen(str.c_str(),"w"); logfile = fopen(str.c_str(),"w");
if (logfile == nullptr) if (logfile == nullptr)
error->one(FLERR,"Cannot open logfile {}: {}",str, utils::getsyserror()); error->one(FLERR,"Cannot open logfile {}: {}",str, utils::getsyserror());
setbuf(logfile, NULL);
} else if (strcmp(arg[logflag],"none") == 0) { } else if (strcmp(arg[logflag],"none") == 0) {
logfile = nullptr; logfile = nullptr;
} else { } else {
@ -587,6 +594,15 @@ LAMMPS::LAMMPS(int narg, char **arg, MPI_Comm communicator) :
} }
} }
// make all screen and logfile output unbuffered for debugging crashes
if (nonbufflag) {
if (universe->uscreen) setbuf(universe->uscreen, nullptr);
if (universe->ulogfile) setbuf(universe->ulogfile, nullptr);
if (screen) setbuf(screen, nullptr);
if (logfile) setbuf(logfile, nullptr);
}
// screen and logfile messages for universe and world // screen and logfile messages for universe and world
if ((universe->me == 0) && (!helpflag)) { if ((universe->me == 0) && (!helpflag)) {