-
Notifications
You must be signed in to change notification settings - Fork 319
/
simple_reduce.cu
57 lines (45 loc) · 1.44 KB
/
simple_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
#include <iostream>
#include <cuda.h>
__global__ void SimpleSumReductionKernel(float* input, float* output) {
unsigned int i = 2 * threadIdx.x;
for (unsigned int stride = 1; stride <= blockDim.x; stride *= 2) {
if (threadIdx.x % stride == 0) {
input[i] += input[i + stride];
}
__syncthreads();
}
if (threadIdx.x == 0) {
*output = input[0];
}
}
int main() {
// Size of the input data
const int size = 2048;
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
cudaMemcpy(d_input, h_input, bytes, cudaMemcpyHostToDevice);
// Launch the kernel
SimpleSumReductionKernel<<<1, size / 2>>>(d_input, d_output);
// 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;
}