forked from alexrenz/AdaPM
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.h
More file actions
122 lines (96 loc) · 2.51 KB
/
utils.h
File metadata and controls
122 lines (96 loc) · 2.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include <stdio.h>
#include <stdlib.h>
#include <sstream>
#include <string>
#include <iostream>
#include <fstream>
#include <chrono>
#include <vector>
#include <valarray>
#include <memory>
#pragma once
using namespace std;
template<typename vT>
std::string str(vT v) {
std::stringstream ss;
for(size_t i = 0; i < v.size(); ++i) {
ss << "\t";
ss << v[i];
}
return ss.str();
}
template<typename T>
std::ostream& operator<<(std::ostream& os, std::vector<T> v) {
std::stringstream ss;
ss << str(v);
os << ss.str();
return os;
}
template<typename T>
std::ostream& operator<<(std::ostream& os, std::valarray<T> v) {
std::stringstream ss;
ss << str(v);
os << ss.str();
return os;
}
namespace util {
class Stopwatch {
chrono::time_point<std::chrono::high_resolution_clock> begin;
std::chrono::high_resolution_clock::duration _elapsed {};
public:
void start() {
reset();
begin = std::chrono::high_resolution_clock::now();
}
void resume() {
begin = std::chrono::high_resolution_clock::now();
}
void stop() {
auto finish = std::chrono::high_resolution_clock::now();
_elapsed += (finish - begin);
}
void reset() {
_elapsed = {};
}
long elapsed_ms() {
return std::chrono::duration_cast<std::chrono::milliseconds>(_elapsed).count();
}
long elapsed_us() {
return std::chrono::duration_cast<std::chrono::microseconds>(_elapsed).count();
}
long elapsed_ns() {
return std::chrono::duration_cast<std::chrono::nanoseconds>(_elapsed).count();
}
double elapsed_s() {
return 1.0*elapsed_ms()/1000;
}
};
class Trace {
std::string fname;
public:
Trace(std::string fn) :fname{fn} {}
void clear() {
// clear the trace file
ofstream tracefile (fname, ofstream::trunc);
tracefile.close();
}
void operator() (int epoch, double time_elapsed, double step_size, double loss, double accuracy=0) {
ofstream tracefile (fname, ofstream::app);
tracefile << epoch << "\t" << time_elapsed << "\t" << loss << "\t" << step_size << "\t" << accuracy << endl;
tracefile.close();
}
};
}
ostream& operator<<(ostream& os, util::Stopwatch& sw) {
std::stringstream ss;
ss.setf(std::ios::fixed);
ss.precision(3);
ss << sw.elapsed_s() << "s";
os << ss.str();
return os;
}
// C++ 11 make_unique
template<typename T, typename... Args>
std::unique_ptr<T> make_unique(Args&&... args) {
return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
}