20 #ifndef JDFTX_CORE_UTIL_H    21 #define JDFTX_CORE_UTIL_H    27 #include <core/MPIUtil.h>    45 void initSystemCmdline(
int argc, 
char** argv, 
const char* description, 
string& inputFilename, 
bool& dryRun, 
bool& printDefaults); 
    59 #define TIME(title,fp,code) \    60 {       double runTime = clock_us(); \    62         runTime = clock_us() - runTime; \    63         fprintf(fp, "%s took %.2le s.\n", title, runTime*1e-6); \    70 #ifdef ENABLE_PROFILING    79         double tPrev, Ttot, TsqTot; 
int nT;
    82 #else //ENABLE_PROFILING    91 #endif //ENABLE_PROFILING    96 void printStack(
bool detailedStackScript=
false); 
    99 #define assert(expr) \   101         (void)((expr) ? 0 : assertStackTraceExit(#expr, __func__, __FILE__, __LINE__))   105 extern FILE* globalLog;
   110 #define logPrintf(...) fprintf(globalLog, __VA_ARGS__)    111 #define logFlush() fflush(globalLog)    115         {       fprintf(globalLog, __VA_ARGS__); \   116                 if(mpiUtil->isHead() && globalLog != stdout) \   117                         fprintf(stderr, __VA_ARGS__); \   118                 finalizeSystem(false); \   123 #define die_alone(...) \   124         {       fprintf(globalLog, __VA_ARGS__); \   126                 if(mpiUtil->isHead() && globalLog != stdout) \   127                         fprintf(stderr, __VA_ARGS__); \   128                 if(mpiUtil->nProcesses() == 1) finalizeSystem(false);  \   135         void add(
string reason, 
string paper);
   139         void print(FILE* fp=globalLog);
   146 #include <sys/stat.h>   148 off_t 
fileSize(
const char *filename);
   152 {       int16_t xMody = x % y;
   153         if(xMody < 0) 
return uint16_t(y + xMody);
   159 {       
static std::array<int,4> primes = {{2,3,5,7}};
   161         for(
int p: primes) 
while(tmpN % p == 0) tmpN /= p;
   167 template<
typename Enum>
   170         std::map<string,Enum> stringToEnum;
   171         std::map<Enum,string> enumToString;
   173         template<
typename...Args> 
void addEntry(Enum e, 
const string& s, Args...args)
   174         {       stringToEnum[s]=e; enumToString[e]=s;
   180         template<
typename...Args> 
EnumStringMap(Args...args) { addEntry(args...); }
   183         bool getEnum(
const char* key, Enum& e)
 const   184         {       
typename std::map<string,Enum>::const_iterator i = stringToEnum.find(key);
   185                 if(i==stringToEnum.end()) 
return false;
   193         const char* getString(Enum e)
 const   194         {       
typename std::map<Enum,string>::const_iterator i = enumToString.find(e);
   195                 return i->second.c_str();
   198         string optionList()
 const   199         {       
typename std::map<string,Enum>::const_iterator i=stringToEnum.begin();
   200                 string ret = i->first; i++;
   201                 for(; i!=stringToEnum.end(); i++) ret += (
"|"+i->first);
   206 #endif //JDFTX_CORE_UTIL_H 
A template to ease option parsing (maps enums <–> strings) 
Definition: Util.h:168
 
void initSystemCmdline(int argc, char **argv, const char *description, string &inputFilename, bool &dryRun, bool &printDefaults)
initSystem along with commandline options 
 
void printVersionBanner()
Print package name, version, revision etc. to log. 
 
bool mpiDebugLog
If true, all processes output to seperate debug log files, otherwise only head process outputs (set b...
 
off_t fileSize(const char *filename)
Get the size of a file. 
 
int assertStackTraceExit(const char *expr, const char *function, const char *file, long line)
 
void finalizeSystem(bool successful=true)
Clean-up corresponding to initSystem(), final messages (depending on successful) and clean-up MPI...
 
double clock_sec()
Elapsed time in microseconds (from start of program) 
 
void logResume()
re-enable logging after a logSuspend() call 
 
bool killFlag
Flag set by signal handlers - all compute loops should quit cleanly when this is set. 
 
void printStack(bool detailedStackScript=false)
Print a minimal stack trace and optionally write a script that, when run, will print a more detailed ...
 
MPI wrapper class. 
Definition: MPIUtil.h:33
 
FILE * nullLog
pointer to /dev/null 
 
bool fftSuitable(int N)
Check if an integer is suitable for Fast Fourier Transforms (small prime factors only) ...
Definition: Util.h:158
 
uint16_t positiveRemainder(int16_t x, uint16_t y)
For any x and y>0, compute z = x % y such that 0 <= z < y. 
Definition: Util.h:151
 
void stackTraceExit(int code)
Exit on error with stack trace. 
 
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 logSuspend()
temporarily disable all log output (until logResume())