curfil  ..
 All Classes Functions Variables Typedefs Friends Groups Pages
utils.h
1 #ifndef CURFIL_UTILS_H
2 #define CURFIL_UTILS_H
3 
4 #include <boost/date_time/posix_time/posix_time.hpp>
5 #include <boost/format.hpp>
6 #include <sstream>
7 #include <string>
8 
9 namespace curfil {
10 
11 void logVersionInfo();
12 
13 #define cudaSafeCall(X) X; curfil::utils::checkCudaError(#X);
14 
15 // for debugging purposes
16 template<class T>
17 static inline void assert_equals(const T a, const T b) {
18  if (a != b) {
19  assert(a == b);
20  }
21 }
22 
23 #ifndef NDEBUG
24 #define assertProbability(probability) { \
25  if (probability < 0.0 || probability > 1.0) { \
26  printf("illegal probability: %lf\n", static_cast<double>(probability)); \
27  } \
28  assert(probability >= 0.0); \
29  assert(probability <= 1.0); \
30 }
31 #else
32 #define assertProbability(probability) {}
33 #endif
34 
35 namespace utils {
36 
37 void checkCudaError(const char* msg);
38 
42 class Timer {
43 public:
44  Timer() :
45  isStopped(false) {
46  start();
47  }
48 
49  void reset();
50  void start();
51  void stop();
53  std::string format(int precision);
55  double getSeconds();
56  double getMilliseconds();
58 private:
59  bool isStopped;
60  boost::posix_time::ptime started;
61  boost::posix_time::ptime stopped;
62 
63 };
64 
68 class Average {
69 public:
70  Average() :
71  sum(0), count(0) {
72  }
73 
77  void addValue(const double& value) {
78  sum += value;
79  count++;
80  }
81 
85  double getAverage() const {
86  if (count == 0)
87  return 0;
88  return sum / count;
89  }
90 
91 private:
92  double sum;
93  size_t count;
94 };
95 
96 void logMessage(const std::string& message, std::ostream& os);
97 
98 #define CURFIL_LOG(level, message, os) { \
99  std::ostringstream o; \
100  o << boost::format("%-8s") % level; \
101  o << message; \
102  curfil::utils::logMessage(o.str(), os); \
103  }
104 
105 #define CURFIL_INFO(x) CURFIL_LOG("INFO", x, std::cout)
106 
107 #define CURFIL_WARNING(x) CURFIL_LOG("WARNING", x, std::cout)
108 
109 #define CURFIL_ERROR(x) CURFIL_LOG("ERROR", x, std::cout)
110 
111 #ifdef CURFIL_DEBUG
112 #undef CURFIL_DEBUG
113 #endif
114 
115 #ifdef NDEBUG
116 #define CURFIL_DEBUG(x) {}
117 #else
118 #define CURFIL_DEBUG(x) CURFIL_LOG("DEBUG", x, std::cout)
119 #endif
120 
124 class Profile {
125 public:
129  Profile(const std::string& name) :
130  name(name), timer() {
131  }
132 
133  ~Profile() {
134  if (isEnabled()) {
135  timer.stop();
136  CURFIL_INFO("PROFILING('" << name << "'): " << timer.format(3));
137  }
138  }
139 
143  double getSeconds() {
144  return timer.getSeconds();
145  }
146 
150  static bool isEnabled() {
151  return enabled;
152  }
153 
157  static void setEnabled(bool enable) {
158  enabled = enable;
159  CURFIL_INFO("profiling " << ((enabled) ? "enabled" : "disabled"));
160  }
161 
162 private:
163 
164  static bool enabled;
165 
166  std::string name;
167  Timer timer;
168 };
169 
170 size_t getFreeMemoryOnGPU(int deviceId);
171 
172 }
173 }
174 
175 #endif