-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathdevice_context.cc
159 lines (131 loc) · 4.26 KB
/
device_context.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
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include "device_context.hh"
#include <sys/time.h>
#include <cutil_inline.h>
#include <assert.h>
static uint64_t get_now() {
struct timeval tv;
assert(gettimeofday(&tv, NULL) == 0);
return tv.tv_sec * 1000000 + tv.tv_usec;
};
device_context::device_context()
{
init_ = false;
}
device_context::~device_context()
{
if (!init_)
return;
for (unsigned i = 1; i <= nstream_; i++) {
cutilSafeCall(cudaStreamDestroy(stream_ctx_[i].stream));
cutilSafeCall(cudaFreeHost(stream_ctx_[i].checkbits));
stream_ctx_[i].pool.destroy();
}
if (nstream_ == 0) {
cutilSafeCall(cudaFreeHost((void*)stream_ctx_[0].checkbits));
stream_ctx_[0].pool.destroy();
}
}
bool device_context::init(const unsigned long size, const unsigned nstream)
{
void *ret = NULL;
assert(nstream >= 0 && nstream <= MAX_STREAM);
assert(init_ == false);
init_ = true;
nstream_ = nstream;
if (nstream_ > 0) {
for (unsigned i = 1; i <= nstream; i++) {
cutilSafeCall(cudaStreamCreate(&stream_ctx_[i].stream));
if (!stream_ctx_[i].pool.init(size))
return false;
stream_ctx_[i].state = READY;
cutilSafeCall(cudaHostAlloc(&ret, MAX_BLOCKS, cudaHostAllocMapped));
stream_ctx_[i].checkbits = (uint8_t*)ret;
cutilSafeCall(cudaHostGetDevicePointer((void **)&stream_ctx_[i].checkbits_d, ret, 0));
}
} else {
stream_ctx_[0].stream = 0;
if (!stream_ctx_[0].pool.init(size))
return false;
stream_ctx_[0].state = READY;
cutilSafeCall(cudaHostAlloc(&ret, MAX_BLOCKS, cudaHostAllocMapped));
stream_ctx_[0].checkbits = (uint8_t*)ret;
cutilSafeCall(cudaHostGetDevicePointer((void **)&stream_ctx_[0].checkbits_d, ret, 0));
}
return true;
}
bool device_context::sync(const unsigned stream_id, const bool block)
{
assert(stream_id >= 0 && stream_id <= nstream_);
assert((stream_id == 0) ^ (nstream_ > 0));
if (!block) {
if (stream_ctx_[stream_id].finished)
return true;
if (stream_ctx_[stream_id].state == WAIT_KERNEL && stream_ctx_[stream_id].num_blks > 0) {
volatile uint8_t *checkbits = stream_ctx_[stream_id].checkbits;
for (unsigned i = 0; i < stream_ctx_[stream_id].num_blks; i++) {
if (checkbits[i] == 0)
return false;
}
} else if (stream_ctx_[stream_id].state != READY) {
cudaError_t ret = cudaStreamQuery(stream_ctx_[stream_id].stream);
if (ret == cudaErrorNotReady)
return false;
assert(ret == cudaSuccess);
}
stream_ctx_[stream_id].finished = true;
} else {
cudaStreamSynchronize(stream_ctx_[stream_id].stream);
}
return true;
}
void device_context::set_state(const unsigned stream_id, const STATE state)
{
assert(stream_id >= 0 && stream_id <= nstream_);
assert((stream_id == 0) ^ (nstream_ > 0));
if (state == READY) {
stream_ctx_[stream_id].end_usec = get_now();
stream_ctx_[stream_id].num_blks = 0;
stream_ctx_[stream_id].pool.reset();
} else if (state == WAIT_KERNEL) {
stream_ctx_[stream_id].begin_usec = get_now();
stream_ctx_[stream_id].finished = false;
} else if (state == WAIT_COPY) {
stream_ctx_[stream_id].finished = false;
}
stream_ctx_[stream_id].state = state;
}
enum STATE device_context::get_state(const unsigned stream_id)
{
assert(stream_id >= 0 && stream_id <= nstream_);
assert((stream_id == 0) ^ (nstream_ > 0));
return stream_ctx_[stream_id].state;
}
cudaStream_t device_context::get_stream(const unsigned stream_id)
{
assert(stream_id >= 0 && stream_id <= nstream_);
assert((stream_id == 0) ^ (nstream_ > 0));
return stream_ctx_[stream_id].stream;
}
uint8_t *device_context::get_dev_checkbits(const unsigned stream_id)
{
assert(stream_id >= 0 && stream_id <= nstream_);
assert((stream_id == 0) ^ (nstream_ > 0));
return stream_ctx_[stream_id].checkbits_d;
}
void device_context::clear_checkbits(const unsigned stream_id, const unsigned num_blks)
{
assert(stream_id >= 0 && stream_id <= nstream_);
assert((stream_id == 0) ^ (nstream_ > 0));
assert(num_blks >= 0 && num_blks <= MAX_BLOCKS);
stream_ctx_[stream_id].num_blks = num_blks;
volatile uint8_t *checkbits = stream_ctx_[stream_id].checkbits;
for (unsigned i = 0; i < num_blks; i++)
checkbits[i] = 0;
stream_ctx_[stream_id].finished = false;
}
cuda_mem_pool *device_context::get_cuda_mem_pool(const unsigned stream_id)
{
assert(stream_id >= 0 && stream_id <= nstream_);
assert((stream_id == 0) ^ (nstream_ > 0));
return &stream_ctx_[stream_id].pool;
}