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);
53 gettimeofday(&tv,NULL);
54 return ((tv.tv_sec & 0x1fffff) * 1e6) + tv.tv_usec;
63 #define TIME(title,fp,code) \ 64 { double runTime = clock_us(); \ 66 runTime = clock_us() - runTime; \ 67 fprintf(fp, "%s took %.2le s.\n", title, runTime*1e-6); \ 74 #ifdef ENABLE_PROFILING 83 double tPrev, Ttot, TsqTot;
int nT;
86 #else //ENABLE_PROFILING 95 #endif //ENABLE_PROFILING 100 void printStack(
bool detailedStackScript=
false);
103 #define assert(expr) \ 105 (void)((expr) ? 0 : assertStackTraceExit(#expr, __func__, __FILE__, __LINE__)) 109 extern FILE* globalLog;
114 #define logPrintf(...) fprintf(globalLog, __VA_ARGS__) 115 #define logFlush() fflush(globalLog) 119 { fprintf(globalLog, __VA_ARGS__); \ 120 if(mpiUtil->isHead() && globalLog != stdout) \ 121 fprintf(stderr, __VA_ARGS__); \ 122 finalizeSystem(false); \ 127 #define die_alone(...) \ 128 { fprintf(globalLog, __VA_ARGS__); \ 130 if(mpiUtil->isHead() && globalLog != stdout) \ 131 fprintf(stderr, __VA_ARGS__); \ 132 if(mpiUtil->nProcesses() == 1) finalizeSystem(false); \ 139 void add(
string reason,
string paper);
143 void print(FILE* fp=globalLog);
150 #include <sys/stat.h> 152 off_t
fileSize(
const char *filename);
156 { int16_t xMody = x % y;
157 if(xMody < 0)
return uint16_t(y + xMody);
163 {
static std::array<int,4> primes = {{2,3,5,7}};
165 for(
int p: primes)
while(tmpN % p == 0) tmpN /= p;
171 template<
typename Enum>
174 std::map<string,Enum> stringToEnum;
175 std::map<Enum,string> enumToString;
177 template<
typename...Args>
void addEntry(Enum e,
const string& s, Args...args)
178 { stringToEnum[s]=e; enumToString[e]=s;
184 template<
typename...Args>
EnumStringMap(Args...args) { addEntry(args...); }
187 bool getEnum(
const char* key, Enum& e)
const 188 {
typename std::map<string,Enum>::const_iterator i = stringToEnum.find(key);
189 if(i==stringToEnum.end())
return false;
197 const char* getString(Enum e)
const 198 {
typename std::map<Enum,string>::const_iterator i = enumToString.find(e);
199 return i->second.c_str();
202 string optionList()
const 203 {
typename std::map<string,Enum>::const_iterator i=stringToEnum.begin();
204 string ret = i->first; i++;
205 for(; i!=stringToEnum.end(); i++) ret += (
"|"+i->first);
210 #endif //JDFTX_CORE_UTIL_H
A template to ease option parsing (maps enums <–> strings)
Definition: Util.h:172
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...
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:162
double clock_us()
Time of day in microseconds.
Definition: Util.h:51
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:155
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())