git-svn-id: svn://svn.icms.temple.edu/lammps-ro/trunk@15203 f3b2605a-c512-4ea7-a41b-209d697bcdaa
This commit is contained in:
116
src/timer.cpp
116
src/timer.cpp
@ -13,9 +13,11 @@
|
||||
|
||||
#include <mpi.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include "timer.h"
|
||||
#include "comm.h"
|
||||
#include "error.h"
|
||||
#include "force.h"
|
||||
#include "memory.h"
|
||||
|
||||
#ifdef _WIN32
|
||||
@ -26,8 +28,37 @@
|
||||
#include <sys/resource.h>
|
||||
#endif
|
||||
|
||||
#include <time.h>
|
||||
|
||||
using namespace LAMMPS_NS;
|
||||
|
||||
// convert a timespec ([[HH:]MM:]SS) to seconds
|
||||
// the strings "off" and "unlimited" result in -1;
|
||||
|
||||
static double timespec2seconds(char *timespec)
|
||||
{
|
||||
double vals[3];
|
||||
char *num;
|
||||
int i = 0;
|
||||
|
||||
// first handle allowed textual inputs
|
||||
if (strcmp(timespec,"off") == 0) return -1;
|
||||
if (strcmp(timespec,"unlimited") == 0) return -1;
|
||||
|
||||
vals[0] = vals[1] = vals[2] = 0;
|
||||
|
||||
num = strtok(timespec,":");
|
||||
while ((num != NULL) && (i < 3)) {
|
||||
vals[i] = atoi(num);
|
||||
++i;
|
||||
num = strtok(NULL,":");
|
||||
}
|
||||
|
||||
if (i == 3) return (vals[0]*60 + vals[1])*60 + vals[2];
|
||||
else if (i == 2) return vals[0]*60 + vals[1];
|
||||
else return vals[0];
|
||||
}
|
||||
|
||||
// Return the CPU time for the current process in seconds very
|
||||
// much in the same way as MPI_Wtime() returns the wall time.
|
||||
|
||||
@ -64,6 +95,9 @@ Timer::Timer(LAMMPS *lmp) : Pointers(lmp)
|
||||
{
|
||||
_level = NORMAL;
|
||||
_sync = OFF;
|
||||
_timeout = -1.0;
|
||||
_checkfreq = 10;
|
||||
_nextcheck = -1;
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------------------- */
|
||||
@ -176,12 +210,65 @@ void Timer::set_wall(enum ttype which, double newtime)
|
||||
wall_array[which] = newtime;
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------------------- */
|
||||
|
||||
void Timer::init_timeout()
|
||||
{
|
||||
if (_timeout < 0)
|
||||
_nextcheck = -1;
|
||||
else
|
||||
_nextcheck = _checkfreq;
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------------------- */
|
||||
|
||||
void Timer::print_timeout(FILE *fp)
|
||||
{
|
||||
if (!fp) return;
|
||||
|
||||
// format timeout setting
|
||||
if (_timeout > 0) {
|
||||
// time since init_timeout()
|
||||
const double d = MPI_Wtime() - timeout_start;
|
||||
// remaining timeout in seconds
|
||||
int s = _timeout - d;
|
||||
// remaining 1/100ths of seconds
|
||||
const int hs = 100*((_timeout - d) - s);
|
||||
// breaking s down into second/minutes/hours
|
||||
const int seconds = s % 60;
|
||||
s = (s - seconds) / 60;
|
||||
const int minutes = s % 60;
|
||||
const int hours = (s - minutes) / 60;
|
||||
fprintf(fp," Walltime left : %d:%02d:%02d.%02d\n",
|
||||
hours,minutes,seconds,hs);
|
||||
}
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------------------- */
|
||||
|
||||
bool Timer::_check_timeout()
|
||||
{
|
||||
double walltime = MPI_Wtime() - timeout_start;
|
||||
// broadcast time to insure all ranks act the same.
|
||||
MPI_Bcast(&walltime,1,MPI_DOUBLE,0,world);
|
||||
|
||||
if (walltime < _timeout) {
|
||||
_nextcheck += _checkfreq;
|
||||
return false;
|
||||
} else {
|
||||
if (comm->me == 0)
|
||||
error->warning(FLERR,"Wall time limit reached");
|
||||
_timeout = 0.0;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/* ----------------------------------------------------------------------
|
||||
modify parameters of the Timer class
|
||||
------------------------------------------------------------------------- */
|
||||
static const char *timer_style[] = { "off", "loop", "normal", "full" };
|
||||
static const char *timer_mode[] = { "nosync", "(dummy)", "sync" };
|
||||
static const char timer_fmt[] = "New timer settings: style=%s mode=%s\n";
|
||||
static const char timer_fmt[] = "New timer settings: style=%s mode=%s timeout=%s\n";
|
||||
|
||||
void Timer::modify_params(int narg, char **arg)
|
||||
{
|
||||
@ -199,14 +286,37 @@ void Timer::modify_params(int narg, char **arg)
|
||||
_sync = OFF;
|
||||
} else if (strcmp(arg[iarg],timer_mode[NORMAL]) == 0) {
|
||||
_sync = NORMAL;
|
||||
} else if (strcmp(arg[iarg],"timeout") == 0) {
|
||||
++iarg;
|
||||
if (iarg < narg) {
|
||||
_timeout = timespec2seconds(arg[iarg]);
|
||||
} else error->all(FLERR,"Illegal timers command");
|
||||
} else if (strcmp(arg[iarg],"every") == 0) {
|
||||
++iarg;
|
||||
if (iarg < narg) {
|
||||
_checkfreq = force->inumeric(FLERR,arg[iarg]);
|
||||
if (_checkfreq <= 0)
|
||||
error->all(FLERR,"Illegal timers command");
|
||||
} else error->all(FLERR,"Illegal timers command");
|
||||
} else error->all(FLERR,"Illegal timers command");
|
||||
++iarg;
|
||||
}
|
||||
|
||||
timeout_start = MPI_Wtime();
|
||||
if (comm->me == 0) {
|
||||
|
||||
// format timeout setting
|
||||
char timebuf[32];
|
||||
if (_timeout < 0) strcpy(timebuf,"off");
|
||||
else {
|
||||
time_t tv = _timeout;
|
||||
struct tm *tm = gmtime(&tv);
|
||||
strftime(timebuf,32,"%H:%M:%S",tm);
|
||||
}
|
||||
|
||||
if (screen)
|
||||
fprintf(screen,timer_fmt,timer_style[_level],timer_mode[_sync]);
|
||||
fprintf(screen,timer_fmt,timer_style[_level],timer_mode[_sync],timebuf);
|
||||
if (logfile)
|
||||
fprintf(logfile,timer_fmt,timer_style[_level],timer_mode[_sync]);
|
||||
fprintf(logfile,timer_fmt,timer_style[_level],timer_mode[_sync],timebuf);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user