-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbuffer.cu
52 lines (39 loc) · 968 Bytes
/
buffer.cu
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.h>
#include <memory>
#include <exception>
#include <stdexcept>
#include "buffer.h"
static void deleteHostBuffer(void* buffer)
{
cudaFreeHost(buffer);
}
static void deleteDeviceBuffer(void* buffer)
{
cudaFree(buffer);
}
BufferPtr createHostBuffer(size_t length, unsigned int flags)
{
void* buffer;
cudaError_t err = cudaHostAlloc(&buffer, length, flags);
if (err != cudaSuccess)
{
throw std::runtime_error(cudaGetErrorString(err));
}
return BufferPtr(buffer, &deleteHostBuffer);
}
BufferPtr createDeviceBuffer(int device, size_t length)
{
cudaError_t err;
void* buffer;
err = cudaSetDevice(device);
if (err != cudaSuccess)
{
throw std::runtime_error(cudaGetErrorString(err));
}
err = cudaMalloc(&buffer, length);
if (err != cudaSuccess)
{
throw std::runtime_error(cudaGetErrorString(err));
}
return BufferPtr(buffer, &deleteDeviceBuffer);
}