# Install CUDA toolkit (example: CUDA 11.
2)
!apt-get update
!apt-get install -y cuda-toolkit-11-2
Get:1 https://cloud.r-project.org/bin/linux/ubuntu jammy-cran40/ InRelease [3,632 B]
Get:2 https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2204/x86_64 InRelease [1,581 B]
Hit:3 http://archive.ubuntu.com/ubuntu jammy InRelease
Get:4 http://archive.ubuntu.com/ubuntu jammy-updates InRelease [128 kB]
Get:5 http://security.ubuntu.com/ubuntu jammy-security InRelease [129 kB]
Get:6 https://r2u.stat.illinois.edu/ubuntu jammy InRelease [6,555 B]
Get:7 http://archive.ubuntu.com/ubuntu jammy-backports InRelease [127 kB]
Get:8 https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2204/x86_64 Packages [1,607 kB]
Hit:9 https://ppa.launchpadcontent.net/deadsnakes/ppa/ubuntu jammy InRelease
Get:10 https://ppa.launchpadcontent.net/graphics-drivers/ppa/ubuntu jammy InRelease [24.3 kB]
Get:11 https://r2u.stat.illinois.edu/ubuntu jammy/main all Packages [8,863 kB]
Hit:12 https://ppa.launchpadcontent.net/ubuntugis/ppa/ubuntu jammy InRelease
Get:13 http://archive.ubuntu.com/ubuntu jammy-updates/universe amd64 Packages [1,543 kB]
Get:14 http://archive.ubuntu.com/ubuntu jammy-updates/main amd64 Packages [3,154 kB]
Get:15 https://r2u.stat.illinois.edu/ubuntu jammy/main amd64 Packages [2,700 kB]
Get:16 https://ppa.launchpadcontent.net/graphics-drivers/ppa/ubuntu jammy/main amd64 Packages [47.4 kB]
Fetched 18.3 MB in 3s (5,951 kB/s)
Reading package lists... Done
W: Skipping acquire of configured file 'main/source/Sources' as repository 'https://r2u.stat.illinois.edu/ubuntu jammy InRelease' do
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
E: Unable to locate package cuda-toolkit-11-2
!nvcc --version
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2024 NVIDIA Corporation
Built on Thu_Jun__6_02:18:23_PDT_2024
Cuda compilation tools, release 12.5, V12.5.82
Build cuda_12.5.r12.5/compiler.34385749_0
!nvidia-smi
Mon Apr 28 03:19:35 2025
+-----------------------------------------------------------------------------------------+
| NVIDIA-SMI 550.54.15 Driver Version: 550.54.15 CUDA Version: 12.4 |
|-----------------------------------------+------------------------+----------------------+
| GPU Name Persistence-M | Bus-Id Disp.A | Volatile Uncorr. ECC |
| Fan Temp Perf Pwr:Usage/Cap | Memory-Usage | GPU-Util Compute M. |
| | | MIG M. |
|=========================================+========================+======================|
| 0 Tesla T4 Off | 00000000:00:04.0 Off | 0 |
| N/A 46C P8 13W / 70W | 0MiB / 15360MiB | 0% Default |
| | | N/A |
+-----------------------------------------+------------------------+----------------------+
+-----------------------------------------------------------------------------------------+
| Processes: |
| GPU GI CI PID Type Process name GPU Memory |
| ID ID Usage |
|=========================================================================================|
| No running processes found |
+-----------------------------------------------------------------------------------------+
%%writefile add.cu
#include <iostream>
#include <cuda_runtime.h>
__global__ void addVectors(int * A, int * B, int * C, int n) {
int i = blockIdx.x * blockDim.x + threadIdx.x;
if (i < n) {
C[i] = A[i] + B[i];
}
}
int main() {
int n;
std::cout << "Enter the number of elements: ";
std::cin >> n;
int * A = new int[n];
int * B = new int[n];
int * C = new int[n];
std::cout << "Enter elements for vector A:" << std::endl;
for (int i = 0; i < n; i++) {
std::cin >> A[i];
}
std::cout << "Enter elements for vector B:" << std::endl;
for (int i = 0; i < n; i++) {
std::cin >> B[i];
}
int size = n * sizeof(int);
int * dev_A, * dev_B, * dev_C;
cudaMalloc( & dev_A, size);
cudaMalloc( & dev_B, size);
cudaMalloc( & dev_C, size);
cudaMemcpy(dev_A, A, size, cudaMemcpyHostToDevice);
cudaMemcpy(dev_B, B, size, cudaMemcpyHostToDevice);
int blockSize = 256;
int numBlocks = (n + blockSize - 1) / blockSize;
addVectors << < numBlocks, blockSize >>> (dev_A, dev_B, dev_C, n);
cudaMemcpy(C, dev_C, size, cudaMemcpyDeviceToHost);
std::cout << "Vector Addition Results:" << std::endl;
for (int i = 0; i < n && i < 10; i++) { //print up to 10 results.
std::cout << C[i] << " ";
}
std::cout << std::endl;
cudaFree(dev_A);
cudaFree(dev_B);
cudaFree(dev_C);
delete[] A;
delete[] B;
delete[] C;
return 0;
}
Writing add.cu
!nvcc add.cu -o add -arch=sm_75
!./add
Enter the number of elements: ^C
58
Start coding or generate with AI.