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);
150 #include <inttypes.h> 152 #define PRIdPTR "zd" //For pre-C++11 compilers 156 void convertToLE(
void* ptr,
size_t size,
size_t nmemb);
158 size_t freadLE(
void *ptr,
size_t size,
size_t nmemb, FILE* fp);
159 size_t fwriteLE(
const void *ptr,
size_t size,
size_t nmemb, FILE *fp);
163 { int16_t xMody = x % y;
164 if(xMody < 0)
return uint16_t(y + xMody);
170 {
static std::array<int,4> primes = {{2,3,5,7}};
172 for(
int p: primes)
while(tmpN % p == 0) tmpN /= p;
178 template<
typename Enum>
181 std::map<string,Enum> stringToEnum;
182 std::map<Enum,string> enumToString;
184 template<
typename...Args>
void addEntry(Enum e,
const string& s, Args...args)
185 { stringToEnum[s]=e; enumToString[e]=s;
191 template<
typename...Args>
EnumStringMap(Args...args) { addEntry(args...); }
194 bool getEnum(
const char* key, Enum& e)
const 195 {
typename std::map<string,Enum>::const_iterator i = stringToEnum.find(key);
196 if(i==stringToEnum.end())
return false;
204 const char* getString(Enum e)
const 205 {
typename std::map<Enum,string>::const_iterator i = enumToString.find(e);
206 return i->second.c_str();
209 string optionList()
const 210 {
typename std::map<string,Enum>::const_iterator i=stringToEnum.begin();
211 string ret = i->first; i++;
212 for(; i!=stringToEnum.end(); i++) ret += (
"|"+i->first);
217 #endif //JDFTX_CORE_UTIL_H
A template to ease option parsing (maps enums <–> strings)
Definition: Util.h:179
void convertFromLE(void *ptr, size_t size, size_t nmemb)
Convert data from little-endian to operating endianness.
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.
size_t freadLE(void *ptr, size_t size, size_t nmemb, FILE *fp)
Read from a little-endian binary file, regardless of operating endianness.
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.
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.
void convertToLE(void *ptr, size_t size, size_t nmemb)
Convert data from operating endianness to little-endian.
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:169
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:162
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())