-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtimer.hpp
More file actions
123 lines (114 loc) · 3.66 KB
/
timer.hpp
File metadata and controls
123 lines (114 loc) · 3.66 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
123
#ifndef TIMER_HPP
#define TIMER_HPP
#include <chrono>
#include <forward_list>
#include <thread>
#include <iostream>
#include <string>
#include <unordered_map>
class Timer {
private:
std::unordered_map<std::string, float> func_timer;
std::unordered_map<std::string, uint64_t> event_counter;
std::chrono::high_resolution_clock::time_point start;
std::chrono::high_resolution_clock::time_point end;
std::string current_func = "";
public:
void start_timer(std::string func_name) {
if (current_func != "") {
std::cerr << "Didn't end previous timer " << current_func
<< " before starting a new one" << std::endl;
exit(1);
}
current_func = func_name;
start = std::chrono::high_resolution_clock::now();
}
void end_timer(std::string func_name) {
if (current_func != func_name) {
std::cerr << "Trying to end previous timer " << current_func
<< " using a different one " << func_name << std::endl;
exit(1);
}
end = std::chrono::high_resolution_clock::now();
float this_time =
std::chrono::duration_cast<std::chrono::microseconds>(end - start)
.count();
current_func = "";
auto it = func_timer.find(func_name);
if (it != func_timer.end())
it->second += this_time;
else
func_timer[func_name] = this_time;
}
void add_event(std::string event_name, uint64_t count = 1) {
auto it = event_counter.find(event_name);
if (it != event_counter.end())
it->second += count;
else
event_counter[event_name] = count;
}
void print_all_times() {
for (auto &p : func_timer) {
std::cout << p.first << ", " << p.second << std::endl;
}
}
void print_all_events() {
for (auto &p : event_counter) {
std::cout << p.first << ", " << p.second << std::endl;
}
}
};
class ManagedHeap{
private:
std::forward_list<void*> ptrs;
void* current_ptr = NULL;
uint64_t size = 0;
uint64_t base = 0;
public:
ManagedHeap(uint64_t size){
this->size = size;
this->current_ptr = calloc(size, 1);
if(this->current_ptr == NULL){
std::cerr << "Failed to allocate memory in the managed heap" << std::endl;
exit(1);
}
this->base = 0;
}
ManagedHeap(): ManagedHeap(((uint64_t)(1))<<30){}
ManagedHeap(const ManagedHeap&) = delete;
~ManagedHeap(){
for(auto ptr : ptrs){
free(ptr);
}
free(current_ptr);
}
void* alloc(uint64_t requested_size){
if(requested_size >= this->size){
std::cerr<<"cant' give memory this big "<<std::endl;
exit(1);
}
if((requested_size + this->base) >= this->size){
ptrs.push_front(this->current_ptr);
this->current_ptr = calloc(size, 1);
if(current_ptr == NULL){
std::cerr << "Failed to re-allocate memory in the managed heap" << std::endl;
exit(1);
}
this->base = 0;
}
void* ret = (void*)((char*)this->current_ptr + this->base);
this->base += requested_size;
return ret;
}
};
static ManagedHeap* tlocal_heaps;
static void init_heaps(int num_threads){
tlocal_heaps = new ManagedHeap[num_threads];
}
static void *my_calloc(uint64_t num_elts, uint64_t size_per_elt, int thread_id) {
return tlocal_heaps[thread_id].alloc(num_elts * size_per_elt);
}
static void *my_malloc(uint64_t size, int thread_id) {
return tlocal_heaps[thread_id].alloc(size);
}
#endif