-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathdevice_raw_buffer.cu
More file actions
93 lines (79 loc) · 2.86 KB
/
Copy pathdevice_raw_buffer.cu
File metadata and controls
93 lines (79 loc) · 2.86 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
#include "device_raw_buffer.h"
#include <assert.h>
#include "cuda_util.h"
#include "util.h"
using namespace std;
DeviceRawBuffer::DeviceRawBuffer(int num_blocks, int num_antennas,
int num_coarse_channels,
int timesteps_per_block, int num_polarizations)
: num_blocks(num_blocks), num_antennas(num_antennas),
num_coarse_channels(num_coarse_channels),
timesteps_per_block(timesteps_per_block), num_polarizations(num_polarizations),
state(DeviceRawBufferState::unused) {
size = sizeof(int8_t) * num_blocks * num_antennas * num_coarse_channels *
timesteps_per_block * num_polarizations * 2;
cudaMalloc(&data, size);
cudaStreamCreateWithFlags(&stream, cudaStreamNonBlocking);
checkCuda("DeviceRawBuffer init");
}
DeviceRawBuffer::~DeviceRawBuffer() {
cudaFree(data);
cudaStreamDestroy(stream);
}
// Should only be called from the producer thread
void DeviceRawBuffer::copyFromAsync(const RawBuffer& other) {
assert(size == other.size);
waitUntilUnused();
unique_lock<mutex> lock(m);
assert(state == DeviceRawBufferState::unused);
state = DeviceRawBufferState::copying;
lock.unlock();
// Nobody waits on copying state, so no need to notify
cudaMemcpyAsync(data, other.data, size, cudaMemcpyHostToDevice, stream);
cudaStreamAddCallback(stream, DeviceRawBuffer::staticCopyCallback, this, 0);
}
void DeviceRawBuffer::waitUntilReady() {
unique_lock<mutex> lock(m);
while (state != DeviceRawBufferState::ready) {
cv.wait(lock);
}
}
void DeviceRawBuffer::waitUntilUnused() {
unique_lock<mutex> lock(m);
while (state != DeviceRawBufferState::unused) {
cv.wait(lock);
}
}
void DeviceRawBuffer::release() {
unique_lock<mutex> lock(m);
assert(state == DeviceRawBufferState::ready);
state = DeviceRawBufferState::unused;
lock.unlock();
cv.notify_all();
}
void CUDART_CB DeviceRawBuffer::staticCopyCallback(cudaStream_t stream,
cudaError_t status,
void *device_raw_buffer) {
assert(status == cudaSuccess);
DeviceRawBuffer* object = (DeviceRawBuffer*) device_raw_buffer;
object->copyCallback();
}
void CUDART_CB DeviceRawBuffer::staticRelease(cudaStream_t stream,
cudaError_t status,
void *device_raw_buffer) {
if (status != cudaSuccess) {
logErrorTimestamp();
cerr << "DeviceRawBuffer::staticRelease: " << cudaGetErrorString(status) << endl;
exit(2);
}
DeviceRawBuffer* object = (DeviceRawBuffer*) device_raw_buffer;
object->release();
}
void DeviceRawBuffer::copyCallback() {
// Advance state to "ready"
unique_lock<mutex> lock(m);
assert(state == DeviceRawBufferState::copying);
state = DeviceRawBufferState::ready;
lock.unlock();
cv.notify_all();
}