-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathrt_cuda.cu
More file actions
195 lines (165 loc) · 6.46 KB
/
rt_cuda.cu
File metadata and controls
195 lines (165 loc) · 6.46 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#include "rt.h"
// return bitmap_a = bitmap_a & bitmap_b
__global__
void bit_andKernel(BITS* bitmap_a, BITS* bitmap_b, int n)
{
int index = threadIdx.x + blockIdx.x * blockDim.x;
int stride = blockDim.x * gridDim.x;
for (int i = index; i < n; i += stride)
bitmap_a[i] &= bitmap_b[i];
}
// copy bitmap_b to bitmap_a
// bitmap_a = bitmap_b
__global__
void bitmap_copyKernel(BITS* bitmap_a, BITS* bitmap_b, int n)
{
int index = threadIdx.x + blockIdx.x * blockDim.x;
int stride = blockDim.x * gridDim.x;
for (int i = index; i < n; i += stride)
bitmap_a[i] = bitmap_b[i];
}
// copy bitmap_b's negation to bitmap_a
// bitmap_a = ~bitmap_b
__global__
void bitmap_copyNegationKernel(BITS* bitmap_a, BITS* bitmap_b, int n)
{
int index = threadIdx.x + blockIdx.x * blockDim.x;
int stride = blockDim.x * gridDim.x;
for (int i = index; i < n; i += stride)
bitmap_a[i] = ~(bitmap_b[i]);
}
__global__
void bitmap_copySIMDKernel(BITS *result, BITS* bitmap_a, BITS* bitmap_b, int n)
{
int index = threadIdx.x + blockIdx.x * blockDim.x;
int stride = blockDim.x * gridDim.x;
for (int i = index; i < n; i += stride)
result[i] = bitmap_b[i] & (~(bitmap_a[i]));
}
cudaError_t GPUbitAndWithCuda(BITS* dev_bitmap_a, BITS* dev_bitmap_b, unsigned int n)
{
cudaError_t cudaStatus;
int bitnum = bits_num_needed(n);
dim3 blockSize(1024);
dim3 gridSize((bitnum + blockSize.x - 1) / blockSize.x);
// Choose which GPU to run on, change this on a multi-GPU system.
cudaStatus = cudaSetDevice(0);
if (cudaStatus != cudaSuccess) {
fprintf(stderr, "cudaSetDevice failed! Do you have a CUDA-capable GPU installed?");
goto Error;
}
// Launch a kernel on the GPU with one thread for each element.
timer.commonGetStartTime(2);
bit_andKernel <<<gridSize, blockSize>>> (dev_bitmap_a, dev_bitmap_b, bitnum);
// Check for any errors launching the kernel
cudaStatus = cudaGetLastError();
if (cudaStatus != cudaSuccess) {
fprintf(stderr, "addKernel launch failed: %s\n", cudaGetErrorString(cudaStatus));
goto Error;
}
// cudaDeviceSynchronize waits for the kernel to finish, and returns
// any errors encountered during the launch.
cudaStatus = cudaDeviceSynchronize();
if (cudaStatus != cudaSuccess) {
fprintf(stderr, "cudaDeviceSynchronize returned error code %d after launching addKernel!\n", cudaStatus);
goto Error;
}
timer.commonGetEndTime(2);
Error:
return cudaStatus;
}
cudaError_t GPUbitCopyWithCuda(BITS* dev_bitmap_a, BITS* dev_bitmap_b, unsigned int bitnum)
{
cudaError_t cudaStatus;
// int bitnum = bits_num_needed(n);
dim3 blockSize(1024);
dim3 gridSize((bitnum + blockSize.x - 1) / blockSize.x);
// Choose which GPU to run on, change this on a multi-GPU system.
cudaStatus = cudaSetDevice(0);
if (cudaStatus != cudaSuccess) {
fprintf(stderr, "cudaSetDevice failed! Do you have a CUDA-capable GPU installed?");
goto Error;
}
// Launch a kernel on the GPU with one thread for each element.
timer.commonGetStartTime(3);
bitmap_copyKernel <<<gridSize, blockSize>>> (dev_bitmap_a, dev_bitmap_b, bitnum);
// Check for any errors launching the kernel
cudaStatus = cudaGetLastError();
if (cudaStatus != cudaSuccess) {
fprintf(stderr, "addKernel launch failed: %s\n", cudaGetErrorString(cudaStatus));
goto Error;
}
// cudaDeviceSynchronize waits for the kernel to finish, and returns
// any errors encountered during the launch.
cudaStatus = cudaDeviceSynchronize();
if (cudaStatus != cudaSuccess) {
fprintf(stderr, "cudaDeviceSynchronize returned error code %d after launching addKernel!\n", cudaStatus);
goto Error;
}
timer.commonGetEndTime(3);
Error:
return cudaStatus;
}
cudaError_t GPUbitCopyNegationWithCuda(BITS* dev_bitmap_a, BITS* dev_bitmap_b, unsigned int bitnum)
{
cudaError_t cudaStatus;
// int bitnum = bits_num_needed(n);
dim3 blockSize(1024);
dim3 gridSize((bitnum + blockSize.x - 1) / blockSize.x);
// Choose which GPU to run on, change this on a multi-GPU system.
cudaStatus = cudaSetDevice(0);
if (cudaStatus != cudaSuccess) {
fprintf(stderr, "cudaSetDevice failed! Do you have a CUDA-capable GPU installed?");
goto Error;
}
// Launch a kernel on the GPU with one thread for each element.
timer.commonGetStartTime(3);
bitmap_copyNegationKernel <<<gridSize, blockSize>>> (dev_bitmap_a, dev_bitmap_b, bitnum);
// Check for any errors launching the kernel
cudaStatus = cudaGetLastError();
if (cudaStatus != cudaSuccess) {
fprintf(stderr, "addKernel launch failed: %s\n", cudaGetErrorString(cudaStatus));
goto Error;
}
// cudaDeviceSynchronize waits for the kernel to finish, and returns
// any errors encountered during the launch.
cudaStatus = cudaDeviceSynchronize();
if (cudaStatus != cudaSuccess) {
fprintf(stderr, "cudaDeviceSynchronize returned error code %d after launching addKernel!\n", cudaStatus);
goto Error;
}
timer.commonGetEndTime(3);
Error:
return cudaStatus;
}
cudaError_t GPUbitCopySIMDWithCuda(BITS* result, BITS* dev_bitmap_a, BITS* dev_bitmap_b, unsigned int bitnum)
{
cudaError_t cudaStatus;
dim3 blockSize(1024);
dim3 gridSize((bitnum + blockSize.x - 1) / blockSize.x);
// Choose which GPU to run on, change this on a multi-GPU system.
cudaStatus = cudaSetDevice(0);
if (cudaStatus != cudaSuccess) {
fprintf(stderr, "cudaSetDevice failed! Do you have a CUDA-capable GPU installed?");
goto Error;
}
// Launch a kernel on the GPU with one thread for each element.
timer.commonGetStartTime(3);
bitmap_copySIMDKernel <<<gridSize, blockSize>>> (result, dev_bitmap_a, dev_bitmap_b, bitnum);
// Check for any errors launching the kernel
cudaStatus = cudaGetLastError();
if (cudaStatus != cudaSuccess) {
fprintf(stderr, "addKernel launch failed: %s\n", cudaGetErrorString(cudaStatus));
goto Error;
}
// cudaDeviceSynchronize waits for the kernel to finish, and returns
// any errors encountered during the launch.
cudaStatus = cudaDeviceSynchronize();
if (cudaStatus != cudaSuccess) {
fprintf(stderr, "cudaDeviceSynchronize returned error code %d after launching addKernel!\n", cudaStatus);
goto Error;
}
timer.commonGetEndTime(3);
Error:
return cudaStatus;
}