QUDA  v1.1.0
A library for QCD on GPUs
util_quda.cpp
Go to the documentation of this file.
1 #include <cstdlib>
2 #include <cstdio>
3 #include <cstring>
4 #include <stack>
5 #include <sstream>
6 #include <sys/time.h>
7 
8 #include <enum_quda.h>
9 #include <util_quda.h>
10 #include <malloc_quda.h>
11 
12 static const size_t MAX_PREFIX_SIZE = 100;
13 
14 static QudaVerbosity verbosity_ = QUDA_SUMMARIZE;
15 static char prefix_[MAX_PREFIX_SIZE] = "";
16 static FILE *outfile_ = stdout;
17 
18 static const int MAX_BUFFER_SIZE = 1000;
19 static char buffer_[MAX_BUFFER_SIZE] = "";
20 
21 QudaVerbosity getVerbosity() { return verbosity_; }
22 char *getOutputPrefix() { return prefix_; }
23 FILE *getOutputFile() { return outfile_; }
24 
26 {
27  verbosity_ = verbosity;
28 }
29 
31  static bool init = false;
32  static bool rank_verbosity = false;
33  static char *rank_verbosity_env = getenv("QUDA_RANK_VERBOSITY");
34 
35  if (!init && rank_verbosity_env) { // set the policies to tune for explicitly
36  std::stringstream rank_list(rank_verbosity_env);
37 
38  int rank_;
39  while (rank_list >> rank_) {
40  if (comm_rank() == rank_ || rank_ == -1) rank_verbosity = true;
41  if (rank_list.peek() == ',') rank_list.ignore();
42  }
43  } else if (!init) {
44  rank_verbosity = comm_rank_global() == 0 ? true : false; // default is process 0 only
45  }
46  init = true;
47 
48  return rank_verbosity;
49 }
50 
51 // default has autotuning enabled but can be overridden with the QUDA_ENABLE_TUNING environment variable
53  static bool init = false;
54  static QudaTune tune = QUDA_TUNE_YES;
55 
56  if (!init) {
57  char *enable_tuning = getenv("QUDA_ENABLE_TUNING");
58  if (!enable_tuning || strcmp(enable_tuning,"0")!=0) {
59  tune = QUDA_TUNE_YES;
60  } else {
61  tune = QUDA_TUNE_NO;
62  }
63  init = true;
64  }
65 
66  return tune;
67 }
68 
69 void setOutputPrefix(const char *prefix)
70 {
71  strncpy(prefix_, prefix, MAX_PREFIX_SIZE);
72  prefix_[MAX_PREFIX_SIZE-1] = '\0';
73 }
74 
75 void setOutputFile(FILE *outfile)
76 {
77  outfile_ = outfile;
78 }
79 
80 
81 static std::stack<QudaVerbosity> vstack;
82 
84 {
85  vstack.push(getVerbosity());
87 
88  if (vstack.size() > 15) {
89  warningQuda("Verbosity stack contains %u elements. Is there a missing popVerbosity() somewhere?",
90  static_cast<unsigned int>(vstack.size()));
91  }
92 }
93 
95 {
96  if (vstack.empty()) {
97  errorQuda("popVerbosity() called with empty stack");
98  }
99  setVerbosity(vstack.top());
100  vstack.pop();
101 }
102 
103 static std::stack<char *> pstack;
104 
105 void pushOutputPrefix(const char *prefix)
106 {
107  // backup current prefix onto the stack
108  char *prefix_backup = (char *)safe_malloc(MAX_PREFIX_SIZE * sizeof(char));
109  strncpy(prefix_backup, getOutputPrefix(), MAX_PREFIX_SIZE);
110  pstack.push(prefix_backup);
111 
112  // set new prefix
113  setOutputPrefix(prefix);
114 
115  if (pstack.size() > 15) {
116  warningQuda("Verbosity stack contains %u elements. Is there a missing popOutputPrefix() somewhere?",
117  static_cast<unsigned int>(vstack.size()));
118  }
119 }
120 
122 {
123  if (pstack.empty()) { errorQuda("popOutputPrefix() called with empty stack"); }
124 
125  // recover prefix from stack
126  char *prefix_restore = pstack.top();
127  setOutputPrefix(prefix_restore);
128  host_free(prefix_restore);
129  pstack.pop();
130 }
131 
132 char *getPrintBuffer() { return buffer_; }
133 
135  static char omp_thread_string[128];
136  static bool init = false;
137  if (!init) {
138  strcpy(omp_thread_string,",omp_threads=");
139  char *omp_threads = getenv("OMP_NUM_THREADS");
140  strcat(omp_thread_string, omp_threads ? omp_threads : "1");
141  init = true;
142  }
143  return omp_thread_string;
144 }
int comm_rank(void)
int comm_rank_global(void)
QudaVerbosity verbosity
@ QUDA_SUMMARIZE
Definition: enum_quda.h:266
enum QudaTune_s QudaTune
@ QUDA_TUNE_YES
Definition: enum_quda.h:272
@ QUDA_TUNE_NO
Definition: enum_quda.h:272
enum QudaVerbosity_s QudaVerbosity
#define safe_malloc(size)
Definition: malloc_quda.h:106
#define host_free(ptr)
Definition: malloc_quda.h:115
void init()
Create the BLAS context.
void pushVerbosity(QudaVerbosity verbosity)
Push a new verbosity onto the stack.
Definition: util_quda.cpp:83
QudaTune getTuning()
Query whether autotuning is enabled or not. Default is enabled but can be overridden by setting QUDA_...
Definition: util_quda.cpp:52
void popOutputPrefix()
Pop the output prefix restoring the prior one on the stack.
Definition: util_quda.cpp:121
char * getPrintBuffer()
Definition: util_quda.cpp:132
void popVerbosity()
Pop the verbosity restoring the prior one on the stack.
Definition: util_quda.cpp:94
char * getOmpThreadStr()
Returns a string of the form ",omp_threads=$OMP_NUM_THREADS", which can be used for storing the numbe...
Definition: util_quda.cpp:134
bool getRankVerbosity()
This function returns true if the calling rank is enabled for verbosity (e.g., whether printQuda and ...
Definition: util_quda.cpp:30
FILE * getOutputFile()
Definition: util_quda.cpp:23
void pushOutputPrefix(const char *prefix)
Push a new output prefix onto the stack.
Definition: util_quda.cpp:105
QudaVerbosity getVerbosity()
Definition: util_quda.cpp:21
char * getOutputPrefix()
Definition: util_quda.cpp:22
void setVerbosity(QudaVerbosity verbosity)
Definition: util_quda.cpp:25
void setOutputPrefix(const char *prefix)
Definition: util_quda.cpp:69
void setOutputFile(FILE *outfile)
Definition: util_quda.cpp:75
#define warningQuda(...)
Definition: util_quda.h:132
#define errorQuda(...)
Definition: util_quda.h:120