-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreduce_v5.cu
76 lines (61 loc) · 1.91 KB
/
reduce_v5.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <cuda.h>
#include <cuda_runtime.h>
#include <iostream>
#include <numeric>
#define BLOCK_SIZE 1024
// 使用 shared memory
// 但是v2中线程数优化失效,因为每个线程至少要负责读取一个元素
__global__ void reduce_sum_v5(float *input, float *output, int n) {
__shared__ float sdata[BLOCK_SIZE];
int tid = threadIdx.x;
int i = blockIdx.x * blockDim.x + threadIdx.x;
if (i < n) {
sdata[tid] = input[i];
} else {
sdata[tid] = 0;
}
__syncthreads();
for (int stride = blockDim.x / 2; stride > 0; stride >>= 1) {
if (tid < stride && i + stride < n) {
sdata[tid] += sdata[tid + stride];
}
__syncthreads();
}
if (tid == 0) {
atomicAdd(output, sdata[0]);
}
}
float *generate_data(int n) {
srand(time(NULL)); // time.h 包含在了 cuda_runtime.h 中
float *data = new float[n];
for (int i = 0; i < n; i++) {
data[i] = rand() % 10;
}
return data;
}
int main() {
int n = 1 << 20;
float *input_h = generate_data(n);
float *output_h = new float;
int numBlocks = (n + BLOCK_SIZE - 1) / BLOCK_SIZE;
float *input_d, *output_d;
cudaMalloc(&input_d, n * sizeof(float));
cudaMalloc(&output_d, sizeof(float));
cudaMemcpy(input_d, input_h, n * sizeof(float), cudaMemcpyHostToDevice);
reduce_sum_v5<<<numBlocks, BLOCK_SIZE>>>(input_d, output_d, n);
cudaMemcpy(output_h, output_d, sizeof(float), cudaMemcpyDeviceToHost);
// c++17 特性
float sum = std::reduce(input_h, input_h + n);
std::cout << "sum (cpu): " << sum << std::endl;
std::cout << "sum (gpu): " << *output_h << std::endl;
if (sum == *output_h) {
std::cout << "Test passed!" << std::endl;
} else {
std::cout << "Test failed!" << std::endl;
}
delete[] input_h;
delete output_h;
cudaFree(input_d);
cudaFree(output_d);
return 0;
}