diff options
| author | Onur Gündüz <ogunduz@gmail.com> | 2026-06-28 07:19:48 +0200 |
|---|---|---|
| committer | Onur Gündüz <ogunduz@gmail.com> | 2026-06-28 07:19:48 +0200 |
| commit | bc940168825da277f5897c89a5591d6fab2315c5 (patch) | |
| tree | 0a84ed6b845e29a4c6d383d058c902ee8ad36fbd /examples | |
| parent | 9807b449c66b06b708766753a3d38ea9022537d8 (diff) | |
| download | colabctl-bc940168825da277f5897c89a5591d6fab2315c5.tar.gz colabctl-bc940168825da277f5897c89a5591d6fab2315c5.zip | |
Add CUDA vector addition example
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/cuda_vec_add.py | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/examples/cuda_vec_add.py b/examples/cuda_vec_add.py new file mode 100644 index 0000000..cc9c4f5 --- /dev/null +++ b/examples/cuda_vec_add.py @@ -0,0 +1,49 @@ +import subprocess + +src = r""" +#include <stdio.h> + +__global__ void vec_add(float *a, float *b, float *c, int n) { + int i = blockIdx.x * blockDim.x + threadIdx.x; + if (i < n) c[i] = a[i] + b[i]; +} + +int main() { + const int N = 1 << 20; + float *ha = new float[N], *hb = new float[N], *hc = new float[N]; + for (int i = 0; i < N; i++) { ha[i] = (float)i; hb[i] = (float)(N - i); } + + float *da, *db, *dc; + cudaMalloc(&da, N * sizeof(float)); + cudaMalloc(&db, N * sizeof(float)); + cudaMalloc(&dc, N * sizeof(float)); + cudaMemcpy(da, ha, N * sizeof(float), cudaMemcpyHostToDevice); + cudaMemcpy(db, hb, N * sizeof(float), cudaMemcpyHostToDevice); + + vec_add<<<(N + 255) / 256, 256>>>(da, db, dc, N); + cudaDeviceSynchronize(); + + cudaMemcpy(hc, dc, N * sizeof(float), cudaMemcpyDeviceToHost); + printf("c[0] = %.0f\n", hc[0]); + printf("c[512] = %.0f\n", hc[512]); + printf("c[N-1] = %.0f\n", hc[N - 1]); + printf("All == N? %s\n", (hc[0] == N && hc[512] == N && hc[N-1] == N) ? "yes" : "no"); + + delete[] ha; delete[] hb; delete[] hc; + cudaFree(da); cudaFree(db); cudaFree(dc); +} +""" + +with open('/tmp/vadd.cu', 'w') as f: + f.write(src) + +r = subprocess.run(['nvcc', '-O2', '-o', '/tmp/vadd', '/tmp/vadd.cu'], + capture_output=True, text=True) +if r.returncode != 0: + print("nvcc stderr:", r.stderr) +else: + print("Compiled OK") + out = subprocess.run(['/tmp/vadd'], capture_output=True, text=True) + print(out.stdout) + if out.stderr: + print("run stderr:", out.stderr) |
