-
Notifications
You must be signed in to change notification settings - Fork 0
/
cuda_time_record.cc
52 lines (44 loc) · 1.17 KB
/
cuda_time_record.cc
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
#include "cuda_time_record.h"
#include <fstream>
#include <cuda.h>
#include<sys/time.h>
CudaTimeRecord::CudaTimeRecord(string rec_file) :
rec_file_(rec_file), is_first_open_(true) {
cudaEventCreate(&timeStartEvent_, 0);
cudaEventCreate(&timeEndEvent_, 0);
}
void CudaTimeRecord::Start() {
cudaEventRecord(timeStartEvent_, 0);
}
void CudaTimeRecord::Comment(string com) {
ofstream rec_fp;
if (OpenFile(rec_file_, rec_fp) == 0) {
rec_fp << com;
rec_fp.close();
}
}
void CudaTimeRecord::Record() {
cudaEventRecord(timeEndEvent_, 0);
cudaEventSynchronize(timeEndEvent_);
cudaEventElapsedTime(&dur_time_,timeStartEvent_,timeEndEvent_);
ofstream rec_fp;
if (OpenFile(rec_file_, rec_fp) == 0) {
rec_fp << dur_time_ << endl;
rec_fp.close();
}
}
int CudaTimeRecord::OpenFile(string file_name, ofstream& fp) {
if (is_first_open_) {
// if (is_first_open_) {
// Any contents that existed in the file before it is open are discarded.
fp.open(file_name.c_str(), ios::trunc);
is_first_open_ = false;
cout << "if " << 1 << endl;
} else {
fp.open(file_name.c_str(), ios::app);
}
if (!fp.is_open()) {
return -1;
}
return 0;
}