-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathcuda_helper.cpp
More file actions
86 lines (73 loc) · 2.11 KB
/
cuda_helper.cpp
File metadata and controls
86 lines (73 loc) · 2.11 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
#include "cuda_helper.h"
#include <cuda_runtime.h>
CUDAException::CUDAException(const char *_const_Message) : std::runtime_error(_const_Message)
{
}
CUDAMallocException::CUDAMallocException(const char *_const_Message) : std::runtime_error(_const_Message)
{
}
CUDAMemCopyException::CUDAMemCopyException(const char *_const_Message) : std::runtime_error(_const_Message)
{
}
void CudaHelper::DeviceSynchronize()
{
cudaError_t cudaStatus = cudaDeviceSynchronize();
if (cudaStatus != cudaSuccess)
{
auto temp = cudaGetErrorString(cudaStatus);
throw CUDAException(temp);
}
}
void CudaHelper::GetThreadBlocks(unsigned int numberOfElements, unsigned int alignment, /*out*/ unsigned int &numberOfThreadBlocks, /*out*/ unsigned int &numberOfThreads)
{
numberOfThreads = (numberOfElements / alignment) * alignment;
numberOfThreadBlocks = (numberOfElements / alignment);
if (numberOfElements % alignment != 0)
{
numberOfThreads += alignment;
numberOfThreadBlocks++;
}
}
void CudaHelper::MemcpyHostToDevice(void* host, void* device, size_t size)
{
cudaError_t cudaStatus = cudaMemcpy(device, host, size, cudaMemcpyHostToDevice);
if (cudaStatus != cudaSuccess)
{
throw CUDAMemCopyException("cudaMemcpy() failed!");
}
}
void CudaHelper::MemcpyDeviceToHost(void* device, void* host, size_t size)
{
cudaError_t cudaStatus = cudaMemcpy(host, device, size, cudaMemcpyDeviceToHost);
if (cudaStatus != cudaSuccess)
{
throw CUDAMemCopyException("cudaMemcpy() failed!");
}
}
void CudaHelper::CheckLastError()
{
cudaError_t cudaStatus = cudaGetLastError();
if (cudaStatus != cudaSuccess)
{
auto temp = cudaGetErrorString(cudaStatus);
throw CUDAException(temp);
}
}
void CudaHelper::CudaMalloc(void** src, size_t size)
{
cudaError_t cudaStatus = cudaMalloc(src, size);
if (cudaStatus != cudaSuccess)
{
printf("Error in CudaMalloc: %s : ", cudaGetErrorString(cudaStatus));
throw cudaErrorMemoryAllocation;
}
}
void CudaHelper::CudaFree(void* src)
{
cudaError_t cudaStatus = cudaFree(src);
if (cudaStatus != cudaSuccess)
{
printf("Error in CudaFree: %s : ", cudaGetErrorString(cudaStatus));
throw cudaErrorMemoryAllocation;
}
}