-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvector_add.cu
More file actions
70 lines (58 loc) · 1.88 KB
/
vector_add.cu
File metadata and controls
70 lines (58 loc) · 1.88 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
#include <cassert>
#include <cstdio>
#include <vector>
#include <cuda_runtime.h>
#include "tinycuda/tinycuda.hpp" // Bundles error, memory, profiler
/**
* Simple CUDA kernel: C = A + B for each element
*/
__global__ void vector_add(float* a, float* b, float* c, size_t n) {
size_t idx = blockIdx.x * blockDim.x + threadIdx.x;
if (idx < n) {
c[idx] = a[idx] + b[idx];
}
}
int main() {
constexpr size_t N = 1 << 16; // 65K elements for demo
// Host vectors
std::vector<float> host_a(N, 1.0f);
std::vector<float> host_b(N, 2.0f);
std::vector<float> host_c(N, 0.0f); // Expected: 3.0f each
// Buffers for GPU mirroring
tinycuda::Buffer<float> buf_a(host_a.data(), N);
tinycuda::Buffer<float> buf_b(host_b.data(), N);
tinycuda::Buffer<float> buf_c(host_c.data(), N);
// Transfer to GPU
buf_a.to_gpu();
buf_b.to_gpu();
buf_c.to_gpu();
// Grid and block dims
dim3 block(256);
dim3 grid((N + block.x - 1) / block.x);
// Optional: Time the kernel with profiler (warmup=3, repeat=10 for quick demo)
tinycuda::KernelProfiler profiler(3, 10);
float avg_ms = profiler([&] {
vector_add<<<grid, block>>>(buf_a.gpu_data(), buf_b.gpu_data(), buf_c.gpu_data(), N);
});
CUDA_CHECK(cudaGetLastError());
CUDA_CHECK(cudaDeviceSynchronize());
printf("Average kernel time: %.4f ms\n", avg_ms);
// Copy result back to host
buf_c.to_cpu();
// Verify correctness
bool correct = true;
for (size_t i = 0; i < N; ++i) {
if (fabsf(host_c[i] - 3.0f) > 1e-5f) { // Tolerance for FP
correct = false;
break;
}
}
if (correct) {
printf("[PASS] Vector addition verified: all elements == 3.0f\n");
} else {
printf("[FAIL] Verification failed!\n");
return 1;
}
printf("[SUCCESS] Example completed.\n");
return 0;
}