17 What is CPU, GPU and CUDA
17 What is CPU, GPU and CUDA
CUDA stands for
Compute Unified Device Architecture. It’s a parallel computing
platform and programming model developed by NVIDIA that allows software to
use the GPU (Graphics Processing Unit) for general-purpose computing, not just
graphics. In other words, it turns your GPU into a super-fast processor for
heavy computations, like deep learning, scientific simulations, or image
processing.
Here’s a breakdown:
1. Why CUDA Exists
- CPUs are great for sequential tasks (one thing after
another).
- GPUs have thousands of smaller cores, which can do many
calculations simultaneously.
- CUDA provides a way for programmers to write code
that runs on the GPU, leveraging that massive parallelism.
2. Key Features
- Parallelism: Run thousands of threads at once.
- C/C++ Extensions: CUDA extends C, C++, and
Python (via PyTorch, TensorFlow, Numba) to work on GPU.
- High Performance: Useful for AI, deep
learning, physics simulations, and cryptocurrency mining.
3. How It Works in PyTorch
In PyTorch:
import torch
# Check if GPU is available
torch.cuda.is_available() # Returns True
if GPU is ready
# Move a tensor to GPU
tensor = torch.rand(3, 3)
tensor_gpu = tensor.to('cuda') # Now
tensor runs on GPU
# Operations on GPU are much faster for large tensors
result = tensor_gpu * 2
- .to('cuda') tells PyTorch to move the data to GPU
memory.
- All operations on that tensor will now use GPU cores
instead of CPU cores.
4. Analogy
Think of your CPU as one chef
cooking a meal step by step, while the GPU is hundreds of chefs in parallel
cooking many parts of the meal at once. CUDA is the recipe system that
tells all those chefs exactly what to do.
Note: For Google Colab
By default, Colab notebooks run on CPU. To use CUDA:
- Go to the menu: Runtime → Change runtime type
- Under Hardware accelerator, select GPU
- Click Save
Now your notebook can access CUDA.
Comments
Post a Comment