-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathcuda_helper.h
More file actions
97 lines (78 loc) · 2.2 KB
/
cuda_helper.h
File metadata and controls
97 lines (78 loc) · 2.2 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
94
95
96
97
#pragma once
#include <thrust/device_vector.h>
class CUDAException : public std::runtime_error
{
public:
CUDAException(const char *_const_Message);
};
class CUDAMallocException : public std::runtime_error
{
public:
CUDAMallocException(const char *_const_Message);
};
class CUDAMemCopyException : public std::runtime_error
{
public:
CUDAMemCopyException(const char *const_Message);
};
/*static*/ class CudaHelper
{
public:
/** Synchronizes the device work with the current thread and throws any errors as exception.
*/
static void DeviceSynchronize();
/** Throws the last error as exception.
*/
static void CheckLastError();
static void GetThreadBlocks(unsigned int numberOfElements, unsigned int alignment, /*out*/ unsigned int &numberOfThreadBlocks, /*out*/ unsigned int &numberOfThreads);
/** Gets a raw pointer from a thrust vector
*/
template<typename T>
static T* GetPointer(thrust::device_vector<T> &vector)
{
return thrust::raw_pointer_cast(&vector[0]);
}
/** Gets the size of the device_vector data in bytes.
*/
template<typename T>
static size_t GetSizeInBytes(const thrust::device_vector<T> &vector)
{
return sizeof(T) * vector.size();
}
/** Copies data from host to device.
*/
static void MemcpyHostToDevice(void* host, void* device, size_t size);
/** Copies data from host to device.
*/
template<typename T>
static void MemcpyHostToDevice(T* host, T* device, size_t elements)
{
MemcpyHostToDevice((void*)host, (void*)device, elements * sizeof(T));
}
/** Copies data from device to host.
*/
static void MemcpyDeviceToHost(void* device, void* host, size_t size);
/** Copies data from device to host.
*/
template<typename T>
static void MemcpyDeviceToHost(T* device, T* host, size_t elements)
{
MemcpyDeviceToHost((void*)device, (void*)host, elements * sizeof(T));
}
static void CudaMalloc(void** src, size_t size);
/** Reserve memory for data structures on device.
*/
template<typename T>
static void CudaMalloc(T** src, size_t elements)
{
CudaMalloc((void**)src, elements * sizeof(T));
}
static void CudaFree(void* src);
/** Reserve memory for data structures on device.
*/
template<typename T>
static void CudaFree(T* src)
{
CudaFree((void*)src);
}
};