CUDA, short for Compute Unified Device Architecture, was created by NVIDIA in 2006. CUDA is a parallel computing platform and programming model that enables developers to harness the power of NVIDIA GPUs for general-purpose computing (GPGPU). Developers can access CUDA through the official site: CUDA Toolkit Downloads, which provides compilers, libraries, SDKs, and documentation for Windows, macOS, and Linux platforms.
CUDA exists to accelerate computationally intensive tasks by offloading parallel workloads to GPUs. Its design philosophy emphasizes high performance, low-level hardware control, and scalability. By providing direct access to GPU cores, memory management, and optimized math libraries, CUDA solves the problem of efficiently executing large-scale numerical simulations, deep learning, image processing, and scientific computing.
CUDA: Kernels and Thread Hierarchy
CUDA programs consist of kernels, which are functions executed in parallel by multiple threads across a GPU. Threads are organized into blocks and grids for scalable execution.
#include <stdio.h>
**global** void add(int *a, int *b, int *c) {
int idx = threadIdx.x;
c[idx] = a[idx] + b[idx];
}
int main() {
int a[5] = {1,2,3,4,5};
int b[5] = {10,20,30,40,50};
int c[5];
int *d_a, *d_b, *d_c;
cudaMalloc(&d_a, 5*sizeof(int));
cudaMalloc(&d_b, 5*sizeof(int));
cudaMalloc(&d_c, 5*sizeof(int));
cudaMemcpy(d_a, a, 5*sizeof(int), cudaMemcpyHostToDevice);
cudaMemcpy(d_b, b, 5*sizeof(int), cudaMemcpyHostToDevice);
add<<<1,5>>>(d_a, d_b, d_c);
cudaMemcpy(c, d_c, 5*sizeof(int), cudaMemcpyDeviceToHost);
for(int i=0;i<5;i++) printf("c[%d]=%d\n", i, c[i]);
cudaFree(d_a); cudaFree(d_b); cudaFree(d_c);
return 0;
}Threads execute the kernel in parallel. Each thread computes a portion of the work. The thread hierarchy allows efficient mapping of computations to GPU cores, similar to parallel loops in OpenCL or multi-threaded tasks in C++.
CUDA: Memory Management
CUDA exposes multiple memory types including global, shared, and local memory for performance tuning.
__global__ void multiply(int *arr) {
int idx = threadIdx.x;
__shared__ int temp[32];
temp[idx] = arr[idx] * 2;
__syncthreads();
arr[idx] = temp[idx];
}Shared memory allows fast data exchange between threads within a block. Proper memory usage is critical for performance optimization, conceptually similar to shared buffers in OpenCL and multithreaded programming in C++.
CUDA: Libraries and Tools
CUDA provides libraries such as cuBLAS, cuFFT, and Thrust for optimized computations.
#include <thrust/device_vector.h>
#include <thrust/transform.h>
#include <thrust/functional.h>
int main() {
thrust::device_vector<int> A(5);
thrust::device_vector<int> B(5);
thrust::device_vector<int> C(5);
thrust::transform(A.begin(), A.end(), B.begin(), C.begin(), thrust::plus<int>());
}These libraries provide pre-optimized GPU operations for linear algebra, FFTs, and vector algorithms. Using them avoids manual kernel programming while maintaining high performance, similar to OpenCL or GPU-accelerated routines in C++.
CUDA is used extensively in scientific computing, machine learning, graphics rendering, and simulations. By combining direct GPU access, parallel kernels, and high-performance libraries, CUDA enables applications to leverage GPU acceleration efficiently alongside OpenCL, C++, and Python-based GPU libraries.