forked from michal1000w/GPUSmoke
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUnified_Buffer.cpp
55 lines (48 loc) · 911 Bytes
/
Unified_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 "Unified_Buffer.h"
#include <utility>
template <typename T>
UnifiedBuffer<T>::UnifiedBuffer()
{
A = 0;
B = 0;
nbytes = 0;
}
template <typename T>
UnifiedBuffer<T>::UnifiedBuffer(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* UnifiedBuffer<T>::readTarget()
{
return A;
}
template <typename T>
T* UnifiedBuffer<T>::writeTarget()
{
return B;
}
template <typename T>
void UnifiedBuffer<T>::swap()
{
std::swap(A, B);
}
template <typename T>
int UnifiedBuffer<T>::byteCount()
{
return nbytes;
}
template <typename T>
UnifiedBuffer<T>::~UnifiedBuffer()
{
cudaFree(A);
cudaFree(B);
}