JDFTx  1.2.0
Util.h File Reference

Miscellaneous utilities. More...

#include <core/MPIUtil.h>
#include <map>
#include <array>
#include <cstring>
#include <cstdio>
#include <cstdint>
#include <sys/time.h>
#include <sys/wait.h>
#include <unistd.h>
#include <execinfo.h>
#include <sys/stat.h>
#include <inttypes.h>

Go to the source code of this file.

Classes

class  StopWatch
 
class  EnumStringMap< Enum >
 A template to ease option parsing (maps enums <–> strings) More...
 

Macros

#define TIME(title, fp, code)
 Elapsed time in seconds (from start of program) More...
 
#define assert(expr)   (void)((expr) ? 0 : assertStackTraceExit(#expr, __func__, __FILE__, __LINE__))
 A custom assertion with stack trace (NOTE: enabled in release modes as well)
 
#define logPrintf(...)   fprintf(globalLog, __VA_ARGS__)
 printf() for log files
 
#define logFlush()   fflush(globalLog)
 fflush() for log files
 
#define die(...)
 Quit with an error message (formatted using printf()). Must be called from all processes. More...
 
#define die_alone(...)
 Version of die that should only be used when it is impossible to guarantee synchronized calls from all processes. More...
 
#define PRIdPTR   "zd"
 

Functions

void printVersionBanner ()
 Print package name, version, revision etc. to log.
 
void initSystem (int argc, char **argv)
 Init MPI (if not already done), print banner, set up threads (play nice with job schedulers), GPU and signal handlers.
 
void initSystemCmdline (int argc, char **argv, const char *description, string &inputFilename, bool &dryRun, bool &printDefaults)
 initSystem along with commandline options
 
void finalizeSystem (bool successful=true)
 Clean-up corresponding to initSystem(), final messages (depending on successful) and clean-up MPI.
 
double clock_us ()
 
double clock_sec ()
 Elapsed time in microseconds (from start of program)
 
void printStack (bool detailedStackScript=false)
 Print a minimal stack trace and optionally write a script that, when run, will print a more detailed stacktrace.
 
void stackTraceExit (int code)
 Exit on error with stack trace.
 
int assertStackTraceExit (const char *expr, const char *function, const char *file, long line)
 
void logSuspend ()
 temporarily disable all log output (until logResume())
 
void logResume ()
 re-enable logging after a logSuspend() call
 
void Citations::add (string reason, string paper)
 
void Citations::print (FILE *fp=globalLog)
 Print the list of citations (with reasons) to the specified stream.
 
off_t fileSize (const char *filename)
 Get the size of a file.
 
void convertToLE (void *ptr, size_t size, size_t nmemb)
 Convert data from operating endianness to little-endian.
 
void convertFromLE (void *ptr, size_t size, size_t nmemb)
 Convert data from little-endian to operating endianness.
 
size_t freadLE (void *ptr, size_t size, size_t nmemb, FILE *fp)
 Read from a little-endian binary file, regardless of operating endianness.
 
size_t fwriteLE (const void *ptr, size_t size, size_t nmemb, FILE *fp)
 Write to a little-endian binary file, regardless of operating endianness.
 
uint16_t positiveRemainder (int16_t x, uint16_t y)
 For any x and y>0, compute z = x % y such that 0 <= z < y.
 
bool fftSuitable (int N)
 Check if an integer is suitable for Fast Fourier Transforms (small prime factors only)
 

Variables

bool killFlag
 Flag set by signal handlers - all compute loops should quit cleanly when this is set.
 
MPIUtilmpiUtil
 
bool mpiDebugLog
 If true, all processes output to seperate debug log files, otherwise only head process outputs (set before calling initSystem())
 
FILE * globalLog
 
FILE * nullLog
 pointer to /dev/null
 

Detailed Description

Miscellaneous utilities.

Macro Definition Documentation

#define die (   ...)
Value:
{ fprintf(globalLog, __VA_ARGS__); \
if(mpiUtil->isHead() && globalLog != stdout) \
fprintf(stderr, __VA_ARGS__); \
finalizeSystem(false); \
exit(1); \
}
bool isHead() const
whether this is the root process (makes code more readable)
Definition: MPIUtil.h:39
void finalizeSystem(bool successful=true)
Clean-up corresponding to initSystem(), final messages (depending on successful) and clean-up MPI...

Quit with an error message (formatted using printf()). Must be called from all processes.

#define die_alone (   ...)
Value:
{ fprintf(globalLog, __VA_ARGS__); \
fflush(globalLog); \
if(mpiUtil->isHead() && globalLog != stdout) \
fprintf(stderr, __VA_ARGS__); \
if(mpiUtil->nProcesses() == 1) finalizeSystem(false); /* Safe to call only if no other process */ \
mpiUtil->exit(1); \
}
int nProcesses() const
number of processes
Definition: MPIUtil.h:38
bool isHead() const
whether this is the root process (makes code more readable)
Definition: MPIUtil.h:39
void finalizeSystem(bool successful=true)
Clean-up corresponding to initSystem(), final messages (depending on successful) and clean-up MPI...

Version of die that should only be used when it is impossible to guarantee synchronized calls from all processes.

#define TIME (   title,
  fp,
  code 
)
Value:
{ double runTime = clock_us(); \
{ code } \
runTime = clock_us() - runTime; \
fprintf(fp, "%s took %.2le s.\n", title, runTime*1e-6); \
}

Elapsed time in seconds (from start of program)

Time a code section and print the timing (with title).
Code must be self-contained as a scope, as it will be surrounded by { }, and should not define "runTime"

Parameters
titleName to use when printing the timing info for the code block
fpStream to output the timing info to
codeThe code block to time (must be a self-contained scope)

Function Documentation

void Citations::add ( string  reason,
string  paper 
)

Add a citation to a paper with a reason (The same paper could be cited multiple times for different reasons)

int assertStackTraceExit ( const char *  expr,
const char *  function,
const char *  file,
long  line 
)

stack trace on failed assertions