-
Notifications
You must be signed in to change notification settings - Fork 319
/
segment_reduce.cu
79 lines (61 loc) · 2.06 KB
/
segment_reduce.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
77
78
79
#include <iostream>
#include <cuda.h>
#define BLOCK_DIM 1024
__global__ void SharedMemoryReduction(float* input, float* output, int n) {
__shared__ float input_s[BLOCK_DIM];
unsigned int idx = blockIdx.x * blockDim.x + threadIdx.x; // index within a block
unsigned int t = threadIdx.x; // global index
// Load elements into shared memory
if (idx < n) {
input_s[t] = input[idx];
} else {
input_s[t] = 0.0f;
}
__syncthreads();
// Reduction in shared memory
for (unsigned int stride = blockDim.x / 2; stride > 0; stride >>= 1) {
if (t < stride && idx + stride < n) {
input_s[t] += input_s[t + stride];
}
__syncthreads();
}
// Reduction across blocks in global memory
// needs to be atomic to avoid contention
if (t == 0) {
atomicAdd(output, input_s[0]);
}
}
int main() {
// Size of the input data
const int size = 100000;
const int bytes = size * sizeof(float);
// Allocate memory for input and output on host
float* h_input = new float[size];
float* h_output = new float;
// Initialize input data on host
for (int i = 0; i < size; i++) {
h_input[i] = 1.0f; // Example: Initialize all elements to 1
}
// Allocate memory for input and output on device
float* d_input;
float* d_output;
cudaMalloc(&d_input, bytes);
cudaMalloc(&d_output, sizeof(float));
// Copy data from host to device
float zero = 0.0f;
cudaMemcpy(d_output, &zero, sizeof(float), cudaMemcpyHostToDevice);
cudaMemcpy(d_input, h_input, bytes, cudaMemcpyHostToDevice);
// Launch the kernel
int numBlocks = (size + BLOCK_DIM - 1) / BLOCK_DIM;
SharedMemoryReduction<<<numBlocks, BLOCK_DIM>>>(d_input, d_output, size);
// Copy result back to host
cudaMemcpy(h_output, d_output, sizeof(float), cudaMemcpyDeviceToHost);
// Print the result
std::cout << "Sum is " << *h_output << std::endl;
// Cleanup
delete[] h_input;
delete h_output;
cudaFree(d_input);
cudaFree(d_output);
return 0;
}