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
|
#pragma once
#include <stdint.h>
#include <cstdlib>
#include <vector>
#include <iostream>
namespace intgemm {
static inline uint64_t rdtsc_begin(uint32_t &processor) {
uint32_t lo, hi;
__asm__ __volatile__ (
"cpuid\n\t"
"rdtscp\n\t"
"mov %%eax, %0\n\t"
"mov %%edx, %1\n\t"
"mov %%ecx, %2\n\t"
: "=r" (lo), "=r" (hi), "=r" (processor)
: /* no input */
: "rax", "rbx", "rcx", "rdx");
return static_cast<uint64_t>(hi) << 32 | lo;
}
static inline uint64_t rdtsc_end(uint32_t &processor) {
uint32_t lo, hi;
__asm__ __volatile__ (
"rdtscp\n\t"
"mov %%eax, %0\n\t"
"mov %%edx, %1\n\t"
"mov %%ecx, %2\n\t"
"cpuid\n\t"
: "=r" (lo), "=r" (hi), "=r" (processor)
: /* no input */
: "rax", "rbx", "rcx", "rdx");
return static_cast<uint64_t>(hi) << 32 | lo;
}
class StopWatch {
public:
StopWatch(std::vector<uint64_t> &stats)
: stats_(stats), start_(rdtsc_begin(processor_)) {}
~StopWatch() {
uint32_t proc;
uint64_t stop = rdtsc_end(proc);
if (proc != processor_) {
std::cerr << "Detected core change from " << processor_ << " to " << proc << std::endl;
abort();
}
stats_.push_back(stop - start_);
}
private:
std::vector<uint64_t> &stats_;
uint32_t processor_;
uint64_t start_;
};
} // namespace intgemm
|