-
Notifications
You must be signed in to change notification settings - Fork 10
/
double_buffer.cpp
55 lines (48 loc) · 896 Bytes
/
double_buffer.cpp
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
#include "double_buffer.h"
#include <utility>
template <typename T>
DoubleBuffer<T>::DoubleBuffer()
{
A = 0;
B = 0;
nbytes = 0;
}
template <typename T>
DoubleBuffer<T>::DoubleBuffer(int nelements)
{
nbytes = sizeof(T)*nelements;
cudaMalloc( (void**)&A, nbytes );
cudaMalloc( (void**)&B, nbytes );
if( 0 == A || 0 == B )
{
printf("couldn't allocate GPU memory\n");
return;
}
printf("Allocated %.2f MB on GPU\n", 2*nbytes/(1024.f*1024.f));
}
template <typename T>
T* DoubleBuffer<T>::readTarget()
{
return A;
}
template <typename T>
T* DoubleBuffer<T>::writeTarget()
{
return B;
}
template <typename T>
void DoubleBuffer<T>::swap()
{
std::swap(A,B);
}
template <typename T>
int DoubleBuffer<T>::byteCount()
{
return nbytes;
}
template <typename T>
DoubleBuffer<T>::~DoubleBuffer()
{
cudaFree(A);
cudaFree(B);
}